package jnpf.cultivate.controller.web.position;


import cn.hutool.core.collection.CollUtil;
import jnpf.base.ActionResult;
import jnpf.base.vo.PageListVO;
import jnpf.cultivate.service.FtbCultivatePositionStatisticesService;
import jnpf.cultivate.utils.UserApiV2Util;
import jnpf.model.cultivate.dto.position.FtbCultivatePersonStatisticesDTO;
import jnpf.model.cultivate.dto.position.FtbCultivatePositionOrgStatisticesDTO;
import jnpf.model.cultivate.dto.position.FtbCultivatePositionPersonStatisticesDTO;
import jnpf.model.cultivate.vo.position.*;
import jnpf.permission.vo.v2.user.UserBoundVO;
import jnpf.util.EasyExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

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

/**
 * web岗位学习统计
 *
 * @author fantaibao
 * @date 2024/03/22
 */
@RequestMapping("/web/position/statistices")
@RestController
public class FtbCultivatePositionStatisticesController {

    @Resource
    private FtbCultivatePositionStatisticesService ftbCultivatePositionStatisticesService;

    @Autowired
    private UserApiV2Util userApiV2Util;

    /**
     * 组织维度统计
     *
     * @param statisticDTO
     * @return
     */
    @PostMapping("/org-list-statistics")
    public ActionResult<PageListVO<FtbCultivatePositionOrgStatisticesVO>> organizationListStatistics(
            @RequestBody FtbCultivatePositionOrgStatisticesDTO statisticDTO) {
        PageListVO<FtbCultivatePositionOrgStatisticesVO> listVO = ftbCultivatePositionStatisticesService.organizationListStatistics(statisticDTO);
        return ActionResult.success(listVO);
    }

    /**
     * 组织维度统计-导出
     *
     * @param statisticDTO
     */
    @PostMapping("/org-list-statistics/export")
    public void exportInformationorganization(@RequestBody FtbCultivatePositionOrgStatisticesDTO statisticDTO, HttpServletResponse response) throws IOException {
        statisticDTO.setPageSize(9999999);
        PageListVO<FtbCultivatePositionOrgStatisticesVO> listVO = ftbCultivatePositionStatisticesService.organizationListStatistics(statisticDTO);
        EasyExcelUtil.simpleWrite(listVO.getList(), "组织维度统计", FtbCultivatePositionOrgStatisticesVO.class, response);
    }

    /**
     * 个人维度统计
     *
     * @param ftbCultivatePositionPersonStatisticesDTO
     */
    @PostMapping("/person-list-statistics")
    public ActionResult<PageListVO<FtbCultivatePositionPersonStatisticesVO>> personListStatistics(
            @RequestBody FtbCultivatePositionPersonStatisticesDTO ftbCultivatePositionPersonStatisticesDTO) {
        PageListVO<FtbCultivatePositionPersonStatisticesVO> listVO = ftbCultivatePositionStatisticesService.personListStatistics(ftbCultivatePositionPersonStatisticesDTO);
        return ActionResult.success(listVO);
    }

    /**
     * 个人维度统计导出信息
     *
     * @param personWisdomStatisticDTO
     */
    @PostMapping("/person-list-statistics/export")
    public void exportInformationPerson(@RequestBody FtbCultivatePositionPersonStatisticesDTO personWisdomStatisticDTO, HttpServletResponse response) throws IOException {
        PageListVO<FtbCultivatePositionPersonStatisticesVO> listVO = ftbCultivatePositionStatisticesService.personListStatistics(personWisdomStatisticDTO);
        List<FtbCultivatePositionPersonStatisticesExportVO> statisticesExportVOS = listVO.getList()
                .stream()
                .map(FtbCultivatePositionPersonStatisticesExportVO::convert)
                .collect(Collectors.toList());
        EasyExcelUtil.simpleWrite(statisticesExportVOS, "个人维度统计", FtbCultivatePositionPersonStatisticesExportVO.class, response);
    }

    /**
     * 岗位维度统计
     *
     * @param ftbCultivatePositionPersonStatisticesDTO
     */
    @PostMapping("/position-list-statistics")
    public ActionResult<PageListVO<FtbCultivatePersonStatisticesVO>> positionListStatistics(
            @RequestBody FtbCultivatePersonStatisticesDTO ftbCultivatePositionPersonStatisticesDTO) {
        PageListVO<FtbCultivatePersonStatisticesVO> listVO = ftbCultivatePositionStatisticesService.positionListStatistics(ftbCultivatePositionPersonStatisticesDTO);
        return ActionResult.success(listVO);
    }

    /**
     * 岗位统计导出信息
     *
     * @param personWisdomStatisticDTO
     */
    @PostMapping("/position-list-statistics/export")
    public void exportInformationPerson(@RequestBody FtbCultivatePersonStatisticesDTO personWisdomStatisticDTO, HttpServletResponse response) throws IOException {
        personWisdomStatisticDTO.setPageSize(-1);
        PageListVO<FtbCultivatePersonStatisticesVO> listVO = ftbCultivatePositionStatisticesService.positionListStatistics(personWisdomStatisticDTO);
        List<FtbCultivatePersonStatisticesExportVO> statisticesExportVOS = listVO.getList()
                .stream()
                .map(FtbCultivatePersonStatisticesExportVO::convert)
                .collect(Collectors.toList());
        EasyExcelUtil.simpleWrite(statisticesExportVOS, "职等维度统计", FtbCultivatePersonStatisticesExportVO.class, response);
    }


    /**
     * 组织维度-课程详情
     *
     * @param startDate 开始日期格式yyyy-mm-dd hh:mm:ss
     * @param endDate   结束日期格式yyyy-mm-dd hh:mm:ss
     * @param orgId     组织id主键(必传)
     * @return {@link ActionResult}<{@link List}<{@link OrganizeCourseDetails}>>
     */
    @GetMapping("/organize-course-details")
    public ActionResult<List<OrganizeCourseDetails>> organizeCourseDetails(String startDate, String endDate, String orgId) {
        List<OrganizeCourseDetails> listVO = ftbCultivatePositionStatisticesService.organizeCourseDetails(startDate, endDate, orgId);
        return ActionResult.success(listVO);
    }

    /**
     * 个人维度-课程详情
     *
     * @param startDate 开始日期格式yyyy-mm-dd hh:mm:ss
     * @param endDate   结束日期格式yyyy-mm-dd hh:mm:ss
     * @param userId    用户id主键(必传)
     * @return {@link ActionResult}<{@link List}<{@link OrganizeCourseDetails}>>
     */
    @GetMapping("/personal-dimension-course-details")
    public ActionResult<List<PersonalDimensionCourseDetails>> personalDimensionCourseDetails(String startDate, String endDate, String userId) {
        List<PersonalDimensionCourseDetails> listVO = ftbCultivatePositionStatisticesService.personalDimensionCourseDetails(startDate, endDate, userId);
        return ActionResult.success(listVO);
    }

    /**
     * 个人信息-所属组织查询
     *
     * @param userId 用户主键id
     * @return {@link ActionResult}
     */
    @GetMapping("/personal-organizational-information")
    public ActionResult<List<PersonalDimensionCourseDetails.PositionInformation>> personalInformation(String userId) {
        Map<String, List<UserBoundVO>> userPrimaryBoundBatchCompatible = userApiV2Util.getUserPrimaryBoundBatchCompatible(List.of(userId),null);
        List<UserBoundVO> userPrimaryBoundVOS = userPrimaryBoundBatchCompatible.get(userId);
        if (CollUtil.isNotEmpty(userPrimaryBoundVOS)) {
            List<PersonalDimensionCourseDetails.PositionInformation> result = userPrimaryBoundVOS.stream().map(userBoundMoreInfoVO -> {
                PersonalDimensionCourseDetails.PositionInformation positionInformation = new PersonalDimensionCourseDetails.PositionInformation();
                positionInformation.setNameOfAssociation(userBoundMoreInfoVO.getOrganizeName());
                positionInformation.setPositionName(userBoundMoreInfoVO.getPositionName());
                positionInformation.setPostGradeName(userBoundMoreInfoVO.getGradeName());
                positionInformation.setOrganizationId(userBoundMoreInfoVO.getOrganizeId());
                return positionInformation;
            }).collect(Collectors.toList());
            return ActionResult.success(result);
        }
        return ActionResult.success(new ArrayList<>());
    }

    /**
     * 岗位维度-点击岗位名称数据统计
     *
     * @param postId 岗位主键ID(必传)
     * @return {@link ActionResult}<{@link JobTitleStatistics}>
     */
    @GetMapping("/job-title-statistics")
    public ActionResult<JobTitleStatistics> jobTitleStatistics(String postId) {
        return ActionResult.success(ftbCultivatePositionStatisticesService.jobTitleStatistics(postId));
    }


}
