package com.srs.web.controller.comprehensive;

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

import com.srs.comprehensive.domain.*;
import com.srs.comprehensive.domain.Vo.CphClasstwoVo;
import com.srs.comprehensive.domain.Vo.stuYearStudentUser;
import com.srs.comprehensive.service.ICphAuditDetailsService;
import com.srs.comprehensive.service.ICphClasstwoService;
import com.srs.comprehensive.service.ICphRulesService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.srs.common.annotation.Log;
import com.srs.common.core.controller.BaseController;
import com.srs.common.core.domain.AjaxResult;
import com.srs.common.enums.BusinessType;
import com.srs.common.utils.poi.ExcelUtil;
import com.srs.common.core.page.TableDataInfo;

/**
 * 第二课堂Controller
 * 
 * @author zhy
 * @date 2023-07-03
 */
@RestController
@RequestMapping("/system/classtwo")
public class CphClasstwoController extends BaseController
{
    @Autowired
    private ICphClasstwoService cphClasstwoService;
    @Autowired
    private ICphRulesService _rulesService;
    /**
     * 查询第二课堂列表
     */
    @GetMapping("/list")
    public TableDataInfo list(CphClasstwo cphClasstwo)
    {
        startPage();
        List<CphClasstwo> list = cphClasstwoService.selectCphClasstwoList(cphClasstwo);
        return getDataTable(list);
    }

    /**
     * 查询第二课堂列表 查询所有相关信息
     */
    @GetMapping("/lists")
    public TableDataInfo lists(stuYearStudentUser cphIamStudentUser)
    {

        CphClasstwo cphClasstwo = new CphClasstwo();
        cphClasstwo.setStuYearId(cphIamStudentUser.getStuYearId());
        SrsStudent srsStudent = new SrsStudent();
        if (cphIamStudentUser.getClassId() != null){
            srsStudent.setClassId(cphIamStudentUser.getClassId());
        }
        if (cphIamStudentUser.getStuNo()!= null){
            srsStudent.setStuNo(cphIamStudentUser.getStuNo());
        }
        if (cphIamStudentUser.getName()!= null){
            cphIamStudentUser.setName("%"+cphIamStudentUser.getName()+"%");
            srsStudent.setName(cphIamStudentUser.getName());
        }
        cphClasstwo.setStudent(srsStudent);
        // 添加学院和专业的查询条件
        cphClasstwo.setDeptId(cphIamStudentUser.getDeptId());
        cphClasstwo.setMajorId(cphIamStudentUser.getMajorId());
        startPage();
        List<CphClasstwo> list = cphClasstwoService.selectCphClasstwoListJoin(cphClasstwo);
        return getDataTable(list);
    }


    /**
     * 导出第二课堂列表
     */
    @Log(title = "第二课堂", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CphClasstwo cphClasstwo, stuYearStudentUser stuYearStudentUser)
    {
        if (stuYearStudentUser != null) {
            cphClasstwo.setStuYearId(stuYearStudentUser.getStuYearId());
            SrsStudent srsStudent = new SrsStudent();
            if (stuYearStudentUser.getClassId() != null){
                srsStudent.setClassId(stuYearStudentUser.getClassId());
            }
            if (stuYearStudentUser.getStuNo() != null){
                srsStudent.setStuNo(stuYearStudentUser.getStuNo());
            }
            if (stuYearStudentUser.getName() != null){
                srsStudent.setName("%" + stuYearStudentUser.getName() + "%");
            }
            cphClasstwo.setStudent(srsStudent);
            // 添加学院和专业的查询条件
            cphClasstwo.setDeptId(stuYearStudentUser.getDeptId());
            cphClasstwo.setMajorId(stuYearStudentUser.getMajorId());
        }
        List<CphClasstwo> list = cphClasstwoService.selectCphClasstwoList(cphClasstwo);
        ExcelUtil<CphClasstwo> util = new ExcelUtil<CphClasstwo>(CphClasstwo.class);
        util.exportExcel(response, list, "第二课堂数据");
    }

    /**
     * 获取第二课堂详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(cphClasstwoService.selectCphClasstwoById(id));
    }


    /**
     * 第二课堂学生提交
     * @author zhy
     * @date 2023-07-04 20:49:40
     * @param
     * @return
     **/
    @Log(title = "第二课堂", businessType = BusinessType.INSERT)
    @PostMapping("/stuAdd")
    public AjaxResult stuAdd(@RequestBody CphClasstwo cphClasstwo){
        CphRules rule = _rulesService.selectCphRulesByRuleId(cphClasstwo.getRuleId());

        cphClasstwo.setToAdd(getUserId());
        cphClasstwo.setClasstwoScore(rule.getMaxScore());
        cphClasstwo.setSubmitter(getUserId());
        cphClasstwo.setAuditStatus("1");
        return toAjax(cphClasstwoService.insertCphClasstwo(cphClasstwo));
    }



    /**
     * 新增第二课堂
     */
    @Log(title = "第二课堂", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CphClasstwo cphClasstwo)
    {
        CphRules rule = _rulesService.selectCphRulesByRuleId(cphClasstwo.getRuleId());

        cphClasstwo.setClasstwoScore( rule.getMaxScore() );
        cphClasstwo.setSubmitter(getUserId());
        cphClasstwo.setAuditStatus("1");
        return toAjax(cphClasstwoService.insertCphClasstwo(cphClasstwo));
    }

    /**
     * 修改第二课堂
     */
    @Log(title = "第二课堂", businessType = BusinessType.UPDATE)
    @PostMapping("/update")
    public AjaxResult edit(@RequestBody CphClasstwo cphClasstwo)
    {
        return toAjax(cphClasstwoService.updateCphClasstwo(cphClasstwo));
    }

    /**
     * 删除第二课堂
     */
    @Log(title = "第二课堂", businessType = BusinessType.DELETE)
    @PostMapping("/delete/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(cphClasstwoService.deleteCphClasstwoByIds(ids));
    }

    /**
     * 中间表的数据添加到本地
     * @return 结果
     */
    @PostMapping("/addDataClassTwo")
    public AjaxResult addDataClassTwo()
    {
        return success("解析成功").put("添加失败的数据",cphClasstwoService.addSumClassTwo());
    }

    //批量导入第二课堂
    @PostMapping("/addDataClassTwoAll")
    public AjaxResult addDataClassTwoAll(@RequestBody List<CphClasstwoVo> cphClasstwoVo)
    {
        boolean b = cphClasstwoService.addSumClassAll(cphClasstwoVo);
        if (b){
            return success();
        }
        return error();
    }
}
