package com.bonus.boot.manager.basic.controller;

import com.alibaba.fastjson.JSONObject;
import com.bonus.boot.manager.basic.entity.PersonnelLibraryBean;
import com.bonus.boot.manager.basic.entity.PersonnelReservationBean;
import com.bonus.boot.manager.basic.service.ImportExcelService;
import com.bonus.boot.manager.manager.annotation.LogAnnotation;
import com.bonus.boot.manager.manager.utils.AjaxRes;
import com.bonus.boot.manager.manager.utils.ImportNoPhotoExcelHelper;
import com.bonus.boot.manager.manager.utils.StringHelper;
import com.bonus.boot.manager.manager.utils.UserUtil;
import com.bonus.boot.manager.physical.entity.NegativeResultBean;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
/**
 * 导入层面的处理
 * @author lzh
 * @Data 2023/6/19 15:30
 */
@RestController
@RequestMapping("export")
public class importExcelController {
    /**
     * 服务对象
     */
    @Resource
    private ImportExcelService service;
    @LogAnnotation
    @PostMapping("/CommonImport")
    @ApiOperation(value = "体检人员库导入")
    public AjaxRes workerInfoImport(@RequestParam("file") MultipartFile file, HttpServletRequest request, HttpServletResponse response) throws IOException {
        int result = 1;
        AjaxRes ar = new AjaxRes();
        try {
            List<JSONObject> lstObj = (List<JSONObject>) ImportNoPhotoExcelHelper.readExcel(file, PersonnelLibraryBean.class);
            String code = service.saveExcelInfo(lstObj);
            if ("导入成功".equals(code)){
                ar.setSucceedMsg("导入成功");
            }else{
                ar.setFailMsg(code);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ar;
    }


    @LogAnnotation
    @PostMapping("/negativeResult")
    @ApiOperation(value = "体检数据统计")
    public String negativeResult(@RequestParam("file") MultipartFile file, HttpServletRequest request, HttpServletResponse response) throws IOException {
        String hospitalId = UserUtil.getLoginUser().getHospitalId();
        if(StringHelper.isEmpty(hospitalId)){
            return "当前操作人员不是医院所属账号，请使用医院人员账号进行操作！";
        }
        try {
            List<JSONObject> lstObj = (List<JSONObject>) ImportNoPhotoExcelHelper.readExcel(file, NegativeResultBean.class);
            System.err.println("lstObj="+lstObj.size());
            return service.savenegativeResult(lstObj);
        } catch (Exception e) {
            e.printStackTrace();
            return "导入出错！";
        }
    }


    @LogAnnotation
    @PostMapping("/reservation")
    @ApiOperation(value = "体检人员库导入")
    public AjaxRes reservation(@RequestParam("file") MultipartFile file, HttpServletRequest request, HttpServletResponse response) throws IOException {
        int result = 1;
        AjaxRes ar = new AjaxRes();
        try {
            List<JSONObject> lstObj = (List<JSONObject>) ImportNoPhotoExcelHelper.readExcel(file, PersonnelReservationBean.class);
            String code = service.reservation(lstObj);
            if ("导入成功".equals(code)){
                ar.setSucceedMsg("导入成功");
            }else{
                ar.setFailMsg(code);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ar;
    }

  }
