package jnpf.personnels.controller.web.transfer;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fantaibao.permission.annotation.FtbCheckPermission;
import jnpf.base.ActionResult;
import jnpf.base.vo.PageListVO;
import jnpf.database.util.TenantDataSourceUtil;
import jnpf.exception.LoginException;
import jnpf.model.cultivate.CultivatePage;
import jnpf.model.personnels.req.transfer.FtbHandleTransferDTO;
import jnpf.model.personnels.req.transfer.FtbHandleTransferOAAddDTO;
import jnpf.model.personnels.req.transfer.FtbHandleTransferOaDTO;
import jnpf.model.personnels.req.transfer.FtbHandleTransferQueryDTO;
import jnpf.model.personnels.vo.transfer.FtbHandleTransferDetailsVO;
import jnpf.model.personnels.vo.transfer.FtbHandleTransferPageExportVO;
import jnpf.model.personnels.vo.transfer.FtbHandleTransferPageVO;
import jnpf.personnels.service.FtbPersonnelsTransferManageService;
import jnpf.util.NoDataSourceBind;
import jnpf.util.excel.EasyExcelUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

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

/**
 * web人事调离调动管理模块
 *
 * @author xxxxx
 */
@RestController
@RequestMapping("/web/ftb-personnels-transfer-manage")
public class FtbPersonnelsTransferManageController {

    @Resource
    private FtbPersonnelsTransferManageService ftbPersonnelsTransferManageService;


    /**
     * 办理调动，直接办理
     */
    @PostMapping("/handle-transfer")
    public ActionResult<String> handleTransfer(@RequestBody @Validated FtbHandleTransferDTO ftbHandleTransferDTO) {
        String handleTransfer = ftbPersonnelsTransferManageService.handleTransfer(ftbHandleTransferDTO);
        return ActionResult.success("操作成功", handleTransfer);
    }

    /**
     * 办理调动，OA审批办理
     */
    @PostMapping("/handle-transfer-for-oa")
    public ActionResult<Void> handleTransferOa(@RequestBody @Validated FtbHandleTransferOAAddDTO ftbHandleTransferDTO) {
        FtbHandleTransferDTO ftbHandleTransferDTOR = ftbHandleTransferDTO.convert(ftbHandleTransferDTO);
        return ftbPersonnelsTransferManageService.handleTransferOa(ftbHandleTransferDTOR);
    }

    /**
     * 删除调动记录
     *
     * @param id 主键id，必传
     * @return {@link ActionResult}
     */
    @DeleteMapping("/delete/{id}")
    public ActionResult delete(@PathVariable("id") String id) {
        ftbPersonnelsTransferManageService.delete(id);
        return ActionResult.success();
    }

    /**
     * 手动办理撤销调动记录
     *
     * @param id 主键id，必传
     * @return {@link ActionResult}
     */
    @PutMapping("/revoke-the-transfer-record/{id}")
    public ActionResult revokeTheTransferRecord(@PathVariable("id") String id) {
        ftbPersonnelsTransferManageService.revokeTheTransferRecord(id);
        return ActionResult.success();
    }

    /**
     * 审批(for oa)
     */
    @PostMapping("/approval")
    public ActionResult<Void> approvalForOA(@Validated @RequestBody FtbHandleTransferOaDTO dto) {
        ftbPersonnelsTransferManageService.approvalForOA(dto);
        return ActionResult.success();
    }

    /**
     * 调动管理列表页
     */
    @PostMapping("/transfer-manage-list")
    @FtbCheckPermission("transferPositionMa")
    public ActionResult<PageListVO<FtbHandleTransferPageVO>> transferManageList(@RequestBody FtbHandleTransferQueryDTO ftbHandleTransferQueryDTO) {
        Page<FtbHandleTransferPageVO> page = ftbHandleTransferQueryDTO.coverCultivatePage();
        page = ftbPersonnelsTransferManageService.transferManageList(page, ftbHandleTransferQueryDTO);
        PageListVO<FtbHandleTransferPageVO> pageListVO = CultivatePage.coverPageList(page);
        return ActionResult.success(pageListVO);
    }

    /**
     * 调动管理导出
     */
    @PostMapping(value = "/list-export")
    public void listExport(@RequestBody FtbHandleTransferQueryDTO ftbHandleTransferQueryDTO) throws IOException {
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletResponse httpServletResponse = requestAttributes.getResponse();
        Page<FtbHandleTransferPageVO> page = ftbHandleTransferQueryDTO.coverCultivatePage();
        page.setSize(-1);
        page = ftbPersonnelsTransferManageService.transferManageList(page, ftbHandleTransferQueryDTO);
        List<FtbHandleTransferPageExportVO> exportVOList = page.getRecords().stream().map(FtbHandleTransferPageExportVO::convert).collect(Collectors.toList());
        EasyExcelUtils.exportExcel(httpServletResponse, "调动管理", exportVOList, FtbHandleTransferPageExportVO.class);
    }

    /**
     * 调动详情
     *
     * @param id 主键id，必传
     */
    @GetMapping("/transfer-details")
    public ActionResult<FtbHandleTransferDetailsVO> transferDetails(@RequestParam(value = "id") String id) {
        FtbHandleTransferDetailsVO ftbHandleTransferDetailsVO = ftbPersonnelsTransferManageService.transferDetails(id);
        return ActionResult.success(ftbHandleTransferDetailsVO);
    }


    /**
     * 调动生效定时任务
     */
    @PostMapping("/transfer-effect-scheduled-task")
    @NoDataSourceBind
    public ActionResult<Void> transferEffectScheduledTask(@RequestParam(value = "tenantId") String tenantId) {
        try {
            TenantDataSourceUtil.switchTenant(tenantId);
        } catch (LoginException e) {
            throw new RuntimeException("切换租户失败");
        }
        ftbPersonnelsTransferManageService.transferEffectScheduledTask(tenantId);
        return ActionResult.success();
    }

    /**
     * 查询调动类型下拉框(OA专用)
     */
    @GetMapping("/query-field-value")
    public ActionResult<List<Map<String, String>>> queryFieldValue() {
        List<Map<String, String>> value = ftbPersonnelsTransferManageService.queryFieldValue();
        return ActionResult.success("成功", value);
    }

}
