package jnpf.personnels.controller.web.uchisuike;

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.cultivate.po.FtbCultivateFile;
import jnpf.model.personnels.dto.uchisuike.DeleteRecommendedPersonnelDTO;
import jnpf.model.personnels.dto.uchisuike.FtbRecommendationPoolOrgDTO;
import jnpf.model.personnels.dto.uchisuike.FtbinternalRecommendationPoolListDTO;
import jnpf.model.personnels.vo.uchisuike.FtbinternalRecommendationPoolExportVO;
import jnpf.model.personnels.vo.uchisuike.FtbinternalRecommendationPoolVO;
import jnpf.personnels.service.FtbPersonnelsUchisuikePondService;
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.stream.Collectors;

/**
 * web人事调离内推池模块
 *
 * @author wcx
 */
@RestController
@RequestMapping("/web/personnels-uchisuike-pond")
public class FtbPersonnelsUchisuikePondController {

    @Resource
    private FtbPersonnelsUchisuikePondService ftbPersonnelsUchisuikePondService;

    /**
     * 查看简历，返回简历地址集合
     *
     * @param id 内推人员主键id(必传)
     * @return {@link ActionResult}
     */
    @GetMapping("/view-resume")
    public ActionResult<List<FtbCultivateFile>> viewResume(String id) {
        List<FtbCultivateFile> result = ftbPersonnelsUchisuikePondService.viewResume(id);
        return ActionResult.success(result);
    }


    /**
     * 批量删除内推人员
     *
     * @return {@link ActionResult}
     */
    @DeleteMapping("/delete-recommended-personnel")
    public ActionResult deleteRecommendedPersonnel(@RequestBody @Validated DeleteRecommendedPersonnelDTO dto) {
        ftbPersonnelsUchisuikePondService.deleteRecommendedPersonnel(dto);
        return ActionResult.success();
    }


    /**
     * 内推池列表查询
     *
     * @param cultivatePage                        分页参数
     * @param ftbinternalRecommendationPoolListDTO 查询参数
     * @return {@link ActionResult}<{@link PageListVO}<{@link FtbinternalRecommendationPoolVO}>>
     */
    @GetMapping("/internal-recommendation-pool-list-query")
    public ActionResult<PageListVO<FtbinternalRecommendationPoolVO>> internalRecommendationPoolListQuery(CultivatePage cultivatePage,
                                                                                                         FtbinternalRecommendationPoolListDTO ftbinternalRecommendationPoolListDTO) {
        PageListVO<FtbinternalRecommendationPoolVO> ftbinternalRecommendationPoolVOPageListVO = ftbPersonnelsUchisuikePondService.internalRecommendationPoolListQuery(cultivatePage
                , ftbinternalRecommendationPoolListDTO);
        return ActionResult.success(ftbinternalRecommendationPoolVOPageListVO);
    }

    /**
     * 内推池列表导出
     *
     * @return {@link ActionResult }
     */
    @GetMapping(value = "/list-export")
    public void listExport(CultivatePage cultivatePage
            , FtbinternalRecommendationPoolListDTO ftbinternalRecommendationPoolListDTO) throws IOException {
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletResponse httpServletResponse = requestAttributes.getResponse();
        PageListVO<FtbinternalRecommendationPoolVO> ftbinternalRecommendationPoolVOPageListVO = ftbPersonnelsUchisuikePondService.internalRecommendationPoolListQuery(cultivatePage
                , ftbinternalRecommendationPoolListDTO);
        List<FtbinternalRecommendationPoolExportVO> result = ftbinternalRecommendationPoolVOPageListVO.getList()
                .stream()
                .map(FtbinternalRecommendationPoolExportVO::convert)
                .collect(Collectors.toList());
        EasyExcelUtils.exportExcel(httpServletResponse, "内推池", result, FtbinternalRecommendationPoolExportVO.class);
    }

    /**
     * 内推池详情
     *
     * @param id 内推人员主键id(必传)
     * @return {@link ActionResult}<{@link FtbinternalRecommendationPoolVO}>
     */
    @GetMapping("/internal-recommendation-details")
    public ActionResult<FtbinternalRecommendationPoolVO> internalRecommendationDetails(String id) {
        FtbinternalRecommendationPoolVO ftbinternalRecommendationPoolVO = ftbPersonnelsUchisuikePondService.internalRecommendationDetails(id);
        return ActionResult.success(ftbinternalRecommendationPoolVO);
    }

    /**
     * 内推池组织变更
     */
    @PostMapping("/internal-recommendation-organizational-changes")
    public ActionResult referralPoolOrganizationChange(@RequestBody @Validated FtbRecommendationPoolOrgDTO ftbRecommendationPoolOrgDTO) {
        ftbPersonnelsUchisuikePondService.referralPoolOrganizationChange(ftbRecommendationPoolOrgDTO);
        return ActionResult.success();
    }

    /**
     * 内推池组织查询
     */
    @GetMapping("/internal-referral-pool-organization-query")
    public ActionResult<List<String>> internalReferralPoolOrganizationQuery() {
        List<String> results = ftbPersonnelsUchisuikePondService.internalReferralPoolOrganizationQuery();
        return ActionResult.success(results);
    }

    /**
     * 内推池组织查询，带租户id
     */
    @GetMapping("/internal-referral-pool-organization-query-tenant")
    @NoDataSourceBind
    public ActionResult<List<String>> internalReferralPoolOrganizationQueryTenant(@RequestParam(value = "tenantId") String tenantId) {
        try {
            TenantDataSourceUtil.switchTenant(tenantId);
        } catch (LoginException e) {
            throw new RuntimeException("切换租户失败");
        }
        List<String> results = ftbPersonnelsUchisuikePondService.internalReferralPoolOrganizationQuery();
        return ActionResult.success(results);
    }

}
