package com.engine.salary.entity.report.bo;

import com.engine.salary.constant.SalaryDefaultTenantConstant;
import com.engine.salary.entity.datacollection.DataCollectionEmployee;
import com.engine.salary.entity.report.po.SalaryAcctResultReportPO;
import com.engine.salary.entity.salaryacct.param.SalaryAcctResultSaveParam;
import com.engine.salary.entity.salaryacct.po.SalaryAcctEmployeePO;
import com.engine.salary.entity.salaryacct.po.SalaryAcctResultTempPO;
import com.engine.salary.util.db.IdGenerator;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.math.NumberUtils;

import java.util.*;
import java.util.stream.Collectors;

public class SalaryAcctResultReportBO {


    /**
     * 薪资核算结果保存参数转换成薪资核算结果po
     *
     * @param saveParam          前端保存参数
     * @param salaryAcctEmployee 薪资核算人员po
     * @param employeeId         当前登陆人员id
     * @return
     */
    public static List<SalaryAcctResultReportPO> convert2PO(SalaryAcctResultSaveParam saveParam,
                                                      SalaryAcctEmployeePO salaryAcctEmployee,
                                                      Long employeeId, Map<Long, DataCollectionEmployee> emps) {
        if (CollectionUtils.isEmpty(saveParam.getItems())) {
            return Collections.emptyList();
        }
        Date now = new Date();
        return saveParam.getItems().stream()
                .map(e -> {
                    SalaryAcctResultReportPO po = SalaryAcctResultReportPO.builder()
                            .id(IdGenerator.generate())
                            .salarySobId(salaryAcctEmployee.getSalarySobId())
                            .salaryItemId(e.getSalaryItemId())
                            .salaryAcctRecordId(salaryAcctEmployee.getSalaryAcctRecordId())
                            .salaryAcctEmpId(salaryAcctEmployee.getId().toString())
                            .employeeId(salaryAcctEmployee.getEmployeeId().toString())
                            .taxAgentId(salaryAcctEmployee.getTaxAgentId())
                            .resultValue(e.getResultValue())
                            .creator(employeeId)
                            .createTime(now)
                            .updateTime(now)
                            .deleteType(NumberUtils.INTEGER_ZERO)
                            .tenantKey(SalaryDefaultTenantConstant.DEFAULT_TENANT_KEY)
                            .build();

                    DataCollectionEmployee dataCollectionEmployee = emps.get(salaryAcctEmployee.getEmployeeId());
                    if (dataCollectionEmployee != null) {
                        po.setDepartmentId(dataCollectionEmployee.getDepartmentId());
                        po.setSubcompanyId(dataCollectionEmployee.getSubcompanyid());
                        po.setCostcenterId(dataCollectionEmployee.getCostcenterId());
                        po.setJobtitleId(dataCollectionEmployee.getJobtitleId());
                        po.setLocationId(dataCollectionEmployee.getLocationId());
                    }
                    return po;
                })
                .collect(Collectors.toList());
    }

    public static List<SalaryAcctResultReportPO> convert2ReportPO(Collection<SalaryAcctResultTempPO> temps, Map<Long, DataCollectionEmployee> emps) {
//        Map<Long, DataCollectionEmployee> longDataCollectionEmployeeMap = SalaryEntityUtil.convert2Map(emps, DataCollectionEmployee::getEmployeeId);
        if (CollectionUtils.isEmpty(temps)) {
            return Collections.emptyList();
        }
        return temps.stream().map(e -> {
            SalaryAcctResultReportPO po = SalaryAcctResultReportPO.builder()
                    .id(IdGenerator.generate())
                    .salarySobId(e.getSalarySobId())
                    .salaryAcctEmpId(e.getSalaryAcctEmpId().toString())
                    .salaryAcctRecordId(e.getSalaryAcctRecordId())
                    .employeeId(e.getEmployeeId().toString())
                    .taxAgentId(e.getTaxAgentId())
                    .salaryItemId(e.getSalaryItemId())
                    .resultValue(e.getResultValue())
                    .originResultValue(e.getOriginResultValue())
                    .creator(e.getCreator())
                    .createTime(e.getCreateTime())
                    .updateTime(e.getUpdateTime())
                    .deleteType(e.getDeleteType())
                    .tenantKey(e.getTenantKey())
                    .build();

            Long employeeId = e.getEmployeeId();
            DataCollectionEmployee dataCollectionEmployee = emps.get(employeeId);
            if (dataCollectionEmployee != null) {
                po.setDepartmentId(dataCollectionEmployee.getDepartmentId());
                po.setSubcompanyId(dataCollectionEmployee.getSubcompanyid());
                po.setCostcenterId(dataCollectionEmployee.getCostcenterId());
                po.setJobtitleId(dataCollectionEmployee.getJobtitleId());
                po.setLocationId(dataCollectionEmployee.getLocationId());
            }
            return po;
        }).collect(Collectors.toList());
    }
}
