package com.srs.web.controller.diagnostic;

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

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.srs.comprehensive.domain.ViewStuInfo;
import com.srs.comprehensive.mapper.ViewStuInfoMapper;
import com.srs.diagnostic.domain.DiaReportItems;
import com.srs.system.domain.SysDeptMap;
import com.srs.system.service.ISysDeptMapService;
import org.springframework.beans.factory.annotation.Autowired;
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.diagnostic.domain.DiaReportStudentCompletion;
import com.srs.diagnostic.service.IDiaReportStudentCompletionService;
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 LSD
 * @date 2025-06-19
 */
@RestController
@RequestMapping("/diagnostic/studentCompletion")
@Api(value = "学生完成情况管理", tags = "学生完成情况管理")
public class DiaReportStudentCompletionController extends BaseController {
    @Autowired
    private IDiaReportStudentCompletionService diaReportStudentCompletionService;

    @GetMapping("/listDeptStu")
    public TableDataInfo listDeptStu(ViewStuInfo param){
        Long oldId = getDeptId();
        SysDeptMap dept = _deptMap.getOldDeptByNewId(oldId);
        Long deptId = 0L;
        if(dept != null){
            deptId = dept.getOldDeptId();
        }
        param.setDeptId(deptId);
        startPage();
        List<ViewStuInfo> list = viewStuInfoMapper.selectViewStuInfoList(param);
        return getDataTable(list);
    }



    @PostMapping("/stuEdit")
    public AjaxResult stuEdit(@RequestBody List<DiaReportStudentCompletion> param){
        return diaReportStudentCompletionService.stuEdit(param);
    }


    @Autowired
    public ViewStuInfoMapper viewStuInfoMapper;
    @Autowired
    ISysDeptMapService _deptMap;


    @GetMapping("/getStuItemByReportId/{relId}/{reportId}")
    public AjaxResult getStuItemByReportId(@PathVariable("relId") Long relId ,@PathVariable("reportId") Long reportId) {
        QueryWrapper<ViewStuInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("stu_no",getUsername());
        ViewStuInfo viewStuInfo = viewStuInfoMapper.selectOne(queryWrapper);
        Long stuDept = viewStuInfo.getDeptId();
        SysDeptMap dept = _deptMap.getNewDeptByOldId(stuDept);
        Long deptId = 0L;
        if(dept != null){
            deptId = dept.getNewDeptId();
        }
        List<DiaReportItems> list = diaReportStudentCompletionService.getStuItemByReportId(deptId,relId,reportId);
        return AjaxResult.success(list);
    }

    @PreAuthorize("@ss.hasPermi('diagnostic:studentCompletion:getStuItemByStuNo')")
    @GetMapping("/getStuItemByStuNo/{stuNo}/{relId}/{reportId}")
    public AjaxResult getStuItemByStuNo( @PathVariable("stuNo") String stuNo,@PathVariable("relId") Long relId ,@PathVariable("reportId") Long reportId) {
        QueryWrapper<ViewStuInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("stu_no",stuNo);
        ViewStuInfo viewStuInfo = viewStuInfoMapper.selectOne(queryWrapper);
        Long stuDept = viewStuInfo.getDeptId();
        SysDeptMap dept = _deptMap.getNewDeptByOldId(stuDept);
        Long deptId = 0L;
        if(dept != null){
            deptId = dept.getNewDeptId();
        }
        List<DiaReportItems> list = diaReportStudentCompletionService.getStuItemByStuNo(stuNo,deptId,relId,reportId);
        return AjaxResult.success(list);
    }

    /**
     * 查询学生完成情况列表
     */
    @PreAuthorize("@ss.hasPermi('diagnostic:studentCompletion:list')")
    @GetMapping("/list")
    @ApiOperation("查询学生完成情况列表")
    public TableDataInfo list(DiaReportStudentCompletion diaReportStudentCompletion) {
        startPage();
        List<DiaReportStudentCompletion> list = diaReportStudentCompletionService.selectDiaReportStudentCompletionList(diaReportStudentCompletion);
        return getDataTable(list);
    }

    /**
     * 导出学生完成情况列表
     */
    @PreAuthorize("@ss.hasPermi('diagnostic:studentCompletion:export')")
    @Log(title = "学生完成情况", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiOperation("导出学生完成情况列表")
    public void export(HttpServletResponse response, DiaReportStudentCompletion diaReportStudentCompletion) {
        List<DiaReportStudentCompletion> list = diaReportStudentCompletionService.selectDiaReportStudentCompletionList(diaReportStudentCompletion);
        ExcelUtil<DiaReportStudentCompletion> util = new ExcelUtil<DiaReportStudentCompletion>(DiaReportStudentCompletion.class);
        util.exportExcel(response, list, "学生完成情况数据");
    }

    /**
     * 获取学生完成情况详细信息
     */
    @PreAuthorize("@ss.hasPermi('diagnostic:studentCompletion:query')")
    @GetMapping(value = "/{completionId}")
    @ApiOperation("获取学生完成情况详细信息")
    public AjaxResult getInfo(@PathVariable("completionId") Long completionId) {
        return success(diaReportStudentCompletionService.selectDiaReportStudentCompletionByCompletionId(completionId));
    }

    /**
     * 新增学生完成情况
     */
    @PreAuthorize("@ss.hasPermi('diagnostic:studentCompletion:add')")
    @Log(title = "学生完成情况", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ApiOperation("新增学生完成情况")
    public AjaxResult add(@RequestBody DiaReportStudentCompletion diaReportStudentCompletion) {
        return toAjax(diaReportStudentCompletionService.insertDiaReportStudentCompletion(diaReportStudentCompletion));
    }

    /**
     * 修改学生完成情况
     */
    @PreAuthorize("@ss.hasPermi('diagnostic:studentCompletion:edit')")
    @Log(title = "学生完成情况", businessType = BusinessType.UPDATE)
    @PostMapping("/update")
    @ApiOperation("修改学生完成情况")
    public AjaxResult edit(@RequestBody DiaReportStudentCompletion diaReportStudentCompletion) {
        return toAjax(diaReportStudentCompletionService.updateDiaReportStudentCompletion(diaReportStudentCompletion));
    }

    /**
     * 删除学生完成情况
     */
    @PreAuthorize("@ss.hasPermi('diagnostic:studentCompletion:remove')")
    @Log(title = "学生完成情况", businessType = BusinessType.DELETE)
    @PostMapping("/{completionIds}")
    @ApiOperation("删除学生完成情况")
    public AjaxResult remove(@PathVariable Long[] completionIds) {
        return toAjax(diaReportStudentCompletionService.deleteDiaReportStudentCompletionByCompletionIds(completionIds));
    }
}
