package digital.laboratory.platform.entrustment.controller;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import digital.laboratory.platform.common.core.exception.CheckedException;
import digital.laboratory.platform.common.core.util.R;
import digital.laboratory.platform.entrustment.convert.EntrustMaterialCheckoutResultConvert;
import digital.laboratory.platform.entrustment.dto.EntrustMaterialCheckoutResultDTO;
import digital.laboratory.platform.entrustment.query.BaseQuery;
import digital.laboratory.platform.entrustment.query.EntrustMaterialCheckoutResultQuery;
import digital.laboratory.platform.entrustment.service.EntrustMaterialCheckoutResultService;
import digital.laboratory.platform.entrustment.vo.EntrustMaterialCheckoutResultVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 委托检材--检出定性定量结果信息
 *
 * @author chenjiangbao
 * @describe 委托检材--检出定性定量结果信息相关接口 前端控制器
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/papp/checkoutResult")
@Api(tags = "017-委托检材--检出定性定量结果信息相关接口")
public class EntrustMaterialCheckoutResultController {

    @Resource
    private EntrustMaterialCheckoutResultService entrustMaterialCheckoutResultService;

    @ApiOperation("查询检出得定性定量结果")
    @GetMapping("/query")
    public R query(@RequestParam("id") String id) {
        return R.ok(
                EntrustMaterialCheckoutResultConvert.entityToVO(entrustMaterialCheckoutResultService.getById(id)),
                "查询检出得定性定量结果成功！"
        );
    }

    @ApiOperation("保存检出得定性定量结果")
    @PostMapping("/save")
    public R save(@RequestBody EntrustMaterialCheckoutResultDTO dto) {
        boolean success = false;
        try {
            success = entrustMaterialCheckoutResultService.save(dto);
        } catch (CheckedException e) {
            return R.failed(e.getMessage());
        }
        return success ? R.ok("保存检出得定性定量结果成功！") : R.ok("保存检出得定性定量结果失败！");
    }

    @ApiOperation("检出得定性定量结果分页查询")
    @GetMapping("/page")
    public R<IPage<EntrustMaterialCheckoutResultVO>> page(EntrustMaterialCheckoutResultQuery query) {
        IPage<EntrustMaterialCheckoutResultVO> voiPage;
        try {
            voiPage = entrustMaterialCheckoutResultService.voiPage(query);
        } catch (CheckedException e) {
            return R.failed(e.getMessage());
        }
        return R.ok(voiPage);
    }

    @ApiOperation("导出检材检出结果的excel表格")
    @PostMapping("/exportExcel")
    public void exportExcel(@RequestBody List<String> entrustIds, HttpServletResponse response) {
        if (CollUtil.isEmpty(entrustIds)) {
            throw new CheckedException("请选择要导出的委托检出结果信息！");
        }
        try {
            entrustMaterialCheckoutResultService.exportExcel(entrustIds, response);
        } catch (IOException e) {
            throw new CheckedException("导出excel失败！");
        }
    }

}
