package com.hxhq.business.controller;

import java.util.*;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hxhq.business.domain.*;
import com.hxhq.business.dto.sj.SjListDto;
import com.hxhq.business.enums.zykgl.JcgjlxEnum;
import com.hxhq.business.enums.zykgl.JcmcysEnum;
import com.hxhq.business.enums.zykgl.JyztEnum;
import com.hxhq.business.enums.zykgl.ZjztEnum;
import com.hxhq.business.form.gsp.GspSearchListForm;
import com.hxhq.business.form.sj.*;
import com.hxhq.business.service.ISjService;
import com.hxhq.business.service.ISjTzService;
import com.hxhq.business.service.ISjJcgjService;
import com.hxhq.business.service.IStudySubjectService;
import com.hxhq.business.utils.JctUtil;
import com.hxhq.business.utils.pdf.PdfExportUtil;
import com.hxhq.common.security.annotation.Logical;
import com.hxhq.common.security.annotation.RequiresPermissions;
import com.hxhq.common.security.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
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 HanLong
 * @date 2025-12-22
 */
@RestController
@RequestMapping("/business/sj")
public class SjController extends BaseController
{
    @Autowired
    private ISjService sjService;

    @Autowired
    private ISjJcgjService sjJcgjService;

    @Autowired
    private ISjTzService sjTzService;

    /**
     * 上传文件存储在本地的根路径
     */
    @Value("${file.path}")
    private String localFilePath;
    /**
     * 资源映射路径 前缀
     */
    @Value("${file.prefix}")
    public String localFilePrefix;

    /**
     * 试验物资列表
     */
    @GetMapping("/studyList")
    @RequiresPermissions(value = {"business:studySj:list"}, logical= Logical.OR)
    public TableDataInfo studyList(GspSearchListForm form)
    {
        startPage();
        List<SjListDto> list = sjService.queryStudyList(form);
        return getDataTable(list);
    }

    /**
     * 稽查轨迹列表
     */
    @GetMapping("/jcgjList")
    @RequiresPermissions(value={"business:resource:sj:xq", "business:studySj:xq", "business:archive:sj:xq"}, logical= Logical.OR)
    public TableDataInfo jcgjList(SjJcgj form)
    {
        startPage();
        List<SjJcgj> list = sjJcgjService.queryList(form);
        return getDataTable(list);
    }

    /**
     * 导出
     */
    @RequiresPermissions(value={"business:resource:sj:xq", "business:studySj:xq", "business:archive:sj:xq"}, logical= Logical.OR)
    @GetMapping(value = "/exportDetail")
    public AjaxResult exportDetail(Long id,String lang) {
        SjJcgj sjJcgj = new SjJcgj();
        sjJcgj.setSjId(id);
        List<SjJcgj> sjJcgjList = sjJcgjService.queryList(sjJcgj);

        SjTz sjTz = new SjTz();
        sjTz.setSjId(id);
        List<SjTz> sjTzList = sjTzService.queryList(sjTz);
        return AjaxResult.success(localFilePrefix + PdfExportUtil.export(
                "com.hxhq.business.utils.pdf.resource.SjPdf",
                "exportDetail",
                sjService.getInfo(id),
                sjTzList,
                sjJcgjList,
                lang,
                localFilePath));
    }


    /**
     * 台账列表
     */
    @GetMapping("/tzList")
    @RequiresPermissions(value={"business:resource:sj:xq", "business:studySj:xq", "business:archive:sj:xq"}, logical= Logical.OR)
    public TableDataInfo tzList(SjTz form)
    {
        startPage();
        List<SjTz> list = sjTzService.queryList(form);
        return getDataTable(list);
    }

    /**
     * 查询试剂列表
     */
    @GetMapping("/list")
    @RequiresPermissions(value = {"business:resource:sj:list"}, logical= Logical.OR)
    public TableDataInfo list(SjSearchListForm form)
    {
        startPage();
        List<SjListDto> list = sjService.queryList(form);
        return getDataTable(list);
    }

    /**
     * 获取试剂详细信息
     */
    @GetMapping(value = "/info")
    @RequiresPermissions(value={"business:resource:sj:xq", "business:studySj:xq", "business:archive:sj:xq"}, logical= Logical.OR)
    public AjaxResult getInfo(Long id)
    {
        return AjaxResult.success(sjService.getInfo(id));
    }

    /**
     * 编辑试剂信息
     */
    @PostMapping("/bj")
    @RequiresPermissions(value={"business:resource:sj:bj", "business:studySj:bj"}, logical= Logical.OR)
    public AjaxResult save(@RequestBody SjBjForm form)
    {
        form.setQmrId(SecurityUtils.getUserId());
        sjService.bj(form);
        return AjaxResult.success("操作成功");
    }

    /**
     * 修改库存
     */
    @PostMapping("xgkc")
    @RequiresPermissions(value={"business:resource:sj:xgkc", "business:studySj:xgkc"}, logical= Logical.OR)
    public AjaxResult xgkc(@RequestBody @Validated SjXgkcForm form)
    {
        form.setQmrId(SecurityUtils.getUserId());
        sjService.xgkc(form);
        return AjaxResult.success("操作成功");
    }

    /** 处置 */
    @PostMapping("/cz")
    @RequiresPermissions(value={"business:resource:sj:cz", "business:studySj:cz"}, logical= Logical.OR)
    public AjaxResult cz(@RequestBody @Validated CzSjForm form)
    {
        form.setQmrId(SecurityUtils.getUserId());
        sjService.cz(form);
        return AjaxResult.success("操作成功");
    }

    /** 批量处置 */
    @PostMapping("/plcz")
    @RequiresPermissions(value={"business:resource:sj:cz", "business:studySj:cz"}, logical= Logical.OR)
    public AjaxResult plcz(@RequestBody @Validated PlczSjForm form)
    {
        form.setQmrId(SecurityUtils.getUserId());
        sjService.plcz(form);
        return AjaxResult.success("操作成功");
    }

    /** 锁定 */
    @PostMapping("/sd")
    @RequiresPermissions("business:resource:sj:sd")
    public AjaxResult sd(@RequestBody @Validated SdSjForm form)
    {
        form.setQmrId(SecurityUtils.getUserId());
        sjService.sd(form);
        return AjaxResult.success("操作成功");
    }

    /** 批量锁定 */
    @PostMapping("/plsd")
    @RequiresPermissions("business:resource:sj:sd")
    public AjaxResult plsd(@RequestBody @Validated SdSjForm form)
    {
        if(form.getIds() == null || form.getIds().length == 0) {
            return error("请选择锁定对象");
        }
        form.setQmrId(SecurityUtils.getUserId());
        sjService.plsd(form);
        return AjaxResult.success("操作成功");
    }

    /** 解锁 */
    @PostMapping("/js")
    @RequiresPermissions("business:resource:sj:js")
    public AjaxResult js(@RequestBody @Validated JsSjForm form)
    {
        form.setQmrId(SecurityUtils.getUserId());
        sjService.js(form);
        return AjaxResult.success("操作成功");
    }

    /**
     * 借阅
     */
    @PostMapping("/jy")
    @RequiresPermissions("business:resource:sj:jy")
    public AjaxResult jy(@RequestBody @Validated JySjForm form)
    {
        form.setQmrId(SecurityUtils.getUserId());
        sjService.jy(form);
        return AjaxResult.success("操作成功");
    }

    /**
     * 解档
     */
    @PostMapping("/jd")
    @RequiresPermissions("business:resource:sj:jd")
    public AjaxResult jd(@RequestBody @Validated JdSjForm form)
    {
        form.setQmrId(SecurityUtils.getUserId());
        sjService.jd(form);
        return AjaxResult.success("操作成功");
    }

    /**
     * 批量归档
     */
    @PostMapping("/plgd")
    @RequiresPermissions("business:resource:sj:gd")
    public AjaxResult plgd(@RequestBody GdSjForm form)
    {
        form.setQmrId(SecurityUtils.getUserId());
        sjService.plgd(form);
        return AjaxResult.success("操作成功");
    }

    /**
     * 归档
     */
    @PostMapping("/gd")
    @RequiresPermissions("business:resource:sj:gd")
    public AjaxResult gd(@RequestBody @Validated GdSjForm form)
    {
        form.setQmrId(SecurityUtils.getUserId());
        sjService.gd(form);
        return AjaxResult.success("操作成功");
    }
}
