package digital.laboratory.platform.entrustment.controller;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import digital.laboratory.platform.common.core.exception.CheckedException;
import digital.laboratory.platform.common.core.util.R;
import digital.laboratory.platform.common.mybatis.security.service.DLPUser;
import digital.laboratory.platform.common.security.util.SecurityUtils;
import digital.laboratory.platform.entrustment.convert.EntrustMaterialCheckoutResultConvert;
import digital.laboratory.platform.entrustment.dto.EntrustMaterialCheckoutResultDTO;
import digital.laboratory.platform.entrustment.dto.GenerateQuarterlyReportDTO;
import digital.laboratory.platform.entrustment.dto.ResultExcelDTO;
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 digital.laboratory.platform.entrustment.vo.EntrustmentIdentificationMaterialVO;
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 javax.validation.Valid;
import java.io.IOException;
import java.sql.Array;
import java.sql.SQLException;
import java.time.LocalDateTime;
import java.util.ArrayList;
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 @Valid 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("查询所有检出检材的毒品种类清单，并按名称进行排序")
    @GetMapping("/getResultType")
    public R getResultType(Integer year, String orgId) {
        return R.ok(entrustMaterialCheckoutResultService.getResultType(year, orgId), "查询成功!");
    }

    @ApiOperation("查询所有毒品的检出率")
    @GetMapping("/getDetectionRate")
    public R getDetectionRateByMaterial(Integer year, String orgId) {
        return R.ok(entrustMaterialCheckoutResultService.getDetectionRateByMaterial(year, orgId), "查询成功!");
    }

    @ApiOperation("根据毒品名称来查询所有检出该毒品的检材列表")
    @GetMapping("/getMaterialByDetection")
    public R<IPage> getMaterialByDetection(String drugName, Integer year, Page page) {
        return R.ok(entrustMaterialCheckoutResultService.getMaterialByDetection(drugName, year, page));
    }

    @ApiOperation("导出Excel")
    @PostMapping("/getExcelByResult")
    public R getResultData(HttpServletResponse response, @RequestBody ResultExcelDTO excelDTO) throws IOException {
        return entrustMaterialCheckoutResultService.getExcelByResult(response, excelDTO);
    }

    @GetMapping("/pushSuspectDetectionResult")
    @ApiOperation("推送检出结果")
    public R pushSuspectDetectionResult(String entrustId) throws SQLException {
        DLPUser dlpUser = SecurityUtils.getUser();
        return R.ok(entrustMaterialCheckoutResultService.pushSuspectDetectionResult(entrustId, dlpUser));
    }

    @GetMapping("/pushSuspectDetectionResultTask")
    @ApiOperation("推送历史检出结果")
    public R pushSuspectDetectionResultTask() {
        DLPUser dlpUser = SecurityUtils.getUser();
        return R.ok(entrustMaterialCheckoutResultService.pushSuspectDetectionResultTask(dlpUser));
    }


    @PostMapping("/generateQuarterlyReportExcel")
    @ApiOperation("生成季度报告")
    public R generateQuarterlyReportExcel(@RequestBody GenerateQuarterlyReportDTO dto, HttpServletResponse response) throws IOException {
        if (dto == null || dto.getQuarterlyList() == null || dto.getQuarterlyList().size() == 0 || dto.getYear() == 0) {
            return R.failed("请输入正确的参数");
        }
        String filePath = entrustMaterialCheckoutResultService.generateQuarterlyReportExcel(dto, response);
        return R.ok(filePath, "导出成功!");
    }
}
