package com.ruoyi.web.core;

/**
 * @author Shawn
 */
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.AmountStatics;
import com.ruoyi.system.mapper.StatisticsMapper;
import com.ruoyi.system.mapper.SysDeptMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.math.BigDecimal;
import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Component
@EnableScheduling
public class StaticssTaskConfig {

    @Autowired
    private StatisticsMapper statisticsMapper;

    @Autowired
    private SysDeptMapper deptMapper;

    @Scheduled(cron = "0 0 2 * * ?")
    public void task() {
        System.out.println("当前线程：" + Thread.currentThread().getName() + " 当前时间" + LocalDateTime.now()+ " 开始执行图表统计任务");
        List<AmountStatics> amountStaticsList = new ArrayList<>();
        List<String> monthsBetween = DateUtils.getMonthsBetween(Integer.parseInt(DateUtils.getFullYear()) - 1 + "-" + "01", DateUtils.getFullYearAndMonth().substring(0, 4) + "-" + DateUtils.getFullYearAndMonth().substring(4, 6));
//        System.out.println(DateUtils.getYearMonthInNearYear());
        List<String> units = deptMapper.selectUnitsList();
//        System.out.println(units);
        for (String unit : units) {
            for (String yearAndMonth : monthsBetween) {
                yearAndMonth = yearAndMonth.replace("-","");
                AmountStatics amountStatics = new AmountStatics();
                amountStatics.setYearMonth(yearAndMonth);
                    amountStatics.setUnit(unit);
                    //成本部分
                    amountStatics.setCost(new BigDecimal(0));
                    JSONObject salary1Statictics = statisticsMapper.getSalary1Statictics(unit, yearAndMonth,yearAndMonth);
                    if (salary1Statictics != null){
                        amountStatics.setCost(amountStatics.getCost().add(salary1Statictics.getBigDecimal("公司承担社保")).add(salary1Statictics.getBigDecimal("应发小计")).add(salary1Statictics.getBigDecimal("公司承担公积金")));
                }

                JSONObject salary2Statictics = statisticsMapper.getSalary2Statictics(unit, yearAndMonth,yearAndMonth);
//        System.out.println(salary2Statictics);
                if (salary2Statictics != null){
                    amountStatics.setCost(amountStatics.getCost().add(salary2Statictics.getBigDecimal("职工教育经费")).add(salary2Statictics.getBigDecimal("职工福利费")).add(salary2Statictics.getBigDecimal("低值易耗品")).add(salary2Statictics.getBigDecimal("其他费用")));
                }

                JSONObject salary3Statictics = statisticsMapper.getSalary3Statictics(unit, yearAndMonth,yearAndMonth);
//        System.out.println(salary3Statictics);
                if (salary3Statictics != null){
                    amountStatics.setCost(amountStatics.getCost().add(salary3Statictics.getBigDecimal("计提年终奖")));
                }

                JSONObject b14cStatictics = statisticsMapper.getB14cStatictics(unit, yearAndMonth.substring(0,4)+"-"+yearAndMonth.substring(4,6)+"-01", DateUtils.getLastdayByYearMonth(yearAndMonth.substring(0,4),yearAndMonth.substring(4,6)));
//        System.out.println(b14cStatictics);
                if (b14cStatictics != null){
                    amountStatics.setCost(amountStatics.getCost().add(b14cStatictics.getBigDecimal("审定金额_含税")));
                }

                JSONObject b15cStatictics = statisticsMapper.getB15cStatictics(unit, yearAndMonth.substring(0,4)+"-"+yearAndMonth.substring(4,6)+"-01", DateUtils.getLastdayByYearMonth(yearAndMonth.substring(0,4),yearAndMonth.substring(4,6)));
//        System.out.println(b15cStatictics);
                if (b15cStatictics != null){
                    amountStatics.setCost(amountStatics.getCost().add(b15cStatictics.getBigDecimal("线下差旅小计")).add(b15cStatictics.getBigDecimal("合计")));
                }

                JSONObject b16Statictics = statisticsMapper.getB16Statictics(unit, yearAndMonth.substring(0,4)+"-"+yearAndMonth.substring(4,6)+"-01", DateUtils.getLastdayByYearMonth(yearAndMonth.substring(0,4),yearAndMonth.substring(4,6)));
//        System.out.println(b16Statictics);
                if (b16Statictics != null){
                    amountStatics.setCost(amountStatics.getCost().add(b16Statictics.getBigDecimal("结算金额")));
                }

                JSONObject b17Statictics = statisticsMapper.getB17Statictics(unit, yearAndMonth.substring(0,4)+"-"+yearAndMonth.substring(4,6)+"-01", DateUtils.getLastdayByYearMonth(yearAndMonth.substring(0,4),yearAndMonth.substring(4,6)));
//        System.out.println(b17Statictics);
                if (b17Statictics != null){
                    amountStatics.setCost(amountStatics.getCost().add(b17Statictics.getBigDecimal("企业结算金额")));
                }

                JSONObject b18Statictics = statisticsMapper.getB18Statictics(unit, yearAndMonth.substring(0,4)+"-"+yearAndMonth.substring(4,6)+"-01", DateUtils.getLastdayByYearMonth(yearAndMonth.substring(0,4),yearAndMonth.substring(4,6)));
//        System.out.println(b18Statictics);
                if (b18Statictics != null){
                    amountStatics.setCost(amountStatics.getCost().add(b18Statictics.getBigDecimal("结算金额")));
                }

                JSONObject b19Statictics = statisticsMapper.getB19Statictics(unit, yearAndMonth.substring(0,4)+"-"+yearAndMonth.substring(4,6)+"-01", DateUtils.getLastdayByYearMonth(yearAndMonth.substring(0,4),yearAndMonth.substring(4,6)));
//        System.out.println(b19Statictics);
                if (b19Statictics != null){
                    amountStatics.setCost(amountStatics.getCost().add(b19Statictics.getBigDecimal("福豆金额")));
                }

                JSONObject banlanceDayStatictics = statisticsMapper.getbanlanceDayStatictics(unit, yearAndMonth.substring(0,4)+"-"+yearAndMonth.substring(4,6)+"-01", DateUtils.getLastdayByYearMonth(yearAndMonth.substring(0,4),yearAndMonth.substring(4,6)));
//        System.out.println(banlanceDayStatictics);
                if (banlanceDayStatictics != null){
                    amountStatics.setCost(amountStatics.getCost().add(banlanceDayStatictics.getBigDecimal("结算金额")));
                }

                JSONObject banlancePieceStatictics = statisticsMapper.getbanlancePieceStatictics(unit, yearAndMonth.substring(0,4)+"-"+yearAndMonth.substring(4,6)+"-01", DateUtils.getLastdayByYearMonth(yearAndMonth.substring(0,4),yearAndMonth.substring(4,6)));
//        System.out.println(banlancePieceStatictics);
                if (banlancePieceStatictics != null){
                    amountStatics.setCost(amountStatics.getCost().add(banlancePieceStatictics.getBigDecimal("结算金额")));
                }

                //收入部分
                amountStatics.setIncome(new BigDecimal(0));

                JSONObject incomeStatictics = statisticsMapper.getIncomeStatictics(unit, yearAndMonth, yearAndMonth);
//        System.out.println(incomeStatictics);
                if(incomeStatictics != null){
                    amountStatics.setIncome(amountStatics.getIncome().add(incomeStatictics.getBigDecimal("本月收入_元")));
                }

                JSONObject incomebanlanceDayStatictics = statisticsMapper.getIncomebanlanceDayStatictics(unit, yearAndMonth.substring(0,4)+"-"+yearAndMonth.substring(4,6)+"-01", DateUtils.getLastdayByYearMonth(yearAndMonth.substring(0,4),yearAndMonth.substring(4,6)));
//        System.out.println(incomebanlanceDayStatictics);
                if(incomebanlanceDayStatictics != null){
                    amountStatics.setIncome(amountStatics.getIncome().add(incomebanlanceDayStatictics.getBigDecimal("结算金额")));
                }

                JSONObject incomebanlancePieceStatictics = statisticsMapper.getIncomebanlancePieceStatictics(unit, yearAndMonth.substring(0,4)+"-"+yearAndMonth.substring(4,6)+"-01", DateUtils.getLastdayByYearMonth(yearAndMonth.substring(0,4),yearAndMonth.substring(4,6)));
//        System.out.println(incomebanlancePieceStatictics);
                if(incomebanlancePieceStatictics != null){
                    amountStatics.setIncome(amountStatics.getIncome().add(incomebanlancePieceStatictics.getBigDecimal("结算金额")));
                }

                amountStatics.setNetProfit(amountStatics.getIncome().subtract(amountStatics.getCost()));
                if (amountStatics.getIncome().doubleValue() == 0){
                    amountStatics.setNetProfitPrecent(new BigDecimal(0));
                }else{
                    amountStatics.setNetProfitPrecent(amountStatics.getIncome().subtract(amountStatics.getCost()).divide(amountStatics.getIncome(),2,BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(100)));
                }
                amountStaticsList.add(amountStatics);
            }
        }
//        System.out.println(amountStatics.toString());

        String s = JSONArray.toJSONString(amountStaticsList);
        writeTxt(s);
    }

    public static void writeTxt(String jsonString){

        try {
//            Path path = Paths.get("H:\\AllStatics.txt");
//        Path path = Paths.get("/usr/local/src/AllStatics.txt");
//            File file = new File("H:\\AllStatics.txt");
            File file = new File("/usr/local/src/AllStatics.txt");
            // 创建上级目录
            if (!file.getParentFile().exists()) {
                file.getParentFile().mkdirs();
            }
            // 如果文件存在，则删除文件
            if (file.exists()) {
                file.delete();
            }
            // 创建文件
            file.createNewFile();
            // 写入文件
            Writer write = new OutputStreamWriter(new FileOutputStream(file), StandardCharsets.UTF_8);
            write.write(jsonString);
            write.flush();
            write.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

