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.utils.poi.ExcelExp;
import com.srs.common.utils.poi.ExcelUtilManySheet;
import com.srs.teacher.domain.*;
import com.srs.teacher.domain.dto.*;
import com.srs.teacher.service.ISysPerformanceActiveService;
import com.srs.teacher.service.ISysPerformancePassService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.annotations.*;
import org.springframework.security.access.prepost.PreAuthorize;

import com.srs.teacher.service.ISysPerformanceService;
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 srs
 * @date 2024-06-16
 */
@RestController
@RequestMapping("/teacher/performance")
@Api(value = "综合绩效管理", tags = "综合绩效管理")
public class SysPerformanceController extends BaseController {
    @Autowired
    private ISysPerformanceService sysPerformanceService;

    @Autowired
    private ISysPerformanceActiveService sysPerformanceActiveService;

    @Autowired
    private ISysPerformancePassService sysPerformancePassService;

    @Value("${srs.url}")
    private String baseUrl;

    /**
     * 查询综合绩效列表
     */
    @PreAuthorize("@ss.hasPermi('teacher:performance:list')")
    @GetMapping("/list")
    @ApiOperation("查询综合绩效列表")
    public TableDataInfo list(SysPerformance sysPerformance) {
        startPage();
        List<SysPerformance> list = sysPerformanceService.selectSysPerformanceList(sysPerformance);
        return getDataTable(list);
    }

    /**
     * 导出综合绩效列表
     */
    @PreAuthorize("@ss.hasPermi('teacher:performance:export')")
    @Log(title = "综合绩效", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiOperation("导出综合绩效列表")
    public void export(HttpServletResponse response, SysPerformance sysPerformance) throws Exception {

//        List<SysPerformance> list = sysPerformanceService.selectSysPerformanceList(sysPerformance);
//
//        ExcelUtil<SysPerformance> util = new ExcelUtil<SysPerformance>(SysPerformance.class);
//        util.exportExcel(response, list, "综合绩效数据");


//        综合
        List<SysPerformance> list = sysPerformanceService.selectSysPerformanceList(sysPerformance);
        List<SysPerformanceDao> sysPerformanceDaos = new ArrayList<>();

        List<SysPerformanceActiveDao> sysPerformanceActiveDaos = new ArrayList<>();
        List<SysPerformanceRwDao> sysPerformanceRwDaos = new ArrayList<>();
        List<SysPerformanceServingDao> sysPerformanceServingDaos = new ArrayList<>();
        List<SysPerformanceTaskDao> sysPerformanceTaskDaos = new ArrayList<>();


//        第二课堂
        for (SysPerformance items : list){
            SysPerformanceDao dao = new SysPerformanceDao();
            dao.setCreditPass(items.getCreditPass());
            dao.setFdName(items.getFdName());
            dao.setYears(items.getYears());
            dao.setShstatus(items.getShstatus());
            dao.setCollege(items.getCollege());
//            for (SysPerformanceActive active : items.getActiveList() ){
//                dao.setActiveType(active.getActiveType());
//                dao.setGzsTime(active.getGzsTime());
//                dao.setGzsPlace(active.getGzsPlace());
//                dao.setGzsParticipate(active.getGzsParticipate());
//                dao.setGzsFdy(active.getGzsFdy());
//                dao.setGzsStu(active.getGzsStu());
//                dao.setGzsTheme(active.getGzsTheme());
//                dao.setGzsIntroduce(active.getGzsIntroduce());
//                dao.setGzsPicture(active.getGzsPicture());
//                dao.setGzsNews(active.getGzsNews());
//                dao.setGzsProportion(active.getGzsProportion());
//            }
            for (SysPerformancePass pass : items.getPassList()){
                dao.setPasss(pass.getPasss());
            }
////            承接区级
//            for (SysPerformanceRw rw : items.getRwList()){
//                dao.setRwType(rw.getRwType());
//                dao.setRwTime(rw.getRwTime());
//                dao.setRwPlace(rw.getRwPlace());
//                dao.setRwParticipate(rw.getRwParticipate());
//                dao.setRwPeople(rw.getRwPeople());
//                dao.setRwTheme(rw.getRwTheme());
//                dao.setRwSituation(rw.getRwSituation());
//                dao.setRwStu(rw.getRwStu());
//                dao.setRwProportion(rw.getRwProportion());
//                dao.setRwFile(rw.getRwFile());
//                dao.setRwNews(rw.getRwNews());
//                dao.setRwPicture(rw.getRwPicture());
//
//            }
////            担任职务
//            for(SysPerformanceServing serving : items.getServingList()){
//                dao.setServingPost(serving.getServingPost());
//                dao.setServingTime(serving.getServingTime());
//                dao.setServingFile(serving.getServingFile());
//                dao.setAppointment(serving.getAppointment());
//            }
////            重大任务奖励
//            for (SysPerformanceTask task : items.getTaskList()){
//                dao.setTaskDescribe(task.getTaskDescribe());
//                dao.setTaskStart(task.getTaskStart());
//                dao.setTaskEnd(task.getTaskEnd());
//            }


            sysPerformanceDaos.add(dao);
        }

//        工作室绩效
        for (SysPerformance items : list) {
            SysPerformanceActiveDao dao1 = new SysPerformanceActiveDao();
            dao1.setCreditPass(items.getCreditPass());
            dao1.setFdName(items.getFdName());
            dao1.setYears(items.getYears());
            dao1.setShstatus(items.getShstatus());
            dao1.setCollege(items.getCollege());
            for (SysPerformanceActive active : items.getActiveList() ){
                dao1.setActiveType(active.getActiveType());
                dao1.setGzsTime(active.getGzsTime());
                dao1.setGzsPlace(active.getGzsPlace());
                dao1.setGzsParticipate(active.getGzsParticipate());
                dao1.setGzsFdy(active.getGzsFdy());
                dao1.setGzsStu(active.getGzsStu());
                dao1.setGzsTheme(active.getGzsTheme());
                dao1.setGzsIntroduce(active.getGzsIntroduce());
                dao1.setGzsPicture(baseUrl.substring(0,baseUrl.length()-1) + active.getGzsPicture());
                dao1.setGzsNews(baseUrl.substring(0,baseUrl.length()-1) + active.getGzsNews());
                dao1.setGzsProportion(active.getGzsProportion());
            }
            sysPerformanceActiveDaos.add(dao1);
        }

//        承担任务
        for (SysPerformance items : list){
            SysPerformanceRwDao dao3 = new SysPerformanceRwDao();
            dao3.setCreditPass(items.getCreditPass());
            dao3.setFdName(items.getFdName());
            dao3.setYears(items.getYears());
            dao3.setShstatus(items.getShstatus());
            dao3.setCollege(items.getCollege());

////            承接区级
            for (SysPerformanceRw rw : items.getRwList()){
                dao3.setRwType(rw.getRwType());
                dao3.setRwTime(rw.getRwTime());
                dao3.setRwPlace(rw.getRwPlace());
                dao3.setRwParticipate(rw.getRwParticipate());
                dao3.setRwPeople(rw.getRwPeople());
                dao3.setRwTheme(rw.getRwTheme());
                dao3.setRwSituation(rw.getRwSituation());
                dao3.setRwStu(rw.getRwStu());
                dao3.setRwProportion(rw.getRwProportion());
                dao3.setRwFile(baseUrl.substring(0,baseUrl.length()-1) + rw.getRwFile());
                dao3.setRwNews(baseUrl.substring(0,baseUrl.length()-1) + rw.getRwNews());
                dao3.setRwPicture(baseUrl.substring(0,baseUrl.length()-1) + rw.getRwPicture());
            }


            sysPerformanceRwDaos.add(dao3);
        }


//        担任职务
        for (SysPerformance items : list){
            SysPerformanceServingDao dao4 = new SysPerformanceServingDao();
            dao4.setCreditPass(items.getCreditPass());
            dao4.setFdName(items.getFdName());
            dao4.setYears(items.getYears());
            dao4.setShstatus(items.getShstatus());
            dao4.setCollege(items.getCollege());
////            担任职务
            for(SysPerformanceServing serving : items.getServingList()){
                dao4.setServingPost(serving.getServingPost());
                dao4.setServingTime(serving.getServingTime());
                dao4.setServingFile(baseUrl.substring(0,baseUrl.length()-1) + serving.getServingFile());
                dao4.setAppointment(serving.getAppointment());
            }


            sysPerformanceServingDaos.add(dao4);
        }


//        重大奖励
        for (SysPerformance items : list){
            SysPerformanceTaskDao dao5 = new SysPerformanceTaskDao();
            dao5.setCreditPass(items.getCreditPass());
            dao5.setFdName(items.getFdName());
            dao5.setYears(items.getYears());
            dao5.setShstatus(items.getShstatus());
            dao5.setCollege(items.getCollege());
////            重大任务奖励
            for (SysPerformanceTask task : items.getTaskList()){
                dao5.setTaskDescribe(task.getTaskDescribe());
                dao5.setTaskStart(task.getTaskStart());
                dao5.setTaskEnd(task.getTaskEnd());
            }


            sysPerformanceTaskDaos.add(dao5);
        }


//        ExcelUtil<SysPerformanceDao> util = new ExcelUtil<SysPerformanceDao>(SysPerformanceDao.class);
//        util.exportExcel(response, sysPerformanceDaos, "综合绩效数据");
        ExcelExp e1 = new ExcelExp("第二课堂及格人数", sysPerformanceDaos, SysPerformanceDao.class);
        ExcelExp e2 = new ExcelExp("辅导员工作室绩效", sysPerformanceActiveDaos, SysPerformanceActiveDao.class);
        ExcelExp e3 = new ExcelExp("承接区级、校级任务", sysPerformanceRwDaos, SysPerformanceRwDao.class);
        ExcelExp e4 = new ExcelExp("担任职务", sysPerformanceServingDaos, SysPerformanceServingDao.class);
        ExcelExp e5 = new ExcelExp("重大任务奖励", sysPerformanceTaskDaos, SysPerformanceTaskDao.class);
        List<ExcelExp> mysheet = new ArrayList<ExcelExp>();
        mysheet.add(e1);
        mysheet.add(e2);
        mysheet.add(e3);
        mysheet.add(e4);
        mysheet.add(e5);
        ExcelUtilManySheet<List<SysTeacherThesis>> util2 = new ExcelUtilManySheet<List<SysTeacherThesis>>(mysheet);
        util2.exportExcelManySheet(response, mysheet);


    }

    /**
     * 获取综合绩效详细信息
     */
    @PreAuthorize("@ss.hasPermi('teacher:performance:query')")
    @GetMapping(value = "/{id}")
    @ApiOperation("获取综合绩效详细信息")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(sysPerformanceService.selectSysPerformanceById(id));
    }

    /**
     * 新增综合绩效
     */
    @PreAuthorize("@ss.hasPermi('teacher:performance:add')")
    @Log(title = "综合绩效", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ApiOperation("新增综合绩效")
    public AjaxResult add(@RequestBody SysPerformance sysPerformance) throws Exception {
        int i = sysPerformanceService.insertSysPerformance(sysPerformance);
        Map<String, Long> map = new HashMap<>();
        if (i > 0) {
            map.put("id", sysPerformance.getId());
        }
        return success(map);
    }

    /**
     * 修改综合绩效
     */
    @PreAuthorize("@ss.hasPermi('teacher:performance:edit')")
    @Log(title = "综合绩效", businessType = BusinessType.UPDATE)
    @PostMapping("/update")
    @ApiOperation("修改综合绩效")
    public AjaxResult edit(@RequestBody SysPerformance sysPerformance) {
        return toAjax(sysPerformanceService.updateSysPerformance(sysPerformance));
    }

    /**
     * 删除综合绩效
     */
    @PreAuthorize("@ss.hasPermi('teacher:performance:remove')")
    @Log(title = "综合绩效", businessType = BusinessType.DELETE)
    @PostMapping("/{ids}")
    @ApiOperation("删除综合绩效")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(sysPerformanceService.deleteSysPerformanceByIds(ids));
    }
}
