package com.bonus.boot.manager.career.service;


import com.alibaba.fastjson.JSONObject;
import com.bonus.boot.manager.career.dao.CareerDao;
import com.bonus.boot.manager.career.entity.CareerBean;
import com.bonus.boot.manager.manager.dao.UtilDao;
import com.bonus.boot.manager.manager.utils.*;
import com.bonus.boot.manager.physical.entity.EmployeeHealthCheckBean;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

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

/**
 * APP登录记录表(AppLoginAudit)表服务实现类
 *
 * @author makejava
 * @since 2022-12-12 13:57:46
 */
@Service("careerService")
public class CareerServiceImpl implements CareerService {
    @Resource
    private CareerDao dao;
    @Resource
    private UtilDao utilDao;
    @Override
    public List<CareerBean> getList(CareerBean o) {
        return dao.getList(o);
    }

    @Override
    public String saveExcelInfo(HttpServletRequest request, MultipartFile file) {
        String res = "";
        try {
            CareerBean entity = new CareerBean();
            entity.setPhysicalId(request.getParameter("physicalId"));
            entity.setPersonName(request.getParameter("personName"));
            entity.setSex(request.getParameter("sex"));
            entity.setAge(request.getParameter("age"));
            entity.setUnit(request.getParameter("unit"));
            entity.setPostName(request.getParameter("postName"));
            entity.setHazards(request.getParameter("hazards"));
            entity.setHazardsNum(request.getParameter("hazardsNum"));
            entity.setPersonType(request.getParameter("personType"));
            entity.setCheckExceptionResult(request.getParameter("checkExceptionResult"));
            entity.setConclusion(request.getParameter("conclusion"));
            entity.setSuggest(request.getParameter("suggest"));

            List<JSONObject> lstObj = (List<JSONObject>) ImportNoPhotoExcelHelper.readExcel(file, CareerBean.class);
            List<CareerBean> list = new ArrayList<>();
            int i = 0;
            boolean active = false;
            String physicalId = "";
            String personName = "";
            String sex = "";
            String age = "";
            String unit = "";
            String postName = "";
            String hazards = "";
            String hazardsNum = "";
            String personType = "";
            String checkExceptionResult = "";
            String conclusion = "";
            String suggest = "";
            if (lstObj != null && lstObj.size() > 0) {
                for (JSONObject obj : lstObj) {
                    i++;
                    CareerBean bean = new CareerBean();
                    physicalId = String.valueOf(obj.getBigDecimal("physicalId"));
                    bean.setPhysicalId(physicalId.trim());
                    if (StringHelper.isEmpty(physicalId) ||  "0.0".equals(physicalId.trim())) {
                        res += "第" + i + "行体检编号为空;";
                        active = true;
                    }
                    personName = obj.getString("personName");
                    bean.setPersonName(personName.trim());
                    if (StringHelper.isEmpty(personName)) {
                        res += "第" + i + "行姓名为空;";
                        active = true;
                    }else if (utilDao.determineIfExists("pm_base_physical","phy_name",personName,"") == 0){
                        res += "第" + i + "行人员不在人员库;";
                        active = true;
                    }

                    sex = obj.getString("sex");
                    bean.setSex(sex.trim());
                    if (StringHelper.isEmpty(sex)) {
                        res += "第" + i + "行性别为空;";
                        active = true;
                    }
                    age = obj.getString("age");
                    bean.setAge(age.trim());
                    if (StringHelper.isEmpty(age)) {
                        res += "第" + i + "行年龄为空;";
                        active = true;
                    }
                    unit = obj.getString("unit");
                    bean.setUnit(unit.trim());
                    if (StringHelper.isEmpty(unit)) {
                        res += "第" + i + "行部门为空;";
                        active = true;
                    }
                    postName = obj.getString("postName");
                    bean.setPostName(postName.trim());
                    if (StringHelper.isEmpty(postName)) {
                        res += "第" + i + "行工种为空;";
                        active = true;
                    }
                    hazards = obj.getString("hazards");
                    bean.setHazards(hazards.trim());
                    if (StringHelper.isEmpty(hazards)) {
                        res += "第" + i + "行接害种类为空;";
                        active = true;
                    }
                    hazardsNum = obj.getString("hazardsNum");
                    bean.setHazardsNum(hazardsNum.trim());
                    if (StringHelper.isEmpty(hazardsNum)) {
                        res += "第" + i + "行接害工龄为空;";
                        active = true;
                    }
                    personType = obj.getString("personType");
                    bean.setPersonType(personType.trim());
                    if (StringHelper.isEmpty(personType)) {
                        res += "第" + i + "行体检类别为空;";
                        active = true;
                    }
                    checkExceptionResult = obj.getString("checkExceptionResult");
                    bean.setCheckExceptionResult(checkExceptionResult.trim());
                    if (StringHelper.isEmpty(checkExceptionResult)) {
                        res += "第" + i + "行检查所见异常结果为空;";
                        active = true;
                    }
                    conclusion = obj.getString("conclusion");
                    bean.setConclusion(conclusion.trim());
                    if (StringHelper.isEmpty(conclusion.trim())) {
                        res += "第" + i + "行结论为空;";
                        active = true;
                    }
                    suggest = obj.getString("suggest");
                    bean.setSuggest(suggest.trim());
                    if (StringHelper.isEmpty(suggest)) {
                        res += "第" + i + "行建议为空;";
                        active = true;
                    }
                    if(conclusion.indexOf("复查") != -1){
                        bean.setPhyStatus("复查");
                    }else {
                        bean.setPhyStatus("正常");
                    }
                    list.add(bean);
                }
            }else {
                return "数据为空";
            }
            if (active) {
                String[] arr = res.split(";");
                String code = "<pre>" +
                        "导入数据错误提示：<br/>";
                for (int j = 0; j < arr.length; j++) {
                    if (j == 0 || j % 2 == 0){
//                        code += arr[j] + ";\t\t";
                        if (arr[j].indexOf("检查所见异常结果") != -1){
                            code += arr[j] + ";\t";
                        }else if(arr[j].indexOf("姓名") != -1 || arr[j].indexOf("性别") != -1 || arr[j].indexOf("年龄") != -1 || arr[j].indexOf("部门") != -1
                                || arr[j].indexOf("工种") != -1 || arr[j].indexOf("结论") != -1 || arr[j].indexOf("姓名") != -1){
                                code += arr[j] + ";\t\t\t";
                        }else {
                            code += arr[j] + ";\t\t";
                        }

                    }else {
                        code += arr[j] + "<br/>";
                    }
                }
                return code+ "<pre>";
            }
            if (ListHelper.isNotEmpty(list)) {
                for (CareerBean vo : list) {
                    System.out.println("vo="+vo.toString());
                    dao.insertDate(vo);
                }
            }
        } catch (Exception e) {
            res = "导入失败";
            e.printStackTrace();
        }
        return "导入成功";
    }


    @Override
    public String saveExcelInfos(HttpServletRequest request, MultipartFile file) {
        String res = "";
        try {
            if (StringHelper.isEmpty(UserUtil.getLoginUser().getHospitalId())) {
                return "当前操作人员不是医院人员,请更换医院人员账号!";
            }
            CareerBean entity = new CareerBean();
            entity.setPersonName(request.getParameter("personName"));
            entity.setIdNumber(request.getParameter("idNumber"));
            entity.setPersonName(request.getParameter("physicalTime"));

            List<JSONObject> lstObj = (List<JSONObject>) ImportNoPhotoExcelHelper.readExcel(file, EmployeeHealthCheckBean.class);
            List<CareerBean> list = new ArrayList<>();
            int i = 0;
            boolean active = false;
            String personName = "";
            String idNumber = "";
            String physicalTime = "";
            List<CareerBean> allPhysicalPersonList = dao.getAllPhysicalPerson();
            if (lstObj != null && lstObj.size() > 0) {
                for (JSONObject obj : lstObj) {
                    i++;
                    /**
                     * 查询所有的体检库人员
                     */
                    List<CareerBean> allIdNumberList = dao.getAllIdNumber();

                    CareerBean bean = new CareerBean();
                    personName = obj.getString("personName");
                    bean.setPersonName(personName);
                    if (StringHelper.isEmpty(personName)) {
                        res += "第" + i + "行姓名为空;";
                        active = true;
                    }
                    idNumber = obj.getString("idNumber");
                    bean.setIdNumber(idNumber);

                    CareerBean compareBean = new CareerBean();
                    compareBean.setIdNumber(idNumber);
                    if (StringHelper.isEmpty(idNumber)) {
                        res += "第" + i + "行身份证号码为空;";
                        active = true;
                    } else if (!IdCardValidateUtil.checkIdCard(idNumber)) {
                        res += "第" + i + "行身份证号码有误;";
                        active = true;
                    } else if (!allIdNumberList.contains(compareBean)) {
                        res += "第" + i + "行该人员不在人员库;";
                        active = true;
                    }
                    physicalTime = obj.getString("physicalTime");
                    bean.setPhysicalTime(physicalTime);
                    if (StringHelper.isEmpty(physicalTime)) {
                        res += "第" + i + "行体检时间为空;";
                        active = true;
                    }else if (physicalTime.indexOf("1900") != -1){
                        res += "第" + i + "行体检时间格式错误;";
                        active = true;
                    }
                    list.add(bean);
                }
            }
            if (active) {
                String[] arr = res.split(";");
                String code = "<pre>" +
                        "导入数据错误提示：<br/>";
                for (int j = 0; j < arr.length; j++) {
                    if (j == 0 || j % 2 == 0) {
                        if (arr[j].indexOf("姓名") != -1) {
                            code += arr[j] + ";\t\t\t";
                        } else {
                            code += arr[j] + ";\t\t";
                        }
                    } else {
                        code += arr[j] + "<br/>";
                    }
                }
                return code + "<pre>";
            }
            if (ListHelper.isNotEmpty(list)) {
                List<CareerBean> noAppointList = new ArrayList();
                for (int j = 0; j < list.size(); j++) {
                    /**
                     * 查询是否有预约
                     */
                    int code = dao.getIsExistAppoint(list.get(j).getIdNumber());
                    if (code > 0){
                        //有预约
                        list.get(j).setPhysicalStatus("已预约已体检");
                        dao.updateAppointStatus(list.get(j).getIdNumber(), "1");
                        dao.updateAppointCancel(list.get(j).getIdNumber(), "3");
                    }else {
                        //无预约
                        list.get(j).setPhysicalStatus("未预约已体检");
                        noAppointList.add(list.get(j));
                    }
                    dao.updateExcelData(list.get(j));
                }
            }
        } catch (Exception e) {
            res = "导入失败";
            e.printStackTrace();
        }
        return "导入成功";
    }

    @Override
    public int updateInfo(CareerBean bean) {
        return dao.updateInfo(bean);
    }

    @Override
    public List<CareerBean> getPhysicalInfoForOne(CareerBean o) {
        return dao.getPhysicalInfoForOne(o);
    }
}
