package com.hxhq.business.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.hxhq.business.domain.*;
import com.hxhq.business.dto.gsp.GspListDto;
import com.hxhq.business.dto.sj.SjListDto;
import com.hxhq.business.enums.zykgl.DaztEnum;
import com.hxhq.business.enums.zykgl.JyztEnum;
import com.hxhq.business.enums.zykgl.ZjztEnum;
import com.hxhq.business.form.gsp.GspGdForm;
import com.hxhq.business.form.gsp.GspJyForm;
import com.hxhq.business.form.gsp.GspSearchListForm;
import com.hxhq.business.form.mjy.GdForm;
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.common.security.annotation.RequiresPermissions;
import com.hxhq.common.security.utils.SecurityUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
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;


    /**
     * 开始配置
     * @return
     */
    @PostMapping("/startConfiguration")
    public AjaxResult startConfiguration(@RequestBody SjBjForm form) {
        Sj sj = sjService.getSjByBh(form.getBh());
        if(sj == null) {
            sj = new Sj();
            sj.setPzrq(new Date());
            sjService.save(sj);
        } else {
            sj.setPzrq(new Date());
            sjService.updateById(sj);
        }

        return success();
    }

    /**
     * 配置完成
     * @param form
     * @return
     */
    @PostMapping("/configurationCompleted")
    public AjaxResult configurationCompleted(@RequestBody SjBjForm form) {
        Sj sj = sjService.getSjByBh(form.getBh());
        if(sj == null) {
            sj = new Sj();
            BeanUtils.copyProperties(form, sj);
            sj.setZjzt(ZjztEnum.rk.getValue());
            sj.setJyzt(JyztEnum.wjy.getValue());
            sjService.save(sj);
        } else {
            BeanUtils.copyProperties(form, sj);
            sj.setZjzt(ZjztEnum.rk.getValue());
            sj.setJyzt(JyztEnum.wjy.getValue());
            sjService.updateById(sj);
        }

        return success();
    }

    /**
     * 配置完成
     * @param form
     * @return
     */
    @PostMapping("/subpackage")
    public AjaxResult subpackage(@RequestBody SjSubpackageForm form) {
        List<SjSubpackageForm.SjSubpackageItemForm> list = form.getList();
        List<Sj> sjList = new ArrayList<>();
        for (SjSubpackageForm.SjSubpackageItemForm sjSubpackageItemForm : list) {
            Sj sj = new Sj();
            sj.setParentBh(form.getBh());
            sj.setBh(sjSubpackageItemForm.getBh());
            sj.setKc(sjSubpackageItemForm.getKc());
            sj.setKcdw(sjSubpackageItemForm.getKcdw());
            sj.setStudyId(form.getStudyId());
            sj.setStudyFormId(form.getStudyFormId());
            sjList.add(sj);
        }
        sjService.saveBatch(sjList);
        return success();
    }


    /**
     * 试验物资列表
     */
    @GetMapping("/studyList")
    public TableDataInfo studyList(GspSearchListForm form)
    {
        startPage();
        List<SjListDto> list = sjService.queryStudyList(form);
        return getDataTable(list);
    }

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

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

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

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

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

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

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

    /** 批量处置 */
    @PostMapping("/plcz")
    @RequiresPermissions("business:resource:sj:cz")
    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("操作成功");
    }
}
