package jnpf.personnels.controller.app.employmentapply;

import jnpf.base.ActionResult;
import jnpf.base.vo.PageListVO;
import jnpf.model.personnels.dto.emp.FtbEmpAddNewDTO;
import jnpf.model.personnels.dto.emp.FtbEmpConfirmDTO;
import jnpf.model.personnels.dto.emp.FtbEmpEntryDTO;
import jnpf.model.personnels.vo.emp.FtbEmpConfirmVO;
import jnpf.model.personnels.vo.emp.FtbEmpEntryVO;
import jnpf.model.personnels.vo.emp.FtbEmpResultVO;
import jnpf.personnels.service.FtbPersonnelsEmEntryService;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

/**
 * app入职管理
 * @Author: peng.hao
 * @create: 2025/4/7
 */
@RestController
@RequestMapping("/app/personnels-emp-entry")
public class FtbPersonnelsEmEntryAppController {

    @Resource
    FtbPersonnelsEmEntryService ftbPersonnelsEmEntryService;

    @Resource
    RedissonClient redissonClient;


    /**
     * 查询入职管理列表
     */
    @GetMapping("/query-list")
    public ActionResult<PageListVO<FtbEmpEntryVO>> pageLists(FtbEmpEntryDTO empEntryDTO) {
        return ActionResult.success(ftbPersonnelsEmEntryService.pageLists(empEntryDTO,"app"));
    }
    /**
     * 新增员工/编辑未办理员工
     */
    @PutMapping("/add-or-update-new-emp")
    public ActionResult<FtbEmpResultVO> addNewEmp(@RequestBody FtbEmpAddNewDTO dto) {
        FtbEmpResultVO resultVO = ftbPersonnelsEmEntryService.addNewEmp(dto);
        return ActionResult.success(resultVO);
    }

    /**
     * 终止入职
     */
    @PutMapping("/terminate-onboarding/{id}")
    public ActionResult<Void> terminateOnboarding(@PathVariable String id) {
        ftbPersonnelsEmEntryService.terminateOnboarding(id);
        return ActionResult.success();
    }
    /**
     * 办理入职
     */
    @PutMapping("/handle-join-job")
    public ActionResult<Void> handleJoinJob(@RequestBody FtbEmpConfirmDTO ftbEmpConfirmDTO) {
        String lockKey = "HANDLE_JOIN_JOB:" + ftbEmpConfirmDTO.getPhone();
        RLock lock = redissonClient.getLock(lockKey);
        try {
            boolean b = lock.tryLock(1000, TimeUnit.MILLISECONDS);
            if (!b) {return ActionResult.fail("当前手机号正在办理中请勿重复点击");}
            ftbPersonnelsEmEntryService.handleJoinJob(ftbEmpConfirmDTO);
        }catch (Exception e){
            e.printStackTrace();
            return ActionResult.fail(e.getMessage());
        }finally {
            if (lock.isHeldByCurrentThread()) {
                lock.unlock();
            }
        }
        return ActionResult.success();
    }

    /**
     * 查看入职详情
     */
    @GetMapping("/onboarding-details")
    public ActionResult<FtbEmpConfirmVO> onboardingDetails(@RequestParam String id){
        return ActionResult.success(ftbPersonnelsEmEntryService.onboardingDetails(id));
    }
    /**
     * 删除员工入职记录
     *
     * @param id 主键id，必传
     */
    @DeleteMapping("/delete/{id}")
    public ActionResult<Void> deleteEmployeeOnboardingRecords(@PathVariable("id") String id) {
        ftbPersonnelsEmEntryService.deleteEmployeeOnboardingRecords(id);
        return ActionResult.success();
    }



}

