package digital.laboratory.platform.inspection.controller;

import cn.hutool.core.util.StrUtil;
import digital.laboratory.platform.common.core.util.R;
import digital.laboratory.platform.inspection.enums.TestRecordFileUrl;
import digital.laboratory.platform.inspection.dto.TestRecordArgumentDto;
import digital.laboratory.platform.inspection.entity.TestRecordInstrumentCondition;
import digital.laboratory.platform.inspection.service.TestRecordInstrumentConditionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/testRecord_instrumentCondition")
@Api(tags = "12-检验记录-仪器的使用条件", description = "检验记录-仪器的使用条件")
public class TestRecordInstrumentConditionController {

    @Resource
    private TestRecordInstrumentConditionService testRecordInstrumentConditionService;


    @ApiOperation(value = "添加实验仪器条件", notes = "添加实验仪器条件")
    @PostMapping("/addInstrumentCondition")
    public R addInstrumentCondition(@RequestBody TestRecordInstrumentCondition testRecordInstrumentCondition) {
        TestRecordInstrumentCondition instrumentCondition = testRecordInstrumentConditionService.addInstrumentCondition(testRecordInstrumentCondition);
        return instrumentCondition != null ? R.ok(instrumentCondition, "添加成功!") : R.failed("添加失败!");
    }

    @ApiOperation(value = "选择不同的模板类型，为实验创建仪器条件模板", notes = "type 1：(生物样本模板)，-1 ：(缴获物样本模板)")
    @PostMapping("/createWord")
    public R createConditionWordByTest(String testId) throws Exception {
        boolean ret = testRecordInstrumentConditionService.createConditionWordByTest(testId);
        return ret ? R.ok(TestRecordFileUrl.TEST_RECORD_CATALOGUE.getFileUrl() + "/" + testId + "/" + "仪器条件.docx", "创建成功!") : R.failed("创建失败!");
    }

    @ApiOperation(value = "选择不同的模板类型，为模板创建仪器条件模板", notes = "type 1：(生物样本模板)，-1 ：(缴获物样本模板)")
    @PostMapping("/createWordTem")
    public R createConditionWordByTemplate(String templateId) throws Exception {
        boolean ret = testRecordInstrumentConditionService.createConditionWordByTemplate(templateId);
        return ret ? R.ok(TestRecordFileUrl.TEST_RECORD_CATALOGUE.getFileUrl() + "/" + templateId + "/" + "仪器条件.docx", "创建成功!") : R.failed("创建失败!");
    }

    @ApiOperation(value = "修改实验仪器条件", notes = "修改实验仪器条件")
    @PostMapping("/updateInstrumentCondition")
    public R updateInstrumentCondition(@RequestBody TestRecordInstrumentCondition testRecordInstrumentCondition) {
        TestRecordInstrumentCondition instrumentCondition = testRecordInstrumentConditionService.updateInstrumentCondition(testRecordInstrumentCondition);
        return instrumentCondition != null ? R.ok(instrumentCondition, "修改成功!") : R.failed("修改失败!");
    }

    @ApiOperation(value = "删除实验仪器条件", notes = "删除实验仪器条件")
    @DeleteMapping("/delInstrumentCondition")
    public R delInstrumentCondition(String id) {
        return testRecordInstrumentConditionService.delInstrumentCondition(id) ? R.ok("删除成功!") : R.failed("删除失败!");
    }

    @ApiOperation(value = "通过ID查询仪器条件")
    @GetMapping("/getConditionById")
    public R getInstrumentConditionById(String testId) {
        return R.ok(testRecordInstrumentConditionService.getInstrumentConditionByTestId(testId));
    }


    @ApiOperation(value = "为模板添加或移除仪器条件", notes = "为模板添加或移除仪器条件")
    @PutMapping("/useConditionToTemplate")
    public R useInstrumentConditionToTemplate(@RequestBody TestRecordArgumentDto argumentDto) {
        return testRecordInstrumentConditionService.useInstrumentConditionToTemplate(argumentDto) ? R.ok("添加成功!") : R.failed("添加失败!");
    }

    @PutMapping("/merge")
    public R mergeFileByBiologicalSample(String testId) throws Exception {
        return testRecordInstrumentConditionService.mergeFile(testId) ? R.ok("合并成功!") : R.failed("合并失败!");
    }

    @GetMapping("/generatedOrNot")
    @ApiOperation(value = "判断是否创建了仪器条件模板", notes = "判断是否创建了仪器条件模板")
    public R generatedOrNot(String testId) {
        String filePath = testRecordInstrumentConditionService.generatedOrNot(testId);
        return StrUtil.isNotBlank(filePath) ? R.ok(filePath) : R.failed("没有创建好仪器条件模板!");
    }
}
