package com.changhong.epc.parsing.service.loop.impl; import com.changhong.epc.bean.form.FormDataEntity; import com.changhong.epc.bean.parsing.BillInfo; import com.changhong.epc.bean.parsing.BillVerify; import com.changhong.epc.bean.tenant.bill.OCRBillEntity; import com.changhong.epc.constter.base.BaseConst; import com.changhong.epc.constter.form.FormConst; import com.changhong.epc.constter.form.utf.FormUrlConst; import com.changhong.epc.constter.parsing.bill.ConstBill; import com.changhong.epc.constter.parsing.loop.ConstDataExpendStart; import com.changhong.epc.constter.parsing.loop.LoopServiceConst; import com.changhong.epc.constter.system.SystemClients; import com.changhong.epc.constter.system.run.EpcRestInterface; import com.changhong.epc.parsing.mapper.tenant.BillInfoMapper; import com.changhong.epc.parsing.service.loop.IBillHandleLoopService; import com.changhong.epc.parsing.service.loop.tools.impl.SortBill; import com.changhong.epc.parsing.service.transition.billInfo.TravelTypeTransverterBillInfo; import com.changhong.epc.parsing.service.util.IFromOperate; import com.iemsoft.framework.cloud.core.base.ResMsg; import com.iemsoft.framework.cloud.core.exception.IEMRuntimeException; import com.iemsoft.framework.cloud.core.thread.ThreadData; import com.iemsoft.framework.cloud.core.tools.JSONTool; import com.iemsoft.framework.cloud.core.tools.ObjectUtil; import com.iemsoft.framework.cloud.core.tools.SpringUtil; import com.iemsoft.framework.cloud.ribbon.RestInterface; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Component; import org.springframework.transaction.annotation.Transactional; import javax.annotation.Resource; import java.util.*; /** * 分配行程服务 * @ClassName: BillHandleLoopServiceImpl * @author [九鼎联合科技] * @date 2017年9月19日 下午6:52:07 */ @Component("billHandleLoopService") @Slf4j public class BillHandleLoopServiceImpl extends LoopSuper implements SystemClients,ConstBill, IBillHandleLoopService, ConstDataExpendStart, FormConst { @Resource private BillInfoMapper billInfoMapper; @Resource private TravelTypeTransverterBillInfo transverterBillInfo; @Resource private IFromOperate fromOperate; @Override public String getThreadName() { return "解析OCR票据服务"; } /** * 实现业务逻辑,由父类回调 */ @Override public void businessHandle(){ log.debug("开始云中心分配行程服务!"); List unBillInfos = billInfoMapper.selectStartZero(LoopServiceConst.UNTREATED); try { log.debug("待分配订单数"+unBillInfos.size()); for (BillInfo billInfo : unBillInfos) { log.debug("当前处理订单:"+billInfo.getOrderCode()); if(ObjectUtil.empty(billInfo.getInfo())){ log.debug("票据信息为空,忽略掉。"); billInfo.setDataStart(LoopServiceConst.AUDIT_STATUS); billInfoMapper.updateByPrimaryKeySelective(billInfo); continue; }else{ try { businessHandle(billInfo); }catch (Exception e){ log.error(e.getMessage(), e); } } } } catch (Exception e) { log.error("---------------------分配行程出现错误---------------------------"); log.error(e.getMessage(), e); } // } } /** * @throws Exception * 根据BillInfo 分配行程 * @Title: businessHandle * @param @param billInfo 设定文件 * @return void 返回类型 * @throws */ @Transactional protected void businessHandle(BillInfo billInfo){ log.debug("开始分配行程。。。。。。。。...."); ThreadData.set(BaseConst.FORMID,billInfo.getFormId()); ThreadData.set(BaseConst.ORDER_CODE,billInfo.getOrderCode()); ThreadData.set(BaseConst.DATA_ROW_NUM,Objects.toString(billInfo.getFormDataId())); int start; try { BillVerify billVerify = billInfo.getVerify(); dealTravel(billInfo); billInfo.setDataStart(LoopServiceConst.TREATED); log.debug("{}分配行程成功", billInfo.getOrderCode()); // 获得表单状态 start = getFormStart(billVerify, billInfo); } catch (Exception e) { log.error(e.getMessage(), e); // 自动审单失败 start = AUTO_FORM_FALSE; billInfo.setDataStart(LoopServiceConst.OVERDUE); Map param = new HashMap<>(); param.put(EXPEND_START_KEY, BILL_DEFEATED); param.put(EXPEND_CLOUD_KEY, billInfo.getOrderCode()); log.error("{}分配行程失败", billInfo.getOrderCode()); billInfo.setMsg(e.getMessage()); } // 修改表单状态 fromOperate.updateFormStatus( billInfo.getFormId() , Objects.toString(JSONTool.toObj(billInfo.getInfo(), OCRBillEntity.class).getTenantId()) , Objects.toString(billInfo.getFormDataId(),"0") , start); billInfoMapper.updateByPrimaryKeySelective(billInfo); } /** * 获得报销单状态 * @param billVerify * @param billInfo * @return */ private int getFormStart(BillVerify billVerify, BillInfo billInfo) { if(billVerify == null || billVerify == BillVerify.QS){ return AUTO_FORM_ING; } if(billInfo.getVerify() == billVerify){ return AUTO_FORM_SUCCESS; } return AUTO_FORM_ING; } public OCRBillEntity dealTravel(BillInfo billInfo){ String info = billInfo.getInfo(); OCRBillEntity oCRBillEntity = JSONTool.toObj(info, OCRBillEntity.class); oCRBillEntity.setFormId(billInfo.getFormId()); boolean exeConvert = transverterBillInfo.exeConvert(oCRBillEntity); checkForm(oCRBillEntity.getTenantId(),billInfo); billInfo.setVerify(exeConvert ? BillVerify.QS : BillVerify.BQS); SortBill.sortBill(oCRBillEntity.getBillDatas()); // 添加离开时间 // addToDate(oCRBillEntity.getBillDatas()); transverterBillInfo.getAutoAllotJourney(billInfo) .addBillInfo(oCRBillEntity, billInfo); billInfo.setInfoLast(JSONTool.toJson(oCRBillEntity)); // billInfoMapper.updateByPrimaryKey(billInfo); return oCRBillEntity; } /** * 对外提供接口的实现 * @throws Exception */ @Override public void businessHandle(String code) { BillInfo billInfo = new BillInfo(); billInfo.setOrderCode(code); billInfo = billInfoMapper.selectOne(billInfo ); businessHandle(billInfo); } public void checkForm(Integer tenantId,BillInfo billInfo){ FormDataEntity formData = new FormDataEntity(); formData.setFormId(billInfo.getFormId()); formData.setDataRowNum(billInfo.getFormDataId()); formData.setTenantId(Objects.toString(tenantId)); RestInterface restInterface = SpringUtil.getBean(SERVER_FORM.getBeanName(), RestInterface.class); Object data = restInterface.post(FormUrlConst.FORM_DATA_SEARCH, formData, ResMsg.class, EpcRestInterface.getEpcHeads()).getData(); Map map = JSONTool.toObj(JSONTool.toJson(data),Map.class); if(!Objects.equals("10",Optional.ofNullable(map).map(m->m.get("processState")).orElse(null))){ log.debug("不可分配"); billInfo.setMsg("此单据已经固化或者被删除"); throw new IEMRuntimeException("此单据不可分配!"); } } }