package jnpf.personnels.controller.web.regular;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import jnpf.base.ActionResult;
import jnpf.base.entity.SuperBaseEntity;
import jnpf.base.vo.PageListVO;
import jnpf.model.cultivate.CultivatePage;
import jnpf.model.personnels.dto.base.PersonnelsQueryDTO;
import jnpf.model.personnels.dto.regular.FtbPersonnelsRegularCreateDTO;
import jnpf.model.personnels.dto.regular.FtbPersonnelsSalaryAuditDto;
import jnpf.model.personnels.po.FtbPersonnelsRegularManagement;
import jnpf.model.personnels.vo.regular.FtbPersonnelsRegularInfoVO;
import jnpf.model.personnels.vo.regular.FtbPersonnelsRegularManagementVO;
import jnpf.personnels.service.FtbPersonnelsRegularManagementService;
import jnpf.util.UserProvider;
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;

/**
 * web转正管理模块
 *
 * @author peng.hao
 */
@RestController
@RequestMapping("/web/personnels-regular")
public class FtbPersonnelsRegularManagementController {

    @Resource
    FtbPersonnelsRegularManagementService service;

    /**
     * 转正管理列表查询，返回分页列表数据
     *
     * @param dto 查询参数对象
     * @return 返回分页列表数据的结果
     */
    @PostMapping("/list-query")
    public ActionResult<PageListVO<FtbPersonnelsRegularManagementVO>> pageList(@RequestBody PersonnelsQueryDTO dto) {
        CultivatePage page = new CultivatePage();
        page.setCurrentPage(dto.getCurrentPage());
        page.setPageSize(dto.getPageSize());
        PageListVO<FtbPersonnelsRegularManagementVO> pageListVO = service.pageList(dto, page, false);
        return ActionResult.success(pageListVO);
    }

    /**
     * 转正管理列表导出
     *
     * @param dto 查询参数对象
     * @return 返回分页列表数据的结果
     */
    @PostMapping("/list-query-export")
    public void pageListExport(@RequestBody PersonnelsQueryDTO dto, CultivatePage page, HttpServletResponse response) throws IOException {
        page.setPageSize(-1);
        PageListVO<FtbPersonnelsRegularManagementVO> pageListVO = service.pageList(dto, page, true);
        // 转换数字->汉字
        List<FtbPersonnelsRegularManagementVO> list = pageListVO.getList();
        if (CollUtil.isNotEmpty(list)) {
            list.parallelStream().forEach(a -> {
                // 审批状态
                FtbPersonnelsRegularManagementVO.approvalStatusTranslation(a);
                // 试用期
                FtbPersonnelsRegularManagementVO.probationPeriodTranslation(a);
            });
        }

        String fileName = "转正管理列表导出";
        EasyExcelUtils.exportExcel(response, fileName, pageListVO.getList(), FtbPersonnelsRegularManagementVO.class);
    }


    /**
     * 办理转正
     *
     * @param createDTO 申请参数对象
     * @return 返回申请结果
     */
    @PutMapping("/apply-regularization")
    public ActionResult<String> applyForRegularization(@Validated @RequestBody FtbPersonnelsRegularCreateDTO createDTO) {
        return ActionResult.success("",service.applyForRegularization(createDTO));
    }

    /**
     * 办理转正(for oa)
     *
     * @param createDTO 申请参数对象
     * @return 返回申请结果
     */
    @PostMapping("/apply-regularization-for-oa")
    public ActionResult<Void> applyForRegularizationForOA(@Validated @RequestBody FtbPersonnelsRegularCreateDTO createDTO) {

        return service.applyForRegularizationForOA(createDTO);
    }


    /**
     * 查看详细信息
     *
     * @param id 审批id
     * @return 返回 regularization 审批的详细信息结果
     */
    @GetMapping("/details-process/{id}")
    public ActionResult<FtbPersonnelsRegularInfoVO> checkTheDetailsOfRegularizationApproval(@PathVariable("id")String id) {
        return ActionResult.success(service.checkTheDetailsOfRegularizationApproval(id));
    }
  
    /**
     * 进行转正审批
     *
     * @param auditDto 审批信息
     * @return 返回 regularization 审批结果
     */
    @PutMapping("/regularization-approval")
    @Deprecated(since = "1.3.0")
    public ActionResult<String> regularizationApproval(@Validated @RequestBody FtbPersonnelsSalaryAuditDto auditDto) {
        service.regularizationApproval(auditDto);
        return ActionResult.success();
    }


    /**
     * 进行转正审批(for oa)
     *
     * @param auditDto 审批信息
     * @return 返回 regularization 审批结果
     */
    @PostMapping("/regularization-approval")
    public ActionResult<Void> regularizationApprovalForOA(@Validated @RequestBody FtbPersonnelsSalaryAuditDto auditDto) {
        ActionResult<Void> map =  service.regularizationApprovalForOA(auditDto);
        return map;
    }


    /**
     * 撤销申请(oa)
     * @param id
     * @return
     */
    @GetMapping("/cancellation-rectification")
    public ActionResult<String> cancellationOfApplicationForRectification(String id,String taskId){
        service.cancellationOfApplicationForRectification(id,taskId);
        return ActionResult.success();
    }

    /**
     * 删除转正申请
     * @param id
     * @return
     */
    @PutMapping("/delete-rectification/{id}")
    public ActionResult<String> deleteApplicationForRectification(@PathVariable("id")String id){
        LambdaUpdateWrapper<FtbPersonnelsRegularManagement> updateWrapper = Wrappers.lambdaUpdate();
        updateWrapper.set(FtbPersonnelsRegularManagement::getEnableMark,1);
        updateWrapper.set(SuperBaseEntity.SuperCUDBaseEntity::getDeleteUserId, UserProvider.getUser().getId());
        updateWrapper.set(SuperBaseEntity.SuperCUDBaseEntity::getDeleteTime,new Date());
        updateWrapper.eq(SuperBaseEntity.SuperIBaseEntity::getId,id);
        service.update(updateWrapper);
        return ActionResult.success();
    }

}
