package com.srs.web.controller.diagnostic;

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

import com.srs.common.core.domain.model.LoginUser;
import com.srs.common.utils.SecurityUtils;
import com.srs.comprehensive.domain.SrsStudent;
import com.srs.comprehensive.service.ISrsStudentService;
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.DiaReportDetails;
import com.srs.diagnostic.service.IDiaReportDetailsService;
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 2025-05-08
 */
@RestController
@RequestMapping("/diagnostic/details")
@Api(value = "诊断报告管理", tags = "诊断报告管理")
public class DiaReportDetailsController extends BaseController {
    @Autowired
    private IDiaReportDetailsService diaReportDetailsService;

    @Autowired
    private ISrsStudentService srsStudentService;

    /**
     * 查询诊断报告列表
     */
    @PreAuthorize("@ss.hasPermi('diagnostic:details:list') || @ss.hasPermi('diagnostic:diaReportSubmissions:list')")
    @GetMapping("/list")
    @ApiOperation("查询诊断报告列表")
    public TableDataInfo list(DiaReportDetails diaReportDetails) {


        //获取登录人
        LoginUser user = SecurityUtils.getLoginUser();
        SrsStudent srsStudent = srsStudentService.selectSrsStudentByStuNo(getUsername());

        List<DiaReportDetails> list = diaReportDetailsService.selectDiaReportDetailsList(diaReportDetails);
        return getDataTable(list);
    }

    ///      新增的批量插入方法，基于层级关系
    @PreAuthorize("@ss.hasPermi('diagnostic:details:add')")
    @Log(title = "诊断报告", businessType = BusinessType.INSERT)
    @PostMapping("/batchAdd")
    @ApiOperation("批量新增诊断报告")
    public AjaxResult batchAdd(@RequestBody List<DiaReportDetails> list) {
        return toAjax(diaReportDetailsService.batchInsertReportDetailsByHierarchy(list));
    }


    /**
     * 提交诊断标准
     */
    @PreAuthorize("@ss.hasPermi('diagnostic:details:submit')")
    @Log(title = "诊断标准提交", businessType = BusinessType.UPDATE)
    @PostMapping("/submitStandards")
    @ApiOperation("提交诊断标准")
    public AjaxResult submitStandards(@RequestBody List<DiaReportDetails> list) {
        // 基础校验
//        if (diaReportDetails.getDimensionId() == null
//                || diaReportDetails.getQualityControlPointId() == null
//                || diaReportDetails.getIndicatorId() == null) {
//            return AjaxResult.error("关键ID字段不能为空");
//        }

        return toAjax(diaReportDetailsService.batchUpdate(list));

    }


    /**
     * 新增诊断报告
     */
    @PreAuthorize("@ss.hasPermi('diagnostic:details:add ')")
    @Log(title = "诊断报告", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ApiOperation("新增诊断报告")
    public AjaxResult add(@RequestBody DiaReportDetails diaReportDetails) {

        return toAjax(diaReportDetailsService.insertDiaReportDetails(diaReportDetails));
    }


    /**
     * 导出诊断报告列表
     */
    @PreAuthorize("@ss.hasPermi('diagnostic:details:export')")
    @Log(title = "诊断报告", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiOperation("导出诊断报告列表")
    public void export(HttpServletResponse response, DiaReportDetails diaReportDetails) {
        List<DiaReportDetails> list = diaReportDetailsService.selectDiaReportDetailsList(diaReportDetails);
        ExcelUtil<DiaReportDetails> util = new ExcelUtil<DiaReportDetails>(DiaReportDetails.class);
        util.exportExcel(response, list, "诊断报告数据");
    }


    /**
     * 获取诊断报告详细信息
     */
    @PreAuthorize("@ss.hasPermi('diagnostic:details:query')")
    @GetMapping(value = "/{id}")
    @ApiOperation("获取诊断报告详细信息")
    public AjaxResult getInfo(@PathVariable("id") Long id) {

        return success(diaReportDetailsService.selectDiaReportDetailsById(id));
    }


    /**
     * 修改诊断报告
     */
    @PreAuthorize("@ss.hasPermi('diagnostic:details:edit')")
    @Log(title = "诊断报告", businessType = BusinessType.UPDATE)
    @PostMapping("/update")
    @ApiOperation("修改诊断报告")
    public AjaxResult edit(@RequestBody DiaReportDetails diaReportDetails) {
        return toAjax(diaReportDetailsService.updateDiaReportDetails(diaReportDetails));
    }

    /**
     * 删除诊断报告
     */
    @PreAuthorize("@ss.hasPermi('diagnostic:details:remove')")
    @Log(title = "诊断报告", businessType = BusinessType.DELETE)
    @PostMapping("/{ids}")
    @ApiOperation("删除诊断报告")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(diaReportDetailsService.deleteDiaReportDetailsByIds(ids));
    }
}
