package com.hxhq.business.controller;

import java.util.Arrays;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.hxhq.business.domain.*;
import com.hxhq.business.dto.study.StudyFormPreListDto;
import com.hxhq.business.enums.study.StudyStatusEnum;
import com.hxhq.business.form.study.StudyFormPreAuditForm;
import com.hxhq.business.form.study.StudyFormPreSearchForm;
import com.hxhq.business.form.study.StudyFormUpdateForm;
import com.hxhq.business.service.IStudyFormPreJcgjService;
import com.hxhq.business.service.IStudyFormPreQmxxService;
import com.hxhq.business.service.IStudyService;
import com.hxhq.common.core.exception.ServiceException;
import com.hxhq.common.security.annotation.Logical;
import com.hxhq.common.security.annotation.RequiresPermissions;
import org.aspectj.weaver.loadtime.Aj;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import com.hxhq.business.service.IStudyFormPreService;
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/studyFormPre")
public class StudyFormPreController extends BaseController
{
    @Autowired
    private IStudyFormPreService studyFormPreService;
    @Autowired
    private IStudyFormPreJcgjService studyFormPreJcgjService;
    @Autowired
    private IStudyFormPreQmxxService studyFormPreQmxxService;
    @Autowired
    private IStudyService studyService;

    /**
     * 查询试验列表
     */
    @GetMapping("/studyList")
    @RequiresPermissions(value={"business:studyFormPre:bj","business:studyFormPre:xz",
            "business:nonTrialFormPre:bj","business:nonTrialFormPre:xz",
            "business:drugFormPre:bj","business:drugFormPre:xz"}, logical= Logical.OR)
    public AjaxResult studyList() {
        QueryWrapper<Study> queryWrapper = Wrappers.query();
        queryWrapper.in("status", StudyStatusEnum.syz.getValue(),StudyStatusEnum.ysd.getValue());
        return AjaxResult.success(studyService.list(queryWrapper));
    }


    /**
     * 查询试验预填表单列表
     */
    @GetMapping("/studyFormPreList")
    @RequiresPermissions(value={"business:studyFormPre:bj","business:studyFormPre:xz",
            "business:nonTrialFormPre:bj","business:nonTrialFormPre:xz",
            "business:drugFormPre:bj","business:drugFormPre:xz"}, logical= Logical.OR)
    public AjaxResult studyFormPreList(StudyFormPreSearchForm form) {
        if (form.getStudyId() == null || form.getStudyId().longValue() < 0) {
            throw new ServiceException("试验id不能为空");
        }
        return AjaxResult.success(studyFormPreService.queryList(form));
    }

    /**
     * 列表
     */
    @GetMapping("/list")
    @RequiresPermissions(value={"business:studyFormPre:list","business:nonTrialFormPre:list","business:drugFormPre:list"}, logical= Logical.OR)
    public TableDataInfo list(StudyFormPreSearchForm form)
    {
        startPage();
        List<StudyFormPreListDto> list = studyFormPreService.queryList(form);
        return getDataTable(list);
    }

    /**
     * 稽查轨迹列表
     */
    @GetMapping("/jcgjList")
    @RequiresPermissions(value={"business:studyFormPre:xq","business:nonTrialFormPre:xq","business:drugFormPre:xq"}, logical= Logical.OR)
    public TableDataInfo jcgjList(StudyFormPreJcgj form)
    {
        startPage();
        List<StudyFormPreJcgj> list = studyFormPreJcgjService.queryList(form);
        return getDataTable(list);
    }

    /**
     * 签名信息列表
     */
    @GetMapping("/qmxxList")
    @RequiresPermissions(value={"business:studyFormPre:xq","business:nonTrialFormPre:xq","business:drugFormPre:xq"}, logical= Logical.OR)
    public TableDataInfo qmxxList(StudyFormPreQmxx form)
    {
        startPage();
        List<StudyFormPreQmxx> list = studyFormPreQmxxService.queryList(form);
        return getDataTable(list);
    }

    /**
     * 详细
     */
    @GetMapping(value = "/info")
    @RequiresPermissions(value={"business:studyFormPre:xq","business:nonTrialFormPre:xq","business:drugFormPre:xq"}, logical= Logical.OR)
    public AjaxResult getInfo(Long id)
    {
        return AjaxResult.success(studyFormPreService.queryInfo(id));
    }

    /**
     * 保存
     */
    @PostMapping("/bc")
    @RequiresPermissions(value={"business:studyFormPre:bj","business:studyFormPre:xz",
            "business:nonTrialFormPre:bj","business:nonTrialFormPre:xz",
            "business:drugFormPre:bj","business:drugFormPre:xz"}, logical= Logical.OR)
    public AjaxResult bc(@RequestBody StudyFormPre studyFormPre)
    {
        return AjaxResult.success(studyFormPreService.bc(studyFormPre));
    }

    /**
     * 仅保存
     */
    @PostMapping("/bcOnly")
    @RequiresPermissions(value={"business:studyFormPre:bj","business:studyFormPre:xz",
            "business:nonTrialFormPre:bj","business:nonTrialFormPre:xz",
            "business:drugFormPre:bj","business:drugFormPre:xz"}, logical= Logical.OR)
    public AjaxResult bcOnly(@RequestBody StudyFormPre studyFormPre)
    {
        return AjaxResult.success(studyFormPreService.bcOnly(studyFormPre));
    }

    /**
     * 提交
     */
    @PostMapping("/tj")
    @RequiresPermissions(value={"business:studyFormPre:bj","business:nonTrialFormPre:bj","business:drugFormPre:bj"}, logical= Logical.OR)
    public AjaxResult tj(@RequestBody StudyFormPre studyFormPre)
    {
        studyFormPreService.tj(studyFormPre);
        return AjaxResult.success("操作成功");
    }


    /**
     * 填报
     */
    @PostMapping("/tb")
    @RequiresPermissions(value={"business:studyFormPre:tb","business:nonTrialFormPre:tb","business:drugFormPre:tb"}, logical= Logical.OR)
    public AjaxResult tb(@RequestBody StudyFormPre studyFormPre)
    {
        return AjaxResult.success(studyFormPreService.tb(studyFormPre));
    }

    /**
     * 通过
     */
    @PostMapping("/tg")
    @RequiresPermissions(value={"business:studyFormPre:sh","business:nonTrialFormPre:sh","business:drugFormPre:sh"}, logical= Logical.OR)
    public AjaxResult tg(@RequestBody StudyFormPreAuditForm form)
    {
        studyFormPreService.tg(form);
        return AjaxResult.success("操作成功");
    }

    /**
     * 拒绝
     */
    @PostMapping("/jj")
    @RequiresPermissions(value={"business:studyFormPre:sh","business:nonTrialFormPre:sh","business:drugFormPre:sh"}, logical= Logical.OR)
    public AjaxResult jj(@RequestBody StudyFormPreAuditForm form)
    {
        studyFormPreService.jj(form);
        return AjaxResult.success("操作成功");
    }

    /**
     * 申请关闭
     */
    @PostMapping("/sqgb")
    public AjaxResult sqgb(@RequestBody StudyFormPre studyFormPre)
    {
        studyFormPreService.sqgb(studyFormPre);
        return AjaxResult.success("操作成功");
    }

    /**
     * 关闭同意
     */
    @PostMapping("/tygb")
    public AjaxResult tygb(@RequestBody StudyFormPreAuditForm form)
    {
        studyFormPreService.tygb(form);
        return AjaxResult.success("操作成功");
    }

    /**
     * 关闭拒绝
     */
    @PostMapping("/jjgb")
    public AjaxResult jjgb(@RequestBody StudyFormPreAuditForm form)
    {
        studyFormPreService.jjgb(form);
        return AjaxResult.success("操作成功");
    }

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

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

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