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

import com.engine.salary.entity.salaryacct.po.SalaryAcctRecordPO;
import com.engine.salary.entity.salaryacct.po.SalaryAcctSobConfigPO;
import com.engine.salary.entity.salarysob.po.*;
import com.engine.salary.enums.sicategory.DeleteTypeEnum;
import com.engine.salary.util.JsonUtil;
import com.engine.salary.util.db.IdGenerator;
import lombok.Builder;
import lombok.Data;

import java.util.Date;
import java.util.List;

@Data
@Builder
public class SalaryAcctConfig {

    private SalaryAcctRecordPO salaryAcctRecord;

    private SalarySobPO salarySob;

    private List<SalarySobEmpFieldPO> salarySobEmpFields;

    private List<SalarySobItemPO> salarySobItems;

    private List<SalarySobItemGroupPO> salarySobItemGroups;

    private List<SalarySobBackItemPO> salarySobBackItems;

    @Deprecated
    private List<SalarySobAdjustRulePO> salarySobAdjustRules;

    @Deprecated
    private List<SalarySobCheckRulePO> salarySobCheckRules;

    public SalaryAcctConfig(SalaryAcctRecordPO salaryAcctRecord,
                            SalarySobPO salarySob,
                            List<SalarySobEmpFieldPO> salarySobEmpFields,
                            List<SalarySobItemPO> salarySobItems,
                            List<SalarySobItemGroupPO> salarySobItemGroups,
                            List<SalarySobBackItemPO> salarySobBackItems,
                            List<SalarySobAdjustRulePO> salarySobAdjustRules,
                            List<SalarySobCheckRulePO> salarySobCheckRules) {
        this.salaryAcctRecord = salaryAcctRecord;
        this.salarySob = salarySob;
        this.salarySobEmpFields = salarySobEmpFields;
        this.salarySobItems = salarySobItems;
        this.salarySobItemGroups = salarySobItemGroups;
        this.salarySobBackItems = salarySobBackItems;
        this.salarySobAdjustRules = salarySobAdjustRules;
        this.salarySobCheckRules = salarySobCheckRules;
    }

    public SalaryAcctSobConfigPO buildAcctSobConfig() {
        Date now = new Date();
        return  SalaryAcctSobConfigPO.builder()
                .id(IdGenerator.generate())
                .salaryAcctRecordId(salaryAcctRecord.getId())
                .basicConfig(JsonUtil.toJsonString(salarySob))
                .employeeFieldConfig(JsonUtil.toJsonString(salarySobEmpFields))
                .itemConfig(JsonUtil.toJsonString(salarySobItems))
                .itemGroupConfig(JsonUtil.toJsonString(salarySobItemGroups))
                .backItemConfig(JsonUtil.toJsonString(salarySobBackItems))
                .adjustRuleConfig(JsonUtil.toJsonString(salarySobAdjustRules))
                .checkRuleConfig(JsonUtil.toJsonString(salarySobCheckRules))
                .tenantKey(salaryAcctRecord.getTenantKey())
                .deleteType(DeleteTypeEnum.NOT_DELETED.getValue())
                .creator(salaryAcctRecord.getCreator())
                .createTime(now)
                .updateTime(now)
                .build();
    }

    public static SalaryAcctConfig parse(SalaryAcctSobConfigPO po) {
        return SalaryAcctConfig.builder()
                .salarySob(JsonUtil.parseObject(po.getBasicConfig(), SalarySobPO.class))
                .salarySobEmpFields(JsonUtil.parseList(po.getEmployeeFieldConfig(), SalarySobEmpFieldPO.class))
                .salarySobItemGroups(JsonUtil.parseList(po.getItemGroupConfig(), SalarySobItemGroupPO.class))
                .salarySobItems(JsonUtil.parseList(po.getItemConfig(), SalarySobItemPO.class))
                .salarySobBackItems(JsonUtil.parseList(po.getBackItemConfig(), SalarySobBackItemPO.class))
                .build();
    }
}
