/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.modules.groupon.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Assert;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import lombok.AllArgsConstructor;
import javax.validation.Valid;

import org.springblade.common.enums.GrouponSourceEnum;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.groupon.entity.GrouponImage;
import org.springblade.modules.groupon.service.IGrouponGoodsService;
import org.springblade.modules.groupon.service.IGrouponImageService;
import org.springblade.modules.mall.entity.GoodsInfo;
import org.springblade.modules.mall.vo.GoodsInfoVO;
import org.springblade.modules.mall.wrapper.GoodsInfoWrapper;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestParam;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springblade.modules.groupon.entity.Groupon;
import org.springblade.modules.groupon.vo.GrouponVO;
import org.springblade.modules.groupon.wrapper.GrouponWrapper;
import org.springblade.modules.groupon.service.IGrouponService;
import org.springblade.core.boot.ctrl.BladeController;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 团购表 控制器
 *
 * @author BladeX
 * @since 2023-06-02
 */
@RestController
@AllArgsConstructor
@RequestMapping("groupon/groupon")
@Api(value = "团购表", tags = "团购表接口")
public class GrouponController extends BladeController {

	private final IGrouponService grouponService;
	private final IGrouponImageService grouponImageService;
	private final IGrouponGoodsService grouponGoodsService;

	/**
	 * 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入groupon")
	public R<GrouponVO> detail(Groupon groupon) {
		Groupon detail = grouponService.getOne(Condition.getQueryWrapper(groupon));
		GrouponVO grouponVO = GrouponWrapper.build().entityVO(detail);
		// 查询团购详情图片
		List<GrouponImage> grouponImages = grouponImageService.lambdaQuery().eq(GrouponImage::getGrouponId, groupon.getId()).list();
		if (CollUtil.isNotEmpty(grouponImages)) {
			List<String> slideshow = grouponImages.stream().filter(e -> e.getType().equals(1)).map(GrouponImage::getUrl).collect(Collectors.toList());
			List<String> detailImage = grouponImages.stream().filter(e -> e.getType().equals(2)).map(GrouponImage::getUrl).collect(Collectors.toList());
			grouponVO.setSlideshow(slideshow);
			grouponVO.setDetailImage(detailImage);
		}
		// 查询商品
		List<GoodsInfoVO> goodsList = grouponGoodsService.getGoodsByGrouponId(detail.getId());
		grouponVO.setGoodsList(goodsList);

		return R.data(grouponVO);
	}

	/**
	 * 分页 团购表
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入groupon")
	public R<IPage<GrouponVO>> list(Groupon groupon, Query query) {
		IPage<Groupon> pages = grouponService.page(Condition.getPage(query), Condition.getQueryWrapper(groupon));
		return R.data(GrouponWrapper.build().pageVO(pages));
	}


	/**
	 * 自定义分页 团购表
	 */
	@GetMapping("/page")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入groupon")
	public R<IPage<GrouponVO>> page(GrouponVO groupon, Query query) {
		IPage<GrouponVO> pages = grouponService.selectGrouponPage(Condition.getPage(query), groupon);
		return R.data(pages);
	}

	/**
	 * 新增 团购表
	 */
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增", notes = "传入groupon")
	public R save(@Valid @RequestBody GrouponVO groupon) {
		Assert.notBlank(groupon.getMainTitle(), "主标题不能为空");
		Assert.notBlank(groupon.getSubTitle(), "副标题不能为空");
		Assert.notEmpty(groupon.getSelfTakeIds(), "自提点不能为空");
		groupon.setSource(GrouponSourceEnum.PLATFORM.getValue());
		grouponService.publishGroupon(groupon);
		return R.status(true);
	}

	/**
	 * 修改 团购表
	 */
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改", notes = "传入groupon")
	@Transactional(rollbackFor = Exception.class)
	public R update(@Valid @RequestBody GrouponVO groupon) {
		grouponService.check(groupon);
		boolean b = grouponService.updateById(groupon);
		return R.status(b);
	}


	@PostMapping("/modify-status")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改状态", notes = "传入groupon")
	public R modifyStatus(@RequestBody GrouponVO groupon) {
		boolean b = grouponService.updateById(groupon);
		return R.status(b);
	}

	/**
	 * 新增或修改 团购表
	 */
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "新增或修改", notes = "传入groupon")
	public R submit(@Valid @RequestBody Groupon groupon) {
		return R.status(grouponService.saveOrUpdate(groupon));
	}

	
	/**
	 * 删除 团购表
	 */
	@PostMapping("/remove")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "逻辑删除", notes = "传入ids")
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		return R.status(grouponService.removeByIds(Func.toLongList(ids)));
	}

	@PostMapping("/audit")
	@ApiOperationSupport(order = 8)
	@ApiOperation(value = "审核")
	public R audit(@ApiParam(value = "id", required = true) @RequestParam String id,
				   @ApiParam(value = "remark", required = true) @RequestParam Integer auditStatus,
				   @ApiParam(value = "remark", required = true) @RequestParam String remark) {
		grouponService.audit(id, auditStatus, remark);
		return R.success("操作成功");
	}


	@PostMapping("/perfect")
	@ApiOperationSupport(order = 9)
	@ApiOperation(value = "完善信息")
	public R perfect(@RequestBody GrouponVO grouponVO) {
		List<GoodsInfoVO> goodsList = grouponVO.getGoodsList();
		for (GoodsInfoVO goodsInfoVO : goodsList) {
			if (goodsInfoVO.getGrouponPrice().doubleValue() <= 0) {
				return R.fail("团购价格设置有误，请检查");
			}
		}
		grouponService.perfect(grouponVO);
		return R.success("操作成功");
	}

	/**
	 * 生成团购测试数据
	 * @return
	 */
	@GetMapping("/gen")
	public R gen() {
		GrouponVO groupon = new GrouponVO();
		groupon.setMainTitle("测试" + DateUtil.formatDateTime(new Date()));
		groupon.setSubTitle(groupon.getMainTitle());
		LocalDateTime now = LocalDateTime.now();
		groupon.setGrouponStartTime(now);
		groupon.setGrouponEndTime(now.plusHours(1));
		groupon.setTakeStartTime(now.plusDays(1));
		groupon.setTakeEndTime(now.plusDays(2));
		grouponService.gen(groupon);
		return R.success("操作成功");
	}
}
