package org.jeecg.modules.performance.service.impl;

import com.alibaba.fastjson.JSONObject;
import org.jeecg.modules.performance.constants.PeriodConstant;
import org.jeecg.modules.performance.constants.RateConstant;
import org.jeecg.modules.performance.constants.StandardValue;
import org.jeecg.modules.performance.entity.PerformanceQuarter;
import org.jeecg.modules.performance.mapper.CalculatePerformanceMapper;
import org.jeecg.modules.performance.service.ICalculatePerformanceService;
import org.jeecg.modules.performance.service.IPerformanceQuarterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 *  Created by JQ.bi
 */
@Service
public class CalculatePerformanceServiceImpl implements ICalculatePerformanceService {

    @Autowired
    CalculatePerformanceMapper calculatePerformanceMapper;
    @Autowired
    IPerformanceQuarterService performanceQuarterService;

    @Override
    public void CalculateOperatePerformance(int year, String quarter, int mediaType) {
        //TODO 查询配置的提成点
        BigDecimal point =new BigDecimal(0);
        if (mediaType == 1) {
            point = RateConstant.THOUSAND_2;
        }
        if (mediaType == 2){
            point = RateConstant.THOUSAND_3;
        }
        String startDate= year+"-"+ PeriodConstant.valueOf(quarter).getStart();
        String endDate= year+"-"+PeriodConstant.valueOf(quarter).getEnd();
        List<JSONObject> operateCost = calculatePerformanceMapper.queryOperateCostGroupByUser(startDate, endDate, mediaType);
        List<PerformanceQuarter> performanceQuarters = new ArrayList<>();
        PerformanceQuarter performanceQuarter;
        for(JSONObject op:operateCost){
            performanceQuarter = new PerformanceQuarter();
            performanceQuarter.setUserId(op.getString("userId"));
            performanceQuarter.setUserName(op.getString("userName"));
            performanceQuarter.setCompanyId(op.getString("companyId"));
            performanceQuarter.setCompanyName(op.getString("companyName"));
            performanceQuarter.setDepartment("运营部");
            performanceQuarter.setRoleCode(op.getString("roleCode"));
            performanceQuarter.setRoleName(op.getString("roleName"));
            performanceQuarter.setLeader(op.getString("leaderId"));
            performanceQuarter.setYear(year);
            performanceQuarter.setQuarter(quarter);
            BigDecimal cost=op.getBigDecimal("cost");
            performanceQuarter.setCost(cost);
            if(cost.compareTo(StandardValue.QUARTER_CONSUMPTION)>=0){
                performanceQuarter.setPerformance(cost.multiply(point));
            }else {
                performanceQuarter.setPerformance(StandardValue.PERFORMANCE_ZERO);
            }
            performanceQuarter.setPoint(point);
            performanceQuarter.setMediaType(mediaType);
            performanceQuarters.add(performanceQuarter);
        }
        performanceQuarterService.saveBatch(performanceQuarters);
    }

    @Override
    public void CalculateDesignPerformance(int year, String quarter, int mediaType) {
        String startDate= year+"-"+ PeriodConstant.valueOf(quarter).getStart();
        String endDate= year+"-"+PeriodConstant.valueOf(quarter).getEnd();
        List<JSONObject> designCost = calculatePerformanceMapper.queryDesignCostGroupByUser(startDate, endDate, mediaType);
        List<PerformanceQuarter> performanceQuarters = new ArrayList<>();
        PerformanceQuarter performanceQuarter;
        for(JSONObject op:designCost){
            performanceQuarter = new PerformanceQuarter();
            performanceQuarter.setUserId(op.getString("userId"));
            performanceQuarter.setUserName(op.getString("userName"));
            performanceQuarter.setCompanyId(op.getString("companyId"));
            performanceQuarter.setCompanyName(op.getString("companyName"));
            performanceQuarter.setDepartment("设计部");
            performanceQuarter.setRoleCode(op.getString("roleCode"));
            performanceQuarter.setRoleName(op.getString("roleName"));
            performanceQuarter.setYear(year);
            performanceQuarter.setQuarter(quarter);
            BigDecimal cost=op.getBigDecimal("cost");
            performanceQuarter.setCost(cost);
            performanceQuarter.setPerformance(RateConstant.PERCENT_0);
            performanceQuarter.setPoint(RateConstant.PERCENT_0);
            performanceQuarter.setMediaType(mediaType);
            performanceQuarters.add(performanceQuarter);
        }
        performanceQuarterService.saveBatch(performanceQuarters);
    }

    @Override
    public void CalculateSalePerformance(int year, String quarter, int mediaType) {
        int quarterInt= Integer.parseInt(quarter.substring(1));
        List<JSONObject> saleCost = calculatePerformanceMapper.querySaleCostGroupByUser(year, quarterInt, mediaType);
        List<PerformanceQuarter> performanceQuarters = new ArrayList<>();
        PerformanceQuarter performanceQuarter;
        for(JSONObject op:saleCost){
            performanceQuarter = new PerformanceQuarter();
            performanceQuarter.setUserId(op.getString("userId"));
            performanceQuarter.setUserName(op.getString("userName"));
            performanceQuarter.setCompanyId(op.getString("companyId"));
            performanceQuarter.setCompanyName(op.getString("companyName"));
            performanceQuarter.setDepartment("销售部");
            performanceQuarter.setRoleCode(op.getString("roleCode"));
            performanceQuarter.setRoleName(op.getString("roleName"));
            performanceQuarter.setYear(year);
            performanceQuarter.setQuarter(quarter);
            BigDecimal cost=op.getBigDecimal("cost");
            performanceQuarter.setCost(cost);
            performanceQuarter.setPerformance(RateConstant.PERCENT_0);
            performanceQuarter.setPoint(RateConstant.PERCENT_0);
            performanceQuarter.setMediaType(mediaType);
            performanceQuarters.add(performanceQuarter);
        }
        performanceQuarterService.saveBatch(performanceQuarters);
    }
}
