package jnpf.personnels.utils;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import jnpf.base.ActionResult;
import jnpf.model.personnels.dto.salary.FtbPersonnelsSalaryInfo;
import jnpf.model.personnels.po.FtbPersonnelsStaffRoster;
import jnpf.model.personnels.vo.salary.UserInfoWithSalary;
import jnpf.permission.V2GradesApi;
import jnpf.permission.V2OrganizeApi;
import jnpf.permission.V2PositionApi;
import jnpf.permission.vo.v2.grades.GradeVO;
import jnpf.permission.vo.v2.organzie.OrganizeGeneralDetailVO;
import jnpf.permission.vo.v2.position.PositionVO;
import jnpf.personnels.mapper.FtbPersonnelsStaffRosterMapper;
import jnpf.personnels.service.FtbPersonnelsSalaryService;
import jnpf.util.JsonUtil;
import jnpf.util.UserProvider;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 人事薪酬调动类
 *
 * @author wangchunxiang
 * @date 2025/05/16
 */
@Component
public class PersonSalaryUtils {

    @Resource
    private FtbPersonnelsSalaryService personnelSalaryService;

    @Resource
    private FtbPersonnelsStaffRosterMapper ftbPersonnelsStaffRosterMapper;

    @Autowired
    private V2OrganizeApi v2OrganizeApi;

    @Autowired
    private V2PositionApi v2PositionApi;

    @Autowired
    private V2GradesApi v2GradesApi;

    /**
     * 同步薪酬数据
     *
     * @param salaryData                  薪资项数据
     * @param userId                      用户ID
     * @param currOrg                     当前组织id
     * @param currPosition                当前岗位id
     * @param currRank                    当前职级id
     * @param date                        薪资生效日期
     * @param updateType                  更新类型：0 重复入职 1-调薪，2-入职
     * @param reasonsForSalaryAdjustments 备注
     * @param operationType               操作标识 1：转正  0：其他  2: 调岗 3 晋升 4 入职，6调动
     * @param salaryType                  调薪方式 1.加薪 2.降薪
     * @param payrollSequenceId           薪资顺序id
     * @param tenantId                    租户Id
     * @param taskId                      任务Id
     */
    public String syncSalaryData(String salaryData, String userId, String currOrg, String currPosition, String currRank,
                               String updateType, Date date, String reasonsForSalaryAdjustments, String operationType, Integer salaryType
            , String payrollSequenceId, String tenantId, String taskId) {
        // 薪酬信息同步
        List<FtbPersonnelsSalaryInfo> salaryItemList = JsonUtil.getJsonToList(salaryData, FtbPersonnelsSalaryInfo.class);
        UserInfoWithSalary userInfoWithSalary = new UserInfoWithSalary();
        ActionResult<OrganizeGeneralDetailVO> actionResult = v2OrganizeApi.organizeInfoByIdNoToken(null, currOrg, tenantId);
        userInfoWithSalary.setFOrgId(currOrg);
        userInfoWithSalary.setTaskId(taskId);
        LambdaQueryWrapper<FtbPersonnelsStaffRoster> query = new LambdaQueryWrapper<>();
        query.eq(FtbPersonnelsStaffRoster::getUserId, userId);
        query.last("limit 1");
        FtbPersonnelsStaffRoster ftbPersonnelsStaffRoster = ftbPersonnelsStaffRosterMapper.selectOne(query);
        if (Objects.nonNull(ftbPersonnelsStaffRoster)) {
            userInfoWithSalary.setEntryDate(ftbPersonnelsStaffRoster.getActualStartDate());
            if (Objects.nonNull(ftbPersonnelsStaffRoster.getActualProbationaryDate())) {
                userInfoWithSalary.setBeComeDate(ftbPersonnelsStaffRoster.getActualProbationaryDate());
            } else {
                userInfoWithSalary.setBeComeDate(ftbPersonnelsStaffRoster.getActualStartDate());
            }
            userInfoWithSalary.setUserTypeId(ftbPersonnelsStaffRoster.getWorkerType());
            LambdaQueryWrapper<FtbPersonnelsStaffRoster> query2 = new LambdaQueryWrapper<>();
            query2.eq(FtbPersonnelsStaffRoster::getUserId, UserProvider.getUser().getUserId());
            FtbPersonnelsStaffRoster ftbPersonnelsStaffRoster2 = ftbPersonnelsStaffRosterMapper.selectOne(query2);
            // 操作人工号
            if (Objects.nonNull(ftbPersonnelsStaffRoster2)) {
                userInfoWithSalary.setOperatorWorkNo(ftbPersonnelsStaffRoster2.getWorkerNo());
            }
        }
        if (actionResult != null && actionResult.getCode() == 200) {
            userInfoWithSalary.setFOrgName(actionResult.getData().getName());
        }
        ActionResult<PositionVO> infoPosition = v2PositionApi.infoPositionNoToken(currPosition, tenantId);
        userInfoWithSalary.setPostId(currPosition);
        if (infoPosition != null && infoPosition.getCode() == 200) {
            userInfoWithSalary.setPostName(infoPosition.getData().getFullName());
        }
        if (StringUtils.isNotEmpty(currRank)) {
            ActionResult<GradeVO> gradeVOActionResult = v2GradesApi.infoGradeNoToken(currRank, tenantId);
            userInfoWithSalary.setRankId(currRank);
            if (gradeVOActionResult != null && gradeVOActionResult.getCode() == 200) {
                userInfoWithSalary.setRankName(gradeVOActionResult.getData().getFullName());
            }
        }
        // 生效日期取实际入职日期
        return personnelSalaryService.saveTheChangePayInformation(
                salaryItemList,
                userId,
                date,
                userInfoWithSalary,
                updateType,
                reasonsForSalaryAdjustments,
                operationType, 0, salaryType, payrollSequenceId, tenantId);
    }

}