package com.mandi.fendan.service.impl; import java.util.ArrayList; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.UUID; import javax.annotation.Resource; import com.mandi.basicconfig.mapper.NczsjMapper; import com.mandi.basicconfig.persist.Nckh; import com.mandi.fendan.mapper.*; import com.mandi.fendan.util.ConvertTool; import com.mandi.servlet.file.impl.FileLoadImpl; import com.mandi.springmvc.logs.OpLogs; import com.mandi.system.persist.ModuleEnum; import org.apache.commons.lang3.StringUtils; import org.apache.log4j.Logger; import org.apache.poi.ss.usermodel.charts.ScatterChartSeries; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import com.google.gson.JsonArray; import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.google.gson.JsonParser; import com.mandi.basicconfig.penum.PrefixEnum; import com.mandi.common.BigDecimalfiter; import com.mandi.common.JSONobj; import com.mandi.common.Jacksonmethod; import com.mandi.common.Wxhtmethod; import com.mandi.common.ParamFileter.Fileoperation; import com.mandi.dao.common.ObjectResult; import com.mandi.fendan.persist.Fd_Bsheet; import com.mandi.fendan.persist.Fd_Company; import com.mandi.fendan.persist.Fd_Company_Vehicle; import com.mandi.fendan.persist.Fd_Vehicle; import com.mandi.fendan.persist.Fd_XSTD; import com.mandi.fendan.persist.Fd_XSTDMX; import com.mandi.fendan.persist.Fd_wlgs_fhdq; import com.mandi.fendan.persist.VehicleStatus; import com.mandi.fendan.persist.VehicleType; import com.mandi.fendan.persist.Vehicle_XSTD; import com.mandi.fendan.service.Ifd_bsheetService; import com.mandi.fendan.util.Contants; import com.mandi.fendan.util.FdtoNc; import com.mandi.fendan.util.persist.Serviceenum; import com.mandi.fendan.util.persist.WaitXSTD; import com.mandi.fendan.util.persist.YktBsheet; import com.mandi.system.persist.Login; import com.mandi.system.persist.UserType; import static com.mandi.fendan.util.Contants.*; @Service public class Fd_BsheetService implements Ifd_bsheetService { private Logger log= Logger.getLogger(Fd_BsheetService.class); @Resource private Fd_BsheetMapper bsper; @Resource private Fd_VehicleMapper fdvper; @Resource private Fd_BsheetitemMapper bsiper; @Resource private VehicleXSTDMapper vhxper; @Resource private Fd_CompanyMapper fdcper; @Resource private Fd_ComVehicleMapper cvper; @Resource private Fd_XSTDMXMapper mxper; @Resource private Fd_XSTDMapper tdper; @Resource private Fd_fhdqMapper dqper; @Resource private FdtoNc fdtonc; @Resource private Fd_YKTMapper yktmper; @Resource private FdMxWlgsMapper fdMxWlgsMapper; @Resource private NczsjMapper zsjper; @Override @Transactional(rollbackFor=Exception.class) public ObjectResult> saveFd_Bsheet(String vehicleInfo,String xstdNo, String bsitemlistStr, Login lg) throws Exception { ObjectResult> orr=new ObjectResult>(); if(StringUtils.isEmpty(bsitemlistStr)){ orr.setCode(1); orr.setErrmsg("业务参数为空,不能保存!"); return orr; } if(lg==null){ orr.setCode(1); orr.setErrmsg("请登录账号!"); return orr; } if(StringUtils.isBlank(xstdNo)){ orr.setCode(1); orr.setErrmsg("提单编号为空!"); return orr; } if(UserType.admin.toString().equals(lg.getWorkerno())){ orr.setCode(1); orr.setErrmsg("管理员账号没有分单权限!"); return orr; } Fd_Company fdc=fdcper.getByCompanyNo(lg.getDepartno()); if(fdc==null){ orr.setCode(1); orr.setErrmsg("账号没有配置物流公司,请联系管理员进行配置!"); return orr; } Fd_XSTD xstd=this.tdper.getbyttdbh(xstdNo); if(xstd==null){ orr.setCode(1); orr.setErrmsg("提单编号:"+xstdNo+" 不正确,未查询到该提单!"); return orr; } if(xstd.isTd_zcwcbz()){ orr.setCode(1); orr.setErrmsg("该提单已经完成装车,不能分单!"); return orr; } if(xstd.getTd_wfdsl()<=0){ orr.setCode(1); orr.setErrmsg("该提单已经分单完成,不能分单!"); return orr; } // 判断是否存在客户 if(StringUtils.isBlank(xstd.getTd_fpbh())){ Nckh nckh = zsjper.getNckhbyCode(xstd.getTd_khbh());// 客户不存在的特殊情况是不应该存在的,以防万一,加判空。 if(nckh == null) { orr.setCode(1); orr.setErrmsg("该提单客户不存在,请检查数据!"); return orr; } } //送货 if(xstd.getTd_ztbz()==2){ if(fdc!=null){ if(!fdc.isHasSPR()){ orr.setCode(1); orr.setErrmsg("该账号没有自提权限!"); return orr; } } }else{ // Fd_wlgs_fhdq wldq=this.dqper.getbyNos(lg.getDepartno(),xstd.getTd_shddbh(), // todo 下面的意义何在????默认查科技??? // xstd.getTd_ywdybh().equals(Contants.nc_ywdy_ZZ)?Contants.nc_ywdy_ZZ // :xstd.getTd_ywdybh().equals(Contants.nc_ywdy_MS)?Contants.nc_ywdy_MS // :Contants.nc_ywdy_KJ); Fd_wlgs_fhdq wldq = this.dqper.getbyNos(lg.getDepartno(),xstd.getTd_shddbh(), ConvertTool.ncGetDefault(xstd.getTd_ywdybh())); int total = fdMxWlgsMapper.selectCountByTdTdls(xstd.getTd_tdls()); if(wldq==null && total == 0){ orr.setCode(1); orr.setErrmsg("该账号没有配置该地区的分单权限!"); return orr; } } String Rcompany=""; Map mm=this.dqper.getKH(xstd.getTd_khbh()); if(StringUtils.isBlank(xstd.getTd_fpbh())){ if(mm!=null) Rcompany=mm.get("name")==null?"":mm.get("name").toString(); }else{ /*Rcompany=(xstd.getTd_ywdybh().equals(Contants.nc_ywdy_ZZ)?"郑州报关": xstd.getTd_ywdybh().equals(Contants.nc_ywdy_MT)?"河南报关": xstd.getTd_ywdybh().equals(Contants.nc_ywdy_MS)?"明晟报关": "科技报关");*/ Rcompany = ConvertTool.covertToCustoms(xstd.getTd_ywdybh()); } List tdl= new ArrayList(); Map> mxMp=new HashMap>(); List mxl=new ArrayList(); //是否保存一卡通业务单表头信息 boolean saveBT=false; /*String bsDE=xstd.getTd_cqNo().equals(Contants.nc_ywdy_ZZ) ? Contants.zzSdepartNo :xstd.getTd_cqNo().equals(Contants.nc_ywdy_MS)?Contants.msSdepartNo:Contants.kjSdepartNo;*/ String bsDE = ConvertTool.ncCovertToYkt(xstd.getTd_cqNo()); String bbsNo=""; String ziti="";//是否自提 // 系统部门未找到 if (StringUtils.isBlank(bsDE)) { orr.setCode(1); orr.setErrmsg("提单未找到系统部门字段,请重新操作!"); return orr; } try { JsonParser jp=new JsonParser(); JsonObject vjo=jp.parse(vehicleInfo).getAsJsonObject(); Fd_Vehicle fdv=new Fd_Vehicle(); fdv=JSONobj.getobj(vjo, fdv); String driver=JSONobj.getstring(vjo, "driver"); String phone=JSONobj.getstring(vjo, "phone"); String vehicleId=JSONobj.getstring(vjo, "vehicleId").toUpperCase(); // String IDNum=JSONobj.getstring(vjo, "IDNum"); fdv.setVehicleId(vehicleId); if(StringUtils.isEmpty(fdv.getVehicleId())||StringUtils.isEmpty(fdv.getDriver())||StringUtils.isEmpty(fdv.getIDNum())||StringUtils.isEmpty(fdv.getPhone())){ orr.setCode(1); orr.setErrmsg("车辆必填项不能为空!"); return orr; } Fd_Vehicle fdv1=this.fdvper.getByVehicleId(fdv.getVehicleId()); if(fdv1==null){ fdv.setCompanyNo(lg.getDepartno()); fdv.setId(UUID.randomUUID()+""); fdv.setNote(""); fdv.setState(true); fdv.setStatus(VehicleStatus.已配车); fdv.setType(VehicleType.临时车); fdvper.insert(fdv); }else{ if(fdv1.getType()==VehicleType.临时车){ fdv.setDriver(fdv.getDriver()); fdv.setPhone(fdv.getPhone()); fdv.setIDNum(fdv.getIDNum()); fdvper.update(fdv1); } } // xstd.getTd_ywdybh().equals(Contants.zzywdyNo)?Contants.zzSdepartNo:Contants.kjSdepartNo Fd_Bsheet fb=this.bsper.getbyVehicleId(fdv.getVehicleId(), Serviceenum.在办,bsDE); if(fb==null){ JsonObject pfob=Wxhtmethod.postJsonStr(Contants.YKTgetPrefix_url,PrefixEnum.productsale.toString()); ObjectResult pfor=new ObjectResult(); String bsNo=JSONobj.getstring(pfob, "item"); int code=JSONobj.getint(pfob, "code"); String errmsg=JSONobj.getstring(pfob, "errmsg"); pfor.setCode(code); pfor.setErrmsg(errmsg); pfor.setItem(bsNo); if(pfor==null||pfor.getCode()!=0){ throw new Exception(pfor.getErrmsg()); } fb=new Fd_Bsheet(); fb.setBsNo(pfor.getItem()); fb.setCompanyName(lg.getDepartname()); fb.setCompanyNo(lg.getDepartno()); fb.setCreateid(lg.getUserid()); fb.setCreateName(lg.getWorkername()); fb.setCtime(new Date()); fb.setDriver(driver); fb.setFdsheetNo(pfor.getItem()); fb.setId(UUID.randomUUID()+""); fb.setPhone(phone); fb.setSendmsg(false); fb.setStatus(Serviceenum.在办); fb.setVehicleId(fdv.getVehicleId()); fb.setSdepartNo(bsDE); fb.setUpdateTime(new Date()); this.bsper.insert(fb); saveBT=true; }else{ fb.setDriver(driver); fb.setPhone(phone); fb.setUpdateTime(new Date()); this.bsper.update(fb); } Fd_Company_Vehicle fdcv=cvper.getbyIDs(fdv.getVehicleId(), lg.getDepartno(),bsDE); if(fdcv==null){ fdcv=new Fd_Company_Vehicle(); fdcv.setCompanyNo(lg.getDepartno()); fdcv.setVehicleId(fdv.getVehicleId()); fdcv.setSdepartNo(bsDE); cvper.insertIDs(fdcv); } // if(StringUtils.isBlank(fb.getSdepartNo())){ // if(xstd.getTd_ywdybh().equals(Contants.zzywdyNo)){ // this.bsper.updatesdepartNo(fb.getId(), Contants.zzSdepartNo); // fb.setSdepartNo(Contants.zzSdepartNo); // Fd_Company_Vehicle fdcv=cvper.getbyIDs(fdv.getVehicleId(), lg.getDepartno(),Contants.zzSdepartNo); // if(fdcv==null){ // fdcv=new Fd_Company_Vehicle(); // fdcv.setCompanyNo(lg.getDepartno()); // fdcv.setVehicleId(fdv.getVehicleId()); // fdcv.setSdepartNo(Contants.zzSdepartNo); // cvper.insertIDs(fdcv); // } // }else{ // this.bsper.updatesdepartNo(fb.getId(), Contants.kjSdepartNo); // fb.setSdepartNo(Contants.kjSdepartNo); // Fd_Company_Vehicle fdcv=cvper.getbyIDs(fdv.getVehicleId(), lg.getDepartno(),Contants.kjSdepartNo); // if(fdcv==null){ // fdcv=new Fd_Company_Vehicle(); // fdcv.setCompanyNo(lg.getDepartno()); // fdcv.setVehicleId(fdv.getVehicleId()); // fdcv.setSdepartNo(Contants.kjSdepartNo); // cvper.insertIDs(fdcv); // } // } // } if(StringUtils.isBlank(ziti)){ ziti=xstd.getTd_ztbz()==2?"自提":"非自提"; } // if(StringUtils.isBlank(fb.getSdepartNo())){ // throw new Exception("生成的分单业务单:"+fb.getFdsheetNo()+"厂区未找到!"); // } //郑州 // if(fb.getSdepartNo().equals(Contants.zzSdepartNo)){ // if(!xstd.getTd_ywdybh().equals(Contants.zzywdyNo)){ //// throw new Exception("车辆生成的是郑州厂区一卡通业务单,科技提单:"+xstd.getTd_tdbh()+"不能配车!"); // if(saveBT){ // throw new Exception("科技厂区提单和郑州厂区提单不能同时使用同一辆车!"); // }else{ // throw new Exception(fb.getVehicleId()+" 有郑州厂区一卡通销售业务单,科技提单:"+xstd.getTd_tdbh()+"不能使用该车!"); // } // } // }else{ // if(xstd.getTd_ywdybh().equals(Contants.zzywdyNo)){ //// throw new Exception("车辆生成的是科技厂区一卡通业务单,郑州提单:"+xstd.getTd_tdbh()+"不能配车!"); // if(saveBT){ // throw new Exception("科技厂区提单和郑州厂区提单不能同时使用同一辆车!"); // }else{ // throw new Exception(fb.getVehicleId()+" 有科技厂区一卡通销售业务单,郑州提单:"+xstd.getTd_tdbh()+"不能使用该车!"); // } // } // } JsonArray ja=jp.parse(bsitemlistStr).getAsJsonArray(); //分单数量和毛重 int fdsl=0; double fdmz=0; for (JsonElement je: ja) { JsonObject itemJo=je.getAsJsonObject(); //主键 String tdmx_tdfl=JSONobj.getstring(itemJo, "tdfl"); Fd_XSTDMX tdmx=this.mxper.get(tdmx_tdfl); //未找到的过滤掉 已分单的过滤掉 已装车的不处理 if(tdmx==null||tdmx.getTdmx_fdbz()!=0||tdmx.getTdmx_zcbz()!=0){ continue; } //关闭标志不为0不做处理 if(tdmx.getTdmx_gbbz()!=0){ continue; } fdsl++; fdmz=BigDecimalfiter.addscal(fdmz, tdmx.getTdmx_mz(), Contants.scale); tdmx.setTdmx_fdbz(1); tdmx.setTdmx_fdcph(fdv.getVehicleId()); tdmx.setTdmx_fdywbh(fb.getBsNo()); tdmx.setTdmx_fdwlgsbh(lg.getDepartno()); this.mxper.updateFD(tdmx); mxl.add(tdmx); } Vehicle_XSTD vhxstd=this.vhxper.getbyIDs(fdv.getVehicleId(), xstdNo,lg.getDepartno()); if(vhxstd==null){ vhxstd=new Vehicle_XSTD(); vhxstd.setBsNo(fb.getBsNo()); vhxstd.setCompanyNo(lg.getDepartno()); vhxstd.setFd_num(fdsl); vhxstd.setFd_weight(fdmz); vhxstd.setVehicleId(fdv.getVehicleId()); vhxstd.setXstdNo(xstdNo); this.vhxper.insert(vhxstd); }else{ vhxstd.setBsNo(fb.getBsNo()); vhxstd.setCompanyNo(lg.getDepartno()); vhxstd.setFd_num(vhxstd.getFd_num()+fdsl); vhxstd.setFd_weight(BigDecimalfiter.add(vhxstd.getFd_weight(),fdmz)); vhxstd.setVehicleId(fdv.getVehicleId()); vhxstd.setXstdNo(xstdNo); this.vhxper.updatewithIDs(vhxstd); } xstd.setTd_wfdsl(xstd.getTd_wfdsl()-fdsl); xstd.setTd_wfdzl(BigDecimalfiter.subscal(xstd.getTd_wfdzl(), fdmz, Contants.scale)); this.tdper.updateFD(xstd); if(saveBT){ YktBsheet bs=new YktBsheet(); // if(iskj==1){ // bs.setSdepartNo(Contants.kjSdepartNo); // bs.setDcompany("明泰科技"); // }else{ // bs.setSdepartNo(Contants.zzSdepartNo); // bs.setDcompany("郑州眀泰"); // } bs.setSdepartNo(fb.getSdepartNo()); /*bs.setDcompany(fb.getSdepartNo().equals(Contants.kjSdepartNo)?"明泰科技": fb.getSdepartNo().equals(Contants.msSdepartNo)?"河南明晟新材料": "郑州眀泰" );*/ bs.setDcompany(ConvertTool.covertToFactoryName(fb.getSdepartNo())); bs.setDriver(fdv.getDriver()); bs.setNumberplates(StringUtils.upperCase(fdv.getVehicleId())); bs.setPhone(fdv.getPhone()); bs.setRcompany(Rcompany); bs.setBussinessNo(fb.getBsNo()); bs.setNote(ziti+";"+lg.getDepartname()+":"+lg.getWorkername()+"; 分单系统自动生成"); String bsStr=Jacksonmethod.tojson(bs, false); bsDE=bs.getSdepartNo(); bbsNo=bs.getBussinessNo(); JsonObject joADDYWD=Wxhtmethod.postJsonStr(Contants.YKTaddYwd_url,bsStr); ObjectResult ors=new ObjectResult(); String bsNo=JSONobj.getstring(joADDYWD, "item"); int code=JSONobj.getint(joADDYWD, "code"); String errmsg=JSONobj.getstring(joADDYWD, "errmsg"); ors.setItem(bsNo); ors.setCode(code); ors.setErrmsg(errmsg); if(ors.getCode()!=0){ throw new Exception(ors.getErrmsg()); } this.bsper.updateShdw(fb.getId(), StringUtils.isBlank(Rcompany)?"未知发货单位":Rcompany); }else { List l=this.bsper.getAllKH(fb.getBsNo(), fb.getId()); if(l!=null) { boolean iscz=false;//收货单位在明细中是否存在 WaitXSTD wt=null; for (WaitXSTD waitXSTD : l) { if(fb.getShdw().equals(waitXSTD.getTd_khmc())) { iscz=true; break; }else { if(wt==null&&StringUtils.isNotBlank(waitXSTD.getTd_khmc())) { wt=waitXSTD; } } } if(!iscz&&wt!=null) { fb.setShdw(wt.getTd_khmc()); this.yktmper.updateshcompany(fb.getBsNo(), wt.getTd_khmc(),wt.getTd_shdd()); } } } tdl.add(xstd); mxMp.put(xstdNo, mxl); //分单推送NC系统 if(tdl!=null&&tdl.size()>0){ String fdhx=this.fdtonc.getNcdata(tdl, mxMp); Fileoperation.AppendContentToFile(Fileoperation.gettestFileName(), "明细配车 请求参数 ::"+fdhx); //todo 先注释掉NC部分,接口不通 JsonObject joADDYWD=Wxhtmethod.postJsonStr(Contants.NCfdhx_url,fdhx); Fileoperation.AppendContentToFile(Fileoperation.gettestFileName(), "明细配车 url返回值 ::"+joADDYWD); if(joADDYWD==null||JSONobj.getint(joADDYWD, "code")!=0){ throw new Exception(StringUtils.isEmpty(joADDYWD.get("msg").getAsString())?"分单回写NC系统出错!":joADDYWD.get("msg").getAsString()); }else{ String restult=joADDYWD.get("result").getAsString(); JsonObject jr=jp.parse(restult).getAsJsonObject(); if(jr==null||JSONobj.getint(jr, "respcode")!=111){ throw new Exception(jr.get("respmsg").getAsString()); } } } }catch(Exception e){ e.printStackTrace(); if(saveBT){ JsonObject jo=new JsonObject(); jo.addProperty("bussinessNo", bbsNo); jo.addProperty("sdepartNo",bsDE); Wxhtmethod.postJsonStr(Contants.YKTdelYwd_url,jo.toString()); } throw new Exception(StringUtils.isEmpty(e.getMessage())?"提单明细分单出错!":e.getMessage()); } orr.setCode(0); orr.setItem(null); return orr; } @Override @Transactional(rollbackFor=Exception.class) public ObjectResult sendMsg(Login lg, String bussinessNo)throws Exception { ObjectResult orr=new ObjectResult(); if(lg==null){ orr.setCode(1); orr.setErrmsg("请登录账号!"); return orr; } if(UserType.admin.toString().equals(lg.getWorkerno())){ orr.setCode(1); orr.setErrmsg("管理员账号没有分单权限!"); return orr; } if(StringUtils.isEmpty(bussinessNo)){ orr.setCode(1); orr.setErrmsg("业务单参数为空!"); return orr; } Fd_Bsheet fdb=bsper.getbybsNo(bussinessNo); if(fdb==null){ orr.setCode(1); orr.setErrmsg("业务单未找到!"); return orr; } if(fdb.getStatus()!=Serviceenum.在办){ orr.setCode(1); orr.setErrmsg("业务单不是在办状态!"); return orr; } JsonObject jos=new JsonObject(); jos.addProperty("bussinessNo", bussinessNo); JsonObject jo=Wxhtmethod.postJsonStr(Contants.YKTsendMsg_url,jos.toString()); if(jo==null||jo.get("code").getAsInt()!=0){ throw new Exception(jo.get("errmsg").getAsString()); } fdb.setSendmsg(true); fdb.setInvitecode(jo.get("item").getAsString()); bsper.updateInvitaCode(fdb); orr.setCode(0); return orr; } }