package com.hxhq.business.controller;

import java.util.List;

import com.hxhq.business.domain.*;
import com.hxhq.business.dto.study.StudyFormPlanListDto;
import com.hxhq.business.form.study.StudyFormPlanSearchForm;
import com.hxhq.business.form.study.StudyFormUpdateForm;
import com.hxhq.business.service.IStudyFormPlanJcgjService;
import com.hxhq.business.service.IStudyFormPlanQmxxService;
import com.hxhq.business.utils.pdf.PdfExportUtil;
import com.hxhq.common.security.annotation.Logical;
import com.hxhq.common.security.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import com.hxhq.business.service.IStudyFormPlanService;
import com.hxhq.common.core.web.controller.BaseController;
import com.hxhq.common.core.web.domain.AjaxResult;
import com.hxhq.common.core.web.page.TableDataInfo;


/**
 * 试验-配置计划Controller
 *
 * @author hxhq
 * @date 2025-12-29
 */
@RestController
@RequestMapping("/business/studyFormPlan")
public class StudyFormPlanController extends BaseController
{
    @Autowired
    private IStudyFormPlanService studyFormPlanService;
    @Autowired
    private IStudyFormPlanJcgjService studyFormPlanJcgjService;
    @Autowired
    private IStudyFormPlanQmxxService studyFormPlanQmxxService;
    /**
     * 上传文件存储在本地的根路径
     */
    @Value("${file.path}")
    private String localFilePath;
    /**
     * 资源映射路径 前缀
     */
    @Value("${file.prefix}")
    public String localFilePrefix;

    /**
     * 列表
     */
    @GetMapping("/list")
    @RequiresPermissions("business:studyFormPlan:list")
    public TableDataInfo list(StudyFormPlanSearchForm form) {
        startPage();
        List<StudyFormPlanListDto> list = studyFormPlanService.queryList(form);
        return getDataTable(list);
    }

    /**
     * 稽查轨迹列表
     */
    @GetMapping("/jcgjList")
    @RequiresPermissions("business:studyFormPlan:xq")
    public TableDataInfo jcgjList(StudyFormPlanJcgj form) {
        startPage();
        List<StudyFormPlanJcgj> list = studyFormPlanJcgjService.queryList(form);
        return getDataTable(list);
    }

    /**
     * 签名信息列表
     */
    @GetMapping("/qmxxList")
    @RequiresPermissions("business:studyFormPlan:xq")
    public TableDataInfo qmxxList(StudyFormPlanQmxx form) {
        startPage();
        List<StudyFormPlanQmxx> list = studyFormPlanQmxxService.queryList(form);
        return getDataTable(list);
    }

    /**
     * 详细
     */
    @GetMapping(value = "/info")
    @RequiresPermissions("business:studyFormPlan:xq")
    public AjaxResult getInfo(Long id) {
        return AjaxResult.success(studyFormPlanService.queryInfo(id));
    }

    /**
     * 导出
     */
    @RequiresPermissions({"business:studyFormPlan:xq"})
    @GetMapping(value = "/exportDetail")
    public AjaxResult exportDetail(StudyFormPlanJcgj form) {
        List<StudyFormPlanJcgj> studyFormPlanJcgjList = studyFormPlanJcgjService.queryList(form);
        StudyFormPlanQmxx studyFormPlanQmxx = new StudyFormPlanQmxx();
        studyFormPlanQmxx.setFormId(form.getFormId());
        List<StudyFormPlanQmxx> studyFormPlanQmxxList = studyFormPlanQmxxService.queryList(studyFormPlanQmxx);
        StudyFormPlan studyFormPlan=studyFormPlanService.queryInfo(form.getFormId());
        return AjaxResult.success(localFilePrefix + PdfExportUtil.export(studyFormPlan.getTemplateExportClass(), studyFormPlan.getTemplateExportMethod(),studyFormPlan,studyFormPlanQmxxList,studyFormPlanJcgjList,form.getLang(),form.getJcgjlx(),localFilePath));
    }

    /**
     * 更换归属人
     */
    @RequiresPermissions("business:studyFormPlan:ghgsr")
    @PostMapping("/ghgsr")
    public AjaxResult ghgsr(@RequestBody StudyFormPlan studyFormPlan) {
        studyFormPlanService.ghgsr(studyFormPlan);
        return AjaxResult.success("操作成功");
    }

    /**
     * 保存
     */
    @PostMapping("/bc")
    @RequiresPermissions(value={"business:studyFormPlan:bj","business:studyFormPlan:xz"}, logical= Logical.OR)
    public AjaxResult bc(@RequestBody StudyFormPlan studyFormPlan) {
        return AjaxResult.success(studyFormPlanService.bc(studyFormPlan));
    }

    /**
     * 仅保存
     */
    @PostMapping("/bcOnly")
    @RequiresPermissions(value={"business:studyFormPlan:bj","business:studyFormPlan:xz"}, logical= Logical.OR)
    public AjaxResult bcOnly(@RequestBody StudyFormPlan studyFormPlan) {
        return AjaxResult.success(studyFormPlanService.bcOnly(studyFormPlan));
    }


    /**
     * 提交
     */
    @RequiresPermissions("business:studyFormPlan:bj")
    @PostMapping("/tj")
    public AjaxResult tj(@RequestBody StudyFormPlan studyFormPlan) {
        studyFormPlanService.tj(studyFormPlan);
        return AjaxResult.success("操作成功");
    }


    /**
     * 复核通过
     */
    @RequiresPermissions("business:studyFormPlan:fh")
    @PostMapping("/fhtg")
    public AjaxResult fhtg(@RequestBody StudyFormPlan studyFormPlan) {
        studyFormPlanService.fhtg(studyFormPlan);
        return AjaxResult.success("操作成功");
    }

    /**
     * 复核拒绝
     */
    @RequiresPermissions("business:studyFormPlan:fh")
    @PostMapping("/fhjj")
    public AjaxResult fhjj(@RequestBody StudyFormPlan studyFormPlan) {
        studyFormPlanService.fhjj(studyFormPlan);
        return AjaxResult.success("操作成功");
    }


    /**
     * 审阅
     */
    @RequiresPermissions("business:studyFormPlan:sy")
    @PostMapping("/sy")
    public AjaxResult sy(@RequestBody StudyFormPlan studyFormPlan) {
        studyFormPlanService.sy(studyFormPlan);
        return AjaxResult.success("操作成功");
    }

    /**
     * 保存复核意见
     */
    @PostMapping("/updateFhyjjl")
    public AjaxResult updateFhyjjl(@RequestBody StudyFormUpdateForm form) {
        studyFormPlanService.updateFhyjjl(form);
        return AjaxResult.success("操作成功");
    }

    /**
     * 保存表单内容
     */
    @PostMapping("/updateBdnr")
    public AjaxResult updateBdnr(@RequestBody StudyFormUpdateForm form) {
        studyFormPlanService.updateBdnr(form);
        return AjaxResult.success("操作成功");
    }

    /**
     * 保存字段勾选记录
     */
    @PostMapping("/updateZdgxjl")
    public AjaxResult updateZdgxjl(@RequestBody StudyFormUpdateForm form) {
        studyFormPlanService.updateZdgxjl(form);
        return AjaxResult.success("操作成功");
    }
}
