package net.vtstar.zhongtong.avi.laboratory.controller;

import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import net.vtstar.user.search.controller.vo.SearchVo;
import net.vtstar.utils.asserts.ParamAssert;
import net.vtstar.utils.domain.Return;
import net.vtstar.zhongtong.avi.laboratory.domain.DailyItemModel;
import net.vtstar.zhongtong.avi.laboratory.domain.vo.CheckRecordVo;
import net.vtstar.zhongtong.avi.laboratory.service.DailyCheckService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

/**
 * @Author: xiadongwei
 * @Date: 2019/10/20 9:40
 */
@Slf4j
@RestController
@Api(description = "涂装化验室检测模块")
@RequestMapping("/api/check")
public class DailyCheckController {

    @Autowired
    DailyCheckService service;

    @ApiOperation(value = "根据化验室类型查询检测模板, 关联查询点检项模板", notes =
            "入参字段：\n{\n" +
            "  \"酸洗\": \"PICKLING\",\n" +
            "  \"电泳槽\": \"ELECTROPHORESIS\",\n" +
            "  \"前处理\": \"PRETREATMENT\"\n" +
            "}")
    @GetMapping("/getCheckByType")
    public Return getCheckByType(@RequestParam String type) {
        ParamAssert.notNull(type, "检测类型不能为空!");
        return Return.success(service.getCheckByType(type));
    }

    @ApiOperation("根据模板ID新增/编辑模板检测项")
    @PostMapping("/saveCheckItemModel")
    public Return saveCheckItemModel(@Valid @RequestBody DailyItemModel itemModel) {
        ParamAssert.notNull(itemModel, "点检项不能为空!");
        service.saveCheckItemModel(itemModel);
        return Return.success();
    }

    @ApiOperation("保存, 新增检测记录 和 点检项记录")
    @PostMapping("/addCheckRecord")
    public Return addCheckRecord(@Valid @RequestBody CheckRecordVo checkRecordVo) {
        ParamAssert.notNull(checkRecordVo, "检测记录不可为空!");
        service.addCheckRecord(checkRecordVo);
        return Return.success();
    }

    @ApiOperation("设备点检记录(酸洗)")
    @PostMapping("/getCheckRecord/pickling")
    public Return getCheckRecordPickling(@RequestBody SearchVo pageFilter) {
        return Return.success(new PageInfo<>(service.getCheckRecord("PICKLING", pageFilter.toJSONObject())));
    }

    @ApiOperation("设备点检记录(电泳槽)")
    @PostMapping("/getCheckRecord/electrophoresis")
    public Return getCheckRecordElectrophoresis(@RequestBody SearchVo pageFilter) {
        return Return.success(new PageInfo<>(service.getCheckRecord("ELECTROPHORESIS", pageFilter.toJSONObject())));
    }

    @ApiOperation("设备点检记录(前处理)")
    @PostMapping("/getCheckRecord/pretreatment")
    public Return getCheckRecordPretreatment(@RequestBody SearchVo pageFilter) {
        return Return.success(new PageInfo<>(service.getCheckRecord("PRETREATMENT", pageFilter.toJSONObject())));
    }

    @ApiOperation("根据检测记录ID 查询对应的检测报告")
    @GetMapping("/getCheckRecordDetailsByID")
    public Return getCheckRecordDetailsByID(@RequestParam("id") Long checkId) {

        ParamAssert.notNull(checkId, "checkId is null");
        return Return.success(service.getCheckRecordDetailsByID(checkId));
    }

    @ApiOperation("删除检测项模板")
    @DeleteMapping("/deleteCheckItemModelById/{id}")
    public Return deleteCheckItemModelById(@PathVariable("id") Long id) {
        ParamAssert.notNull(id, "id is null");
        service.deleteCheckItemModelById(id);
        return Return.success();
    }

    @ApiOperation("打印")
    @GetMapping("/expert")
    public void expert(@RequestParam("recordId") Long recordId, HttpServletResponse response){
        service.expert(recordId, response);
    }
}
