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.*;
import cn.com.countrygarden.bi.main.dto.wcx.WcxInnovationDto;
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.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 UserLeaderBestJoin extends WcxPrizeHandle {
    @Autowired
    private WcxInnovationMapper wcxInnovationMapper;
    @Autowired
    private WcxUserMapper wcxUserMapper;
    @Autowired
    private WcxThumbMapper wcxThumbMapper;
    @Autowired
    private WcxPrizeMapper wcxPrizeMapper;
    @Autowired
    private WcxRegionProjectMapper wcxRegionProjectMapper;

    @Override
    public List<WcxPrizeDto> selectItem(Date startDate, Date endDate) {
        int limitNum = 10;
        Integer thumbCountNumMin = wcxThumbMapper.findThumbCountNumMinOfLeaderExclude(startDate, endDate, limitNum);
        if (thumbCountNumMin == null) {
            return Collections.EMPTY_LIST;
            //throw new ServiceException("对应抽取条件下的数据不存在");
        }

        List<WcxInnovationDto> wcxInnDtos = wcxInnovationMapper.findInnByThumbCountMinExclude(startDate, endDate, thumbCountNumMin);

        Date nowDate = new Date();
        int allAwardNumber = wcxInnDtos.size();
        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_LEADER_PROJECT);
            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;
    }

    private WcxPrizeDto setProjectAndRegion(WcxPrizeDto wcxPrizeDto, String employeeCardId){
        WcxRegionProjectDto condition = new WcxRegionProjectDto();
        condition.setEmployeeCardId(employeeCardId);
        WcxRegionProjectDto wcxRegProDto = wcxRegionProjectMapper.selectOne(condition);

        if (wcxRegProDto != null) {
            wcxPrizeDto.setPrizeUserProject(wcxRegProDto.getProjectName());
            wcxPrizeDto.setPrizeUserRegion(wcxRegProDto.getRegionName());
        }
        return wcxPrizeDto;
    }

    /**
     * 获取点赞次数
     * @param userId
     * @param startDate
     * @param endDate
     * @return
     */
    private int getThumbedCountNum(String userId,Date startDate, Date endDate){
        Integer thumbedNum = wcxThumbMapper.findThumbedCountNum(userId, startDate, endDate);
        if (thumbedNum == null) {
            thumbedNum = 0;
        }
        return thumbedNum;
    }

    private int getInnNum(String userId,Date startDate, Date endDate){
        Integer innNum = wcxInnovationMapper.findInnNum(userId, startDate, endDate);
        if (innNum == null) {
            innNum = 0;
        }
        return innNum;
    }

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

}