package digital.laboratory.platform.inspection.controller;

import digital.laboratory.platform.common.core.util.R;
import digital.laboratory.platform.inspection.dto.ResetSampleInjectorDTO;
import digital.laboratory.platform.inspection.entity.SampleInjector;
import digital.laboratory.platform.inspection.service.SampleInjectorService;
import digital.laboratory.platform.inspection.vo.TestRecordSolutionVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/sampleInjector")
@Api(tags = "14-进样器信息相关接口管理", description = "14-进样器信息相关接口管理")
public class SampleInjectorController {

    @Resource
    private SampleInjectorService sampleInjectorService;

    @GetMapping
    @ApiOperation(value = "根据实验id获取进样信息", notes = "根据实验id获取进样信息")
    @ApiImplicitParam(name = "testId", value = "实验id", required = true)
    public R<SampleInjector> getByTestId(@RequestParam("testId") String testId) {
        SampleInjector one = null;
        try {
            one = sampleInjectorService.getByTestId(testId);
        } catch (Exception e) {
            e.printStackTrace();
            return R.failed("进样信息查询失败！");
        }
        return R.ok(one);
    }

    @GetMapping("/materialList")
    @ApiOperation(value = "检材列表", notes = "检材列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "testId", value = "实验id", required = true),
            @ApiImplicitParam(name = "key", value = "查询数据字段， 仅支持编号查询", required = false)
    })
    public R<List<TestRecordSolutionVO>> queryMaterialList(@RequestParam("testId") String testId,
                                                           @RequestParam(value = "key", required = false) String key) {
        List<TestRecordSolutionVO> resultList = null;
        try {
            resultList = sampleInjectorService.queryMaterialList(testId, key);
        } catch (Exception e) {
            e.printStackTrace();
            return R.failed("查询列表失败！");
        }
        return R.ok(resultList);
    }

    @PostMapping("/saveSampleInjector")
    @ApiOperation(value = "保存进样信息", notes = "保存进样信息，进样位置信息使用json存储，直方图(方盘有4个json数组，外层是个大数组)： [\n" +
            "              [{ sampleNo: '', injectorCount: '', name: 'A1', value: ['A', '1'], active: false, frameNumber: '' },\n" +
            "              { sampleNo: '', injectorCount: '', name: 'A2', value: ['A', '2'], active: false, frameNumber: '' }]," +
            "              [{ sampleNo: '', injectorCount: '', name: 'A1', value: ['A', '1'], active: false, frameNumber: '' },\\n\" +\n" +
            "              { sampleNo: '', injectorCount: '', name: 'A2', value: ['A', '2'], active: false, frameNumber: '' }]],\n" +
            "               圆盘：[\n" +
            "    {\n" +
            "        \"sampleNo\": ''," +
            "        \"injectorCount\": ''," +
            "        \"frameNumber\": ''," +
            "        \"index\": 1,\n" +
            "        \"active\": false,\n" +
            "        \"title\": 1\n" +
            "    },\n" +
            "    {\n" +
            "        \"sampleNo\": ''," +
            "        \"injectorCount\": ''," +
            "        \"frameNumber\": ''," +
            "        \"index\": 2,\n" +
            "        \"active\": false,\n" +
            "        \"title\": 6\n" +
            "    }]; sampleNo 溶液编号、injectorCount 进样次数、frameNumber 架号")
    public R<SampleInjector> saveSampleInjector(@Valid @RequestBody SampleInjector sampleInjector) {
        SampleInjector resultInfo = null;
        try {
            resultInfo = sampleInjectorService.saveSampleInjector(sampleInjector);
        } catch (Exception e) {
            e.printStackTrace();
            if (e instanceof RuntimeException) {
                return R.failed(e.getMessage());
            }
            return R.failed("保存失败！");
        }
        return R.ok(resultInfo);
    }

    @PutMapping("/reset")
    @ApiOperation(value = "重置进样信息", notes = "重置进样信息")
    public R<SampleInjector> reset(@Valid @RequestBody ResetSampleInjectorDTO dto) {
        SampleInjector resultInfo = null;
        try {
            resultInfo = sampleInjectorService.reset(dto);
        } catch (Exception e) {
            e.printStackTrace();
            return R.failed("重置失败！");
        }
        return R.ok(resultInfo);
    }

    @GetMapping("/export")
    @ApiOperation(value = "进样信息导出", notes = "根据实验id查询对应的进样信息导出")
    @ApiImplicitParam(name = "id", value = "进样信息的id",  required = true)
    public void exportSampleInjectorInfoToExcel(@RequestParam("id") String id, HttpServletResponse response) {
        try {
            sampleInjectorService.exportSampleInjectorInfoToExcel(id, response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
