package com.docus.server.collection.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.write.builder.ExcelWriterBuilder;
import com.docus.core.util.DateUtil;
import com.docus.core.util.Func;
import com.docus.infrastructure.core.exception.BaseException;
import com.docus.infrastructure.util.easyexcel.ExcelUtil;
import com.docus.infrastructure.web.api.CommonResult;
import com.docus.infrastructure.web.api.ResultCode;
import com.docus.server.collection.converter.FirstPageCheckoutConverter;
import com.docus.server.collection.dto.FirstPageCheckoutInDTO;
import com.docus.server.collection.dto.FirstPageCheckoutRequest;
import com.docus.server.collection.dto.VJsjWzh7addnjreportDto;
import com.docus.server.collection.entity.VJsjWzh7addnjreport;
import com.docus.server.collection.entity.VJsjWzh7addnjreportVo;
import com.docus.server.collection.feign.service.MedicalrecordService;
import com.docus.server.collection.infrastructure.dao.sqlserver.VJsjWzh7addnjreportMapper;
import com.docus.server.collection.service.MzZyHisService;
import com.docus.server.collection.util.PersistRequestUtil;
import com.docus.server.collection.validator.RequestValidator;
import com.docus.server.collection.validator.ValidateResult;
import com.docus.server.collection.validator.impl.FirstPageCheckoutRequestValidator;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

@Api("梅州中西HIS相关接口")
@Slf4j
@RestController
@RequestMapping("/his")
public class MzZyHisController {
    @Resource
    private MzZyHisService mzZyHisService;

    @Resource
    private MedicalrecordService medicalrecordService;

    @Resource
    private VJsjWzh7addnjreportMapper vmp;

    @ApiOperation("首页签出")
    @PostMapping("/firstPageCheckout")
    public CommonResult<String> firstPageCheckout(@RequestBody FirstPageCheckoutRequest firstPageCheckoutRequest, HttpServletRequest servletRequest) {
        String method = servletRequest.getRequestURI().replace("/", "_").replace("\\", "_");
        log.info("首页签出接口参数：{}", Func.toJson(firstPageCheckoutRequest));
        // 验证数据
        RequestValidator validator = new FirstPageCheckoutRequestValidator(firstPageCheckoutRequest);
        ValidateResult validateResult = validator.validate();
        if (validateResult.getResultCode() == ResultCode.FAILED) {
            return CommonResult.failed(validateResult.getMsg());
        }
        // 转换数据，业务处理
        FirstPageCheckoutConverter converter = new FirstPageCheckoutConverter(firstPageCheckoutRequest);
        FirstPageCheckoutInDTO checkoutInDTO = converter.req2dto();
        try {
            mzZyHisService.firstPageCheckout(checkoutInDTO);
            return CommonResult.success("操作成功！");
        } catch (BaseException ex) {
            log.error(ex.getMessage(), ex);
            return CommonResult.failed(ex.getMessage());
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            PersistRequestUtil.persistRequest(method,Func.toJson(firstPageCheckoutRequest));
            return CommonResult.failed("系统出错啦，请稍后再试！");
        }
    }


    @ApiOperation("查询报告")
    @PostMapping("/getReport")
    public CommonResult<List<VJsjWzh7addnjreport>> getReport(@RequestBody VJsjWzh7addnjreportDto dto) {
        List<VJsjWzh7addnjreport> reports = vmp.getReport(dto);
        return CommonResult.success(reports);
    }
    @ApiOperation("内镜超期导出excel---按勾选")
    @PostMapping("/export")
    public void export(HttpServletResponse resp, @RequestBody List<VJsjWzh7addnjreportVo> vos) {

        OutputStream outputStream = ExcelUtil.getOutputStream("内镜超期报告.xls", resp);
        ExcelWriterBuilder write = EasyExcel.write(outputStream, VJsjWzh7addnjreportVo.class);
        write.sheet("sheet1").doWrite(vos);
    }

    @ApiOperation("内镜超期导出excel---按条件")
    @PostMapping("/exportAll")
    public void exportAll(HttpServletResponse resp,
                          @RequestBody VJsjWzh7addnjreportDto dto) {
        List<VJsjWzh7addnjreport> reports = vmp.getReport(dto);
        List<VJsjWzh7addnjreportVo> reportsVo=new ArrayList<VJsjWzh7addnjreportVo>();
        reports.forEach(report->{
            VJsjWzh7addnjreportVo vo=new VJsjWzh7addnjreportVo();
            vo.setDept(report.getDept());
            vo.setInpatientNo(report.getInpatientNo());
            vo.setItem(report.getItem());
            vo.setName(report.getName());
            vo.setRegistDateTime(DateUtil.format(report.getRegistDateTime(),"yyyy-MM-dd HH:mm:ss"));
            vo.setReportDateTime(DateUtil.format(report.getReportDateTime(),"yyyy-MM-dd HH:mm:ss"));
            vo.setType(report.getType());
            reportsVo.add(vo);
        });
        export(resp, reportsVo);
    }
    @ApiOperation("测试")
    @GetMapping("/test")
    public CommonResult<List> test() {
        return CommonResult.success(mzZyHisService.getPid());
    }

    @ApiOperation("测试sql")
    @GetMapping("/vmp")
    public CommonResult<List> vmp() {
        return CommonResult.success(vmp.getList());
    }
}
