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

/**
 * 最多创意奖
 */
@Component
public class UserBestNumInn 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 innNumMax = wcxInnovationMapper.findBestNumberInnNumMax(startDate, endDate);
        // 获取奖项内容
        List<WcxUserDto> wcxUserDtos = wcxUserMapper.findBestNumberInn(startDate, endDate, innNumMax);
        if (wcxUserDtos == null || wcxUserDtos.isEmpty()) {
            return Collections.EMPTY_LIST;
        }

        Date nowDate = new Date();
        long allJoinNumber = wcxUserMapper.selectAllJoinNumber(startDate, endDate);
        long allThumbNumber = wcxThumbMapper.selectAllThumbNumber(startDate, endDate);

        // 设置奖项内容
        List<WcxPrizeDto> wcxPrizeDtos = new ArrayList<>();
        for (WcxUserDto wcxUserDto : wcxUserDtos) {
            WcxPrizeDto wcxPrizeDto = new WcxPrizeDto();
            // master
            wcxPrizeDto.setPrizeId(null);
            wcxPrizeDto.setPrizeTitle(null);
            wcxPrizeDto.setPrizeType(WcxConstant.PRIZE_TYPE_BEST_NUMBER_INNOVATION);
            wcxPrizeDto.setAllAwardNumber((long) wcxUserDtos.size()); // 奖项数量
            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 - user
            String userId = wcxUserDto.getUserId();
            wcxPrizeDto.setPrizeUserId(userId);
            wcxPrizeDto.setPrizeEmployeeName(wcxUserDto.getEmployeeName());
            // 设置区域,项目
            setProjectAndRegion(wcxPrizeDto, wcxUserDto.getEmployeeCardId());
            wcxPrizeDto.setPrizeEmployeeHeadPicUrl(wcxUserMapper.findHeadPicUrlByPrimaryKey(userId));
            wcxPrizeDto.setPrizeUserInnNum(getInnNum(userId, startDate, endDate));
            wcxPrizeDto.setPrizeUserThumbNum(null); // getThumbedNum(userId, startDate, endDate)

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

    private int getThumbedNum(String userId,Date startDate, Date endDate){
        Integer thumbedNum = wcxThumbMapper.findThumbedNum(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;
        }
        Date newDate = new Date();

        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_NUMBER_INNOVATION);
        wcxPrize.setPrizeMonthDate(wcxPrizeDto.getPrizeMonthDate());
        wcxPrize.setStatus(WcxConstant.STATUS_GET);
        wcxPrize.setAllAwardNumber(wcxPrizeDto.getAllAwardNumber());
        wcxPrize.setAllJoinNumber(wcxPrizeDto.getAllJoinNumber());
        wcxPrize.setAllThumbNumber(wcxPrizeDto.getAllThumbNumber());
        wcxPrize.setCreateDate(newDate);
        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.setPrizeUserId(ele.getPrizeUserId());
            item.setPrizeEmployeeName(ele.getPrizeEmployeeName());
            item.setPrizeUserProject(ele.getPrizeUserProject());
            item.setPrizeUserRegion(ele.getPrizeUserRegion());
            item.setPrizeEmployeeHeadPicUrl(ele.getPrizeEmployeeHeadPicUrl());
            item.setPrizeUserInnNum(ele.getPrizeUserInnNum());
            item.setPrizeUserThumbNum(ele.getPrizeUserThumbNum());

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

}