package com.srs.web.controller.comprehensive;

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

import com.srs.common.annotation.Anonymous;
import com.srs.common.annotation.RepeatSubmit;
import com.srs.common.utils.StringUtils;
import com.srs.comprehensive.domain.*;
import com.srs.comprehensive.domain.Vo.*;
import com.srs.comprehensive.service.ICphAuditDetailsService;
import com.srs.comprehensive.service.ICphIamService;
import com.srs.comprehensive.service.ICphRulesService;
import com.srs.comprehensive.service.ISrsStudentService;
import com.srs.system.service.ISysRoleService;
import com.srs.web.controller.common.RoleBool;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import liquibase.pro.packaged.A;
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;

import static com.srs.common.utils.SecurityUtils.getUserId;

/**
 * 思想品德Controller
 * 
 * @author zhy
 * @date 2023-07-03
 */
@Api("思想品德")
@RestController
@RequestMapping("/system/iam")
public class CphIamController extends BaseController {
    @Autowired
    private ICphIamService cphIamService;
    @Autowired
    private ICphRulesService _rulesService;
    @Autowired
    private ICphAuditDetailsService _auditService;
    @Autowired
    private ISrsStudentService _stuService;
    @Autowired
    private ISysRoleService _roleService;


    //查询错误的加分记录 iam_id重复
    @GetMapping("/listErrorAddRecord")
    public AjaxResult listErrorAddRecord(CphSearch param) {
        boolean hasRole = RoleBool.notStu(getUserId(), _roleService);
        if (hasRole) {
            List<Map<String, Object>> list = cphIamService.listErrorAddRecord(param);
            return AjaxResult.success(list);
        } else {
            return AjaxResult.error(401, "你无权做这些");
        }
    }


    //删除异常加分记录
    @PostMapping("/deleteError/{id}")
    @RepeatSubmit(interval = 10000, message = "请不要重复提交")
    public AjaxResult deleteError(@PathVariable("id") Long id) {
        boolean hasRole = RoleBool.notStu(getUserId(), _roleService);
        if (hasRole) {
            return cphIamService.deleteError(id);
        } else {
            return AjaxResult.error(401, "你无权做这些");
        }
    }

    @GetMapping("/listIamByStuNoYear")
    public TableDataInfo listIamByStuNoYear(CphSearch param) {
        startPage();
        List<CphIamSearchVo> list = cphIamService.listIamByStuNoYear(param);
        return getDataTable(list);
    }

    @GetMapping("/getOwnRecord/{year}")
    public TableDataInfo getOwnRecord(@PathVariable("year") String year) {
        startPage();
        String stuNo = getUsername();
        List<CphRecord> list = cphIamService.getOwnRecord(stuNo,year);
        return getDataTable(list);
    }

    /**
     * 查询思想品德列表
     */
//    @PreAuthorize("@ss.hasPermi('system:iam:list')")
    @GetMapping("/list")
    public TableDataInfo list(CphIam cphIam)
    {
        startPage();
        List<CphIam> list = cphIamService.selectCphIamList(cphIam);
        return getDataTable(list);
    }
    /**
     * 查询思想品德列表 连表
     */
//    @PreAuthorize("@ss.hasPermi('system:iam:list')")
    @ApiOperation("思想品德连表查询")
    @GetMapping("/lists")
    public TableDataInfo lists(stuYearStudentUser cphIamStudentUser)
    {
        startPage();
        CphIam cphIam = new CphIam();//toAdd:学生id
        cphIam.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());
        }
        cphIam.setStudent(srsStudent);
        List<CphIam>  cphIam1= cphIamService.selectCphIamListJoin(cphIam);
        return getDataTable(cphIam1);
    }

    /**
     * 查询学生思想品德总分
     */
    @ApiOperation("查询学生思想品德总分")
    @GetMapping("/sumList")
    public TableDataInfo sumLists(stuYearStudentUser cphIamStudentUser){
        startPage();
        CphIdeologyScore cphIam = new CphIdeologyScore();//toAdd:学生id
        if(StringUtils.isNotNull(cphIamStudentUser.getStuYearId()))
            cphIam.setStuYearId(cphIamStudentUser.getStuYearId());
        SrsStudent srsStudent = new SrsStudent();
        cphIam.setStuNo(cphIamStudentUser.getStuNo());
        if(StringUtils.isNotNull(cphIamStudentUser.getName()))
            cphIam.setName("%"+cphIamStudentUser.getName()+"%");
        cphIam.setClassId(cphIamStudentUser.getClassId());

        List<CphIdeologyScore>  cphIam1= cphIamService.selectCphIamSumList(cphIam);
        return getDataTable(cphIam1);
    }

    /**
     * 导出思想品德列表
     */
//    @PreAuthorize("@ss.hasPermi('system:iam:export')")
    @Log(title = "思想品德", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CphIam cphIam)
    {
        List<CphIam> list = cphIamService.selectCphIamList(cphIam);
        ExcelUtil<CphIam> util = new ExcelUtil<CphIam>(CphIam.class);
        util.exportExcel(response, list, "思想品德数据");
    }

    /**
     * 获取思想品德详细信息
     */
//    @PreAuthorize("@ss.hasPermi('system:iam:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(cphIamService.selectCphIamById(id));
    }

    /**
     * 思想品德学生提交
     * @author zhy
     * @date 2023-07-04 20:49:40
     * @param cphIam
     * @return
     **/
    @Log(title = "思想品德", businessType = BusinessType.INSERT)
    @PostMapping("/stuAdd")
    @RepeatSubmit(interval = 10000, message = "请不要重复提交")
    public AjaxResult stuAdd(@RequestBody CphIam cphIam){
        CphRules rule = _rulesService.selectCphRulesByRuleId(cphIam.getRuleId());
        SrsStudent stu = _stuService.selectSrsStudentByStuNo(getUsername());
        cphIam.setStuNo(stu.getStuNo());
        cphIam.setToAdd(stu.getStuId());
        cphIam.setIamScore( rule.getMaxScore() );
        cphIam.setSubmitter(getUserId());
        cphIam.setAuditStatus("1");
        return toAjax(cphIamService.insertCphIam(cphIam));
    }

    //辅导员提交加减分
    @PostMapping("/CounsellorAdd")
    @RepeatSubmit(interval = 10000, message = "请不要重复提交")
    public AjaxResult CounsellorAdd(@RequestBody CphIamVo cphIamVO){
        CphRules rule = _rulesService.selectCphRulesByRuleId(cphIamVO.getRuleId());//规则记录
        SrsStudent stu = _stuService.selectSrsStudentByStuNo(cphIamVO.getStuNo());//学号查询学生
        cphIamVO.setToAdd(stu.getStuId());//加分人id
        cphIamVO.setStuNo(stu.getStuNo());
        cphIamVO.setIamScore( rule.getMaxScore() );//得分
        cphIamVO.setSubmitter(getUserId());//提交人id
        cphIamVO.setAuditStatus("6");//审核状态
        return toAjax(cphIamService.insertCphIamCounsellor(cphIamVO));
    }



    /**
     * 新增思想品德
     */
//    @PreAuthorize("@ss.hasPermi('system:iam:add')")
    @Log(title = "思想品德", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CphIam cphIam)
    {
        CphRules rule = _rulesService.selectCphRulesByRuleId(cphIam.getRuleId());
        cphIam.setIamScore( rule.getMaxScore() );
        cphIam.setSubmitter(getUserId());
        cphIam.setAuditStatus("1");
        return toAjax(cphIamService.insertCphIam(cphIam));
    }

    /**
     * 修改思想品德
     */
//    @PreAuthorize("@ss.hasPermi('system:iam:edit')")
    @Log(title = "思想品德", businessType = BusinessType.UPDATE)
    @PostMapping("/update")
    public AjaxResult edit(@RequestBody CphIam cphIam)
    {
        return toAjax(cphIamService.updateCphIam(cphIam));
    }

    /**
     * 删除思想品德
     */
//    @PreAuthorize("@ss.hasPermi('system:iam:remove')")
    @Log(title = "思想品德", businessType = BusinessType.DELETE)
    @PostMapping("/delete/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(cphIamService.deleteCphIamByIds(ids));
    }
}
