package jnpf.model.personnels.dto.secondment;

import cn.hutool.core.collection.CollUtil;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.annotation.TableField;
import jnpf.model.cultivate.dto.offline.FtbCultivateOfflineFileDTO;
import jnpf.model.personnels.dto.base.FtbPersonnelsBaseForOA;
import jnpf.model.personnels.dto.salary.FtbPersonnelsSalaryInfo;
import jnpf.model.personnels.dto.secondment.msg.SecondmentMsgUserInfo;
import jnpf.model.personnels.po.FtbPersonnelsSecondmentManagement;
import lombok.Data;
import org.hibernate.validator.constraints.Length;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @Author: peng.hao
 * @create: 2025/9/11
 */
@Data
public class FtbHandleSecondmentDTO extends FtbPersonnelsBaseForOA  {
    private String id;

    /** userid */
    private String userId;

    /** 员工姓名 */
    private String userName;

    /** 员工ID */
    private String employeeId;

    /** 借调前组织ID（关联组织表） */
    @NotNull(message = "借调前组织不能为空")
    private String originalOrganizationId;

    /** 借调前组织名称 */
    private String originalOrganization;

    /** 借调后组织ID（关联组织表） */
    @NotNull(message = "借调后组织不能为空")
    private String secondedOrganizationId;

    /** 借调后组织名称 */
    private String secondedOrganization;

    /** 借调前岗位ID（关联岗位表） */
    @NotNull(message = "借调前岗位不能为空")
    private String originalPositionId;

    /** 借调前岗位名称 */
    private String originalPosition;

    /** 借调后岗位ID（关联岗位表） */
    @NotNull(message = "借调后岗位不能为空")
    private String secondedPositionId;

    /** 借调后岗位名称 */
    private String secondedPosition;

    /** 借调前职级ID（关联职级表） */
    private String originalRankId;

    /** 借调前职级名称 */
    private String originalRank;

    /** 借调后职级ID（关联职级表） */
    private String secondedRankId;

    /** 借调后职级名称 */
    private String secondedRank;

    /** 借调开始时间（格式：yyyy/mm/dd-hh:mm） */
    @NotNull(message = "借调开始时间不能为空")
    private Date secondmentStartTime;

    /** 借调结束时间（格式：yyyy/mm/dd-hh:mm） */
    @NotNull(message = "借调结束时间不能为空")
    private Date secondmentEndTime;

    /** 提前结束时间*/
    @TableField(value = "F_AdvanceEndTime")
    private Date advanceEndTime;

    /** 薪资设置 */
    @NotNull(message = "薪资设置不能为空")
    private String salarySetting;

    /** 是否设置薪酬：0=否，1=是 */
    private Integer isResetSalary;

    /** 薪资序列名称 */
    private String payrollSequenceName;

    /** 薪资序列 id */
    private String payrollSequenceId;

    /** 调薪方式 1.加薪 2.降薪 */
    private Integer salaryType;

    /** 薪资详情 */
    private List<FtbPersonnelsSalaryInfo> salaryStructureList;

    /** 借调备注 */
    @Length(max = 500, message = "借调备注长度不能超过500字")
    private String secondmentRemarks;

    /** 借调附件存储路径 */
    @Size(max = 10, message = "附件信息上限10个")
    @Valid
    private List<FtbCultivateOfflineFileDTO> files;
    /**
     * oa 文件传递
     */
    private String filesWithOa;

    private Integer isDelete;

    public static SecondmentMsgUserInfo convert(FtbPersonnelsSecondmentManagement ftbHandleSecondmentDTO) {
        if (ftbHandleSecondmentDTO == null) {
            return null;
        }
        SecondmentMsgUserInfo secondmentMsgUserInfo = new SecondmentMsgUserInfo();
        secondmentMsgUserInfo.setUserId(ftbHandleSecondmentDTO.getUserId());
        secondmentMsgUserInfo.setUserName(ftbHandleSecondmentDTO.getUserName());
        secondmentMsgUserInfo.setEmployeeId(ftbHandleSecondmentDTO.getEmployeeId());
        secondmentMsgUserInfo.setOriginalOrganizationId(ftbHandleSecondmentDTO.getOriginalOrganizationId());
        secondmentMsgUserInfo.setOriginalOrganization(ftbHandleSecondmentDTO.getOriginalOrganization());
        secondmentMsgUserInfo.setSecondedOrganizationId(ftbHandleSecondmentDTO.getSecondedOrganizationId());
        secondmentMsgUserInfo.setSecondedOrganization(ftbHandleSecondmentDTO.getSecondedOrganization());
        secondmentMsgUserInfo.setOriginalPositionId(ftbHandleSecondmentDTO.getOriginalPositionId());
        secondmentMsgUserInfo.setOriginalPosition(ftbHandleSecondmentDTO.getOriginalPosition());
        secondmentMsgUserInfo.setSecondedPositionId(ftbHandleSecondmentDTO.getSecondedPositionId());
        secondmentMsgUserInfo.setSecondedPosition(ftbHandleSecondmentDTO.getSecondedPosition());
        secondmentMsgUserInfo.setOriginalRankId(ftbHandleSecondmentDTO.getOriginalRankId());
        secondmentMsgUserInfo.setOriginalRank(ftbHandleSecondmentDTO.getOriginalRank());
        secondmentMsgUserInfo.setSecondedRankId(ftbHandleSecondmentDTO.getSecondedRankId());
        secondmentMsgUserInfo.setSecondedRank(ftbHandleSecondmentDTO.getSecondedRank());
        secondmentMsgUserInfo.setSecondmentStartTime(ftbHandleSecondmentDTO.getSecondmentStartTime());
        secondmentMsgUserInfo.setSecondmentEndTime(ftbHandleSecondmentDTO.getSecondmentEndTime());
        secondmentMsgUserInfo.setAdvanceEndTime(ftbHandleSecondmentDTO.getAdvanceEndTime());
        return secondmentMsgUserInfo;
    }
    public static SecondmentMsgUserInfo convert(FtbHandleSecondmentDTO ftbHandleSecondmentDTO) {
        if (ftbHandleSecondmentDTO == null) {
            return null;
        }
        SecondmentMsgUserInfo secondmentMsgUserInfo = new SecondmentMsgUserInfo();
        secondmentMsgUserInfo.setUserId(ftbHandleSecondmentDTO.getUserId());
        secondmentMsgUserInfo.setUserName(ftbHandleSecondmentDTO.getUserName());
        secondmentMsgUserInfo.setEmployeeId(ftbHandleSecondmentDTO.getEmployeeId());
        secondmentMsgUserInfo.setOriginalOrganizationId(ftbHandleSecondmentDTO.getOriginalOrganizationId());
        secondmentMsgUserInfo.setOriginalOrganization(ftbHandleSecondmentDTO.getOriginalOrganization());
        secondmentMsgUserInfo.setSecondedOrganizationId(ftbHandleSecondmentDTO.getSecondedOrganizationId());
        secondmentMsgUserInfo.setSecondedOrganization(ftbHandleSecondmentDTO.getSecondedOrganization());
        secondmentMsgUserInfo.setOriginalPositionId(ftbHandleSecondmentDTO.getOriginalPositionId());
        secondmentMsgUserInfo.setOriginalPosition(ftbHandleSecondmentDTO.getOriginalPosition());
        secondmentMsgUserInfo.setSecondedPositionId(ftbHandleSecondmentDTO.getSecondedPositionId());
        secondmentMsgUserInfo.setSecondedPosition(ftbHandleSecondmentDTO.getSecondedPosition());
        secondmentMsgUserInfo.setOriginalRankId(ftbHandleSecondmentDTO.getOriginalRankId());
        secondmentMsgUserInfo.setOriginalRank(ftbHandleSecondmentDTO.getOriginalRank());
        secondmentMsgUserInfo.setSecondedRankId(ftbHandleSecondmentDTO.getSecondedRankId());
        secondmentMsgUserInfo.setSecondedRank(ftbHandleSecondmentDTO.getSecondedRank());
        secondmentMsgUserInfo.setSecondmentStartTime(ftbHandleSecondmentDTO.getSecondmentStartTime());
        secondmentMsgUserInfo.setSecondmentEndTime(ftbHandleSecondmentDTO.getSecondmentEndTime());
        return secondmentMsgUserInfo;
    }


    public static FtbPersonnelsSecondmentManagement convertSecondmentManagement(FtbHandleSecondmentDTO ftbHandleSecondmentDTO) {
        if (ftbHandleSecondmentDTO == null) {
            return null;
        }
        FtbPersonnelsSecondmentManagement ftbPersonnelsSecondmentManagement = new FtbPersonnelsSecondmentManagement();
        ftbPersonnelsSecondmentManagement.setUserId(ftbHandleSecondmentDTO.getUserId());
        ftbPersonnelsSecondmentManagement.setUserName(ftbHandleSecondmentDTO.getUserName());
        ftbPersonnelsSecondmentManagement.setEmployeeId(ftbHandleSecondmentDTO.getEmployeeId());
        ftbPersonnelsSecondmentManagement.setOriginalOrganizationId(ftbHandleSecondmentDTO.getOriginalOrganizationId());
        ftbPersonnelsSecondmentManagement.setOriginalOrganization(ftbHandleSecondmentDTO.getOriginalOrganization());
        ftbPersonnelsSecondmentManagement.setSecondedOrganizationId(ftbHandleSecondmentDTO.getSecondedOrganizationId());
        ftbPersonnelsSecondmentManagement.setSecondedOrganization(ftbHandleSecondmentDTO.getSecondedOrganization());
        ftbPersonnelsSecondmentManagement.setOriginalPositionId(ftbHandleSecondmentDTO.getOriginalPositionId());
        ftbPersonnelsSecondmentManagement.setOriginalPosition(ftbHandleSecondmentDTO.getOriginalPosition());
        ftbPersonnelsSecondmentManagement.setSecondedPositionId(ftbHandleSecondmentDTO.getSecondedPositionId());
        ftbPersonnelsSecondmentManagement.setSecondedPosition(ftbHandleSecondmentDTO.getSecondedPosition());
        ftbPersonnelsSecondmentManagement.setOriginalRankId(ftbHandleSecondmentDTO.getOriginalRankId());
        ftbPersonnelsSecondmentManagement.setOriginalRank(ftbHandleSecondmentDTO.getOriginalRank());
        ftbPersonnelsSecondmentManagement.setSecondedRankId(ftbHandleSecondmentDTO.getSecondedRankId());
        ftbPersonnelsSecondmentManagement.setSecondedRank(ftbHandleSecondmentDTO.getSecondedRank());
        ftbPersonnelsSecondmentManagement.setSecondmentStartTime(ftbHandleSecondmentDTO.getSecondmentStartTime());
        ftbPersonnelsSecondmentManagement.setSecondmentEndTime(ftbHandleSecondmentDTO.getSecondmentEndTime());
        ftbPersonnelsSecondmentManagement.setSalarySetting(ftbHandleSecondmentDTO.getSalarySetting());
        ftbPersonnelsSecondmentManagement.setIsResetSalary(ftbHandleSecondmentDTO.getIsResetSalary());
        ftbPersonnelsSecondmentManagement.setPayrollSequenceId(ftbHandleSecondmentDTO.getPayrollSequenceId());
        ftbPersonnelsSecondmentManagement.setSalaryType(ftbHandleSecondmentDTO.getSalaryType());
        ftbPersonnelsSecondmentManagement.setSalaryStructureList(JSONArray.toJSONString(ftbHandleSecondmentDTO.getSalaryStructureList()));
        ftbPersonnelsSecondmentManagement.setSecondmentRemarks(ftbHandleSecondmentDTO.getSecondmentRemarks());
        ftbPersonnelsSecondmentManagement.setAttachment(ftbHandleSecondmentDTO.getFilesWithOa());
        ftbPersonnelsSecondmentManagement.setTaskInfoId(ftbHandleSecondmentDTO.getTaskId());
        ftbPersonnelsSecondmentManagement.setId(ftbHandleSecondmentDTO.getSpecialBusinessId());
        ftbPersonnelsSecondmentManagement.setPayrollSequenceName(ftbHandleSecondmentDTO.getPayrollSequenceName());
        return ftbPersonnelsSecondmentManagement;
    }

    /**
     * 重写用户Id
     */
    public String getUserId() {
        if (CollUtil.isEmpty(getUserIds()) && Objects.isNull(userId)) {
            throw new RuntimeException("借调用户不能为空");
        }
        if (Objects.nonNull(userId)) {
            return userId;
        }
        if (CollUtil.isNotEmpty(getUserIds())) {
            return getUserIds().get(0);
        }
        throw new RuntimeException("借调用户不能为空");
    }
}
