package com.zfire.jiasm.syncdata.service.impl;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zfire.jiasm.syncdata.constant.ESInstallEnum;
import com.zfire.jiasm.syncdata.data.Token;
import com.zfire.jiasm.syncdata.plus.entity.*;
import com.zfire.jiasm.syncdata.plus.service.*;
import com.zfire.jiasm.syncdata.service.InstallDownloadService;
import com.zfire.jiasm.syncdata.service.PublicService;
import com.zfire.jiasm.syncdata.utils.DataUtil;
import com.zfire.jiasm.syncdata.utils.HttpUtil;
import com.zfire.jiasm.syncdata.utils.JSONResult;
import com.zfire.jiasm.syncdata.utils.TokenCenter;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;

@Slf4j
@Service
public class InstallDownloadServiceImpl implements InstallDownloadService {
    @Value("${baseurl}")
    private String baseurl = "";

    @Autowired
    private TokenCenter tokenCenter;
    @Resource
    ItfInstallBatchDownloadRecService itfInstallBatchDownloadRecService;
    @Resource
    ItfTblAzAssignLcLsDownloadService itfTblAzAssignLcLsDownloadService;
    @Resource
    ItfTblAzAssignMxDownloadService itfTblAzAssignMxDownloadService;
    @Resource
    ItfTblAzAssignFkmxDownloadService itfTblAzAssignFkMxDownloadService;
    @Resource
    ItfTblAzAssignFeeDownloadService itfTblAzAssignFeeDownloadService;
    @Resource
    ItfTblAzAssignAppointmentDownloadService itfTblAzAssignAppointmentDownloadService;
    @Resource
    ItfTblAzAssignLcFzryDownloadService itfTblAzAssignLcFzryDownloadService;
    @Resource
    ItfTblAzAssignSatisfactionDownloadService itfTblAzAssignSatisfactionDownloadService;
    @Resource
    ItfTblAzAssignPlatformDownloadService itfTblAzAssignPlatformDownloadService;
    @Resource
    PublicService publicService;


    @Override
    public void processInstallDownload() throws Exception {
        List<ItfInstallBatchDownloadRec> rec = new ArrayList<>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        log.info("开始下载安装工单: \uF0B7{}",sdf.format(new Date()));

        String downloadBatchNo = publicService.getUUID();
        // 按批次-获取数据
        JSONArray datas = getDownloadData(downloadBatchNo, ESInstallEnum.SALES_WEBSIT_NUMBER.getCode());

        // 写销售单号-派工单号 方便日志查询
        if (datas.size()>0){
            putLogInfo(datas);
            log.info("安装工单下载了{}条数据",datas.size());
        }

        try{
            // 按单来处理 数据源为数组(每一个成员为一个订单)
            for (int i = 0; i < datas.size(); i++) {
                JSONObject sheet = datas.getJSONObject(i);
                // 提取主表数据 主表无数据时不处理
                JSONArray mainTableData = sheet.getJSONArray("TBL_AZ_ASSIGN_LC_LS");
                if (mainTableData != null && mainTableData.size() > 0) {
                    String downloadTaskNo = publicService.getUUID();
                    // 最后修改日期不能为空处理
                    DataUtil.initLastModifiedDate(mainTableData.getJSONObject(0),"lastModifiedDate");
                    // 添加数据
                    addData(rec, downloadBatchNo, sheet, mainTableData, downloadTaskNo);
                } else {
                    log.info("安装工单找不到主表数据不做处理 mainTableData：{}" ,mainTableData);
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            log.info("安装工单落本地表失败");
        }

        datas.clear();
        log.info("安装工单下载完成 \uF0B7{}",sdf.format(new Date()));

        log.info("与总部同步安装工单下载状态");
        updateStatus();
        log.info("与总部同步安装工单下载状态完成");
    }


    @Transactional(rollbackFor = Exception.class)
    public void addData(List<ItfInstallBatchDownloadRec> rec, String downloadBatchNo, JSONObject sheet, JSONArray mainTableData, String downloadTaskNo) throws Exception {
        // 查看安装数据批量下载记录内是否已存在
        //     已存在不再下载，
        //     无数据时落本地表
        Boolean flag = Boolean.FALSE;
        flag = insertInstallRecData(mainTableData.getJSONObject(0), downloadBatchNo, downloadTaskNo, rec,
                ESInstallEnum.SALES_WEBSIT_NUMBER.getCode());
        // 历史下载记录中无数据时数据落表,有数据时不再下载
        if (flag){
            parseData(sheet, downloadTaskNo, downloadBatchNo, rec);
        }
    }



    // 与总部进行状态同步下次获取时不再拉取数据
    private void updateStatus() {
        try {
            List<ItfInstallBatchDownloadRec> recs = new ArrayList<>();
            // greeStatSynStatus:总部同步标志是否已成功更新 0=否 1=是 99=其他
            LambdaQueryWrapper<ItfInstallBatchDownloadRec> queryWrapper = new LambdaQueryWrapper<ItfInstallBatchDownloadRec>()
                    .eq(ItfInstallBatchDownloadRec::getGreeStatSynStatus,0)
                    .or(wrapper->wrapper.eq(ItfInstallBatchDownloadRec::getGreeStatSynStatus,99)
                            .le(ItfInstallBatchDownloadRec::getSynTimes,5))
                    .orderByAsc(ItfInstallBatchDownloadRec::getSynReqId);
            recs = itfInstallBatchDownloadRecService.list(queryWrapper);

            if (CollectionUtils.isNotEmpty(recs)){
                log.info("需要与总部同步安装工单下载状态记录数: {}",recs.size());
                Token token = tokenCenter.getCurrentToken();
                String url = baseurl + "tblAzAssignLcLs" + "/updateExtendfiled5";
                for (ItfInstallBatchDownloadRec item :recs){
                    log.info("与总部同步下装状态的派工单号: {}",item.getPgid());
                    String pgguid = item.getPgguid();
                    Date lastModifiedDate = item.getLastModifiedDate();
                    Map<String, Object> paramMap = new HashMap<String, Object>();
                    paramMap.put("pgguid", pgguid);
                    paramMap.put("lastModifiedDate", lastModifiedDate);
                    JSONResult jsonResult = HttpUtil.httpRequestWithToken(url, "POST",
                            JSON.toJSONStringWithDateFormat(paramMap, "yyyy-MM-dd HH:mm:ss"), token);
                    // 与总部进行状态同步
                    syncStatus(item, jsonResult);
                }
            }
        } catch (Exception ex) {
            log.error("与总部同步更新安装工单下载状态失败", ex);
        }
    }

    private void syncStatus(ItfInstallBatchDownloadRec item, JSONResult jsonResult) {
        ItfInstallBatchDownloadRec update = new ItfInstallBatchDownloadRec();
        update.setSynReqId(item.getSynReqId());
        update.setGreeStatSynTime(new Date());
        update.setSynTimes(item.getSynTimes()+1);

        if (jsonResult.isOK()) {
            update.setGreeStatSynStatus(1);
        } else {
            update.setGreeStatSynStatus(99);
            update.setGreeStatSynErrMsg(jsonResult.getMsg());
        }
        itfInstallBatchDownloadRecService.updateById(update);
    }

    public void downloadAdd(List<ItfInstallBatchDownloadRec> rec, List<ItfTblAzAssignLcLsDownload> lcls,
                            List<ItfTblAzAssignMxDownload> mx, List<ItfTblAzAssignFkmxDownload> fk,
                            List<ItfTblAzAssignFeeDownload> fe, List<ItfTblAzAssignAppointmentDownload> app,
                            List<ItfTblAzAssignLcFzryDownload> fz, List<ItfTblAzAssignSatisfactionDownload> sa,
                            List<ItfTblAzAssignPlatformDownload> pla) {
        if (rec.size()>0){
            itfInstallBatchDownloadRecService.saveBatch(rec);
            rec.clear();
        }
        if (lcls.size()>0){
            itfTblAzAssignLcLsDownloadService.saveBatch(lcls);
            lcls.clear();
        }
        if (mx.size()>0){
            itfTblAzAssignMxDownloadService.saveBatch(mx);
            mx.clear();
        }
        if (fk.size()>0){
            itfTblAzAssignFkMxDownloadService.saveBatch(fk);
            fk.clear();
        }
        if (fe.size()>0){
            itfTblAzAssignFeeDownloadService.saveBatch(fe);
            fe.clear();
        }
        if (app.size()>0){
            itfTblAzAssignAppointmentDownloadService.saveBatch(app);
            app.clear();
        }
        if (fz.size()>0){
            itfTblAzAssignLcFzryDownloadService.saveBatch(fz);
            fz.clear();
        }
        if (sa.size()>0){
            itfTblAzAssignSatisfactionDownloadService.saveBatch(sa);
            sa.clear();
        }
        if (pla.size()>0){
            itfTblAzAssignPlatformDownloadService.saveBatch(pla);
            pla.clear();
        }
    }

    private void putLogInfo(JSONArray datas) {
        StringBuffer str = new StringBuffer();
        str.append("安装工单下载数据日志销售单号-派工单号：【");
        for (int i = 0; i < datas.size(); i++) {
            JSONObject sheet = datas.getJSONObject(i);
            // 提取主表数据
            JSONArray mainTableData = sheet.getJSONArray("TBL_AZ_ASSIGN_LC_LS");
            if (mainTableData != null && mainTableData.size() > 0) {
                JSONArray tableData = new JSONArray();
                tableData = sheet.getJSONArray(ESInstallEnum.TBL_AZ_ASSIGN_LC_LS.getCode());
                for (int j = 0; j < tableData.size(); j++) {
                    // 生成安装工单主表对象
                    ItfTblAzAssignLcLsDownload item = JSON.toJavaObject(tableData.getJSONObject(j),ItfTblAzAssignLcLsDownload.class);
                    str.append("("+item.getXsdh()+"-"+ item.getPgid()+")");
                }
            }
        }
        str.append("】");
        log.info(str.toString());
    }


    public void parseData(JSONObject sheet, String downloadTaskNo, String downloadBatchNo, List<ItfInstallBatchDownloadRec> rec) throws Exception {
        List<ItfTblAzAssignLcLsDownload> lcls = new ArrayList<>();
        List<ItfTblAzAssignMxDownload> mx = new ArrayList<>();
        List<ItfTblAzAssignFkmxDownload> fk = new ArrayList<>();
        List<ItfTblAzAssignFeeDownload> fe = new ArrayList<>();
        List<ItfTblAzAssignAppointmentDownload> app = new ArrayList<>();
        List<ItfTblAzAssignLcFzryDownload> fz = new ArrayList<>();
        List<ItfTblAzAssignSatisfactionDownload> sa = new ArrayList<>();
        List<ItfTblAzAssignPlatformDownload> pla = new ArrayList<>();

        // 获取报文对应数据信息
        getDataInfo(sheet, downloadTaskNo, downloadBatchNo, lcls, mx, fk, fe, app, fz, sa, pla);

        // 打印预落表安装工单信息方便查询出错的订单信息
        logAZGDInfo(lcls);

        // 数据同步落到同步信息表 事务处理
        downloadAdd(rec, lcls, mx, fk, fe, app, fz, sa, pla);
    }

    private void logAZGDInfo(List<ItfTblAzAssignLcLsDownload> lcls) {
        StringBuffer str = new StringBuffer();
        str.append("预落表安装工单信息销售单号-派工单号：【");
        if (lcls.size()>0){
            for (ItfTblAzAssignLcLsDownload item : lcls){
                str.append("("+item.getXsdh()+"-"+ item.getPgid()+")");
            }
            str.append("】");
            log.info(str.toString());

            for (ItfTblAzAssignLcLsDownload item : lcls){
                log.info("item: {}",item);
            }
        }
    }

    private void getDataInfo(JSONObject sheet, String downloadTaskNo, String downloadBatchNo, List<ItfTblAzAssignLcLsDownload> lcls, List<ItfTblAzAssignMxDownload> mx, List<ItfTblAzAssignFkmxDownload> fk, List<ItfTblAzAssignFeeDownload> fe, List<ItfTblAzAssignAppointmentDownload> app, List<ItfTblAzAssignLcFzryDownload> fz, List<ItfTblAzAssignSatisfactionDownload> sa, List<ItfTblAzAssignPlatformDownload> pla) throws Exception {
        JSONArray tableData = new JSONArray();
        // 报文中提取安装工单主表数据 itf_tbl_az_assign_lc_ls_download
        tableData = sheet.getJSONArray(ESInstallEnum.TBL_AZ_ASSIGN_LC_LS.getCode());
        for (int j = 0; j < tableData.size(); j++) {
            // 生成安装工单主表对象
            parseTblAzAssignLcLsData(downloadBatchNo, downloadTaskNo, tableData.getJSONObject(j), lcls);
        }

        // 安装工单明细表 itf_tbl_az_assign_lc_ls_download
        tableData = sheet.getJSONArray(ESInstallEnum.TBL_AZ_ASSIGN_MX.getCode());
        for (int j = 0; j < tableData.size(); j++) {
            // 生成安装工单明细主表对象
            parseTblAzAssignMxData(downloadBatchNo, downloadTaskNo, tableData.getJSONObject(j), mx);
        }
        // 安装反馈明细表 itf_tbl_az_assign_fkmx_download
        tableData = sheet.getJSONArray(ESInstallEnum.TBL_AZ_ASSIGN_FKMX.getCode());
        for (int j = 0; j < tableData.size(); j++) {
            // 生成安装反馈明细表对象
            parseTblAzAssignFkMXData(downloadBatchNo, downloadTaskNo, tableData.getJSONObject(j), fk);
        }
        // 安装费用清单表 itf_tbl_az_assign_fee_download
        tableData = sheet.getJSONArray(ESInstallEnum.TBL_AZ_ASSIGN_FEE.getCode());
        for (int j = 0; j < tableData.size(); j++) {
            // 生成安装费用清单表对象
            parseTblAzAssignFeeData(downloadBatchNo, downloadTaskNo, tableData.getJSONObject(j), fe);
        }
        // 用户预约改约表 itf_tbl_az_assign_appointment_download
        tableData = sheet.getJSONArray(ESInstallEnum.TBL_AZ_ASSIGN_APPOINTMENT.getCode());
        for (int j = 0; j < tableData.size(); j++) {
            // 生成用户预约改约表对象
            parseTblAzAssignAppointmentData(downloadBatchNo, downloadTaskNo, tableData.getJSONObject(j), app);
        }
        // 安装辅助人员表 itf_tbl_az_assign_lc_fzry_download
        tableData = sheet.getJSONArray(ESInstallEnum.TBL_AZ_ASSIGN_LC_FZRY.getCode());
        for (int j = 0; j < tableData.size(); j++) {
            // 生成安装辅助人员表对象
            parseTblAzAssignLcFzryData(downloadBatchNo, downloadTaskNo, tableData.getJSONObject(j), fz);
        }
        // 客户评价表 itf_tbl_az_assign_satisfaction_download
        tableData = sheet.getJSONArray(ESInstallEnum.TBL_AZ_ASSIGN_SATISFACTION.getCode());
        for (int j = 0; j < tableData.size(); j++) {
            // 生成客户评价表对象
            parseTblAzAssignSatisfactionData(downloadBatchNo, downloadTaskNo, tableData.getJSONObject(j) , sa);
        }
        // 第三方平台订单信息表 itf_tbl_az_assign_platform_download
        tableData = sheet.getJSONArray(ESInstallEnum.TBL_AZ_ASSIGN_PLATFORM.getCode());
        for (int j = 0; j < tableData.size(); j++) {
            // 生成第三方平台订单信息表对象
            parseTblAzAssignPlatformData(downloadBatchNo, downloadTaskNo, tableData.getJSONObject(j), pla);
        }
    }

    // itf_tbl_az_assign_platform_download
    private void parseTblAzAssignPlatformData(String downloadBatchNo, String downloadTaskNo, JSONObject aData,
                                              List<ItfTblAzAssignPlatformDownload> pla) throws Exception {
        ItfTblAzAssignPlatformDownload item = JSON.toJavaObject(aData,ItfTblAzAssignPlatformDownload.class);
        item.setDownloadBatchNo(downloadBatchNo);
        item.setDownloadTaskNo(downloadTaskNo);
        pla.add(item);

    }

    // itf_tbl_az_assign_satisfaction_download
    private void parseTblAzAssignSatisfactionData(String downloadBatchNo, String downloadTaskNo, JSONObject aData ,
                                                  List<ItfTblAzAssignSatisfactionDownload> sa) throws Exception {
        ItfTblAzAssignSatisfactionDownload item = JSON.toJavaObject(aData,ItfTblAzAssignSatisfactionDownload.class);
        item.setDownloadBatchNo(downloadBatchNo);
        item.setDownloadTaskNo(downloadTaskNo);
        sa.add(item);
    }


    // itf_tbl_az_assign_lc_fzry_download
    private void parseTblAzAssignLcFzryData(String downloadBatchNo, String downloadTaskNo, JSONObject aData,
                                            List<ItfTblAzAssignLcFzryDownload> fz) throws Exception {
        ItfTblAzAssignLcFzryDownload item = JSON.toJavaObject(aData,ItfTblAzAssignLcFzryDownload.class);
        item.setDownloadBatchNo(downloadBatchNo);
        item.setDownloadTaskNo(downloadTaskNo);
        fz.add(item);
    }

    // itf_tbl_az_assign_appointment_download
    private void parseTblAzAssignAppointmentData(String downloadBatchNo, String downloadTaskNo, JSONObject aData,
                                                 List<ItfTblAzAssignAppointmentDownload> app) throws Exception {
        ItfTblAzAssignAppointmentDownload item = JSON.toJavaObject(aData,ItfTblAzAssignAppointmentDownload.class);
        item.setDownloadBatchNo(downloadBatchNo);
        item.setDownloadTaskNo(downloadTaskNo);
        app.add(item);
    }

    // itf_tbl_az_assign_fkmx_download
    private void parseTblAzAssignFeeData(String downloadBatchNo, String downloadTaskNo, JSONObject aData,
                                         List<ItfTblAzAssignFeeDownload> fe) throws Exception {
        ItfTblAzAssignFeeDownload item = JSON.toJavaObject(aData,ItfTblAzAssignFeeDownload.class);
        item.setDownloadBatchNo(downloadBatchNo);
        item.setDownloadTaskNo(downloadTaskNo);
        fe.add(item);
    }

    // itf_tbl_az_assign_fkmx_download
    private void parseTblAzAssignFkMXData(String downloadBatchNo, String downloadTaskNo, JSONObject aData,
                                          List<ItfTblAzAssignFkmxDownload> fk) throws Exception {
        ItfTblAzAssignFkmxDownload item = JSON.toJavaObject(aData,ItfTblAzAssignFkmxDownload.class);
        item.setDownloadBatchNo(downloadBatchNo);
        item.setDownloadTaskNo(downloadTaskNo);
        fk.add(item);
    }

    // itf_tbl_az_assign_mx_download
    private void parseTblAzAssignMxData(String downloadBatchNo, String downloadTaskNo, JSONObject aData,
                                        List<ItfTblAzAssignMxDownload> mx) throws Exception {
        ItfTblAzAssignMxDownload item = JSON.toJavaObject(aData,ItfTblAzAssignMxDownload.class);
        item.setDownloadBatchNo(downloadBatchNo);
        item.setDownloadTaskNo(downloadTaskNo);
        mx.add(item);
    }

    // itf_tbl_az_assign_lc_ls_download
    private void parseTblAzAssignLcLsData(String downloadBatchNo, String downloadTaskNo, JSONObject aData, List<ItfTblAzAssignLcLsDownload> lcls) throws Exception {
        ItfTblAzAssignLcLsDownload item = JSON.toJavaObject(aData,ItfTblAzAssignLcLsDownload.class);
        item.setDownloadBatchNo(downloadBatchNo);
        item.setDownloadTaskNo(downloadTaskNo);
        lcls.add(item);
    }

    /**
     * 查看下载记录表有无数据
     * 无数据，插入一条记录
     * 有数据
     *    还未做处理时不做插入动作    总部同步标志是否已成功更新 0=否
     *    其他插一条数据，做单独说明
     */
    public boolean insertInstallRecData(JSONObject aData, String downloadBatchNo, String downloadTaskNo,
                                        List<ItfInstallBatchDownloadRec> rec, String salesWebsitNumber) throws Exception {
        List<ItfInstallBatchDownloadRec> oldRec = new ArrayList<>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        // 下载记录表查看数据 同一修改时间同一订单 itf_install_batch_download_rec
        oldRec = itfInstallBatchDownloadRecService.lambdaQuery()
                .eq(ItfInstallBatchDownloadRec::getPgguid,aData.getString("pgguid"))
                .eq(ItfInstallBatchDownloadRec::getLastModifiedDate,sdf.parse(aData.getString("lastModifiedDate")))
                .list();
        boolean duplicate = (oldRec.size() > 0);
        // 下载记录表中存在数据
        if (duplicate) {
            // 相同修改时间、相同订单存在不再插入
            if (!recHasNoUploadStatus(oldRec)) {
                // 数据处理失败的 插一条新数据再次同步并做说明
                ItfInstallBatchDownloadRec item = getItfInstallBatchDownloadRec(Boolean.TRUE,aData, downloadBatchNo, downloadTaskNo, salesWebsitNumber);
                rec.add(item);
            }
            return Boolean.FALSE;
        } else {
            // 无数据插入新记录 生成对象
            ItfInstallBatchDownloadRec item = getItfInstallBatchDownloadRec(Boolean.FALSE,aData, downloadBatchNo, downloadTaskNo, salesWebsitNumber);
            rec.add(item);
            return Boolean.TRUE;
        }
    }

    @NotNull
    private ItfInstallBatchDownloadRec getItfInstallBatchDownloadRec(Boolean flag, JSONObject aData, String downloadBatchNo, String downloadTaskNo, String salesWebsitNumber) {
        ItfInstallBatchDownloadRec item = new ItfInstallBatchDownloadRec();
        item.setSalesWebsitNumber(salesWebsitNumber);
        item.setDownloadBatchNo(downloadBatchNo);
        item.setDownloadTaskNo(downloadTaskNo);
        item.setPgguid(aData.getString("pgguid"));
        item.setCreatedBy(aData.getString("createdBy"));
        item.setCreatedDate(aData.getDate("createdDate"));
        item.setLastModifiedBy(aData.getString("lastModifiedBy"));
        item.setLastModifiedDate(aData.getDate("lastModifiedDate"));
        item.setPgid(aData.getString("pgid"));
        item.setExtendfiled1(aData.getString("extendfiled1"));
        item.setExtendfiled2(aData.getString("extendfiled2"));
        item.setExtendfiled3(aData.getString("extendfiled3"));
        item.setExtendfiled4(aData.getString("extendfiled4"));
        item.setExtendfiled5(aData.getString("extendfiled5"));
        if (flag){
            item.setGreeStatSynErrMsg("任务已经存在，不再产生下载数据");
        }
        return item;
    }


    private boolean recHasNoUploadStatus(List<ItfInstallBatchDownloadRec> rec) {
        for (ItfInstallBatchDownloadRec data: rec){
            if (data.getGreeStatSynStatus() == 0){
                return true;
            }
        }
        return false;
    }




    private JSONArray getDownloadData(String downloadBatchNo, String salesWebsitNumber) throws Exception {

        Token token = tokenCenter.getCurrentToken();
        String url = baseurl + "api/install/search/batch?ssxsgs=" + salesWebsitNumber;
        // log.info("url[" + url + "],downloadBatchNo[" + downloadBatchNo + "],send[]");
        JSONObject jsonResult = HttpUtil.httpRequestGetPgid(url, "GET", token, downloadBatchNo, null);
        String statusCode = jsonResult.getString("statusCode");
        if ("200".equals(statusCode)) {
            JSONArray datas = jsonResult.getJSONArray("data");
            return datas;
        } else {
            String errorMessage = jsonResult.getString("message");
            if (errorMessage == null || errorMessage.trim().length() == 0) {
                errorMessage = "失败";
            }
            log.info(errorMessage);
            throw new Exception(errorMessage);
        }

    }
}
