package jnpf.personnels.controller.web.analysis;


import cn.hutool.core.date.DateUtil;
import jnpf.attendance.FtbStatisticsApi;
import jnpf.attendance.dto.AttendanceCountAvgHoursDto;
import jnpf.attendance.dto.AttendanceCountAvgHoursVo;
import jnpf.base.ActionResult;
import jnpf.base.vo.PageListVO;
import jnpf.model.personnels.dto.analysis.FtbPersonnlesAnalysisDTO;
import jnpf.model.personnels.vo.analysis.*;
import jnpf.personnels.service.FtbPersonnelsOverviewAnalysisService;
import jnpf.personnels.service.FtbPersonnelsTurnoverAnalysisService;
import jnpf.personnels.utils.PersonnelDataAnalysisUtil;
import jnpf.util.excel.EasyExcelUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

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

/**
 * web数据看板-人员概况
 *
 * @author wangchunxiang
 * @date 2024/10/16
 */
@RestController
@RequestMapping("/web/personnel-profile-analysis")
public class FtbPersonnelsOverviewAnalysisController {

    @Resource
    private FtbPersonnelsOverviewAnalysisService ftbPersonnelOverviewAnalysisService;

    @Autowired
    private FtbStatisticsApi ftbStatisticsApi;

    @Resource
    FtbPersonnelsTurnoverAnalysisService ftbPersonnelsTurnoverAnalysisService;


    /**
     * 在职员工
     *
     * @param dto 入参
     * @return {@link ActionResult }
     */
    @PostMapping("/current-employees")
    public ActionResult<PersonnelsOverviewEmployeesVO> currentEmployees(@RequestBody FtbPersonnlesAnalysisDTO dto) {
        PersonnelsOverviewEmployeesVO personnelsOverviewEmployeesVO = ftbPersonnelOverviewAnalysisService.currentEmployees(dto);
        return ActionResult.success(personnelsOverviewEmployeesVO);
    }

    /**
     * 在职员工柱状图
     *
     * @param dto 入参
     * @return {@link ActionResult }
     */
    @PostMapping("/status-employees")
    public ActionResult<List<PersonnelsOverviewProportionVO>> statusOfCurrentEmployees(@RequestBody FtbPersonnlesAnalysisDTO dto) {
        List<PersonnelsOverviewProportionVO> personnelsOverviewEmployeesVO = ftbPersonnelOverviewAnalysisService.statusOfCurrentEmployees(dto);
        return ActionResult.success(personnelsOverviewEmployeesVO);
    }

    /**
     * 在职员工列表
     *
     * @param dto 入参
     * @return {@link ActionResult }
     */
    @PostMapping("/status-employees-details")
    public ActionResult<PersonnelDataAnalysisListVO> statusOfCurrentEmployeesDetails(@RequestBody FtbPersonnlesAnalysisDTO dto) {
        PageListVO<PersonnelsOverviewEmployeesDetailsVO> result = ftbPersonnelOverviewAnalysisService.statusOfCurrentEmployeesDetails(dto);
        return ActionResult.success(PersonnelDataAnalysisUtil.encapsulatePageData(result, PersonnelsOverviewEmployeesDetailsVO.class));
    }

    /**
     * 在职员工列表导出
     *
     * @param dto 入参
     * @return {@link ActionResult }
     */
    @PostMapping("/status-employees-details-export")
    public void statusOfCurrentEmployeesDetailsExport(@RequestBody FtbPersonnlesAnalysisDTO dto) throws IOException {
        ftbPersonnelOverviewAnalysisService.statusOfCurrentEmployeesDetailsExport(dto);
    }


    /**
     * 平均年龄
     *
     * @param dto 入参
     * @return {@link ActionResult }
     */
    @PostMapping("/composite-life")
    public ActionResult<PersonnelsOverviewEmployeesVO> compositeLife(@RequestBody FtbPersonnlesAnalysisDTO dto) {
        PersonnelsOverviewEmployeesVO personnelsOverviewEmployeesVO = ftbPersonnelOverviewAnalysisService.compositeLife(dto);
        return ActionResult.success(personnelsOverviewEmployeesVO);
    }

    /**
     * 平均司龄
     *
     * @param dto 入参
     * @return {@link ActionResult }
     */
    @PostMapping("/average-age")
    public ActionResult<PersonnelsOverviewEmployeesAgeVO> averageAge(@RequestBody FtbPersonnlesAnalysisDTO dto) {
        PersonnelsOverviewEmployeesAgeVO personnelsOverviewEmployeesVO = ftbPersonnelOverviewAnalysisService.averageAge(dto);
        return ActionResult.success(personnelsOverviewEmployeesVO);
    }

    /**
     * 性别分布
     *
     * @param dto 入参
     * @return {@link ActionResult }
     */
    @PostMapping("/gender-distribution")
    public ActionResult<List<PersonnelsOverviewProportionVO>> genderDistribution(@RequestBody FtbPersonnlesAnalysisDTO dto) {
        List<PersonnelsOverviewProportionVO> personnelsOverviewEmployeesVO = ftbPersonnelOverviewAnalysisService.genderDistribution(dto);
        return ActionResult.success(personnelsOverviewEmployeesVO);
    }

    /**
     * 性别分布列表
     *
     * @param dto 入参
     * @return {@link ActionResult }
     */
    @PostMapping("/gender-distribution-details")
    public ActionResult<PersonnelDataAnalysisListVO> genderDistributionDetails(@RequestBody FtbPersonnlesAnalysisDTO dto) {
        PageListVO<PersonnelsOverviewProportionDetailsVO> result = ftbPersonnelOverviewAnalysisService.genderDistributionDetails(dto);
        return ActionResult.success(PersonnelDataAnalysisUtil.encapsulatePageData(result, PersonnelsOverviewProportionDetailsVO.class));
    }

    /**
     * 性别分布列表导出
     *
     * @param dto 入参
     * @return {@link ActionResult }
     */
    @PostMapping("/gender-distribution-details-export")
    public void genderDistributionDetailsExport(@RequestBody FtbPersonnlesAnalysisDTO dto) throws IOException {
        ftbPersonnelOverviewAnalysisService.genderDistributionDetailsExport(dto);
    }

    /**
     * 员工状态占比
     *
     * @param dto 入参
     * @return {@link ActionResult }
     */
    @PostMapping("/employee-status-proportion")
    public ActionResult<List<PersonnelsOverviewProportionVO>> employeeStatusProportion(@RequestBody FtbPersonnlesAnalysisDTO dto) {
        return ActionResult.success(ftbPersonnelOverviewAnalysisService.employeeStatusProportion(dto));
    }

    /**
     * 员工状态占比列表
     *
     * @param dto 入参
     * @return {@link ActionResult }
     */
    @PostMapping("/employee-status-proportion-details")
    public ActionResult<PersonnelDataAnalysisListVO> employeeStatusProportionDetails(@RequestBody FtbPersonnlesAnalysisDTO dto) {
        PageListVO<PersonnelsOverviewSituationDetailsVO> result = ftbPersonnelOverviewAnalysisService.employeeStatusProportionDetails(dto);
        return ActionResult.success(PersonnelDataAnalysisUtil.encapsulatePageData(result, PersonnelsOverviewSituationDetailsVO.class));
    }

    /**
     * 员工状态占比列表导出
     *
     * @param dto 入参
     * @return {@link ActionResult }
     */
    @PostMapping("/employee-status-proportion-details-export")
    public void employeeStatusProportionDetailsExport(@RequestBody FtbPersonnlesAnalysisDTO dto) throws IOException {
        ftbPersonnelOverviewAnalysisService.employeeStatusProportionDetailsExport(dto);
    }

    /**
     * 用工类型占比
     *
     * @param dto 入参
     * @return {@link ActionResult }
     */
    @PostMapping("/proportion-employment-types")
    public ActionResult<List<PersonnelsOverviewProportionVO>> proportionOfEmploymentTypes(@RequestBody FtbPersonnlesAnalysisDTO dto) {
        return ActionResult.success(ftbPersonnelOverviewAnalysisService.proportionOfEmploymentTypes(dto));
    }

    /**
     * 员工类型占比列表
     *
     * @param dto 入参
     * @return {@link ActionResult }
     */
    @PostMapping("/proportion-employment-types-details")
    public ActionResult<PersonnelDataAnalysisListVO> proportionOfEmploymentTypesDetails(@RequestBody FtbPersonnlesAnalysisDTO dto) {
        PageListVO<PersonnelsOverviewTypeDetailsVO> result = ftbPersonnelOverviewAnalysisService.proportionOfEmploymentTypesDetails(dto);
        return ActionResult.success(PersonnelDataAnalysisUtil.encapsulatePageData(result, PersonnelsOverviewTypeDetailsVO.class));
    }

    /**
     * 员工类型占比列表导出
     *
     * @param dto 入参
     * @return {@link ActionResult }
     */
    @PostMapping("/proportion-employment-types-details-export")
    public void proportionOfEmploymentTypesDetailsExport(@RequestBody FtbPersonnlesAnalysisDTO dto) throws IOException {
        ftbPersonnelOverviewAnalysisService.proportionOfEmploymentTypesDetailsExport(dto);
    }


    /**
     * 年龄分布占比
     *
     * @param dto 入参
     * @return {@link ActionResult }
     */
    @PostMapping("/age-distribution-proportion")
    public ActionResult<List<PersonnelsOverviewProportionVO>> ageDistributionProportion(@RequestBody FtbPersonnlesAnalysisDTO dto) {
        return ActionResult.success(ftbPersonnelOverviewAnalysisService.ageDistributionProportion(dto));
    }

    /**
     * 年龄分布占比列表
     *
     * @param dto 入参
     * @return {@link ActionResult }
     */
    @PostMapping("/age-distribution-proportion-details")
    public ActionResult<PersonnelDataAnalysisListVO> ageDistributionProportionDetails(@RequestBody FtbPersonnlesAnalysisDTO dto) {
        PageListVO<PersonnelsOverviewAgeDetailsVO> result = ftbPersonnelOverviewAnalysisService.ageDistributionProportionDetails(dto);
        return ActionResult.success(PersonnelDataAnalysisUtil.encapsulatePageData(result, PersonnelsOverviewAgeDetailsVO.class));
    }

    /**
     * 年龄分布占比列表导出
     *
     * @param dto 入参
     * @return {@link ActionResult }
     */
    @PostMapping("/age-distribution-proportion-details-export")
    public void ageDistributionProportionDetailsExport(@RequestBody FtbPersonnlesAnalysisDTO dto) throws IOException {
        ftbPersonnelOverviewAnalysisService.ageDistributionProportionDetailsExport(dto);
    }

    /**
     * 司龄分布
     *
     * @param dto 入参
     * @return {@link ActionResult }
     */
    @PostMapping("/company-distribution")
    public ActionResult<List<PersonnelsOverviewProportionVO>> ageDistribution(@RequestBody FtbPersonnlesAnalysisDTO dto) {
        return ActionResult.success(ftbPersonnelOverviewAnalysisService.ageDistribution(dto));
    }

    /**
     * 司龄分布占比列表
     *
     * @param dto 入参
     * @return {@link ActionResult }
     */
    @PostMapping("/company-distribution-details")
    public ActionResult<PersonnelDataAnalysisListVO> companyDistributionDetails(@RequestBody FtbPersonnlesAnalysisDTO dto) {
        PageListVO<PersonnelsOverviewCompanyDetailsVO> result = ftbPersonnelOverviewAnalysisService.companyDistributionDetails(dto);
        return ActionResult.success(PersonnelDataAnalysisUtil.encapsulatePageData(result, PersonnelsOverviewCompanyDetailsVO.class));
    }

    /**
     * 司龄分布占比列表导出
     *
     * @param dto 入参
     * @return {@link ActionResult }
     */
    @PostMapping("/company-distribution-details-export")
    public void companyDistributionDetailsExport(@RequestBody FtbPersonnlesAnalysisDTO dto) {
        ftbPersonnelOverviewAnalysisService.companyDistributionDetailsExport(dto);
    }

    /**
     * 学历占比
     *
     * @param dto 入参
     * @return {@link ActionResult }
     */
    @PostMapping("/educational-background")
    public ActionResult<List<PersonnelsOverviewProportionVO>> educationalBackground(@RequestBody FtbPersonnlesAnalysisDTO dto) {
        return ActionResult.success(ftbPersonnelOverviewAnalysisService.educationalBackground(dto));
    }

    /**
     * 学历占比列表
     *
     * @param dto 入参
     * @return {@link ActionResult }
     */
    @PostMapping("/educational-background-details")
    public ActionResult<PersonnelDataAnalysisListVO> educationalBackgroundDetails(@RequestBody FtbPersonnlesAnalysisDTO dto) {
        PageListVO<PersonnelsOverviewEducationDetailsVO> result = ftbPersonnelOverviewAnalysisService.educationalBackgroundDetails(dto);
        return ActionResult.success(PersonnelDataAnalysisUtil.encapsulatePageData(result, PersonnelsOverviewEducationDetailsVO.class));
    }

    /**
     * 学历占比列表导出
     *
     * @param dto 入参
     * @return {@link ActionResult }
     */
    @PostMapping("/educational-background-details-export")
    public void educationalBackgroundDetailsExport(@RequestBody FtbPersonnlesAnalysisDTO dto) throws IOException {
        ftbPersonnelOverviewAnalysisService.educationalBackgroundDetailsExport(dto);
    }

    /**
     * 部门人数占比
     *
     * @param dto 入参
     * @return {@link ActionResult }
     */
    @PostMapping("/department-headcount-ratio")
    public ActionResult<List<PersonnelsOverviewProportionVO>> departmentHeadcountRatio(@RequestBody FtbPersonnlesAnalysisDTO dto) {
        return ActionResult.success(ftbPersonnelOverviewAnalysisService.departmentHeadcountRatio(dto));
    }

    /**
     * 门店人数占比
     *
     * @param dto 入参
     * @return {@link ActionResult }
     */
    @PostMapping("/proportion-of-store-numbers")
    public ActionResult<List<PersonnelsOverviewProportionVO>> proportionOfStoreNumbers(@RequestBody FtbPersonnlesAnalysisDTO dto) {
        return ActionResult.success(ftbPersonnelOverviewAnalysisService.proportionOfStoreNumbers(dto));
    }

    /**
     * 岗位人数占比
     *
     * @param dto 入参
     * @return {@link ActionResult }
     */
    @PostMapping("/proportion-job-headcount")
    public ActionResult<List<PersonnelsOverviewProportionVO>> proportionOfJobHeadcount(@RequestBody FtbPersonnlesAnalysisDTO dto) {
        return ActionResult.success(ftbPersonnelOverviewAnalysisService.proportionOfJobHeadcount(dto));
    }

    /**
     * 部门人数占比列表
     *
     * @param dto 入参
     * @return {@link ActionResult }
     */
    @PostMapping("/department-headcount-ratio-details")
    public ActionResult<PersonnelDataAnalysisListVO> departmentHeadcountRatioDetails(@RequestBody FtbPersonnlesAnalysisDTO dto) {
        PageListVO<PersonnelsOverviewDepartmentDetailsVO> result = ftbPersonnelOverviewAnalysisService.departmentHeadcountRatioDetails(dto);
        return ActionResult.success(PersonnelDataAnalysisUtil.encapsulatePageData(result, PersonnelsOverviewDepartmentDetailsVO.class));
    }

    /**
     * 部门人数占比列表导出
     *
     * @param dto 入参
     * @return {@link ActionResult }
     */
    @PostMapping("/department-headcount-ratio-details-export")
    public void departmentHeadcountRatioDetailsExport(@RequestBody FtbPersonnlesAnalysisDTO dto) throws IOException {
        ftbPersonnelOverviewAnalysisService.departmentHeadcountRatioDetailsExport(dto);
    }

    /**
     * 门店人数占比列表
     *
     * @param dto 入参
     * @return {@link ActionResult }
     */
    @PostMapping("/proportion-of-store-numbers-details")
    public ActionResult<PersonnelDataAnalysisListVO> proportionOfStoreNumbersDetails(@RequestBody FtbPersonnlesAnalysisDTO dto) {
        PageListVO<PersonnelsOverviewStoreDetailsVO> result = ftbPersonnelOverviewAnalysisService.proportionOfStoreNumbersDetails(dto);
        return ActionResult.success(PersonnelDataAnalysisUtil.encapsulatePageData(result, PersonnelsOverviewStoreDetailsVO.class));
    }

    /**
     * 门店人数占比列表导出
     *
     * @param dto 入参
     * @return {@link ActionResult }
     */
    @PostMapping("/proportion-of-store-numbers-details-export")
    public void proportionOfStoreNumbersDetailsExport(@RequestBody FtbPersonnlesAnalysisDTO dto) throws IOException {
        ftbPersonnelOverviewAnalysisService.proportionOfStoreNumbersDetailsExport(dto);
    }

    /**
     * 岗位人数占比列表
     *
     * @param dto 入参
     * @return {@link ActionResult }
     */
    @PostMapping("/proportion-job-headcount-details")
    public ActionResult<PersonnelDataAnalysisListVO> proportionOfJobHeadcountDetails(@RequestBody FtbPersonnlesAnalysisDTO dto) {
        PageListVO<PersonnelsOverviewJobDetailsVO> result = ftbPersonnelOverviewAnalysisService.proportionOfJobHeadcountDetails(dto);
        return ActionResult.success(PersonnelDataAnalysisUtil.encapsulatePageData(result, PersonnelsOverviewJobDetailsVO.class));
    }

    /**
     * 岗位人数占比列表导出
     *
     * @param dto 入参
     * @return {@link ActionResult }
     */
    @PostMapping("/proportion-job-headcount-details-export")
    public void proportionOfJobHeadcountDetailsExport(@RequestBody FtbPersonnlesAnalysisDTO dto) throws IOException {
        ftbPersonnelOverviewAnalysisService.proportionOfJobHeadcountDetailsExport(dto);
    }


    /**
     * 计算考勤组平均工时(实际出勤工时)
     */
    @PostMapping(value = "/countAttendanceAvgHours")
    public ActionResult<List<AttendanceCountAvgHoursVo>> countAttendanceAvgHours(@RequestBody FtbPersonnlesAnalysisDTO dto) {
        AttendanceCountAvgHoursDto attendanceCountAvgHoursDto = new AttendanceCountAvgHoursDto();
        attendanceCountAvgHoursDto.setOrgIds(dto.getOrganizationId());
        attendanceCountAvgHoursDto.setStartMonth(DateUtil.format(dto.getStartTime(), "yyyy-MM"));
        attendanceCountAvgHoursDto.setEndMonth(DateUtil.format(dto.getEndTime(), "yyyy-MM"));
        List<AttendanceCountAvgHoursVo> attendanceCountAvgHoursVos = ftbStatisticsApi.countAttendanceAvgHours(attendanceCountAvgHoursDto);
        return ActionResult.success(attendanceCountAvgHoursVos);
    }


    /**
     * 离职率情况
     */
    @PostMapping("/leave-rate")
    public ActionResult<FtbPersonnelsTurnoverRateVO> leaveRate(@RequestBody FtbPersonnlesAnalysisDTO dto){
        return ActionResult.success(ftbPersonnelsTurnoverAnalysisService.ftbPersonnelsTurnoverAnalysisService(dto));
    }

    /**
     * 离职率明细
     */
    @PostMapping("/leave-rate-detail")
    public ActionResult<PersonnelDataAnalysisListVO> leaveRateDetail(@RequestBody FtbPersonnlesAnalysisDTO dto){
        PageListVO<FtbPersonnelsTurnoverDetailVO> result = ftbPersonnelsTurnoverAnalysisService.getLeaveRateDetail(dto);
        return ActionResult.success(PersonnelDataAnalysisUtil.encapsulatePageData(result, FtbPersonnelsTurnoverDetailVO.class));
    }

    /**
     * 离职率明细-导出
     */
    @PostMapping("/leave-rate-detail-export")
    public void leaveRateDetailExport(@RequestBody FtbPersonnlesAnalysisDTO dto, HttpServletResponse response) throws Exception {
        dto.setPageSize(-1);
        List<FtbPersonnelsTurnoverDetailVO> detailVOS = ftbPersonnelsTurnoverAnalysisService.getLeaveRateDetail(dto).getList();
        EasyExcelUtils.exportExcel(response, "离职率明细", detailVOS,FtbPersonnelsTurnoverDetailVO. class);
    }

    /**
     * 工龄分布统计
     */
    @PostMapping("/seniority-distribution")
    public ActionResult<List<PersonnelsOverviewProportionVO>> seniorityDistribution(@RequestBody FtbPersonnlesAnalysisDTO dto) {
        List<PersonnelsOverviewProportionVO> re =   ftbPersonnelOverviewAnalysisService.seniorityDistribution(dto);
        return ActionResult.success(re);
    }

    /**
     * 保险购买分析
     */
    @PostMapping("/insurance-purchase-analysis")
    public ActionResult<List<PersonnelsOverviewProportionVO>> insurancePurchaseAnalysis(@RequestBody FtbPersonnlesAnalysisDTO dto) {
        List<PersonnelsOverviewProportionVO> result = ftbPersonnelOverviewAnalysisService.insurancePurchaseAnalysis(dto);
        return ActionResult.success(result);
    }

    /**
     * 工龄分布统计列表
     *
     * @param dto 入参
     * @return {@link ActionResult }
     */
    @PostMapping("/seniority-distribution-details")
    public ActionResult<PersonnelDataAnalysisListVO> seniorityDistributionDetails(@RequestBody FtbPersonnlesAnalysisDTO dto) {
        PageListVO<PersonnelsOverviewSeniorityDetailsVO> result = ftbPersonnelOverviewAnalysisService.seniorityDistributionDetails(dto);
        return ActionResult.success(PersonnelDataAnalysisUtil.encapsulatePageData(result, PersonnelsOverviewSeniorityDetailsVO.class));
    }

    /**
     * 工龄分布统计列表导出
     *
     * @param dto 入参
     * @return {@link ActionResult }
     */
    @PostMapping("/seniority-distribution-details-export")
    public void seniorityDistributionDetailsExport(@RequestBody FtbPersonnlesAnalysisDTO dto) throws IOException {
        ftbPersonnelOverviewAnalysisService.seniorityDistributionDetailsExport(dto);
    }

    /**
     * 保险购买分析列表
     *
     * @param dto 入参
     * @return {@link ActionResult }
     */
    @PostMapping("/insurance-purchase-analysis-details")
    public ActionResult<PersonnelDataAnalysisListVO> insurancePurchaseAnalysisDetails(@RequestBody FtbPersonnlesAnalysisDTO dto) {
        PageListVO<PersonnelsOverviewInsuranceDetailsVO> result = ftbPersonnelOverviewAnalysisService.insurancePurchaseAnalysisDetails(dto);
        return ActionResult.success(PersonnelDataAnalysisUtil.encapsulatePageData(result, PersonnelsOverviewInsuranceDetailsVO.class));
    }

    /**
     * 保险购买分析列表导出
     *
     * @param dto 入参
     * @return {@link ActionResult }
     */
    @PostMapping("/insurance-purchase-analysis-details-export")
    public void insurancePurchaseAnalysisDetailsExport(@RequestBody FtbPersonnlesAnalysisDTO dto) throws IOException {
        ftbPersonnelOverviewAnalysisService.insurancePurchaseAnalysisDetailsExport(dto);
    }

}
