package digital.laboratory.platform.inspection.controller;

import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import digital.laboratory.platform.common.core.util.R;
import digital.laboratory.platform.common.oss.service.OssFile;
import digital.laboratory.platform.inspetion.api.enums.BusinessType;
import digital.laboratory.platform.inspection.enums.TestRecordFileUrl;
import digital.laboratory.platform.inspection.dto.EntrustInfoDto;
import digital.laboratory.platform.inspetion.api.entity.EntrustInfo;
import digital.laboratory.platform.inspection.service.EntrustInfoService;
import digital.laboratory.platform.inspection.service.TestRecordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/*
 *@title EntrustInfoController
 *@description
 *@author xy
 *@version 1.0
 *@create 2023/12/7 15:31
 */
@RestController
@RequestMapping("/entrustInfo")
@Api(tags = "01-委托基本信息管理", description = "委托基本信息管理")
public class EntrustInfoController {
    @Resource
    private EntrustInfoService entrustInfoService;

    @Resource
    private TestRecordService testRecordService;

    @Resource
    private OssFile ossFile;

    //添加接口
    @PostMapping("/addEntrustInfo")
    @ApiOperation(value = "添加委托基本信息", notes = "添加委托基本信息,同时也作为其他系统调用的外部接口")
    public R addEntrustInfo(@RequestBody EntrustInfoDto entrustInfo) {
        EntrustInfo ret = entrustInfoService.addEntrustInfo(entrustInfo);
        if (ret != null) {
            return R.ok(ret, "添加成功");
        } else {
            return R.failed("false", "添加失败");
        }

    }

    //修改接口
    @PostMapping("/updateEntrustInfo")
    @ApiOperation(value = "修改委托基本信息", notes = "")
    public R updateEntrustInfo(@RequestBody EntrustInfo entrustInfo) {
        EntrustInfo ret = entrustInfoService.updateEntrustInfo(entrustInfo);
        if (ret != null) {
            return R.ok(ret, "修改成功");
        } else {
            return R.failed("false", "修改失败");
        }
    }

    //删除数据
    @DeleteMapping("/deleteEntrustInfo")
    @ApiOperation(value = "删除委托基本信息", notes = "")
    public R deleteEntrustInfo(String id) {
        Assert.notBlank(id, "参数id不能为空");
        boolean ret = entrustInfoService.deleteEntrustInfo(id);
        if (ret) {
            return R.ok("true", "删除成功");
        } else {
            return R.failed("false", "删除成功");
        }
    }

    //显示列表分页
    @GetMapping("/getEntrustPageList")
    @ApiOperation(value = "获取分页数据", notes = "")
    public R<IPage<EntrustInfo>> getEntrustPageList(Page page, Integer status, EntrustInfo entrustInfo, String keywords) {
        if (entrustInfo == null) {
            entrustInfo = new EntrustInfo();
        }
        return R.ok(entrustInfoService.getEntrustPageList(page, status, entrustInfo, keywords), "获取数据成功");
    }

    //显示列表
    @GetMapping("/getEntrustList")
    @ApiOperation(value = "获取列表", notes = "")
    public R getEntrustList(EntrustInfo entrustInfo) {
        if (entrustInfo == null) {
            entrustInfo = new EntrustInfo();
        }
        return R.ok(entrustInfoService.getEntrustList(entrustInfo), "获取数据成功");
    }

    //检查是否有重复编号
    @GetMapping("/checkRepeatNo")
    @ApiOperation(value = "检查是否有重复编号", notes = "检查是否有重复编号")
    public R checkRepeatNo(String acceptNo, String id) {
        return R.ok(entrustInfoService.checkRepeatNo(acceptNo, id));
    }

    @GetMapping("/createInspectionRecord")
    @ApiOperation(value = "生成检验记录", notes = "参数 :businessId、businessType:10001 或 10002")
    public R getPrintData(String businessId, String businessType) throws Exception {
        String fileName = "";
        if (businessType.equals(BusinessType.BOINT_CASE.getBusinessType())) {
            fileName = "生物样本检验记录.docx";
        } else {
            fileName = "缴获物检验记录.docx";
        }
        //判断是否生成了检验记录
        List<String> fileNameList = ossFile.fileList(TestRecordFileUrl.TEST_RECORD_CATALOGUE.getFileUrl() + "/" + businessId);
        boolean isCreate = fileNameList.contains(fileName);
        //如果生成了 就直接返回word地址
        if (isCreate) {
            return R.ok(TestRecordFileUrl.TEST_RECORD_CATALOGUE.getFileUrl() + "/" + businessId + "/" + fileName, "创建成功!");
        }
        //如果没有生成，那么现在创建检验记录
        boolean ret = testRecordService.createInspectionRecord(businessId);
        if (ret) {
            return R.ok(TestRecordFileUrl.TEST_RECORD_CATALOGUE.getFileUrl() + "/" + businessId + "/" + fileName, "创建成功!");

        } else {
            return R.failed("创建失败!");
        }
    }
}

