package io.renren.modules.business.service.impl;

import io.renren.modules.business.dao.*;
import io.renren.modules.business.entity.*;
import io.renren.modules.business.service.BusinessDictService;
import io.renren.modules.business.service.BusinessSearchByLxService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author lsr
 * @version 2022/10/19
 */
@Service("businessSearchByLxService")
public class BusinessSearchByLxServiceImpl implements BusinessSearchByLxService {

    @Autowired
    BusinessDictService businessDictService;//字典

    @Autowired
    BusinessProjectDao businessProjectDao;//立项

    @Autowired
    BusinessContractDao businessContractDao;//合同

    @Autowired
    BusinessCommentsDao businessCommentsDao;//留言板

    @Autowired
    BusinessOutlayDao businessOutlayDao;//支出明细

    @Autowired
    BusinessTotalDao businessTotalDao;//总结的记录-日记

    @Autowired
    BusinessMoneyDao businessMoneyDao;

    @Override
    public Map<String, Object> getAllInfoById(Integer projectId) {
        //获取的所有的信息数组
        Map<String, Object> allInfo = new HashMap<>();
        //获取字典信息数组
        //List<BusinessDictEntity> dicList = businessDictService.listWithTree();
        List<BusinessDictEntity> dicList = businessDictService.queryAll();
        //1    3   根据id获取立项属性
        BusinessProjectEntity project = businessProjectDao.projectInfo(projectId);
        //设置一个空字符串为了给立项名称处理用
        StringBuilder gclb = new StringBuilder();
        if(project.getProjectGclbId() != null){
            for(String id : project.getProjectGclbId().split(",")){
                for(BusinessDictEntity dic : dicList){
                    if(dic.getDictId().toString().equals(id)){//和字典匹配获取对应的名称
                        if(gclb.toString().equals("")){//判断接收工程类别的字段是不是空的字段 如果不是空的字段说明已经存在一个 需要用逗号分割
                            gclb = new StringBuilder(dic.getName());
                        }else{
                            gclb.append(",").append(dic.getName());
                        }
                    }
                }
            }
            project.setGclb(gclb.toString());
        }
        //2    根据id获取合同属性
        List<BusinessContractEntity> contract = businessContractDao.getByProjectId(projectId.toString());
        //4    留言板
        List<BusinessCommentsEntity> comments = businessCommentsDao.getListByProjectId(projectId.toString());
        //5     成本总汇
        List<Map<String, Object>> cbzh = businessOutlayDao.totalCbzh(projectId);
        //5.1   人员明细(逻辑不对)
        // List<Map<String, Object>> rymx = businessOutlayDao.rymx(projectId);
        //5.2   各种明细数据
        List<Map<String, Object>> threeCl = businessOutlayDao.threeCl(projectId);
        Map<String, BigDecimal> threeMoney = new HashMap<>();//各种明细总额
        BigDecimal totalCl = BigDecimal.ZERO;
        BigDecimal totalJx = BigDecimal.ZERO;
        BigDecimal totalQt = BigDecimal.ZERO;
        for (Map<String, Object> item : threeCl) {
            item.put("out_money",new BigDecimal(item.get("out_money").toString()));
            if (item.get("out_type").toString().equals("137")) {
                totalCl = totalCl.add(new BigDecimal(item.get("out_money").toString()));
            } else if (item.get("out_type").toString().equals("138")) {
                totalJx = totalJx.add(new BigDecimal(item.get("out_money").toString()));
            } else if (item.get("out_type").toString().equals("139")) {
                totalQt = totalQt.add(new BigDecimal(item.get("out_money").toString()));
            }
        }
        threeMoney.put("totalQt",totalQt);
        threeMoney.put("totalJx",totalJx);
        threeMoney.put("totalCl",totalCl);
        //6    施工日记
        List<BusinessTotalEntity> rjs = businessTotalDao.getListByProjectId(projectId.toString());

        // 2023年3月5日，工资换计算方式
        List<Map<String, Object>> rymxList = businessMoneyDao.getRymx(projectId+"");
        BigDecimal totalMoney = BigDecimal.ZERO;
        Integer totalDay = 0;
        for(Map<String, Object> tmp : rymxList) {
            totalMoney = totalMoney.add(new BigDecimal(tmp.get("money").toString()));
            totalDay += ((Number)tmp.get("day")).intValue();
            tmp.put("money",new BigDecimal(tmp.get("money").toString()));
        }

        //7  获取基本工资，用于计算
        /*HashMap<String, HashMap<String, HashMap<String, String>>> rymx = new HashMap<>();
        Double totalMoney = 0.0;
        for(BusinessTotalEntity item: rjs){
            SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
            String date = sdf1.format(item.getPlanTime());
            // 查询当前施工时间对应的基本工资
            BusinessMoneyEntity bm = businessMoneyDao.queryById(date);
            Double money = "".equals(bm.getNewMoney()+"")||null==bm.getNewMoney()? bm.getMoney() : bm.getNewMoney();
            // 带队人
            String ddID = item.getTotalDdId().toString(); // 带队人id
            String ddName = item.getDdName(); // 带队人姓名
            String ddXS = item.getDdXS();// 带队人折算系数
            // 施工人
            List<String> perids = new ArrayList<>() ;
            Collections.addAll(perids, item.getSelectPersionId().split(","));
            perids.add(ddID);
            List<String> pers = new ArrayList<>();
            Collections.addAll(pers, item.getSelectPer().split(","));
            pers.add(ddName);
            // 施工人折算系数
            List<String> persXS = new ArrayList<>();
            Collections.addAll(persXS, item.getSelectPerXS().split(","));
            persXS.add(ddXS);
            double day = "是".equals(item.getOneDay()) ? 1: 0.5;
            // 判断存放人员信息的数据里是否有当前日期
            if(rymx.containsKey(date)){
                for (int i = 0; i < perids.size(); i++) {
                    String perid = perids.get(i);
                    String per = pers.get(i);
                    String perXS = persXS.get(i);
                    Double YE = money*Double.parseDouble(perXS)*day;
                    totalMoney += YE;
                    // 判断当前日期是否有人员已经存在，存在则更前人员金额，否则添加
                    if(rymx.get(date).containsKey(perid)){
                        rymx.get(date).get(perid).put("money", Double.parseDouble(rymx.get(date).get(perid).get("money")) + YE + "");
                    }
                    else {
                        rymx.get(date).put(perid, new HashMap<String, String>(){{
                            put("name", per);
                            put("day", day+"");
                            put("money", YE+"");
                        }});
                    }

                }

            }
            else {
                HashMap<String, HashMap<String, String>> temp = new HashMap<>();
                // 遍历所有人员添加人员数据
                for (int i = 0; i < perids.size(); i++) {
                    String perid = perids.get(i);
                    String per = pers.get(i);
                    String perXS = persXS.get(i);
                    Double YE = money*Double.parseDouble(perXS)*day;
                    totalMoney += YE;
                    temp.put(perid, new HashMap<String, String>(){{
                        put("name", per);
                        put("day", day+"");
                        put("money", YE+"");
                    }});
                }
                rymx.put(date, temp);
            }
        }
        // 将人员计算的字典信息改造成集合存储
        ArrayList<HashMap<String, String>> rymxList = new ArrayList<>();
        for(String date: rymx.keySet()){
            for(String id: rymx.get(date).keySet()){
                rymx.get(date).get(id).put("date",date);
                rymxList.add(rymx.get(date).get(id));
            }
        }*/
        // 人员信息
        Map<String, Object> wkInfo = new HashMap<>();
        wkInfo.put("info", rymxList);
        wkInfo.put("totalMoney", totalMoney);
        wkInfo.put("totalDay", totalDay);

        // 把人工加入到成本总和
        Map<String, Object> rgCbzh = new HashMap<>();
        rgCbzh.put("name", "人工");
        rgCbzh.put("totalMoney", totalMoney);
        cbzh.add(0, rgCbzh);

        allInfo.put("project", project);
        if(contract.size() > 0){
            allInfo.put("contract", contract.get(0));
        }else{
            allInfo.put("contract", null);
        }
        allInfo.put("comments", comments);
        allInfo.put("cbzh", cbzh);
        allInfo.put("rymx", wkInfo);
        allInfo.put("threeCl", threeCl);
        allInfo.put("threeMoney", threeMoney);
        allInfo.put("rj", rjs);
        return allInfo;
    }
}
