package com.ym.mec.web.controller;

import com.ym.mec.auth.api.client.SysUserFeignService;
import com.ym.mec.auth.api.entity.SysUser;
import com.ym.mec.biz.dal.dto.StudentStatisticsDto;
import com.ym.mec.biz.dal.dto.StudentStatisticsSumDto;
import com.ym.mec.biz.dal.dto.StudentStatisticsSumForDateDto;
import com.ym.mec.biz.dal.entity.ManagerDownload;
import com.ym.mec.biz.dal.enums.ExportTypeEnum;
import com.ym.mec.biz.dal.page.StudentStatisticsQueryInfo;
import com.ym.mec.biz.service.ExportService;
import com.ym.mec.biz.service.OrganizationService;
import com.ym.mec.biz.service.StudentStatisticsService;
import com.ym.mec.common.controller.BaseController;
import com.ym.mec.common.entity.HttpResponseResult;
import com.ym.mec.common.exception.BizException;
import com.ym.mec.common.page.PageInfo;
import com.ym.mec.common.service.IdGeneratorService;
import com.ym.mec.util.date.DateUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

@RequestMapping("studentStatistics")
@Api(tags = "学员小课统计")
@RestController
public class StudentStatisticsController extends BaseController {

    @Autowired
    private StudentStatisticsService studentStatisticsService;
    @Autowired
    private OrganizationService organizationService;
    @Autowired
    private SysUserFeignService sysUserFeignService;
    @Autowired
    private ExportService exportService;
    @Autowired
    private IdGeneratorService idGeneratorService;

    @ApiOperation(value = "分页查询收费类型列表")
    @PostMapping("/queryPage")
    @PreAuthorize("@pcs.hasPermissions('studentStatistics/queryPage')")
    public HttpResponseResult<PageInfo<StudentStatisticsDto>> queryPage(StudentStatisticsQueryInfo queryInfo) {
        queryInfo.setOrganId(organizationService.getEmployeeOrgan(queryInfo.getOrganId()));
        return succeed(studentStatisticsService.queryStatisticsPage(queryInfo));
    }

    @ApiOperation(value = "小课学员管理汇总")
    @GetMapping("/studentSmallClassStatisticsSum")
    @PreAuthorize("@pcs.hasPermissions('studentStatistics/studentSmallClassStatisticsSum')")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "groupType", value = "VIP、THEORY、PRACTICE", required = true, dataType = "String"),
            @ApiImplicitParam(name = "organId", value = "分部", dataType = "String"),
    })
    public HttpResponseResult<StudentStatisticsSumDto> studentSmallClassStatisticsSum(String groupType,String organId) {
        return succeed(studentStatisticsService.studentSmallClassStatisticsSum(groupType,organizationService.getEmployeeOrgan(organId)));
    }

    @ApiOperation(value = "小课学员管理按时间汇总")
    @PostMapping("/studentSmallClassStatisticsSumForDate")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "groupType", value = "VIP、THEORY、PRACTICE", required = true, dataType = "String"),
            @ApiImplicitParam(name = "startDate", value = "开始时间-年月日", dataType = "String"),
            @ApiImplicitParam(name = "endDate", value = "结束时间-年月日", dataType = "String"),
            @ApiImplicitParam(name = "organId", value = "分部", dataType = "String"),
    })
    @PreAuthorize("@pcs.hasPermissions('studentStatistics/studentSmallClassStatisticsSumForDate')")
    public HttpResponseResult<StudentStatisticsSumForDateDto> studentSmallClassStatisticsSumForDate(String groupType, String startDate, String endDate,String organId) {
        return succeed(studentStatisticsService.studentSmallClassStatisticsSumForDate(groupType,startDate,endDate,organizationService.getEmployeeOrgan(organId)));
    }

    @ApiOperation(value = "获取指导老师列表")
    @GetMapping("/queryTeacherList")
    @PreAuthorize("@pcs.hasPermissions('studentStatistics/queryTeacherList')")
    @ApiImplicitParam(name = "groupType", value = "VIP、THEORY、PRACTICE", required = true, dataType = "String")
    public HttpResponseResult<List<Map<Integer, String>>> queryTeacherList(String groupType) {
        return succeed(studentStatisticsService.queryTeacherList(groupType));
    }

    @ApiOperation(value = "获取乐团主管列表")
    @GetMapping("/queryDirectorList")
    @PreAuthorize("@pcs.hasPermissions('studentStatistics/queryDirectorList')")
    @ApiImplicitParam(name = "groupType", value = "VIP、THEORY、PRACTICE", required = true, dataType = "String")
    public HttpResponseResult<List<Map<Integer, String>>> queryDirectorList(String groupType) {
        return succeed(studentStatisticsService.queryDirectorList(groupType));
    }

    @ApiOperation(value = "导出小课学员管理")
    @PostMapping("/exportStudentSmallClassStatisticsSum")
    @PreAuthorize("@pcs.hasPermissions('studentStatistics/exportStudentSmallClassStatisticsSum')")
    public HttpResponseResult exportStudentSmallClassStatisticsSum(StudentStatisticsQueryInfo queryInfo) throws IOException {
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        queryInfo.setOrganId(organizationService.getEmployeeOrgan(sysUser.getId(),queryInfo.getOrganId(),sysUser.getIsSuperAdmin()));
        PageInfo<StudentStatisticsDto> pageList = studentStatisticsService.queryStatisticsPage(queryInfo);
        if (pageList.getTotal() <= 0) {
            throw new BizException("没有可导出的记录");
        }
        ManagerDownload managerDownload = exportService.saveManagerDownload(ExportTypeEnum.STUDENT_SMALL_CLASS_STATISTICS,sysUser.getId());
        String name = "网管课学员统计";
        if(Objects.equals(queryInfo.getGroupType(),"VIP")){
            name = "VIP课学员统计";
        }else if(Objects.equals(queryInfo.getGroupType(),"THEORY")){
            name = "乐理课学员统计";
        }
        String fileName = new StringBuffer(name)
                .append("-")
                .append(idGeneratorService.generatorId("download"))
                .append("-")
                .append(DateUtil.getDate(new Date()))
                .append(".xls").toString();
        managerDownload.setName(fileName);
        queryInfo.setPage(1);
        queryInfo.setRows(65535);
        return exportService.asyncExport(() -> exportService.exportStudentSmallClassStatisticsSum(queryInfo, managerDownload),managerDownload.getName());
    }
}
