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

import io.renren.modules.business.dao.BusinessContractDao;
import io.renren.modules.business.dao.BusinessMoneyDao;
import io.renren.modules.business.dao.BusinessOutlayDao;
import io.renren.modules.business.dao.BusinessSsjdexcelDao;
import io.renren.modules.business.entity.BusinessContractEntity;
import io.renren.modules.business.entity.BusinessSsjdexcelEntity;
import io.renren.modules.business.service.BusinessSearchByHTGZService;
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 lsr
 * @version 2022/10/19
 */
@Service("businessSearchByHTGZService")
public class BusinessSearchByHTGZServiceImpl implements BusinessSearchByHTGZService {

    @Autowired
    BusinessContractDao businessContractDao;//合同
    @Autowired
    BusinessSsjdexcelDao businessSsjdexcelDao;//进项发票
    @Autowired
    BusinessOutlayDao businessOutlayDao;//进项发票

    @Autowired
    BusinessMoneyDao businessMoneyDao;
    /**
     * 根据合同id 获取相关信息
     * @param contractId
     * @return
     */
    @Override
    public Map<String, Object> getAllInfoByConId(Integer contractId) {
        //获取的所有的信息数组
        Map<String, Object> allInfo = new HashMap<>();
        //获取合同基本基本信息
        BusinessContractEntity contract = businessContractDao.conInfoById(contractId);
        //成本总汇
        List<Map<String,Object>> cb = new ArrayList<>();
        Map<String,Object> hj = new HashMap<>(); // 合计金额
        // 人员工资
        Map<String, BigDecimal> rymx = new HashMap<>();
        if(contract.getProjectId() != null){
            for (String projectId: contract.getProjectId().split(",")){
                List<Map<String, Object>> rymxList = businessMoneyDao.getRymx(projectId+"");
                BigDecimal totalMoney = BigDecimal.ZERO;
                for(Map<String, Object> tmp : rymxList) {
                    totalMoney = totalMoney.add(new BigDecimal(tmp.get("money").toString()));
                }
                rymx.put(projectId,totalMoney);
            }
            cb = businessOutlayDao.cbByContractIds(contract.getProjectId());

            BigDecimal totalClGj = BigDecimal.ZERO;
            BigDecimal totalJx = BigDecimal.ZERO;
            BigDecimal totalRg = BigDecimal.ZERO;
            BigDecimal totalQt = BigDecimal.ZERO;
            for (Map<String, Object> item : cb) {
                item.put("gz",rymx.get(item.get("project_id").toString()));
                item.put("money", new BigDecimal(item.get("cl").toString()).add(new BigDecimal(item.get("jx").toString())).add(new BigDecimal(item.get("gz").toString())).add(new BigDecimal(item.get("qt").toString())));
                totalClGj = totalClGj.add(new BigDecimal(item.get("cl").toString())).add(new BigDecimal(item.get("gj").toString()));
                totalJx = totalJx.add(new BigDecimal(item.get("jx").toString()));
                totalRg = totalRg.add(new BigDecimal(item.get("gz").toString()));
                totalQt = totalQt.add(new BigDecimal(item.get("qt").toString()));
            }

            hj.put("totalClGj",totalClGj);
            hj.put("totalJx",totalJx);
            hj.put("totalRg",totalRg);
            hj.put("totalQt",totalQt);
            hj.put("money",totalClGj.add(totalQt).add(totalJx).add(totalRg));
        }
        //获取进项发票基本信息
        BusinessSsjdexcelEntity excel = businessSsjdexcelDao.contractInfo(contractId);
        System.out.println(excel);
        allInfo.put("contract",contract);
        allInfo.put("excel",excel);
        allInfo.put("cb",cb);
        allInfo.put("rymx",rymx);
        allInfo.put("hj",hj);
        return allInfo;
    }
}
