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.common.mybatis.security.service.DLPUser;
import digital.laboratory.platform.inspection.dto.TestRecordArgumentDto;
import digital.laboratory.platform.inspetion.api.entity.SampleInfo;
import digital.laboratory.platform.inspection.service.SampleInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.oauth2.provider.OAuth2Authentication;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.security.Principal;

/*
 *@title TaskInfoController
 *@description
 *@author xy
 *@version 1.0
 *@create 2023/12/8 11:24
 */
@RestController
@RequestMapping("/sampleInfo")
@Api(tags = "04-样本基本信息管理", description = "样本基本信息管理")
public class SampleInfoController {
    @Resource
    private SampleInfoService sampleInfoService;

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

    }

    //修改接口
    @PostMapping("/updateSampleInfo")
    @ApiOperation(value = "修改任务基本信息", notes = "")
    public R updateSampleInfo(@RequestBody SampleInfo sampleInfo) {
        SampleInfo ret = sampleInfoService.updateSampleInfo(sampleInfo);
        if (ret != null) {
            return R.ok(ret, "添加成功");
        } else {
            return R.failed("false", "添加失败");
        }
    }

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

    //显示列表分页
    @GetMapping("/getSampleInfoPageList")
    @ApiOperation(value = "获取分页数据", notes = "")
    public R getSampleInfoPageList(Page page, SampleInfo sampleInfo) {
        return R.ok(sampleInfoService.getSampleInfoPageList(page, sampleInfo), "获取数据成功");
    }

    //显示列表
    @GetMapping("/getSampleInfoList")
    @ApiOperation(value = "获取列表", notes = "")
    public R getSampleInfoList(SampleInfo sampleInfo) {
        return R.ok(sampleInfoService.getSampleInfoList(sampleInfo), "获取数据成功");
    }

    @GetMapping("/getPageForTestRecord")
    @ApiOperation(value = "在实验阶段获取该用户获得分配的检材列表", notes = "在实验阶段获取该用户获得分配的检材列表")
    public R getPageForTestRecord(Page page, String testId, String keywords, HttpServletRequest httpServletRequest) {
        Principal principal = httpServletRequest.getUserPrincipal();
        DLPUser dlpUser = (DLPUser) ((OAuth2Authentication) principal).getUserAuthentication().getPrincipal();
        return R.ok(sampleInfoService.getPageForTestRecord(page, dlpUser, testId,keywords), "获取数据成功");
    }


    @PutMapping("/useSample")
    @ApiOperation(value = "在实验阶段使用检材或取消使用检材", notes = "在实验阶段使用检材或取消使用检材")
    public R useTestRecordSample(@RequestBody TestRecordArgumentDto argumentDto) {
        return sampleInfoService.useTestRecordSample(argumentDto) ? R.ok("操作成功!") : R.failed("操作失败!");
    }

    @GetMapping("/checkRepeatNo")
    @ApiOperation(value = "检查是否有重复编号", notes = "检查是否有重复编号")
    public R checkRepeatNo(String acceptNo,String id) {
        return sampleInfoService.checkRepeatNo(acceptNo,id) ? R.ok(true,"操作成功!") : R.failed(false,"操作失败!");
    }
}
