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.service.TestRecordMethodService;
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 TestRecordMethodController
 * @description
 * @create 2023/12/19 15:25
 */
@RestController
@RequestMapping("/testRecord_method")
@Api(tags = "08-检验记录-使用方法管理", description = "检验记录-使用方法管理")
public class TestRecordMethodController {
    @Resource
    private TestRecordMethodService testRecordMethodService;

    //添加
    @PostMapping("/addMethod")
    @ApiOperation(value = "添加实验使用方法", notes = "添加设备仪器")
    public R addMethod(@RequestBody TestRecordMethod testRecordMethod) {
        TestRecordMethod ret = testRecordMethodService.addTestRecordMethod(testRecordMethod);
        if (ret != null) {
            return R.ok(ret, "添加实验方法成功");
        } else {
            return R.failed("false", "添加实验方法失败");
        }
    }

    //修改接口
    @PostMapping("/updateMethod")
    @ApiOperation(value = "修改实验使用方法", notes = "")
    public R updateMethod(@RequestBody TestRecordMethod testRecordMethod) {
        TestRecordMethod ret = testRecordMethodService.updateTestRecordMethod(testRecordMethod);
        if (ret != null) {
            return R.ok(ret, "添加成功");
        } else {
            return R.failed("false", "添加失败");
        }
    }

    //删除数据
    @DeleteMapping("/deleteMethod")
    @ApiOperation(value = "删除实验使用方法", notes = "")
    public R deleteMethod(String id) {
        Assert.notBlank(id, "参数id不能为空");
        boolean ret = testRecordMethodService.deleteTestRecordMethod(id);
        if (ret) {
            return R.ok("true", "删除成功");
        } else {
            return R.failed("false", "删除成功");
        }
    }

    //显示列表分页
    @GetMapping("/getMethodPageList")
    @ApiOperation(value = "获取分页数据", notes = "")
    public R getMethodPageList(Page page, TestRecordMethod testRecordMethod) {
        return R.ok(testRecordMethodService.getTestRecordMethodPageList(page, testRecordMethod), "获取数据成功");
    }

    //显示列表
    @GetMapping("/getMethodList")
    @ApiOperation(value = "获取列表", notes = "")
    public R getMethodList(TestRecordMethod testRecordMethod) {
        return R.ok(testRecordMethodService.getTestRecordMethodList(testRecordMethod), "获取数据成功");
    }

    @PutMapping("/useMethod")
    @ApiOperation(value = "使用方法、取消使用方法", notes = "testRecordId:实验ID、methodId：方法ID、opCode：1使用，-1取消使用")
    public R useTestRecordMethod(@RequestBody TestRecordArgumentDto argumentDto) {
        return testRecordMethodService.useTestRecordMethod(argumentDto) ? R.ok("操作成功!") : R.failed("操作失败!");
    }

    @PutMapping("/useMethodToTemplate")
    @ApiOperation(value = "针对模板使用方法、取消使用方法", notes = "templateId:模板ID、methodId：方法ID、opCode：1使用，-1取消使用")
    public R useTemplateMethod(@RequestBody TestRecordArgumentDto argumentDto) {
        return testRecordMethodService.useTemplateMethod(argumentDto) ? R.ok("操作成功!") : R.failed("操作失败!");
    }
}
