package com.winsun.TimingTask;

import com.baomidou.mybatisplus.enums.SqlLike;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.winsun.auth.core.common.model.ResponseData;
import com.winsun.bean.HhrUser;
import com.winsun.bean.LzKpi;
import com.winsun.bean.LzWage;
import com.winsun.bean.SalesList;
import com.winsun.mapper.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.LinkedList;
import java.util.List;

/**
 * 〈每月自动生成上月楼长薪酬〉
 *
 * @author PXL
 * @create 2020/6/8 9:44
 */

@Slf4j
@RestController
@Configuration
@EnableScheduling
public class LzSalaryTask {

    private static LzWageMpapper lzWageMpapper;

    private static LzKpiMapper lzKpiMapper;

    private static SchoolMapper schoolMapper;

    private static UserSchoolMapper userSchoolMapper;

    private static HhrUserMapper hhrUserMapper;

    private static SalesListMapper salesOrderMapper;

    private static SysUserMapper sysUserMapper;

    @Autowired
    public LzSalaryTask(LzWageMpapper LzWageMpapper,LzKpiMapper lzKpiMapper,SchoolMapper schoolMapper,
                        UserSchoolMapper userSchoolMapper,HhrUserMapper hhrUserMapper,SysUserMapper sysUserMapper) {
        LzSalaryTask.lzWageMpapper = LzWageMpapper;
        LzSalaryTask.lzKpiMapper = lzKpiMapper;
        LzSalaryTask.schoolMapper = schoolMapper;
        LzSalaryTask.userSchoolMapper = userSchoolMapper;
        LzSalaryTask.hhrUserMapper = hhrUserMapper;
        LzSalaryTask.sysUserMapper = sysUserMapper;


    }
    /**
     * 每月生成楼长上月薪酬
     * @return
     */
    @Scheduled(cron = "0 10 2 1 * ?")//每月1号两点10分执行
    //@Scheduled(cron = "0 15 10 15 * ?")
    //@Scheduled(fixedRate=1000*60)
    @PostMapping("/lzSalaryTask")
    public ResponseData<String> createSalary() {

        List<LzWage> lzWageList = new LinkedList<>();
        List<String> userIds = new LinkedList<>();

        Date date = new Date();
        // 使用String类format()方法格式化日期
        String year = String.format("%tY", date);
        Integer m = Integer.valueOf(String.format("%tm", date));
        String month = "";
        if(m > 10){
            month = year+"-"+(m-1);
        }else if(m == 0){
            month = year+"-12";
        }else{
            month = year+"-0"+(m-1);
        }

        Wrapper<HhrUser> hhrUserWrapper = new EntityWrapper<>();
        hhrUserWrapper.eq("position","楼长");
        List<HhrUser> hhrUserList = hhrUserMapper.selectList(hhrUserWrapper);
        for (HhrUser hhrUser : hhrUserList) {
            LzWage lzWage = new LzWage();
            String userId = hhrUser.getId();
            lzWage.setMonth(month);
            lzWage.setUserId(Integer.valueOf(userId));
            userIds.add(userId);
            lzWageList.add(lzWage);
        }

        Wrapper<SalesList> salesOrderWrapper =  new EntityWrapper<>();
        salesOrderWrapper.in("user_id",userIds);
        salesOrderWrapper.like("activate_time",month, SqlLike.RIGHT);
        List<SalesList> salesOrderList = salesOrderMapper.selectList(salesOrderWrapper);

        Wrapper<LzKpi> lzKpiWrapper =  new EntityWrapper<>();
        lzKpiWrapper.in("user_id",userIds);
        lzKpiWrapper.eq("month",month);
        List<LzKpi> lzKpiList = lzKpiMapper.selectList(lzKpiWrapper);

        Wrapper<LzWage> wrageWrapper =  new EntityWrapper<>();
        wrageWrapper.in("user_id",userIds);
        wrageWrapper.in("month",month);
        List<LzWage> wrageList = lzWageMpapper.selectList(wrageWrapper);
        List<Integer> wrageIds = new LinkedList<>();
        if(wrageList.size()>0){
            for (LzWage lzWage : wrageList) {
                wrageIds.add(lzWage.getId());
            }
        }

        Integer insertCount = 0;
        for (String userId : userIds) {
            Integer count = 0;    //充值达标数
            for (SalesList salesOrder : salesOrderList) {
                if(userId.equals(salesOrder.getUserId())){
                    double recentlyAmount = salesOrder.getRecentlyAmount();
                    //充值金额50以上算充值达标
                    if(recentlyAmount > 50){
                        count++;
                    }
                }
            }
            double kpiTotail = 0;
            for (LzKpi lzKpi : lzKpiList) {
                if(userId.equals(lzKpi.getUserId())){
                    kpiTotail += lzKpi.getKpi();
                }
            }
            for (LzWage lzWage : lzWageList) {
                if(userId.equals(lzWage.getUserId().toString())){
                    lzWage.setRechargeNum(count);      //充值达标数
                    double rechargeWage = count * 20;//充值薪酬=充值达标数*20
                    //充值薪酬和kpi薪酬最高为500
                    if(rechargeWage > 500){
                        rechargeWage = 500;
                    }
                    lzWage.setRechargeWage(rechargeWage); //充值达标薪酬
                    lzWage.setKpi(kpiTotail);
                    double kpiWage = kpiTotail * 5 ;  //kpi薪酬=kpi*5
                    //充值薪酬和kpi薪酬最高为500
                    if(kpiWage > 500){
                        kpiWage = 500;
                    }
                    lzWage.setKpiWage(kpiWage);
                    //总薪酬=充值薪酬+kpi薪酬；总薪酬超过800的部分要收20%的税
                    double wage = kpiWage + rechargeWage;
                    if(wage > 800){
                        wage = wage + (wage-800)*20/100;
                    }
                    lzWage.setWage(wage);
                    if(wrageIds.size()>0){
                        Wrapper<LzWage> delWrapper = new EntityWrapper<>();
                        delWrapper.in("id",wrageIds);
                        lzWageMpapper.delete(delWrapper);
                    }
                    insertCount += lzWageMpapper.insert(lzWage);
                }
            }
        }
        return ResponseData.success(insertCount+"位楼长"+month+"月份薪酬已生成！");
    }
}
