package com.zcloud.basic.info.domain.model;

import com.jjb.saas.framework.domain.model.BaseE;
import com.zcloud.basic.info.domain.enums.UserChangeRecordStatusEnum;
import com.zcloud.basic.info.domain.enums.UserEmploymentFlagEnum;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.util.Date;

/**
 * web-domain
 *
 * @Author SondonYong
 * @Date 2025-12-02 17:54:06
 */
@Data
public class UserChangeRecordE extends BaseE {
    //主键
    private Long id;
    //业务主键id
    private String userChangeRecordId;
    //用户id
    private Long userId;
    //变更时间
    private Date changeTime;
    //入职状态，1-待审批, 2-通过
    private Integer status;
    //变更前企业id
    private Long corpinfoIdBefore;
    //变更前企业名称
    private String corpinfoNameBefore;
    //变更前负责部门id
    private Long departmentIdBefore;
    //变更前负责部门名称
    private String departmentNameBefore;
    //变更前负责岗位id
    private Long postIdBefore;
    //变更前负责岗位名称
    private String postNameBefore;
    //变更前状态
    private Integer userStatusBefore;
    //变更后企业id
    private Long corpinfoIdAfter;
    //变更后企业名称
    private String corpinfoNameAfter;
    //变更后负责部门id
    private Long departmentIdAfter;
    //变更后负责部门名称
    private String departmentNameAfter;
    //变更后负责岗位id
    private Long postIdAfter;
    //变更后负责岗位名称
    private String postNameAfter;
    //变更后状态
    private Integer userStatusAfter;
    //
    private String resignationReason;

    public void initUserAdd(UserE userE, String corpinfoName, String departmentName) {
        this.setUserId(userE.getId());
        this.setChangeTime(new Date());
        this.setCorpinfoIdAfter(userE.getCorpinfoId());
        this.setCorpinfoNameAfter(corpinfoName);
        this.setDepartmentIdAfter(userE.getDepartmentId());
        this.setDepartmentNameAfter(departmentName);
        this.setPostIdAfter(userE.getPostId());
        this.setPostNameAfter(userE.getPostName());
        this.setUserStatusAfter(UserEmploymentFlagEnum.ON.getCode());
        this.setStatus(UserChangeRecordStatusEnum.APPROVED.getCode());
    }

    public void initUserAddFromApp(UserE userE, String corpinfoName, String departmentName) {
        this.setUserId(userE.getId());
        this.setChangeTime(new Date());
        this.setCorpinfoIdAfter(userE.getCorpinfoId());
        this.setCorpinfoNameAfter(corpinfoName);
        this.setDepartmentIdAfter(userE.getDepartmentId());
        this.setDepartmentNameAfter(departmentName);
        this.setPostIdAfter(userE.getPostId());
        this.setPostNameAfter(userE.getPostName());
        this.setUserStatusAfter(UserEmploymentFlagEnum.ON.getCode());
        this.setStatus(UserChangeRecordStatusEnum.PENDING.getCode());
    }

    public void initUserAddFromRZ(UserE userE, RzUserRecordE rzUserRecordE, Long userId, String corpinfoName, String departmentName) {
        this.setUserId(userId);
        this.setChangeTime(new Date());

        // todo 企业id, 部门id, 岗位信息等对应关系
        this.setCorpinfoIdBefore(null);
        this.setCorpinfoNameBefore(corpinfoName);
        this.setDepartmentIdBefore(null);
        this.setDepartmentNameBefore(departmentName);
        this.setPostIdBefore(null);
        this.setPostNameBefore(userE.getPostName());
        // corpinfoName == null是新增的时候, != null是修改
        if(corpinfoName == null){
            this.setUserStatusBefore(null);
        }else {
            this.setUserStatusBefore(userE.getEmploymentFlag());
        }


        this.setCorpinfoIdAfter(null);
        this.setCorpinfoNameAfter(rzUserRecordE.getCorporationName());
        this.setDepartmentIdAfter(null);
        this.setDepartmentNameAfter(rzUserRecordE.getDeptName());
        this.setPostIdAfter(null);
        this.setPostNameAfter(rzUserRecordE.getPositionName());
        this.setUserStatusAfter(UserEmploymentFlagEnum.getByDesc(rzUserRecordE.getEmployeeStatus()).getCode());

        this.setStatus(UserChangeRecordStatusEnum.APPROVED.getCode());
    }

    public void initUserUpdate(UserE userE, UserE oldUserE, String corpName, String name) {
        this.setUserId(userE.getId());
        this.setChangeTime(new Date());
        this.setCorpinfoIdBefore(oldUserE.getCorpinfoId());
        this.setCorpinfoNameBefore(oldUserE.getCorpinfoName());
        this.setDepartmentIdBefore(oldUserE.getDepartmentId());
        this.setDepartmentNameBefore(oldUserE.getDepartmentName());
        this.setPostIdBefore(oldUserE.getPostId());
        this.setPostNameBefore(oldUserE.getPostName());
        this.setUserStatusBefore(oldUserE.getEmploymentFlag());
        this.setCorpinfoIdAfter(userE.getCorpinfoId());
        this.setCorpinfoNameAfter(corpName);
        this.setDepartmentIdAfter(userE.getDepartmentId());
        this.setDepartmentNameAfter(name);
        this.setPostIdAfter(userE.getPostId());
        this.setPostNameAfter(userE.getPostName());
        this.setUserStatusAfter(oldUserE.getEmploymentFlag());
        this.setStatus(UserChangeRecordStatusEnum.APPROVED.getCode());
    }
    /**
     * 离职相关
     * @param userE
     * @param code
     */
    public void executeResignation(UserE userE, Integer code, Integer status, Integer employmentFlagBefore) {
        this.setUserId(userE.getId());
        this.setChangeTime(new Date());
        this.setCorpinfoIdBefore(userE.getCorpinfoId());
        this.setCorpinfoNameBefore(userE.getCorpinfoName());
        this.setDepartmentIdBefore(userE.getDepartmentId());
        this.setDepartmentNameBefore(userE.getDepartmentName());
        this.setPostIdBefore(userE.getPostId());
        this.setPostNameBefore(userE.getPostName());
        this.setUserStatusBefore(employmentFlagBefore);
        this.setCorpinfoIdAfter(null);
        this.setCorpinfoNameAfter(null);
        this.setDepartmentIdAfter(null);
        this.setDepartmentNameAfter(null);
        this.setPostIdAfter(null);
        this.setPostNameAfter(null);
        this.setUserStatusAfter(code);
        this.setStatus(status);
    }

    /**
     * 审核相关
     * @param userE
     * @param employmentFlagAfter
     */
    public void executeReviewStatus(UserE userE, Integer employmentFlagAfter, Integer status) {
        this.setUserId(userE.getId());
        this.setChangeTime(new Date());
        this.setCorpinfoIdBefore(userE.getCorpinfoId());
        this.setCorpinfoNameBefore(userE.getCorpinfoName());
        this.setDepartmentIdBefore(userE.getDepartmentId());
        this.setDepartmentNameBefore(userE.getDepartmentName());
        this.setPostIdBefore(userE.getPostId());
        this.setPostNameBefore(userE.getPostName());
        this.setUserStatusBefore(userE.getEmploymentFlag());
        this.setCorpinfoIdAfter(null);
        this.setCorpinfoNameAfter(null);
        this.setDepartmentIdAfter(null);
        this.setDepartmentNameAfter(null);
        this.setPostIdAfter(null);
        this.setPostNameAfter(null);
        this.setUserStatusAfter(employmentFlagAfter);
        this.setStatus(status);
    }
}

