package jnpf.cultivate.controller.web.course;

import jnpf.base.ActionResult;
import jnpf.base.vo.PageListVO;
import jnpf.cultivate.service.FtbCultivateCourseService;
import jnpf.cultivate.service.FtbCultivateCourseStatisticesService;
import jnpf.cultivate.service.FtbCultivateCourseTypeService;
import jnpf.model.cultivate.dto.course.FtbCultivateCourseOrgStatisticsDTO;
import jnpf.model.cultivate.dto.course.FtbCultivateCoursePersonStatisticesDTO;
import jnpf.model.cultivate.po.course.FtbCultivateCourse;
import jnpf.model.cultivate.po.course.FtbCultivateCourseType;
import jnpf.model.cultivate.vo.course.web.FtbCultivateCourseOrgStatisticesVO;
import jnpf.model.cultivate.vo.course.web.FtbCultivateCoursePersonStatisticesVO;
import jnpf.util.EasyExcelUtil;
import lombok.Data;
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.List;
import java.util.stream.Collectors;

/**
 * web培训数据看板 -课程统计
 *
 * @author wcx
 */
@RestController
@RequestMapping("/web/course/statistices")
public class FtbCultivateCourseStatisticesController {

    @Resource
    private FtbCultivateCourseStatisticesService ftbCultivateCourseStatisticesService;

    @Autowired
    private FtbCultivateCourseService service;

    @Resource
    private FtbCultivateCourseTypeService ftbCultivateCourseTypeService;

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

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

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

    /**
     * 个人维度统计导出信息
     *
     * @param personWisdomStatisticDTO
     */
    @PostMapping("/person-list-statistics/export")
    public void exportInformationPerson(@RequestBody FtbCultivateCoursePersonStatisticesDTO personWisdomStatisticDTO, HttpServletResponse response) throws IOException {
        PageListVO<FtbCultivateCoursePersonStatisticesVO> listVO = ftbCultivateCourseStatisticesService.personListStatistics(personWisdomStatisticDTO);
        EasyExcelUtil.simpleWrite(listVO.getList(), "个人维度统计", FtbCultivateCoursePersonStatisticesVO.class, response);
    }

    /**
     * 课程查询列表
     * @return
     */
    @GetMapping("/query-correspond-list")
    public ActionResult<List<FtbCultivateStatisticCourseVO>> queryCorrespondingList() {
        List<FtbCultivateCourse> list = service.lambdaQuery()
                .eq(FtbCultivateCourse::getEnableMark, "0").list();
        List<FtbCultivateStatisticCourseVO> result = list.stream()
                .map(FtbCultivateStatisticCourseVO::convertA)
                .collect(Collectors.toList());
        return ActionResult.success(result);
    }

    /**
     * 课程类型查询列表
     * @return
     */
    @GetMapping("/query-name-type-list")
    public ActionResult<List<FtbCultivateStatisticCourseVO>> queryNameTypeList() {
        List<FtbCultivateCourseType> courseTypes = ftbCultivateCourseTypeService.lambdaQuery()
                .eq(FtbCultivateCourseType::getEnableMark, "0").list();
        List<FtbCultivateStatisticCourseVO> result = courseTypes.stream()
                .map(FtbCultivateStatisticCourseVO::convertB)
                .collect(Collectors.toList());
        return ActionResult.success(result);
    }


    @Data
    public static class FtbCultivateStatisticCourseVO {
        /**
         * 名称
         */
        private String name;
        /**
         * 主键id
         */
        private String id;

        public static FtbCultivateStatisticCourseVO convertA(FtbCultivateCourse course) {
            FtbCultivateStatisticCourseVO ftbCultivateStatisticCourseVO = new FtbCultivateStatisticCourseVO();
            ftbCultivateStatisticCourseVO.setId(course.getId());
            ftbCultivateStatisticCourseVO.setName(course.getName());
            return ftbCultivateStatisticCourseVO;
        }

        public static FtbCultivateStatisticCourseVO convertB(FtbCultivateCourseType course) {
            FtbCultivateStatisticCourseVO ftbCultivateStatisticCourseVO = new FtbCultivateStatisticCourseVO();
            ftbCultivateStatisticCourseVO.setId(course.getId());
            ftbCultivateStatisticCourseVO.setName(course.getName());
            return ftbCultivateStatisticCourseVO;
        }

    }

}
