package com.tzy.controller.group;


import com.github.pagehelper.PageInfo;
import com.tzy.annotation.ApiLog;
import com.tzy.common.base.AppBaseController;
import com.tzy.common.dto.GoodsQuery;
import com.tzy.common.dto.InDto;
import com.tzy.common.dto.OutDTO;
import com.tzy.common.enums.BusinessType;
import com.tzy.common.config.handle.ApiVersion;
import com.tzy.common.utils.bean.DozerUtils;
import com.tzy.dto.SpuDTO;
import com.tzy.pojo.item.Spu;
import com.tzy.sportcard.api.bean.param.CalendarParam;
import com.tzy.sportcard.api.domain.GoodsInfo;
import com.tzy.sportcard.api.dto.group.CalendarSearchDTO;
import com.tzy.sportcard.api.dto.group.GroupGoodsDTO;
import com.tzy.sportcard.api.dto.group.ListSearchDTO;
import com.tzy.sportcard.api.service.ChecklistCalendarService;
import com.tzy.sportcard.api.service.GroupApiService;
import com.tzy.sportcard.group.domain.CardGroupInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.teasoft.bee.osql.SuidRich;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author by po'yi
 * @Classname ListCalendarController
 * @Description checklist日历
 * @Date 2022/11/15 14:17
 */
@Api("checklist日历api")
@RestController
@RequestMapping("/api/calendar")
public class ListCalendarController extends AppBaseController {


	@Autowired
	private ChecklistCalendarService checklistCalendarService;
	@Autowired
	private GroupApiService groupApiService;
	@Autowired
	private SuidRich suidRich;

	@ApiLog(title = "日历刷选条件", businessType = BusinessType.SEARCH)
	@ResponseBody
	@RequestMapping(value = "/{version}/search/param", method = RequestMethod.POST)
	@ApiVersion(1.0)
	@ApiOperation("日历刷选条件")
	public OutDTO getSearchParam(@RequestBody InDto inDto) {
		CalendarSearchDTO searchParams=checklistCalendarService.selectCalendarSearchParam();
		return OutDTO.ok().put("searchParams",searchParams);
	}

	@ApiLog(title = "checklist刷选条件", businessType = BusinessType.SEARCH)
	@ResponseBody
	@RequestMapping(value = "/{version}/list/search/id", method = RequestMethod.POST)
	@ApiVersion(1.0)
	@ApiOperation("checklist刷选条件")
	public OutDTO getListSearchParam(@RequestBody InDto inDto) {
		ListSearchDTO searchDTO=checklistCalendarService.selectListSearchParam(Long.valueOf(inDto.getString("id")));
		return OutDTO.ok().put("searchParams",searchDTO);
	}

	@ApiLog(title = "获取checklist日历", businessType = BusinessType.SEARCH)
	@ResponseBody
	@RequestMapping(value = "/{version}/list", method = RequestMethod.POST)
	@ApiVersion(1.0)
	@ApiOperation("获取checklist日历")
	public OutDTO getCalendarList(@RequestBody InDto inDto) {
		CalendarParam param=inDto.buildParam(new CalendarParam());
		param.setPageNo(inDto.getPageNo());
		param.setPageSize(inDto.getPageSize());
		param.setUserId(inDto.getUserId());
		return OutDTO.ok().put("calendars",checklistCalendarService.selectCalendarList(param));
	}

	@ApiLog(title = "查询已出卡密", businessType = BusinessType.SEARCH)
	@ResponseBody
	@RequestMapping(value = "/{version}/goods/id", method = RequestMethod.POST)
	@ApiVersion(1.0)
	@ApiOperation("查询已出卡密")
	public OutDTO getListGoods(@RequestBody InDto inDto) {
		CalendarParam param=inDto.buildParam(new CalendarParam());
		if(param.getPaniniListId()==null){
			return OutDTO.error500("参数为空!");
		}
		param.setPageNo(inDto.getPageNo());
		param.setPageSize(inDto.getPageSize());
		param.setUserId(inDto.getUserId());
		PageInfo<GroupGoodsDTO> goods=checklistCalendarService.selectListGoods(param);
		return OutDTO.ok().put("goods",goods);
	}

	@ApiLog(title = "推荐商品", businessType = BusinessType.SEARCH)
	@ResponseBody
	@RequestMapping(value = "/{version}/goods/recommend", method = RequestMethod.POST)
	@ApiVersion(1.0)
	@ApiOperation("推荐商品")
	public OutDTO getRecommendGoods(@RequestBody InDto inDto) {
		Long paniniListId = Long.valueOf(inDto.getString("paniniListId"));
		CardGroupInfo cardGroupInfo = new CardGroupInfo();
		cardGroupInfo.setShowApplet("1");
		cardGroupInfo.setColumnsearch("and cgi.status=201 and cgi.panini_list_id= "+paniniListId);
		List<GoodsInfo> groupInfo = groupApiService.findGroupInfo(inDto, cardGroupInfo);
		Spu spu = new Spu();
		spu.setPaniniListId(paniniListId);
		spu.setStatus(201L);
		spu.setShopType(1);
		spu.setShowApplet(1);
		List<SpuDTO> spuList = suidRich.select(spu).stream().map(s -> DozerUtils.map(s, SpuDTO.class)).collect(Collectors.toList());
		return OutDTO.ok().put("groupInfos",groupInfo).put("spuList",spuList);
	}

	@ApiLog(title = "查询PaniniCheckList", businessType = BusinessType.SEARCH)
	@ResponseBody
	@RequestMapping(value = "/{version}/panini/goods/list", method = RequestMethod.POST)
	@ApiVersion(1.0)
	@ApiOperation("查询PaniniCheckList,参数,paniniListId,搜索:keyword")
	public OutDTO getPaniniCheckList(@RequestBody InDto inDto) {
		GoodsQuery param=inDto.buildParam(new GoodsQuery());
		if(param.getPaniniListId()==null){
			return OutDTO.error500("参数为空!");
		}
		param.setPageNo(inDto.getPageNo());
		param.setPageSize(inDto.getPageSize());
		return OutDTO.ok().put("goods",checklistCalendarService.getPaniniCheckList(param));
	}
}
