package digital.laboratory.platform.inspection.controller;

import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import digital.laboratory.platform.common.core.util.R;
import digital.laboratory.platform.inspection.dto.TestRecordArgumentDto;
import digital.laboratory.platform.inspection.entity.TestRecordMethod;
import digital.laboratory.platform.inspection.entity.TestRecordReagent;
import digital.laboratory.platform.inspection.service.TestRecordMethodService;
import digital.laboratory.platform.inspection.service.TestRecordReagentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author xy
 * @version 1.0
 * @title TestRecordReagent
 * @description 实验用到的试剂耗材，标准物质
 * @create 2023/12/20 10:07
 */
@RestController
@RequestMapping("/testRecord_reagent")
@Api(tags = "09-检验记录-使用的试剂耗材，标准物质管理", description = "检验记录-使用的试剂耗材，标准物质管理")
public class TestRecordReagentController {
    @Resource
    private TestRecordReagentService testRecordReagentService;

    //添加
    @PostMapping("/addReagent")
    @ApiOperation(value = "添加实验使用的试剂耗材、标准物质", notes = "添加实验使用的试剂耗材、标准物质")
    public R addReagent(@RequestBody TestRecordReagent testRecordReagent) {
        TestRecordReagent ret = testRecordReagentService.addTestRecordReagent(testRecordReagent);
        if (ret != null) {
            return R.ok(ret, "添加数据成功");
        } else {
            return R.failed("false", "添加数据失败");
        }
    }

    //修改接口
    @PutMapping("/updateReagent")
    @ApiOperation(value = "修改实验使用试剂耗材、标准物质", notes = "")
    public R updateReagent(@RequestBody TestRecordReagent testRecordReagent) {
        try {
            TestRecordReagent ret = testRecordReagentService.updateTestRecordReagent(testRecordReagent);
            if (ret != null) {
                return R.ok(ret, "添加成功");
            } else {
                return R.failed("false", "添加失败");
            }
        } catch (Exception e) {
            return R.failed(e.getMessage());
        }
    }

    //删除数据
    @DeleteMapping("/deleteReagent")
    @ApiOperation(value = "删除实验使用试剂耗材、标准物质", notes = "")
    public R deleteReagent(String id) {
        Assert.notBlank(id, "参数id不能为空");
        boolean ret = testRecordReagentService.deleteTestRecordReagent(id);
        if (ret) {
            return R.ok("true", "删除成功");
        } else {
            return R.failed("false", "删除成功");
        }
    }

    //显示列表分页
    @GetMapping("/getReagentPageList")
    @ApiOperation(value = "获取实验中试剂耗材分页数据", notes = "参数:" + "\n" + "1.testId：实验ID" + "\n" + "2.keywords：查询参数" + "\n" + "3.category：试剂耗材类别（传入试剂耗材/标准物质二选一即可）" + "\n" + "4.opCode:传入1或-1（1是查询所有（去重），-1是查询这个实验下的所有）")
    public R getReagentPageList(Page page, String testId, String keywords, String category, Integer opCode) {
        return R.ok(testRecordReagentService.getTestRecordReagentPageList(page, testId, keywords, category, opCode), "获取数据成功");
    }

    //显示列表
    @GetMapping("/getReagentList")
    @ApiOperation(value = "获取实验中试剂耗材列表数据", notes = "获取实验中试剂耗材列表数据")
    public R getReagentList(String testId, String category) {
        return R.ok(testRecordReagentService.getTestRecordReagentList(testId, category), "获取数据成功");
    }

    @PutMapping("/useReagent")
    @ApiOperation(value = "添加或移除实验过程中的试剂耗材", notes = "testRecordId:实验ID、reagentId：试剂耗材ID、opCode：1使用，-1取消使用")
    public R useTestRecordReagent(@RequestBody TestRecordArgumentDto testRecordArgumentDto) {
        try {
            return testRecordReagentService.useTestRecordReagent(testRecordArgumentDto) ? R.ok("添加成功!") : R.failed("添加失败!");
        } catch (Exception e) {
            e.printStackTrace();
            return R.failed(e.getMessage());
        }
    }

    @PutMapping("/useReagentToTemplate")
    @ApiOperation(value = "添加或移除模板中的试剂耗材", notes = "testRecordId:实验ID、reagentId：试剂耗材ID、opCode：1使用，-1取消使用")
    public R useTestTemplateReagent(@RequestBody TestRecordArgumentDto testRecordArgumentDto) {
        return testRecordReagentService.useTestTemplateReagent(testRecordArgumentDto) ? R.ok("添加成功!") : R.failed("添加失败!");
    }

    @GetMapping("/getReagentToTemplatePage")
    @ApiOperation(value = "获取模板中试剂耗材分页数据", notes = "参数:" + "\n" + "1.templateId：模板ID" + "\n" + "2.keywords：查询参数" + "\n" + "3.category：试剂耗材类别（传入试剂耗材/标准物质二选一即可）" + "\n" + "4.opCode:传入1或-1（1是查询所有（去重），-1是查询这个模板下的所有）")
    public R getReagentToTemplatePageList(Page page, String templateId, String keywords, String category, Integer opCode) {
        return R.ok(testRecordReagentService.getTestTemplateReagentPageList(page, templateId, keywords, category, opCode), "获取数据成功");
    }
}
