package com.engine.salary.entity.salaryBill.param;

import com.alibaba.nacos.client.naming.utils.CollectionUtils;
import com.engine.salary.entity.salaryBill.po.SalarySendRangeObj;
import com.engine.salary.entity.salaryBill.po.SalarySendRangePO;
import com.engine.salary.enums.salarysend.SalarySendGrantTypeEnum;
import com.engine.salary.enums.salarysend.SalarySendRangeTypeEnum;
import com.engine.salary.exception.SalaryRunTimeException;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import weaver.hrm.User;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import static com.engine.salary.constant.SalaryDefaultTenantConstant.DEFAULT_TENANT_KEY;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class SalarySendRangeSaveParam {
    /**
     * 发放id
     */
    private Long salarySendId;


    /**
     * 发放范围id
     */
    private Long salarySendRangeId;


    /**
     * 发放/撤回
     *
     * @see SalarySendGrantTypeEnum
     */
    private String grantType;


    private List<SalarySendRangeTargetParam> includeObjParams;


    private List<SalarySendRangeTargetParam> excludeObjParams;

    public static void checkParam(SalarySendRangeSaveParam saveParam, Long employeeId, String tenantKey) {
        if (saveParam.getSalarySendId() == null) {
            throw new SalaryRunTimeException("工资单发放id不能为空");
        }

        if (saveParam.getGrantType() == null) {
            throw new SalaryRunTimeException("操作类型不能为空");
        }

        if (CollectionUtils.isEmpty(saveParam.getIncludeObjParams())) {
            throw new SalaryRunTimeException("对象不能为空");
        }
    }

    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    public static class SalarySendRangeTargetParam {

        /**
         * 对象不能为空
         */
        private Long targetId;

        /**
         * 对象类型不能为空
         * @see com.engine.salary.enums.salarysend.SalarySendRangeTargetTypeEnum
         */
        private Integer targetType;
    }

    public SalarySendRangePO toSalarySendRangePO(User user) {
        return SalarySendRangePO.builder()
                .id(salarySendRangeId)
                .grantType(grantType)
                .salarySendId(salarySendId)
                .updateTime(new Date())
                .creator((long) user.getUID())
                .deleteType(0)
                .tenantKey(DEFAULT_TENANT_KEY)
                .createTime(salarySendId == null ? new Date() : null)
                .build();
    }

    public List<SalarySendRangeObj> toSalarySendRangeObj(User user) {
        List<SalarySendRangeObj> includeObjs = includeObjParams.stream()
                .map(obj -> mapObj(user, obj))
                .map(obj->obj.setRangeType(SalarySendRangeTypeEnum.INCLUDE_OBJ.getValue()))
                .collect(Collectors.toList());
        List<SalarySendRangeObj> excludeObjs = excludeObjParams.stream()
                .map(obj -> mapObj(user, obj))
                .map(obj -> obj.setRangeType(SalarySendRangeTypeEnum.EXCLUDE_OBJ.getValue()))
                .collect(Collectors.toList());
        List<SalarySendRangeObj> rangeObjs = new ArrayList<>();
        rangeObjs.addAll(includeObjs);
        rangeObjs.addAll(excludeObjs);
        return rangeObjs;
    }

    private SalarySendRangeObj mapObj(User user, SalarySendRangeTargetParam obj) {
        return SalarySendRangeObj.builder()
                .salarySendRangeId(salarySendRangeId)
                .salarySendId(salarySendId)
                .targetId(obj.getTargetId())
                .targetType(obj.getTargetType())
                .deleteType(0)
                .tenantKey(DEFAULT_TENANT_KEY)
                .updateTime(new Date())
                .createTime(new Date())
                .creator((long) user.getUID())
                .build();
    }
}