package cn.com.countrygarden.bi.main.service.wcx.impl;

import cn.com.countrygarden.bi.main.constant.wcx.WcxConstant;
import cn.com.countrygarden.bi.main.dao.wcx.WcxInnovationMapper;
import cn.com.countrygarden.bi.main.dao.wcx.WcxPrizeMapper;
import cn.com.countrygarden.bi.main.dao.wcx.WcxThumbMapper;
import cn.com.countrygarden.bi.main.dao.wcx.WcxUserMapper;
import cn.com.countrygarden.bi.main.dto.wcx.WcxPrizeDto;
import cn.com.countrygarden.bi.main.dto.wcx.WcxRegionProjectDto;
import cn.com.countrygarden.bi.main.handler.wcx.WcxPrizeHandle;
import cn.com.countrygarden.bi.main.model.wcx.WcxPrize;
import cn.com.countrygarden.bi.main.model.wcx.WcxPrizeItem;
import cn.com.countrygarden.bi.main.service.wcx.WcxRegionProjectService;
import cn.com.countrygarden.bi.main.util.wcx.CommonUtil;
import cn.com.countrygarden.bi.main.util.wcx.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * 区域下创意最多
 */
@Component
public class RegionBest extends WcxPrizeHandle {
    @Autowired
    private WcxInnovationMapper wcxInnovationMapper;
    @Autowired
    private WcxUserMapper wcxUserMapper;
    @Autowired
    private WcxThumbMapper wcxThumbMapper;
    @Autowired
    private WcxPrizeMapper wcxPrizeMapper;
    @Autowired
    private WcxRegionProjectService wcxRegionProjectService;

    @Override
    public List<WcxPrizeDto> selectItem(Date startDate, Date endDate) {
        List<WcxRegionProjectDto> wcxRegionProjectDtos = wcxRegionProjectService.selectBestPassNumberRegion(startDate, endDate);

        if (wcxRegionProjectDtos == null || wcxRegionProjectDtos.size() <= 0) {
            return Collections.emptyList();
        }

        // 处理边界条件
        long allJoinNumber = wcxUserMapper.selectAllJoinNumber(startDate, endDate);
        long allThumbNumber = wcxThumbMapper.selectAllThumbNumber(startDate, endDate);
        Date nowDate = new Date();

        List<WcxPrizeDto> wcxPrizeDtos = new ArrayList<>();
        for (WcxRegionProjectDto rpDto : wcxRegionProjectDtos) {
            WcxPrizeDto wcxPrizeDto = new WcxPrizeDto();
            // master
            wcxPrizeDto.setPrizeId(null);
            wcxPrizeDto.setPrizeTitle(null);
            wcxPrizeDto.setPrizeType(WcxConstant.PRIZE_TYPE_BEST_PASS_REGION);
            //wcxPrizeDto.setAllAwardNumber((long) allAwardNumber); // 奖项数量
            wcxPrizeDto.setAllJoinNumber(allJoinNumber); // 参与人数
            wcxPrizeDto.setAllThumbNumber(allThumbNumber);
            wcxPrizeDto.setPrizeMonthDate(DateUtil.formatDate(startDate, DateUtil.COMMON_DATE_FORMAT_YM));
            wcxPrizeDto.setBatchCode(null);
            wcxPrizeDto.setPrizeOpenDate(null);
            wcxPrizeDto.setStatus(WcxConstant.STATUS_GET);
            wcxPrizeDto.setCreateDate(nowDate);
            wcxPrizeDto.setStartDate(startDate);
            wcxPrizeDto.setEndDate(endDate);
            // items - project
            /*wcxPrizeDto.setProjectUserId(null);
            wcxPrizeDto.setProjectEmployeeName(rpDto.getProjectEmployeeName());
            wcxPrizeDto.setProjectPrizeInnNumber(rpDto.getProjectPrizeInnNumber());
            wcxPrizeDto.setProjectName(rpDto.getProjectName());
            wcxPrizeDto.setProjectHeadPicUrl(rpDto.getProjectHeadPicUrl());*/
            // items - region
            wcxPrizeDto.setRegionUserId(null);
            wcxPrizeDto.setRegionEmployeeName(rpDto.getRegionEmployeeName());
            wcxPrizeDto.setRegionPrizeInnNumber(rpDto.getRegionPrizeInnNumber());
            wcxPrizeDto.setRegionName(rpDto.getRegionName());
            wcxPrizeDto.setRegionHeadPicUrl(rpDto.getRegionHeadPicUrl());
            wcxPrizeDtos.add(wcxPrizeDto);
        }
        return wcxPrizeDtos;
    }

    @Override
    @Transactional
    public int save(List<WcxPrizeDto> wcxPrizeDtos) {
        if (wcxPrizeDtos == null || wcxPrizeDtos.size() <= 0) {
            return 0;
        }

        WcxPrizeDto wcxPrizeDto = wcxPrizeDtos.get(0);
        // 奖单
        WcxPrize wcxPrize = new WcxPrize();
        wcxPrize.setPrizeTitle(WcxConstant.DEFAULT_TITLE); // todo 缺陷
        String prizeId = CommonUtil.getUUID();
        wcxPrize.setPrizeId(prizeId);
        wcxPrize.setBatchCode(null);
        wcxPrize.setPrizeType(WcxConstant.PRIZE_TYPE_BEST_PASS_REGION);
        wcxPrize.setPrizeMonthDate(wcxPrizeDto.getPrizeMonthDate());
        wcxPrize.setStatus(WcxConstant.STATUS_GET);
        wcxPrize.setAllAwardNumber(wcxPrizeDto.getAllAwardNumber());
        wcxPrize.setAllJoinNumber(wcxPrizeDto.getAllJoinNumber());
        wcxPrize.setAllThumbNumber(wcxPrizeDto.getAllThumbNumber());
        wcxPrize.setCreateDate(wcxPrizeDto.getCreateDate());
        wcxPrize.setStartDate(wcxPrizeDto.getStartDate());
        wcxPrize.setEndDate(wcxPrizeDto.getEndDate());
        // 奖单明细
        List<WcxPrizeItem> wcxPrizeItems = new ArrayList<>();
        for (WcxPrizeDto ele : wcxPrizeDtos) {
            WcxPrizeItem item = new WcxPrizeItem();
            String itemId = CommonUtil.getUUID();
            item.setPrizeItemId(itemId);
            /*item.setInnId(ele.getInnId());
            item.setInnTitle(ele.getInnTitle());
            item.setInnContent(ele.getInnContent());
            item.setInnType(ele.getInnType());
            item.setInnBaseline(ele.getInnBaseline());
            item.setInnClassification(ele.getInnClassification());
            item.setInnTeamContent(ele.getInnTeamContent());
            item.setInnProjectName(ele.getInnProjectName());
            item.setInnRegionName(ele.getRegionName());
            item.setInnUserId(ele.getInnUserId());
            item.setInnEmployeeName(ele.getInnEmployeeName());
            item.setInnThumbNumber(ele.getInnThumbNumber());*/

            /*item.setPrizeUserId(ele.getPrizeUserId());
            item.setPrizeEmployeeName(ele.getPrizeEmployeeName());
            item.setPrizeUserProject(ele.getPrizeUserProject());
            item.setPrizeUserRegion(ele.getPrizeUserRegion());
            item.setInnEmployeeHeadPicUrl(ele.getInnEmployeeHeadPicUrl());*/
            // todo 区域总 项目总待确认
            /*item.setProjectUserId(ele.getProjectUserId());
            item.setProjectName(ele.getProjectName());
            item.setProjectEmployeeName(ele.getProjectEmployeeName());
            item.setProjectHeadPicUrl(ele.getProjectHeadPicUrl());
            item.setProjectPrizeInnNumber(ele.getProjectPrizeInnNumber());*/
            // todo 区域总 项目总待确认
            item.setRegionUserId(ele.getRegionUserId());
            item.setRegionName(ele.getRegionName());
            item.setRegionEmployeeName(ele.getRegionEmployeeName());
            item.setRegionHeadPicUrl(ele.getRegionHeadPicUrl());
            item.setRegionPrizeInnNumber(ele.getRegionPrizeInnNumber());

            item.setPrizeId(prizeId);
            item.setStatus(WcxConstant.STATUS_GET);
            item.setCreateDate(ele.getCreateDate());

            // 返回对应id
            ele.setPrizeId(prizeId);
            ele.setPrizeItemId(itemId);
            wcxPrizeItems.add(item);
        }
        wcxPrizeMapper.insertPrize(wcxPrize);
        wcxPrizeMapper.batchInsertPrizeItems(wcxPrizeItems);
        return wcxPrizeItems.size();
    }

}