package com.sifang.modules.sunshine.SystemSetting.service;

import com.google.common.collect.Maps;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Record;
import com.sifang.modules.alioss.AliyunOSSClientUtil;
import com.sifang.modules.customer.constant.PartnerConstants;
import com.sifang.modules.esign.cn.tsign.hz.exception.EsignDemoException;
import com.sifang.modules.esign.constants.ESignConstants;
import com.sifang.modules.esign.service.ESignAuthService;
import com.sifang.modules.esign.service.ESignSignService;
import com.sifang.modules.order.utils.excelExt.ExcelReader;
import com.sifang.modules.platform.service.AddressService;
import com.sifang.modules.platform.utils.AddressUtils;
import com.sifang.modules.sunshine.SystemSetting.dao.CrmProjectCompanyPcdDao;
import com.sifang.modules.sunshine.SystemSetting.dao.ProjectCompanyDao;
import com.sifang.modules.sunshine.SystemSetting.dao.ProjectPcdDao;
import com.sifang.modules.sunshine.SystemSetting.entity.CrmProjectCompanyPcd;
import com.sifang.modules.sunshine.SystemSetting.entity.ProjectCompanySettings;
import com.sifang.modules.sunshine.SystemSetting.form.AddAreaVo;
import com.sifang.modules.sunshine.SystemSetting.form.AddAuthorizedArea;
import com.sifang.modules.sunshine.SystemSetting.form.AddProjectCompany;
import com.sifang.modules.sunshine.SystemSetting.handler.ExcelImportAuthorizedAreaNumberHandler;
import com.sifang.modules.sunshine.SystemSetting.handler.ExcelImportProjectCompanyNumberHandler;
import com.sifang.modules.sunshine.SystemSetting.utils.BusinessException;
import com.sifang.modules.sunshine.SystemSetting.utils.StringUtil;
import com.sifang.modules.sunshine.SystemSetting.vo.ProjectCompanySettingsVo;
import com.sifang.modules.sunshine.partnerManagement.entity.CrmPartner;
import com.sifang.modules.sunshine.partnerManagement.service.PartnerService;
import com.sifang.modules.sys.common.UserUtils;
import com.sifang.modules.sys.common.entity.User;
import com.sifang.modules.sys.common.service.BaseService;
import com.sifang.modules.sys.common.utils.DateUtils;
import com.sifang.modules.sys.common.utils.IdGen;
import com.sifang.modules.sys.util.LayuiPage;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.PictureData;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class ProjectCompanyService extends BaseService {

    @Autowired
    private ProjectCompanyDao projectCompanyDao;

    @Autowired
    private ESignAuthService eSignAuthService;
    @Autowired
    private ProjectPcdDao pcdDao;

    @Autowired
    private CrmProjectCompanyPcdDao companyPcdDao;

    @Resource
    AddressService addressService;
    @Autowired
    private PartnerService partnerService;

    //列表数据
    public void projectCompanyList(LayuiPage<Record> page, Map<String, String> map) {
        projectCompanyDao.projectCompanyList(page, map);
    }
    public void projectCompanyPage(LayuiPage<Record> page, Map<String, String> map,List<AddAreaVo> areaList) {
        projectCompanyDao.projectCompanyPage(page, map,areaList);
    }

    public Record getProjectCompanyDetail(String id){
        return projectCompanyDao.getProjectCompanyDetail(id);
    }

    public List<Record> getProjectCompany(){
        return projectCompanyDao.getProjectCompany();
    }

    public List<Record> getProjectCompany(String type){
        return projectCompanyDao.getProjectCompany(type);
    }

    public List<Record> getProjectCompanyById(String type,String id){
        return projectCompanyDao.getProjectCompany(type);
    }

    //保存项目公司
    @Transactional(rollbackFor = Exception.class)
    public void saveProjectCompany(ProjectCompanySettingsVo vo) {

        User user = UserUtils.getUser();
        ProjectCompanySettings newSetting= new ProjectCompanySettings();
        // 校验是否存在已启用的相同名称的项目公司
        boolean codeFlag = projectCompanyDao.checkUniqueByCode(vo.getCompanyCode(),vo.getId());
        if (codeFlag) {
            throw new IllegalArgumentException("已添加过相同公司代码的项目公司");
        }
        if (StringUtil.isNotBlank(vo.getId())){
            logger.info(">>>>>>>>>>>>编辑项目公司 vo:{}", vo.toString());
            ProjectCompanySettings editInfo = projectCompanyDao.get(vo.getId());
            newSetting = editInfo;
            newSetting.setUpdateTime(new Date());
            newSetting.setUpdateUserId(user.getId());
        }else {
            long byName = projectCompanyDao.queryByName(vo.getName());
            if (byName > 0) {
                throw new IllegalArgumentException("已添加过相同名称且已启用的项目公司");
            }

            logger.info(">>>>>>>>>>>>保存项目公司 vo:{}", vo.toString());
            newSetting.setCreateTime(new Date());
            newSetting.setCreateUserId(user.getId());
        }

        newSetting.setName(vo.getName());
        newSetting.setCompanyCode(vo.getCompanyCode());
        newSetting.setEnterpriseQualificationNumber(vo.getEnterpriseQualificationNumber());
        newSetting.setChargePerson(vo.getChargePerson());
        newSetting.setChargePersonIdCard(vo.getChargePersonIdCard());
        newSetting.setTelephone(vo.getTelephone());
        newSetting.setProvince(vo.getProvince());
        newSetting.setCity(vo.getCity());
        newSetting.setArea(vo.getArea());
        newSetting.setAddress(vo.getAddress());
      //  newSetting.setPsnAccount(EsignConfig.PsnAccountPhone);
        newSetting.setPsnAccount(vo.getPsnAccount());//法人授权电话
        newSetting.setOrgStatus(ESignConstants.ORG_STATUS_NO_CERTIFIED);
        newSetting.setStatus(ESignConstants.status_enable);
        newSetting.setContractPrefix("NHHT");
        newSetting.setType(vo.getType());
        newSetting.setBusinessLicense(vo.getBusinessLicense());
        newSetting.setCertificationImgName(vo.getCertificationImgName());
        newSetting.setAuthorizeProvince(vo.getAuthorizeProvince());
        newSetting.setAuthorizeArea(vo.getAuthorizeArea());
        newSetting.setAuthorizeCity(vo.getAuthorizeCity());
        newSetting.setPartner(vo.getPartner());
        newSetting.setFilingType(vo.getFilingType());
        newSetting.setInsuranceName(vo.getInsuranceName());
        newSetting.setInsuranceId(vo.getInsuranceId());
        newSetting.setInsuranceUrl(vo.getInsuranceUrl());

        try {
            //华融
           /* if(PartnerConstants.PARTNER_ID_HR.equals(newSetting.getPartner()) && !ESignConstants.ORG_STATUS_CERTIFIED.equals(newSetting.getOrgStatus())){
                eSignAuthService.createOrgAccount(newSetting);
            }*/
            projectCompanyDao.save(newSetting);
            int i = 1;
            companyPcdDao.deleteInfoById(newSetting.getId());
        //    companyPcdDao.updateBySql("DELETE FROM crm_project_company_pcd WHERE company_id=:p1", new Parameter(newSetting.getId()));
            if (!vo.getAreaList().isEmpty()){
                for (AddAreaVo areaVo : vo.getAreaList()) {
                    CrmProjectCompanyPcd companyPcd = new CrmProjectCompanyPcd();
                    companyPcd.setCompanyId(newSetting.getId());
                    companyPcd.setProvince(areaVo.getProvince());
                    companyPcd.setCity(areaVo.getCity());
                    companyPcd.setArea(areaVo.getArea());
                    companyPcd.setSort(i++);
                    companyPcd.setCreateTime(new Date());
                    companyPcd.setStatus("0");
                    companyPcdDao.save(companyPcd);
                }
            }
        }/* catch (EsignDemoException e) {
            logger.error(">>>>>>>>>>>>>>>>>saveProjectCompany e:{}",e);
            throw new IllegalArgumentException(e.getMessage());
        }*/catch (Exception e){
            logger.error(">>>>>>>>>>>>>>>saveProjectCompany  ex：{}",e);
            throw new IllegalArgumentException("保存项目公司失败!");
        }
    }

    //启用/禁用 项目公司
    @Transactional(rollbackFor = Exception.class)
    public void changeCompanyStatus(String id,String status){
        logger.info(">>>>>>>>>>>>changeCompanyStatus  id:{},status:{}",id,status);
        ProjectCompanySettings settings=projectCompanyDao.get(id);
        User user = UserUtils.getUser();
        settings.setStatus(status);
        settings.setUpdateTime(new Date());
        settings.setUpdateUserId(user.getId());
        projectCompanyDao.save(settings);
    }

    @Transactional(rollbackFor = Exception.class)
    public Map<String, Object> importProjectCompany(Map<String, String> params, InputStream in) {
        Map<String, Object> retMap = Maps.newHashMap();
        retMap.put("operType", "import");
        ExcelImportProjectCompanyNumberHandler handler=new ExcelImportProjectCompanyNumberHandler();
        handler.setStartSheet(1);
        handler.setStartRow(3);
        int imgCount = 1;
        User user = UserUtils.getUser();
        String fileName = params.get("fileName");
        try {
//            new ExcelReader().readExcel(in, fileName, handler);
            try {
                new ExcelReader().readExcelPictures(in,fileName, handler,imgCount);
            }catch (Exception e){
                throw new BusinessException(e.getMessage());
            }
            List<CrmPartner> partnerList =  partnerService.getAllForChoose();
            Map<String,String> partnerMap = partnerList.stream().collect(Collectors.toMap(CrmPartner::getName,CrmPartner::getId));
            List<AddProjectCompany> projectList = handler.getAddCompanyList();
            for (AddProjectCompany add : projectList) {
                ProjectCompanySettings projectCompanySettings = getProjectCompanySettings(add,user);
                projectCompanySettings.setPartner(partnerMap.get(add.getPartnerName()));
                projectCompanyDao.save(projectCompanySettings);
            }
            retMap.put("pass", "y");
        }catch (Exception e){
            e.printStackTrace();
            logger.error(" 导入项目公司信息异常,e:{}",e);
            throw new BusinessException(e.getMessage());
        }finally {
            if(in != null){
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return retMap;
    }

    private static ProjectCompanySettings getProjectCompanySettings(AddProjectCompany add,User user) {
        ProjectCompanySettings projectCompanySettings = new ProjectCompanySettings();
        projectCompanySettings.setName(add.getName());
        projectCompanySettings.setType("0");
        projectCompanySettings.setEnterpriseQualificationNumber(add.getEnterpriseQualificationNumber());
        projectCompanySettings.setChargePerson(add.getChargePerson());
        projectCompanySettings.setChargePersonIdCard(add.getChargePersonIdCard());
        projectCompanySettings.setTelephone(add.getTelephone());
        projectCompanySettings.setCompanyCode(add.getCompanyCode());
        projectCompanySettings.setProvince(add.getProvince());
        projectCompanySettings.setCity(add.getCity());
        projectCompanySettings.setArea(add.getArea());
        projectCompanySettings.setAddress(add.getAddress());
        projectCompanySettings.setOrgStatus(ESignConstants.ORG_STATUS_NO_CERTIFIED);
        projectCompanySettings.setStatus(ESignConstants.status_enable);
        projectCompanySettings.setContractPrefix("NHHT");
        projectCompanySettings.setPsnAccount(add.getPsnAccount());
        //上传图片
        PictureData pictureData = add.getBusinessLicense();
        projectCompanySettings.setCreateUserId(user.getId());
        projectCompanySettings.setCreateTime(new Date());
        String filename = IdGen.uuid();
        String imageUrl = AliyunOSSClientUtil.uploadImage(pictureData.getData(), pictureData.suggestFileExtension(),filename );
        projectCompanySettings.setBusinessLicense(imageUrl);
        projectCompanySettings.setCertificationImgName(filename+"."+ pictureData.suggestFileExtension());
        return projectCompanySettings;
    }
    @Transactional(rollbackFor = Exception.class)
    public Map<String, Object> checkImportProjectCompany(Map<String, String> params, InputStream in) {
        Map<String, Object> retMap = Maps.newHashMap();
        retMap.put("operType", "check");
        ExcelImportProjectCompanyNumberHandler handler=new ExcelImportProjectCompanyNumberHandler();
        handler.setStartSheet(1);
        handler.setStartRow(2);
        try {
            new ExcelReader().checkPictures(in, params.get("extName"), handler);
            Map<String, Object> map = (Map<String, Object>) handler.getHandlerResult();
            String templateError = (String) map.get("TemplateError");
            if(!handler.isCheckFlag()){
                handler.appendErrorDetail("<p>未填充数据</p>");
            }
            //省
            List<String> provinceList = new ArrayList<>();
            List<Record> provinces = AddressUtils.getProvincesList();
            for (Record province : provinces) {
                String provinceName = province.getStr("ProvinceName");
                provinceList.add(provinceName);
            }

            //市
            List<String> cityList = new ArrayList<>();
            //区
            List<String> areaList = new ArrayList<>();
            List<Record> citys = AddressUtils.getCityList();
            for (Record city : citys) {
                String cityName = city.getStr("CityName");
                cityList.add(cityName);
                List<Record> areas = AddressUtils.getAreaByCityName(cityName);
                for (Record area : areas) {
                    String areaName = area.getStr("DistrictName");
                    areaList.add(areaName);
                }
            }

            List<AddProjectCompany> retList = handler.getAddCompanyList();

            //全部资方
            List<Record> records = partnerService.partnerList();
            Set<String> partnerNames =  records.stream().map(r->r.getStr("name")).collect(Collectors.toSet());
            int i = 2;
            for (AddProjectCompany info : retList) {
                if (!provinceList.contains(info.getProvince())){
                    handler.appendErrorDetail("第" + (i + 1) + "行省不存在");
                }
                if (!cityList.contains(info.getCity())){
                    handler.appendErrorDetail("第" + (i + 1) + "行市不存在");
                }
                if(!areaList.contains(info.getArea())){
                    handler.appendErrorDetail("第" + (i + 1) + "行区不存在");
                }

                // 校验是否存在已启用的相同名称的项目公司
                boolean codeFlag = projectCompanyDao.checkUniqueByCode(info.getCompanyCode(),info.getId());
                if (codeFlag) {
                    handler.appendErrorDetail("第" + (i + 1) + "行已添加过相同公司代码的项目公司");
                }
                if (StringUtil.isBlank(info.getId())){
                    long byName = projectCompanyDao.queryByName(info.getName());
                    if (byName > 0) {
                        handler.appendErrorDetail("第" + (i + 1) + "行已添加过相同名称且已启用的公司名称");
                    }
                }
                if(!partnerNames.contains(info.getPartnerName())){
                    handler.appendErrorDetail("第" + (i + 1) + "行所属资方不存在");
                }
                i++;
            }
            String hanl = handler.getErrorDetail();
            if (StrKit.notBlank(templateError)){
                retMap.put("errorDetail", templateError);
                retMap.put("errorMessage", "y");
            }else if (StringUtils.isNotBlank(hanl)) {
                retMap.put("errorDetail", hanl);
                retMap.put("errorMessage", "y");
            } else if ("overLimit".equalsIgnoreCase((String) map.get("overLimit"))) {
                retMap.put("overLimit", "y");
            } else {
                retMap.put("pass", "y");
            }
        }catch (Exception e){
            retMap.put("errorMessage", e.toString());
        }finally {
            try {
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return retMap;
    }
    @Transactional(rollbackFor = Exception.class)
    public Map<String, Object> importAuthorizedArea(Map<String, String> params, InputStream in) {
        Map<String, Object> retMap = Maps.newHashMap();
        retMap.put("operType", "import");
        ExcelImportAuthorizedAreaNumberHandler handler=new ExcelImportAuthorizedAreaNumberHandler();
        handler.setStartSheet(1);
        handler.setStartRow(3);
        String fileName = params.get("fileName");
        try {
            new ExcelReader().readExcel(in, fileName, handler);
            List<AddAuthorizedArea> authorizedAreaList = handler.getAdditionalAreaList();
            int i = 1;
            for (AddAuthorizedArea add : authorizedAreaList) {
                Record record = projectCompanyDao.queryIdByName(add.getName());
                CrmProjectCompanyPcd companyPcd = new CrmProjectCompanyPcd();
                companyPcdDao.deleteInfoById(record.getStr("ID"));
                companyPcd.setCompanyId(record.getStr("ID"));
                companyPcd.setProvince(add.getProvince());
                companyPcd.setCity(add.getCity());
                companyPcd.setArea(add.getArea());
                companyPcd.setSort(i++);
                companyPcd.setCreateTime(new Date());
                companyPcd.setStatus("0");
                companyPcdDao.save(companyPcd);
            }
            retMap.put("pass", "y");
        }catch (Exception e){
            e.printStackTrace();
            logger.error(" 导入项目公司信息异常,e:{}",e);
            throw new BusinessException(e.getMessage());
        }finally {
            if(in != null){
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return retMap;
    }
    @Transactional(rollbackFor = Exception.class)
    public Map<String, Object> checkAuthorizedArea(Map<String, String> params, InputStream in) {
        Map<String, Object> retMap = Maps.newHashMap();
        retMap.put("operType", "check");
        ExcelImportAuthorizedAreaNumberHandler handler=new ExcelImportAuthorizedAreaNumberHandler();
        handler.setStartSheet(1);
        handler.setStartRow(2);
        try {
            new ExcelReader().checkExcel(in, params.get("extName"), handler);
            Map<String, Object> map = (Map<String, Object>) handler.getHandlerResult();
            String templateError = (String) map.get("TemplateError");
            if(!handler.isCheckFlag()){
                handler.appendErrorDetail("<p>未填充数据</p>");
            }
            //省
            List<String> provinceList = new ArrayList<>();
            List<Record> provinces = addressService.getProvinces();
            for (Record province : provinces) {
                String provinceName = province.getStr("ProvinceName");
                provinceList.add(provinceName);
            }
            //市
            List<String> cityList = new ArrayList<>();
            //区
            List<String> areaList = new ArrayList<>();
            List<Record> citys = addressService.getCityList();
            for (Record city : citys) {
                String cityName = city.getStr("CityName");
                cityList.add(cityName);
                List<Record> areas = addressService.getAreaByCityName(cityName);
                for (Record area : areas) {
                    String areaName = area.getStr("DistrictName");
                    areaList.add(areaName);
                }
            }
            List<Record> records = projectCompanyDao.getProjectCompany();
            Set<String> companyNameSet = records.stream().map(r->r.getStr("name")).collect(Collectors.toSet());
            List<AddAuthorizedArea> retList = handler.getAdditionalAreaList();
            Set<String> toAeraSet = new HashSet<>();
            Set<String> errorSet = new HashSet<>();
            for (AddAuthorizedArea info : retList) {
                if (!provinceList.contains(info.getProvince())) {
                    if(errorSet.add(info.getProvince() + "该省不存在")){
                        handler.appendErrorDetail(info.getProvince() + "该省不存在");
                    }
                }
                if (StringUtil.isNotBlank(info.getCity())) {
                    if (!cityList.contains(info.getCity())) {
                        if(errorSet.add(info.getCity() + "该市不存在")){
                            handler.appendErrorDetail(info.getCity() + "该市不存在");
                        }
                    }
                    if (StringUtil.isNotBlank(info.getArea())) {
                        if (!areaList.contains(info.getArea())) {
                            if(errorSet.add(info.getArea() + "该区不存在")){
                                handler.appendErrorDetail(info.getArea() + "该区不存在");
                            }
                        }
                    }else {
                       toAeraSet.add(info.getName() + info.getProvince()+info.getCity()+"NULL");
                    }
                    if(!toAeraSet.add(info.getName() + info.getProvince() + info.getCity() + info.getArea())){
                        if(errorSet.add(info.getProvince()+info.getCity()+ info.getArea()+ "已存在")){
                            handler.appendErrorDetail(info.getProvince()+info.getCity()+ info.getArea()+ "已存在");
                        }
                    }
                }else {
                    if(!toAeraSet.add(info.getName() + info.getProvince()+"NULL")){
                        if(errorSet.add(info.getProvince()+info.getCity()+ info.getArea()+ "已存在")){
                            handler.appendErrorDetail(info.getProvince()+info.getCity()+ info.getArea()+ "已存在");
                        }
                    }
                }
                if(errorSet.add(info.getName()+" 不存在该项目公司") &&
                        !companyNameSet.contains(info.getName())){
                    handler.appendErrorDetail(info.getName()+" 不存在该项目公司");
                }
            }
            for(AddAuthorizedArea info: retList){
                if(StringUtil.isNotBlank(info.getCity())){
                    if(toAeraSet.contains(info.getName() + info.getProvince()+"NULL")){
                        if(errorSet.add(info.getProvince()+info.getCity()+ info.getArea()+ "已存在")){
                            handler.appendErrorDetail(info.getProvince()+info.getCity()+ info.getArea()+ "已存在");
                        }
                    }
                    if(StringUtil.isNotBlank(info.getArea())){
                        if(toAeraSet.contains(info.getName() + info.getProvince()+info.getCity()+"NULL")){
                            if(errorSet.add(info.getProvince()+info.getCity()+ info.getArea()+ "已存在")){
                                handler.appendErrorDetail(info.getProvince() +info.getCity()+ info.getArea()+ "已存在");
                            }
                        }
                    }
                }
            }
                String hanl = handler.getErrorDetail();
                if (StrKit.notBlank(templateError)) {
                    retMap.put("errorDetail", templateError);
                    retMap.put("errorMessage", "y");
                } else if (StringUtils.isNotBlank(hanl)) {
                    retMap.put("errorDetail", hanl);
                    retMap.put("errorMessage", "y");
                } else if ("overLimit".equalsIgnoreCase((String) map.get("overLimit"))) {
                    retMap.put("overLimit", "y");
                } else {
                    retMap.put("pass", "y");
                }

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try {
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return retMap;
    }

    //获取印章授权的签署链接
    public String viewSealAuthContract(String companyId){
        ProjectCompanySettings settings=projectCompanyDao.get(companyId);
        if(StringUtil.isBlank(settings.getSealContractUrl())){
            try {
                String url= ESignAuthService.getCompanyAuthSealUrl(settings.getSignerCode(),settings.getSealAuthBizId());
                settings.setSealContractUrl(url);
                projectCompanyDao.save(settings);
            } catch (EsignDemoException e) {
                e.printStackTrace();
                logger.error("viewSealAuthContract  e:{}",e);
                throw new BusinessException("获取印章签署链接失败！"+e.getMessage());
            }
        }
        return settings.getSealContractUrl();
    }

    //印章授权
    public String createSealAuth(String companyId){
        ProjectCompanySettings settings=projectCompanyDao.get(companyId);
        //先判断公司有没有授权认证
        if(StringUtil.isBlank(settings.getSignerCode())){
            //先认证，认证通过后，会发起印章授权
            if(PartnerConstants.PARTNER_ID_HR.equals(settings.getPartner())){
                try {
                    eSignAuthService.createOrgAccount(settings);
                    projectCompanyDao.save(settings);
                    return "已发起机构授权，请通知项目公司签署授权书！链接地址："+settings.getAuthShortUrl();
                } catch (EsignDemoException e) {
                    e.printStackTrace();
                    throw new BusinessException("机构授权认证失败，无法印章授权！"+e.getMessage());
                }
            }
        }else{
               //直接发起印章授权
               if("3".equals(settings.getSealExternalAuth())){
                   throw new BusinessException("印章已授权，无需再次授权！");
               }
               if("1".equals(settings.getSealExternalAuth()) && StringUtil.isNotBlank(settings.getSealAuthorizationSignUrl())){
                   return "印章授权早已发起，请通知法人签署！签署链接："+settings.getSealAuthorizationSignUrl();
               }
            try {
                String  sealAuthBizId = ESignAuthService.orgSealExternalAuth(settings.getSignerCode(),settings.getSealId(),settings.getPsnId(),settings);
                settings.setSealAuthBizId(sealAuthBizId);
                settings.setSealExternalAuth("1");
                settings.setSealEffectiveTime(new Date());
                settings.setSealExpireTime(DateUtils.yearSub(new Date(),3));
                projectCompanyDao.save(settings);
                return "已发起印章授权，请通知项目公司签署授权书！";
            } catch (EsignDemoException e) {
                e.printStackTrace();
                throw new BusinessException("印章发起授权失败！"+e.getMessage());
            }

        }
        return "200";
    }
}
