package jnpf.personnels.controller.web.secondment;

import jnpf.base.ActionResult;
import jnpf.base.vo.PageListVO;
import jnpf.database.util.TenantDataSourceUtil;
import jnpf.exception.LoginException;
import jnpf.model.attendance.vo.DailyApprovalVo;
import jnpf.model.cultivate.CultivatePage;
import jnpf.model.enums.FtbPersonnelsAuditTaskEnum;
import jnpf.model.personnels.dto.audit.FtbPersonnelsAuditDto;
import jnpf.model.personnels.dto.base.PersonnelsQueryDTO;
import jnpf.model.personnels.dto.secondment.FtbHandleSecondmentDTO;
import jnpf.model.personnels.dto.secondment.FtbSecondMentQueryDTO;
import jnpf.model.personnels.vo.secondment.FtbPersonnelsSecondmentInfoVO;
import jnpf.model.personnels.vo.secondment.FtbPersonnelsSecondmentVO;
import jnpf.personnels.msg.PersonnelsConsumerSourceMsg;
import jnpf.personnels.service.FtbPersonnelsSecondmentManagementService;
import jnpf.util.NoDataSourceBind;
import jnpf.util.excel.EasyExcelUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * web借调管理
 * @Author: peng.hao
 * @create: 2025/9/11
 */
@RestController
@RequestMapping("/web/secondment")
public class FtbPersonnelsSecondmentController {

    @Resource
    private FtbPersonnelsSecondmentManagementService ftbPersonnelsSecondmentService;

    @Resource
    PersonnelsConsumerSourceMsg secondmentConsumerSourceMsg;


    /**
     * 列表查询
     */
    @PostMapping("/list-query")
    public ActionResult<PageListVO<FtbPersonnelsSecondmentVO>> pageList(@RequestBody PersonnelsQueryDTO dto) {
        CultivatePage page = new CultivatePage();
        page.setCurrentPage(dto.getCurrentPage());
        page.setPageSize(dto.getPageSize());
        PageListVO<FtbPersonnelsSecondmentVO> pageListVO = ftbPersonnelsSecondmentService.pageList(dto, page);
        return ActionResult.success(pageListVO);
    }
    /**
     * 导出
     */
    @PostMapping("/list-query-export")
    public void pageListExport(@RequestBody PersonnelsQueryDTO dto, HttpServletResponse response) throws IOException {
        CultivatePage page = new CultivatePage();
        page.setPageSize(-1);
        List<FtbPersonnelsSecondmentVO> secondmentVOS = ftbPersonnelsSecondmentService.pageList(dto, page).getList();
        secondmentVOS.forEach(v->{
            String message = FtbPersonnelsAuditTaskEnum.getMessage(v.getTransferStatus());
            v.setTransferStatusName(message);
        });
        EasyExcelUtils.exportExcel(response, "借调列表", secondmentVOS, FtbPersonnelsSecondmentVO.class);
    }

    /**
     * 详情
     */
    @GetMapping("/details/{id}")
    public ActionResult<FtbPersonnelsSecondmentInfoVO> details(@PathVariable String id) {
        return ActionResult.success(ftbPersonnelsSecondmentService.details(id));
    }

    /**
     * 办理借调
     */
    @PostMapping("/handle-secondment")
    public ActionResult<String> handleSecondment(@RequestBody @Validated FtbHandleSecondmentDTO ftbHandleSecondmentDTO) {
        return ActionResult.success("",ftbPersonnelsSecondmentService.handleSecondment(ftbHandleSecondmentDTO));
    }
    /**
     * 借调删除
     */
    @DeleteMapping("/delete/{id}")
    public ActionResult<Void> delete(@PathVariable String id) {
        ftbPersonnelsSecondmentService.delete(id);
        return ActionResult.success();
    }

    /**
     * 办理借调-oa
     */
    @PostMapping("/handle-secondment-oa")
    public ActionResult<Void> handleSecondmentForOA(@RequestBody @Validated FtbHandleSecondmentDTO ftbHandleSecondmentDTO) {
        ftbPersonnelsSecondmentService.handleSecondmentForOA(ftbHandleSecondmentDTO);
        return ActionResult.success();
    }
    /**
     * 借调审批
     */
    @PostMapping("/secondment-approval")
    public ActionResult<Void> secondmentApproval(@RequestBody FtbPersonnelsAuditDto ftbPersonnelsAuditDto) {
        ftbPersonnelsSecondmentService.secondmentApproval(ftbPersonnelsAuditDto);
        return ActionResult.success();
    }
    /**
     * 借调撤回
     */
    @PostMapping("/revoke")
    public ActionResult<Void> secondmentWithdrawal(@RequestBody FtbPersonnelsAuditDto ftbPersonnelsAuditDto) {
        ftbPersonnelsSecondmentService.secondmentWithdrawal(ftbPersonnelsAuditDto);
        return ActionResult.success();
    }
    /**
     * 提前结束
     *   {
     *        "id": "借调id",
     *        "endTime" : "最新借调结束时间"
     *   }
     */
    @PutMapping("/early-end")
    public ActionResult<Void> earlyEnd(@RequestBody Map<String, Object> map) {
        ftbPersonnelsSecondmentService.earlyEnd(map);
        return ActionResult.success();
    }
    /**
     * 延长借调时间
     *{
     *      "id": "借调id",
     *      "endTime" : "最新借调结束时间"
     * }
     */
    @PutMapping("/delay-time")
    public ActionResult<Void> delayTime(@RequestBody Map<String, Object> map) {
        ftbPersonnelsSecondmentService.delayTime(map);
        return ActionResult.success();
    }

    /**
     * test 消息发送
     */
    @GetMapping("/send-message")
    @NoDataSourceBind
    public ActionResult<Void> sendMessage() {
        secondmentConsumerSourceMsg.sendMessage("test msg");
        return ActionResult.success();
    }
    /**
     * 选人后校验员工是否处于借调中
     */
    @GetMapping("/check-is-in-the-process")
    public ActionResult<Boolean> checkIsInTheProcess(String userId, Date startTime, Date endTime) {
       return ActionResult.success(ftbPersonnelsSecondmentService.checkIsInTheProcess(userId,startTime,endTime));
    }
    /**
     * 校验是否有借调记录
     *
     */
    @GetMapping("/check-whether-there-is-a-secondment-record")
    public ActionResult<Integer> checkWhetherThereIsASecondmentRecord(String userId, Date leaveTime) {
        return ActionResult.success( ftbPersonnelsSecondmentService.checkWhetherThereIsASecondmentRecord(userId, leaveTime));
    }
    /**
     * 根据userI 和开始时间结束时间查询借调记录
     */
    @GetMapping("/get-secondment-record")
    public List<FtbPersonnelsSecondmentVO> getSecondmentRecord(@RequestParam("userId") String userId,
                                                               @RequestParam(value = "startLeaveTime",required = false)String startLeaveTime,
                                                               @RequestParam(value = "endLeaveTime",required = false)String endLeaveTime) {
        return ftbPersonnelsSecondmentService.getSecondmentRecord(userId, startLeaveTime,endLeaveTime);
    }
    /**
     * 批量查询审批中和审批完成的借调记录
     */
    @PostMapping("/get-secondment-record-bath")
    public List<FtbPersonnelsSecondmentVO> getSecondmentRecordBath(@RequestBody FtbSecondMentQueryDTO dto) {
        return ftbPersonnelsSecondmentService.getSecondmentRecordBath(dto);
    }
    /**
     * 借调返岗
     */
    @GetMapping("/secondedBackToWork")
    @NoDataSourceBind
    public void  secondedBackToWork(String tenantId) throws LoginException {
        TenantDataSourceUtil.switchTenant(tenantId);
        // 定时任务检测当借调结束后薪资生成 借调返岗
        ftbPersonnelsSecondmentService.checkSecondmentEnd(tenantId);
    }
    /**
     * 查询审批中和审批完成的借调记录
     */
    @PostMapping("/list-query-approval")
    public List<DailyApprovalVo> queryListApproval(@RequestBody FtbSecondMentQueryDTO  dto) {
        return ftbPersonnelsSecondmentService.queryListApproval(dto);
    }
    /**
     * 借调作废
     */
    @PutMapping("/cancel/{id}")
    public ActionResult<Void> cancel(@PathVariable String id) {
        ftbPersonnelsSecondmentService.cancel(id);
        return ActionResult.success();
    }
}
