package cn.com.ctop.performanceappraisal.service.impl;

import cn.com.ctop.common.module.utils.ResultMapUtils;
import cn.com.ctop.common.module.utils.StatusCode;
import cn.com.ctop.performanceappraisal.entity.OptimizerConfig;
import cn.com.ctop.performanceappraisal.entity.PerformanceOptimizer;
import cn.com.ctop.performanceappraisal.mapper.PerformanceOptimizerMapper;
import cn.com.ctop.performanceappraisal.service.IOptimizerConfigService;
import cn.com.ctop.performanceappraisal.service.IPerformanceOptimizerService;
import cn.com.ctop.performanceappraisal.service.IPerformanceService;
import cn.com.ctop.performanceappraisal.vo.OptimizerCostDetailVO;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.system.vo.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 优化师绩效信息
 *
 * @author jeecg-boot
 * @version V1.0
 * @date 2019-12-13
 */
@Service
public class PerformanceOptimizerServiceImpl extends ServiceImpl<PerformanceOptimizerMapper, PerformanceOptimizer> implements IPerformanceOptimizerService {
    @Autowired
    IPerformanceService performanceService;
    @Autowired
    private IOptimizerConfigService optimizerConfigService;

    @Override
    public Map<String, Object> getPerformanceDetail(String mediaType, Integer pageNo, Integer pageSize) {
        Map<String, Object> result = new HashMap<>();
        Integer startIndex = (pageNo - 1) * pageSize;
        //查询当前登录人信息
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
//        LoginUser sysUser = new LoginUser();
//        sysUser.setId("fcb3fbf6192640adaadb2a7df9c02dae");
        //查询当前媒体配置信息
        OptimizerConfig config = optimizerConfigService.getEnabledConfigByMediaType(mediaType);
        if (null == config) {
            ResultMapUtils.setResultMap(result, StatusCode.KUAISHOU_OPTINIZER_CONFIG_IS_NULL.getCode());
            return result;
        }
        //:查询当前用户截止到目前当前季度的消耗统计数据
        List<OptimizerCostDetailVO> vos = performanceService.getOptimizerTotalCostDetailGroupByUserId(sysUser.getId(), startIndex, pageSize);
        if (null == vos || vos.size() <= 0) {
            ResultMapUtils.setResultMap(result, StatusCode.KUAISHOU_OPTINIZER_PERSONNAL_DATA_IS_NULL.getCode());
            return result;
        }
        //2: 查询媒体季度任务完成情况
        OptimizerCostDetailVO totalCostVo = performanceService.getOptimizerTotalCost();
        OptimizerCostDetailVO detailVO = vos.get(0);
        detailVO.setTotalRoyalty(getTotalRoyalty(config, detailVO, totalCostVo));
        JSONObject detail = new JSONObject();
        detail.put("realName", detailVO.getRealName());
        detail.put("totalCost", detailVO.getTotalCost());
        detail.put("totalRoyalty", detailVO.getTotalRoyalty());
        detail.put("mediaTask", config.getMediaTask());
        detail.put("personalTask", config.getPersonalTask());

        detail.put("year", detailVO.getYear());
        detail.put("quarter", detailVO.getQuarter());
        detail.put("royaltyRatio", getRate(config, detailVO, totalCostVo).toString() + "‰");
        result.put("userDetail", detail);
        ResultMapUtils.setResultMap(result, StatusCode.COMMON_SUCCESS.getCode());
        return result;
    }

    @Override
    public Map<String, Object> getRankingList(String mediaType, Integer pageNo, Integer pageSize) {
        Map<String, Object> result = new HashMap<>();
        Integer startIndex = (pageNo - 1) * pageSize;
        List<OptimizerCostDetailVO> vos = performanceService.getOptimizerTotalCostDetailGroupByUserId(null, startIndex, pageSize);
        if (null == vos || vos.size() <= 0) {
            ResultMapUtils.setResultMap(result, StatusCode.KUAISHOU_CRAWLER_APP_ELEMENT_IS_NULL.getCode());
            return result;
        }
        //查询优化师系统设置数据
        OptimizerConfig config = optimizerConfigService.getEnabledConfigByMediaType("2");
        //2: 查询媒体季度任务完成情况
        OptimizerCostDetailVO totalCostVo = performanceService.getOptimizerTotalCost();
        //3：计算单个优化师提成总额
        List<OptimizerCostDetailVO> getDetailVos = new ArrayList<>();
        if (null != vos && vos.size() > 0) {
            for (OptimizerCostDetailVO vo : vos) {
                vo.setTotalRoyalty(getTotalRoyalty(config, vo, totalCostVo));
                getDetailVos.add(vo);
            }
        }
        result.put("data", getDetailVos);
        ResultMapUtils.setResultMap(result, StatusCode.COMMON_SUCCESS.getCode());
        return result;
    }

    @Override
    public Map<String, Object> getManagerPerformanceDetail(String mediaType) {
        Map<String, Object> result = new HashMap<>();
        //获取经理相关信息
        //查询优化师系统设置数据
        OptimizerConfig config = optimizerConfigService.getEnabledConfigByMediaType(mediaType);
        //2: 查询媒体季度任务完成情况
        OptimizerCostDetailVO totalCostVo = performanceService.getOptimizerTotalCost();
        result.put("totalCost", totalCostVo.getTotalCost().toString());
        if (totalCostVo.getTotalCost().compareTo(config.getMediaTask()) < 0) {
            //当前消耗季度任务未完成
            ResultMapUtils.setResultMap(result, StatusCode.COMMON_SUCCESS.getCode());
            result.put("totalRoyalty", totalCostVo.getTotalRoyalty().toString());
            return result;
        }
        //3：查询经理手下所有优化师的总提成
        result = performanceService.getMemberPerformanceByUserId(config, totalCostVo);
        return result;
    }

    private BigDecimal getTotalRoyalty(OptimizerConfig config, OptimizerCostDetailVO vo, OptimizerCostDetailVO quarterCost) {
        BigDecimal royalty = BigDecimal.ZERO;
        //个人总消耗
        BigDecimal totalCost = vo.getTotalCost();
        //个人季度任务
        BigDecimal personalTask = config.getPersonalTask();
        //媒体季度任务
        BigDecimal mediaTask = config.getMediaTask();
        //个人任务未完成
        if (totalCost.compareTo(personalTask) < 0) {
            return royalty;
        }
        //季度任务未完成
        if (totalCost.compareTo(mediaTask) < 0) {
            return totalCost.multiply(config.getMediaTaskNoFinishRate()).divide(new BigDecimal("1000")).setScale(2, RoundingMode.HALF_UP);
        }

        return totalCost.multiply(config.getMediaTaskFinishRate()).divide(new BigDecimal("1000")).setScale(2, RoundingMode.HALF_UP);
    }

    private BigDecimal getRate(OptimizerConfig config, OptimizerCostDetailVO vo, OptimizerCostDetailVO quarterCost) {
        BigDecimal royalty = BigDecimal.ZERO;
        //个人总消耗
        BigDecimal totalCost = vo.getTotalCost();
        //个人季度任务
        BigDecimal personalTask = config.getPersonalTask();
        //媒体季度任务
        BigDecimal mediaTask = config.getMediaTask();
        //个人任务未完成
        if (totalCost.compareTo(personalTask) < 0) {
            return BigDecimal.ZERO;
        }
        //季度任务未完成
        if (totalCost.compareTo(mediaTask) < 0) {
            return config.getMediaTaskNoFinishRate().setScale(2, RoundingMode.HALF_UP);
        }

        return config.getMediaTaskFinishRate().setScale(2, RoundingMode.HALF_UP);
    }
}
