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

import cn.com.countrygarden.bi.main.constant.wcx.WcxConstant;
import cn.com.countrygarden.bi.main.constant.wcx.enumeration.WcxBaselineEnum;
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.exception.ServiceException;
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;

/**
 * 各基础业务线前3名
 */
@Component
public class InnoBaselineTop3 extends WcxPrizeHandle {
    @Autowired
    private WcxInnovationMapper wcxInnovationMapper;
    @Autowired
    private WcxUserMapper wcxUserMapper;
    @Autowired
    private WcxThumbMapper wcxThumbMapper;
    @Autowired
    private WcxPrizeMapper wcxPrizeMapper;

    /**
     * 抽取各业务线奖前3名奖
     * 1,获取各基础业务线前3名的点赞量A,B,C
     * 2,获取点赞量为A,B,C的创新
     * @param startDate
     * @param endDate
     * @return
     */
    @Override
    public List<WcxPrizeDto> selectItem(Date startDate, Date endDate) {
        // 获取奖项内容
        List<WcxInnovationDto> wcxInnDtos = new ArrayList<>();
        int allAwardNumber = 0; // 奖项数量
        // 获取各基础业务线前三的点赞量
        for (WcxBaselineEnum c : WcxBaselineEnum.values()) {
            String baseline = c.getBaselineCode();
            if(baseline.equals("7"))continue;
            List<Integer> thumbNum = wcxInnovationMapper.selectInnTop3NumberThumbNum(startDate, endDate, baseline);
            if (thumbNum == null || thumbNum.isEmpty()) {
                continue;
            }
            List<WcxInnovationDto> wcxInnovationDtos = wcxInnovationMapper.selectInnTop3Number(startDate, endDate, baseline, thumbNum);
            wcxInnDtos.addAll(wcxInnovationDtos);
            allAwardNumber = allAwardNumber + thumbNum.size();
        }
        if (allAwardNumber == 0) {
            return Collections.EMPTY_LIST;
            //throw new ServiceException("对应抽取条件下的数据不存在");
        }

        Date nowDate = new Date();
        long allJoinNumber = wcxUserMapper.selectAllJoinNumberExclude(startDate, endDate);
        long allThumbNumber = wcxThumbMapper.selectAllThumbNumberExclude(startDate, endDate);
        // 设置奖项内容
        List<WcxPrizeDto> wcxPrizeDtos = new ArrayList<>();
        for (WcxInnovationDto wcxInnDto : wcxInnDtos) {
            WcxPrizeDto wcxPrizeDto = new WcxPrizeDto();
            // master
            wcxPrizeDto.setPrizeId(null);
            wcxPrizeDto.setPrizeTitle(null);
            wcxPrizeDto.setPrizeType(WcxConstant.PRIZE_TYPE_BEST_GET_POINT_BASELINE_TOP3);
            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 - 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());

            wcxPrizeDto.setItemStatus(WcxConstant.STATUS_GET);
            wcxPrizeDto.setCreateDate(wcxInnDto.getCreateDate());
            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);
        Date nowDate = new Date();
        // 奖单
        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_BASELINE_TOP3);
        wcxPrize.setPrizeMonthDate(wcxPrizeDto.getPrizeMonthDate());
        wcxPrize.setStatus(WcxConstant.STATUS_GET);
        wcxPrize.setAllAwardNumber(wcxPrizeDto.getAllAwardNumber());
        wcxPrize.setAllJoinNumber(wcxPrizeDto.getAllJoinNumber());
        wcxPrize.setAllThumbNumber(wcxPrizeDto.getAllThumbNumber());
        wcxPrize.setCreateDate(nowDate);
        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.setInnEmployeeHeadPicUrl(ele.getInnEmployeeHeadPicUrl());
            item.setInnThumbNumber(ele.getInnThumbNumber());
            /*item.setPrizeUserId(ele.getPrizeUserId());
            item.setPrizeEmployeeName(ele.getPrizeEmployeeName());
            item.setPrizeUserProject(ele.getPrizeUserProject());
            item.setPrizeUserRegion(ele.getPrizeUserRegion());*/
            // 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(nowDate);
            // 返回对应id
            ele.setPrizeItemId(itemId);
            ele.setPrizeId(prizeId);
            wcxPrizeItems.add(item);
        }
        wcxPrizeMapper.insertPrize(wcxPrize);
        wcxPrizeMapper.batchInsertPrizeItems(wcxPrizeItems);
        return wcxPrizeItems.size();
    }

}