package com.srs.web.controller.teacher;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.srs.common.annotation.RepeatSubmit;
import com.srs.teacher.domain.SysTeacherKpiFillingBusinessWork;
import com.srs.teacher.domain.SysTeacherKpiFillingStuEmergency;
import com.srs.teacher.domain.dto.SysTeacherKpiFillingDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import org.springframework.security.access.prepost.PreAuthorize;

import com.srs.teacher.domain.SysTeacherKpiFilling;
import com.srs.teacher.service.ISysTeacherKpiFillingService;
import com.srs.common.core.controller.BaseController;
import com.srs.common.core.domain.AjaxResult;
import com.srs.common.utils.poi.ExcelUtil;
import com.srs.common.enums.BusinessType;
import com.srs.common.annotation.Log;
import com.srs.common.core.page.TableDataInfo;

/**
 * 业绩考核-个人填报Controller
 *
 * @author chc
 * @date 2024-06-06
 */
@RestController
@RequestMapping("/teacher/kpiFilling")
@Api(value = "业绩考核-个人填报管理", tags = "业绩考核-个人填报管理")
public class SysTeacherKpiFillingController extends BaseController {
    @Autowired
    private ISysTeacherKpiFillingService sysTeacherKpiFillingService;

    /**
     * 查询业绩考核-个人填报列表
     */
    @PreAuthorize("@ss.hasPermi('teacher:kpiFilling:list')")
    @GetMapping("/list")
    @ApiOperation("查询业绩考核-个人填报列表")
    public TableDataInfo list(SysTeacherKpiFilling sysTeacherKpiFilling) {
        startPage();
        List<SysTeacherKpiFilling> list = sysTeacherKpiFillingService.selectSysTeacherKpiFillingList(sysTeacherKpiFilling);
        return getDataTable(list);
    }

    /**
     * 查询业绩考核-个人填报列表
     */
    @PreAuthorize("@ss.hasPermi('teacher:kpiFilling:list')")
    @GetMapping("/selectList")
    @ApiOperation("查询业绩考核-个人填报列表")
    public AjaxResult selectList() {
        List<SysTeacherKpiFilling> list = sysTeacherKpiFillingService.selectListKpiFilling();
        return success(list);
    }

    /**
     * 根据辅导员名称、年份 月份 查询详细信息
     */
    @PreAuthorize("@ss.hasPermi('teacher:kpiFilling:list')")
    @GetMapping(value = "/getByFdyNameAndYearAndMonth")
    @ApiOperation("根据名称年月查询个人填报")
    public AjaxResult getByFdyNameAndYearAndMonth(@RequestParam String fdyName, @RequestParam String fillingYear, @RequestParam String fillingMonth) {
        SysTeacherKpiFilling sysTeacherKpiFillingList = sysTeacherKpiFillingService.selectSysTeacherKpiFillingByFdyName(fdyName, fillingYear, fillingMonth);
        return success(sysTeacherKpiFillingList);
    }

    /**
     * 导出业绩考核-个人填报列表
     */
    @PreAuthorize("@ss.hasPermi('teacher:kpiFilling:export')")
    @Log(title = "业绩考核-个人填报", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiOperation("导出业绩考核-个人填报列表")
    public void export(HttpServletResponse response, SysTeacherKpiFilling sysTeacherKpiFilling) {
        List<SysTeacherKpiFilling> list = sysTeacherKpiFillingService.selectSysTeacherKpiFillingList(sysTeacherKpiFilling);
        List<SysTeacherKpiFillingDto> sysTeacherKpiFillingDtos = new ArrayList<>();
        // 初始化序列号
        int serialNumber = 1; // 或者从0开始，取决于需求

        for (SysTeacherKpiFilling item : list) {
            SysTeacherKpiFillingDto dto = new SysTeacherKpiFillingDto();
            // 设置序列号
            dto.setSerialNumber(String.valueOf(serialNumber++)); // 将整数转换为字符串并递增
            dto.setFdyName(item.getFdyName());
            dto.setJobNumber(item.getJobNumber());
            dto.setDepartmentName(item.getDepartmentName());
            // 确保getKpiFillingRewardsKpiList()不为空，或者处理空列表的情况
            dto.setDayDuty(item.getKpiFillingRewardsKpiList() != null ? item.getKpiFillingRewardsKpiList().size() : 0);
            dto.setFinalRating(item.getFinalRating());

            // 初始化事件计数器
            int generalCount = 0;
            int majorCount = 0;

            // 遍历紧急事件列表
            List<SysTeacherKpiFillingStuEmergency> emergencies = item.getKpiFillingStuEmergencyList();
            if (emergencies != null) { // 检查列表是否为空
                for (SysTeacherKpiFillingStuEmergency emergency : emergencies) {
                    if (emergency.getEventLevel() == 0) { // 一般事件
                        generalCount++;
                    } else if (emergency.getEventLevel() == 1) { // 重大事件
                        majorCount++;
                    }
                }
            }

            // 设置事件级别字符串
            dto.setEventLevel("一般事件：" + generalCount + "天" + ", 重大事件：" + majorCount + "天");

            sysTeacherKpiFillingDtos.add(dto);
        }

        ExcelUtil<SysTeacherKpiFillingDto> util = new ExcelUtil<>(SysTeacherKpiFillingDto.class);
        util.exportExcel(response, sysTeacherKpiFillingDtos, "业绩考核-个人填报数据", "业绩考核-个人填报数据");
    }

    /**
     * 获取业绩考核-个人填报详细信息
     */
    @PreAuthorize("@ss.hasPermi('teacher:kpiFilling:query')")
    @GetMapping(value = "/{id}")
    @ApiOperation("获取业绩考核-个人填报详细信息")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(sysTeacherKpiFillingService.selectSysTeacherKpiFillingById(id));
    }

    /**
     * 新增业绩考核-个人填报
     */
    @PreAuthorize("@ss.hasPermi('teacher:kpiFilling:add')")
    @Log(title = "业绩考核-个人填报", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ApiOperation("新增业绩考核-个人填报")
    @RepeatSubmit(interval = 1000, message = "请求过于频繁")
    public AjaxResult add(@RequestBody SysTeacherKpiFilling sysTeacherKpiFilling) {
        int i = sysTeacherKpiFillingService.insertSysTeacherKpiFilling(sysTeacherKpiFilling);
        Map<String, Long> map = new HashMap<>();
        if (i > 0) {
            map.put("id", sysTeacherKpiFilling.getId());
        }
        return success(map);
    }

    /**
     * 修改业绩考核-个人填报
     */
    @PreAuthorize("@ss.hasPermi('teacher:kpiFilling:edit')")
    @Log(title = "业绩考核-个人填报", businessType = BusinessType.UPDATE)
    @PostMapping("/update")
    @ApiOperation("修改业绩考核-个人填报")
    @RepeatSubmit(interval = 1000, message = "请求过于频繁")
    public AjaxResult edit(@RequestBody SysTeacherKpiFilling sysTeacherKpiFilling) {
        return toAjax(sysTeacherKpiFillingService.updateSysTeacherKpiFilling(sysTeacherKpiFilling));
    }

    /**
     * 批量修改业绩考核-个人填报
     */
    @PreAuthorize("@ss.hasPermi('teacher:kpiFilling:edit')")
    @Log(title = "业绩考核-个人填报", businessType = BusinessType.UPDATE)
    @PostMapping("/batchUpdate")
    @ApiOperation("批量修改业绩考核-个人填报")
    public AjaxResult batchUpdateSysTeacherKpiFilling(@RequestBody List<SysTeacherKpiFilling> sysTeacherKpiFillingList) {
        return toAjax(sysTeacherKpiFillingService.batchUpdateSysTeacherKpiFilling(sysTeacherKpiFillingList));
    }

    /**
     * 删除业绩考核-个人填报
     */
    @PreAuthorize("@ss.hasPermi('teacher:kpiFilling:remove')")
    @Log(title = "业绩考核-个人填报", businessType = BusinessType.DELETE)
    @PostMapping("/{ids}")
    @ApiOperation("删除业绩考核-个人填报")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(sysTeacherKpiFillingService.deleteSysTeacherKpiFillingByIds(ids));
    }
}
