package jnpf.personnels.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import jnpf.ContractSignPersonnelNoticeApi;
import jnpf.base.UserInfo;
import jnpf.base.entity.SuperBaseEntity;
import jnpf.model.enums.StaffWorkerStatus;
import jnpf.model.personnels.dto.black.FtbPersonnelsBlacklistAddDTO;
import jnpf.model.personnels.dto.roster.FtbPersonnelsTrialDTO;
import jnpf.model.personnels.dto.roster.FtbjobTrialRejectedDTO;
import jnpf.model.personnels.dto.staff.registerform.ProbationPeriodDto;
import jnpf.model.personnels.dto.staff.roster.StaffTravlFailDto;
import jnpf.model.personnels.po.FtbPersonnelsStaffRoster;
import jnpf.personnels.service.*;
import jnpf.util.UserProvider;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

@Service
public class FtbPersonnelsTrialServiceImpl implements FtbPersonnelsTrialService {

    @Resource
    private FtbPersonnelsStaffRosterService ftbPersonnelsStaffRosterService;

    @Resource
    private FtbPersonnelsStaffRegistrationFormDataService registrationFormDataService;

    @Resource
    private FtbPersonnelsTurnoverManagementService ftbPersonnelsTurnoverManagementService;

    @Resource
    private FtbPersonnelsBlacklistService ftbPersonnelsBlacklistService;

    @Resource
    private ContractSignPersonnelNoticeApi contractSignPersonnelNoticeAPI;

    @Override
    @Transactional
    public void passedTheTrialJob(FtbPersonnelsTrialDTO ftbPersonnelsTrialDTO) {
        LambdaQueryWrapper<FtbPersonnelsStaffRoster> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(FtbPersonnelsStaffRoster::getId, ftbPersonnelsTrialDTO.getId());
        wrapper.eq(FtbPersonnelsStaffRoster::getEnabledMark, 0);
        FtbPersonnelsStaffRoster ftbPersonnelsStaffRoster = ftbPersonnelsStaffRosterService.getOne(wrapper);
        if (null != ftbPersonnelsStaffRoster && ftbPersonnelsStaffRoster.getWorkerStatus().equals("306")) {
            LambdaUpdateWrapper<FtbPersonnelsStaffRoster> updateWrapper = Wrappers.lambdaUpdate();
            updateWrapper.eq(SuperBaseEntity.SuperIBaseEntity::getId, ftbPersonnelsStaffRoster.getId());
            // 结束试岗后若该员工有试用期，工作状态变更为“试用”，若无试用期，工作状态则变更为“正式”
            String workerStatus = "302";
            // 是否有试岗期
            ProbationPeriodDto probationPeriodDto = ftbPersonnelsStaffRosterService.queryProbationPeriod(registrationFormDataService, ftbPersonnelsStaffRoster.getId());
            if (probationPeriodDto == null || "100".equals(probationPeriodDto.getType())) {
                workerStatus = "303";
            }
            updateWrapper.set(FtbPersonnelsStaffRoster::getWorkerStatus, workerStatus);
            // 更新工作状态
            registrationFormDataService.updateForOneField(ftbPersonnelsStaffRoster.getId(),
                    ftbPersonnelsStaffRoster.getPhone(), "workerStatus", workerStatus);
            ftbPersonnelsStaffRosterService.update(new FtbPersonnelsStaffRoster(), updateWrapper);

        }

    }

    @Override
    @Transactional
    public void jobTrialRejected(FtbjobTrialRejectedDTO ftbjobTrialRejectedDTO) {
        UserInfo userInfo = UserProvider.getUser();
        // 驳回原因
        StaffTravlFailDto dto = new StaffTravlFailDto();
        dto.setRemarks(ftbjobTrialRejectedDTO.getReasonsForJobRejection());
        dto.setList(ftbjobTrialRejectedDTO.getOrganizationInfo());
        dto.setFailDate(new Date());
        // 试岗员工试岗驳回时，系统判断该员工是否已签署劳动合同
//        ActionResult<ContractSignPersonnelFlagVo> result = contractSignPersonnelNoticeAPI.signPersonnel(ftbjobTrialRejectedDTO.getUserId(), userInfo.getTenantId());
//        ContractSignPersonnelFlagVo contractSignPersonnelFlagVo = result.getData();
//        if (Objects.isNull(contractSignPersonnelFlagVo)) {
//            throw new RuntimeException("电子合同降级处理查询员工是否已签署劳动合同失败");
//        }
        //【判断结果一】已签署-需要签署离职证明，完成签署后工作状态为“已离职”；已发起离职证明完成签署前，工作状态为“待离职”
        //【判断结果二】未签署-可直接变更为“已离职”
        dto.setStaffWorkerStatus(StaffWorkerStatus.RESIGNED);
//        if (!contractSignPersonnelFlagVo.getSignType().isEmpty()) {
//            FtbPersonnelsStaffRoster ftbPersonnelsStaffRoster = ftbPersonnelsStaffRosterService.queryRosterInfoByUserId(ftbjobTrialRejectedDTO.getUserId());
//            // 是否签署离职证明 0-未签署 1-已签署
//            if (ftbPersonnelsStaffRoster.getIsSignSeparation() == 0) {
//                dto.setStaffWorkerStatus(StaffWorkerStatus.PENDING_RESIGNATION);
//            }
//        }
        ftbPersonnelsStaffRosterService.innerTravelFail(ftbjobTrialRejectedDTO.getUserId(), dto);
        // 办理离职，工作状态为已离职才进行数据清除
        if (dto.getStaffWorkerStatus().equals(StaffWorkerStatus.RESIGNED)) {
            ftbPersonnelsTurnoverManagementService.jobTrialRejected(ftbjobTrialRejectedDTO);
        }
        // 是否纳入黑名单，0不纳入，1纳入
        if (ftbjobTrialRejectedDTO.getWhetherToBeIncluded() == 1) {
            FtbPersonnelsBlacklistAddDTO blacklistAddDTO = FtbjobTrialRejectedDTO.convertBlacklistAddDto(ftbjobTrialRejectedDTO);
            ftbPersonnelsBlacklistService.addBlacklist(blacklistAddDTO);
        }
    }


}
