package com.engine.salary.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.engine.common.util.ServiceUtil;
import com.engine.core.impl.Service;
import com.engine.salary.biz.SalarySobItemGroupBiz;
import com.engine.salary.entity.salaryformula.config.FormluaConfig;
import com.engine.salary.entity.salaryformula.po.FormulaPO;
import com.engine.salary.entity.salaryformula.po.FormulaVar;
import com.engine.salary.entity.salaryitem.po.SalaryItemPO;
import com.engine.salary.entity.salarysob.po.SalarySobItemGroupPO;
import com.engine.salary.entity.salarysob.po.SalarySobItemPO;
import com.engine.salary.service.SalaryFormulaService;
import com.engine.salary.service.SalaryItemService;
import com.engine.salary.service.SalarySobItemGroupService;
import com.engine.salary.service.SalarySobItemService;
import com.engine.salary.util.SalaryEntityUtil;
import weaver.hrm.User;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 薪资账套中的薪资项目分类
 * <p>Copyright: Copyright (c) 2022</p>
 * <p>Company: 泛微软件</p>
 *
 * @author qiantao
 * @version 1.0
 **/
public class SalarySobItemGroupServiceImpl extends Service implements SalarySobItemGroupService {

    private SalarySobItemGroupBiz salarySobItemGroupMapper = new SalarySobItemGroupBiz();

    private SalarySobItemService getSalarySobItemService(User user) {
        return ServiceUtil.getService(SalarySobItemServiceImpl.class, user);
    }

    private SalaryFormulaService getSalaryFormulaService(User user) {
        return ServiceUtil.getService(SalaryFormulaServiceImpl.class, user);
    }

    private SalaryItemService getSalaryItemService(User user) {
        return ServiceUtil.getService(SalaryItemServiceImpl.class, user);
    }


    @Override
    public SalarySobItemGroupPO getById(Long id) {
        return salarySobItemGroupMapper.getById(id);
    }

    @Override
    public List<SalarySobItemGroupPO> listBySalarySobId(Long salarySobId) {
        return salarySobItemGroupMapper.listSome(SalarySobItemGroupPO.builder().salarySobId(salarySobId).build());
    }

    @Override
    public void batchSave(Collection<SalarySobItemGroupPO> salarySobItemGroupPOS) {
        salarySobItemGroupMapper.batchInsert(salarySobItemGroupPOS);
    }

    @Override
    public void deleteBySalarySobIds(Collection<Long> salarySobIds) {
        salarySobItemGroupMapper.deleteBySalarySobIds(salarySobIds);
    }

    @Override
    public List<SalarySobItemGroupPO> getConfig(Long salarySobId) {
        List<SalarySobItemGroupPO> salarySobItemGroupPOS = salarySobItemGroupMapper.listSome(SalarySobItemGroupPO.builder().salarySobId(salarySobId).build());
        if (CollectionUtil.isNotEmpty(salarySobItemGroupPOS)) {
            List<SalarySobItemPO> sobItemPOS = getSalarySobItemService(user).listBySalarySobId(salarySobId);
            Map<Long, List<SalarySobItemPO>> itemListMap = SalaryEntityUtil.group2Map(sobItemPOS, SalarySobItemPO::getSalarySobItemGroupId);

            FormluaConfig config = getSalaryFormulaService(user).getConfig();
            List<FormulaPO> formulaPOS = config.getFormulas();
            Map<Long, FormulaPO> formulaMap = SalaryEntityUtil.convert2Map(formulaPOS, FormulaPO::getId);
            List<FormulaVar> formulaVars = config.getFormulaVars();
            Map<Long, List<FormulaVar>> varListMap = SalaryEntityUtil.group2Map(formulaVars, FormulaVar::getFormulaId);

            List<SalaryItemPO> salaryItems = getSalaryItemService(user).listAll();
            Map<Long, SalaryItemPO> itemIdMap = SalaryEntityUtil.convert2Map(salaryItems, SalaryItemPO::getId);


            salarySobItemGroupPOS.forEach(salarySobItemGroupPO -> {
                List<SalarySobItemPO> salarySobItemPOS = itemListMap.getOrDefault(salarySobItemGroupPO.getId(), new ArrayList<>());
                salarySobItemPOS.forEach(po -> {
                    if (SalaryEntityUtil.isNotNullOrEmpty(po.getFormulaId())) {
                        FormulaPO formulaPO = formulaMap.get(po.getFormulaId());
                        if (SalaryEntityUtil.isNotNullOrEmpty(formulaPO)) {
                            List<FormulaVar> formulaVarList = varListMap.getOrDefault(formulaPO.getId(), new ArrayList<>());
                            formulaPO.setFormulaVars(formulaVarList);
                        }
                        po.setFormula(formulaPO);
                    }
                    if(SalaryEntityUtil.isNotNullOrEmpty(po.getSalaryItemId())){
                        SalaryItemPO salaryItemPO = itemIdMap.get(po.getSalaryItemId());
                        po.setSalaryItem(salaryItemPO);
                    }
                });
                salarySobItemGroupPO.setSalarySobItems(salarySobItemPOS);
            });
        }

        return salarySobItemGroupPOS;
    }
}
