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.WcxInnovationDto;
import cn.com.countrygarden.bi.main.dto.wcx.WcxPrizeDto;
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.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;

/**
 * - 筛选掉以前获过奖的创意
 * - 取`时间A,时间B之间新增的创意`中`时间A,时间B之间对创意点赞`量的前20%,比如新增了101个,取创意点赞的前20位
 * - 当其中有角色编码为leader(大领导)的人员对创意C点赞,但并不在20位中,则从后顶替掉本该抽取的创意
 */
@Component
public class InnoTop20 extends WcxPrizeHandle {
    @Autowired
    private WcxInnovationMapper wcxInnovationMapper;
    @Autowired
    private WcxUserMapper wcxUserMapper;
    @Autowired
    private WcxThumbMapper wcxThumbMapper;
    @Autowired
    private WcxPrizeMapper wcxPrizeMapper;

    public List<WcxPrizeDto> selectItem(Date startDate, Date endDate) {
        Date defaultDate = DateUtil.formatStrToDate("2010-01-01", DateUtil.COMMON_DATE_FORMAT);
        // 时间段内获取所有创意的数量,且没获过奖
        //int i = wcxInnovationMapper.findInnCountBetweenDate(defaultDate, endDate);
        Integer i = wcxThumbMapper.findInnCountBetweenDateThumbExclude(startDate, endDate);
        if (i == null || i == 0) {
            return Collections.EMPTY_LIST;
        }

        // 时间段内大领导点赞过的创意,且没获过奖
        List<String> bigLeaderThumbInnIds = wcxInnovationMapper.findInnIdsBigLeaderThumbExclude(startDate, endDate);
        int bigLeaderThumbNum = 0;
        if (bigLeaderThumbInnIds != null && bigLeaderThumbInnIds.size() > 0) {
            bigLeaderThumbNum = bigLeaderThumbInnIds.size();
        }

        // 如果大领导点赞过的创意超过20%
        double percent20 = 0.2;
        int num = (int) Math.floor(i * percent20);

        List<WcxInnovationDto> wcxInnDtos = null;
        int limitNum = 250;
        if (num >= limitNum) {
            // 当大于250
            if (bigLeaderThumbNum >= limitNum) {
                // 大领导点赞数大于等于限制数
                wcxInnDtos = wcxInnovationMapper.findInnExclude(startDate, endDate, bigLeaderThumbInnIds);
            } else {
                // 大领导点赞数小于限制数
                if (bigLeaderThumbNum == 0) {
                    bigLeaderThumbInnIds = new ArrayList<>();
                    bigLeaderThumbInnIds.add("1");
                }
                List<String> innIds = wcxInnovationMapper.findInnIdsPercent20Exclude(startDate, endDate, limitNum, bigLeaderThumbInnIds);
                // 查询最后一位数量
                String percent20Id = innIds.get(innIds.size() - 1);
                Integer percent20Num = wcxThumbMapper.findThumbedNumByInnoId(percent20Id, startDate, endDate);
                percent20Num = percent20Num == null ? 0 : percent20Num;

                List<String> innIdsExtra = wcxInnovationMapper.findInnIdsByThumbNumberExclude(startDate, endDate, percent20Num);
                if (innIdsExtra != null && innIdsExtra.size() > 0) {
                    innIds.addAll(innIdsExtra);
                }
                wcxInnDtos = wcxInnovationMapper.findInnExclude(startDate, endDate, innIds);
            }
        } else if (num > 0){
            // 当小于250
            if (bigLeaderThumbNum >= limitNum) {
                // 大领导点赞数大于等于限制数
                wcxInnDtos = wcxInnovationMapper.findInnExclude(startDate, endDate, bigLeaderThumbInnIds);
            } else {
                // 大领导点赞数小于限制数
                if (bigLeaderThumbNum == 0) {
                    bigLeaderThumbInnIds = new ArrayList<>();
                    bigLeaderThumbInnIds.add("1");
                }
                List<String> innIds = wcxInnovationMapper.findInnIdsPercent20Exclude(startDate, endDate, num, bigLeaderThumbInnIds);

                // 查询最后一位数量
                String percent20Id = "";
                if(innIds.size()!=0){
                    percent20Id = innIds.get(num - 1);
                }
                Integer percent20Num = wcxThumbMapper.findThumbedNumByInnoId(percent20Id, startDate, endDate);
                percent20Num = percent20Num == null ? 0 : percent20Num;

                List<String> innIdsExtra = wcxInnovationMapper.findInnIdsByThumbNumberExclude(startDate, endDate, percent20Num);
                if (innIdsExtra != null && innIdsExtra.size() > 0) {
                    innIds.addAll(innIdsExtra);
                }
                wcxInnDtos = wcxInnovationMapper.findInnExclude(startDate, endDate, innIds);
            }
        } else {
            return Collections.EMPTY_LIST;
        }

        // 处理边界条件
        long allJoinNumber = wcxUserMapper.selectAllJoinNumberExclude(startDate, endDate);
        long allThumbNumber = wcxThumbMapper.selectAllThumbNumberExclude(startDate, endDate);
        Date nowDate = new Date();
        Long awardNum = (long) wcxInnDtos.size();

        List<WcxPrizeDto> wcxPrizeDtos = new ArrayList<>();
        for (WcxInnovationDto wcxInnDto : wcxInnDtos) {
            WcxPrizeDto wcxPrizeDto = new WcxPrizeDto();
            // master
            wcxPrizeDto.setPrizeId(CommonUtil.getUUID());
            wcxPrizeDto.setPrizeTitle(null);
            wcxPrizeDto.setPrizeType(WcxConstant.PRIZE_TYPE_BEST_GET_POINT_20);
            wcxPrizeDto.setAllAwardNumber(awardNum);
            wcxPrizeDto.setAllJoinNumber(allJoinNumber);
            wcxPrizeDto.setAllThumbNumber(allThumbNumber);
            wcxPrizeDto.setPrizeMonthDate(DateUtil.formatDate(startDate, DateUtil.COMMON_DATE_FORMAT_YM));
            wcxPrizeDto.setBatchCode(null);
            wcxPrizeDto.setPrizeOpenDate(null);
            wcxPrizeDto.setStatus(null);
            wcxPrizeDto.setCreateDate(nowDate);
            wcxPrizeDto.setStartDate(startDate);
            wcxPrizeDto.setEndDate(endDate);
            // items - inn
            wcxPrizeDto.setPrizeItemId(null);
            wcxPrizeDto.setInnId(wcxInnDto.getInnovationId());
            wcxPrizeDto.setInnTitle(wcxInnDto.getInnovationTitle());
            wcxPrizeDto.setInnContent(wcxInnDto.getInnovationContent());
            wcxPrizeDto.setInnType(wcxInnDto.getInnovationType());
            wcxPrizeDto.setInnBaseline(wcxInnDto.getBaseline());
            wcxPrizeDto.setInnClassification(wcxInnDto.getClassification());
            wcxPrizeDto.setInnTeamContent(wcxInnDto.getTeamContent());
            wcxPrizeDto.setInnProjectName(wcxInnDto.getProjectName());
            wcxPrizeDto.setInnRegionName(wcxInnDto.getRegionName());
            wcxPrizeDto.setInnUserId(wcxInnDto.getUserId());
            wcxPrizeDto.setInnEmployeeName(wcxInnDto.getEmployeeName());
            wcxPrizeDto.setInnEmployeeHeadPicUrl(wcxUserMapper.findHeadPicUrlByPrimaryKey(wcxInnDto.getUserId()));
            wcxPrizeDto.setInnThumbNumber(wcxInnDto.getThumbNumber());
            // items - user
            wcxPrizeDto.setPrizeUserId(null);
            wcxPrizeDto.setPrizeEmployeeName(null);
            wcxPrizeDto.setPrizeUserProject(null);
            wcxPrizeDto.setPrizeUserRegion(null);
            // items - project
            wcxPrizeDto.setProjectUserId(null);
            wcxPrizeDto.setProjectEmployeeName(null);
            wcxPrizeDto.setProjectPrizeInnNumber(null);
            wcxPrizeDto.setProjectName(null);
            wcxPrizeDto.setProjectHeadPicUrl(null);
            // items - region
            wcxPrizeDto.setRegionUserId(null);
            wcxPrizeDto.setRegionEmployeeName(null);
            wcxPrizeDto.setRegionPrizeInnNumber(null);
            wcxPrizeDto.setRegionName(null);
            wcxPrizeDtos.add(wcxPrizeDto);
        }
        return wcxPrizeDtos;
    }

    @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_GET_POINT_20);
        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.getInnRegionName());
            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(null);
            item.setProjectName(null);
            item.setProjectEmployeeName(null);
            item.setProjectHeadPicUrl(null);
            item.setProjectPrizeInnNumber(null);
            // todo 区域总 项目总待确认
            item.setRegionUserId(null);
            item.setRegionName(null);
            item.setRegionEmployeeName(null);
            item.setRegionHeadPicUrl(null);
            item.setRegionPrizeInnNumber(null);

            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();
    }

}