package com.mandi.fendan.service.impl; import java.lang.reflect.InvocationTargetException; import java.util.*; import javax.annotation.Resource; import javax.transaction.Transactional; import com.mandi.fendan.mapper.FdMxWlgsMapper; import com.mandi.system.persist.Login; import com.mandi.system.persist.UserType; import org.springframework.stereotype.Service; import com.google.gson.JsonObject; import com.mandi.fendan.persist.Fd_Company; import com.mandi.fendan.persist.Fd_Vehicle; import org.apache.commons.beanutils.BeanUtils; import org.apache.commons.lang3.StringUtils; import com.mandi.basicconfig.mapper.NczsjMapper; import com.mandi.basicconfig.penum.PrefixEnum; import com.mandi.basicconfig.persist.Ncywdy; import com.mandi.common.JSONobj; import com.mandi.common.Wxhtmethod; import com.mandi.dao.common.Daomethod; import com.mandi.dao.common.ObjectResult; import com.mandi.dao.common.PageRequest; import com.mandi.dao.common.PageResult; import com.mandi.fendan.mapper.Fd_CompanyMapper; import com.mandi.fendan.mapper.Fd_VehicleMapper; import com.mandi.fendan.mapper.Fd_fhdqMapper; import com.mandi.fendan.service.Ifd_companyService; import com.mandi.fendan.util.Contants; import com.mandi.common.Pinyinmethod; @Service public class Fd_CompanyService implements Ifd_companyService { @Resource private Fd_CompanyMapper cmper; @Resource private Fd_fhdqMapper dqper; @Resource private Fd_VehicleMapper vhper; @Resource private NczsjMapper zsjper; @Resource private FdMxWlgsMapper fdMxWlgsMapper; @Override public ObjectResult get(String id) { ObjectResult prr = new ObjectResult(); if(!StringUtils.isBlank(id)){ Fd_Company fcy = cmper.get(id); prr.setCode(0); prr.setItem(fcy); }else{ prr.setCode(1); prr.setErrmsg("编号不能为空");; } return prr; } @Override public ObjectResult getByCompanyNo(String companyNo) { ObjectResult prr = new ObjectResult(); if(!StringUtils.isBlank(companyNo)){ Fd_Company fcy = cmper.getByCompanyNo(companyNo); prr.setCode(0); prr.setItem(fcy); }else{ prr.setCode(1); prr.setErrmsg("公司编号不能为空");; } return prr; } @Override public PageResult> getlist(String name, PageRequest pr) { PageResult> prr=new PageResult>(); List> rl=new ArrayList>(); Map mp=new HashMap(); mp.put("pagesize", pr.getPageSize()); mp.put("page", pr.getStart()); mp.put("name", name); List list; if(pr.isNeedPages()) { int pages=cmper.getPages(mp); pages=Daomethod.countpages(pages, pr.getPageSize()); prr.setPages(pages); list =cmper.getList(mp); }else{ list = cmper.getList1(mp); } if(list!=null){ for (Fd_Company fdc : list) { String comNo=fdc.getCompanyNo(); Map rm=new HashMap(); try { rm=BeanUtils.describe(fdc); } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) { } List cyl=this.cmper.getncYwdylist(comNo); rm.put("ywList", cyl); rl.add(rm); } } prr.setCode(0); prr.setList(rl); return prr; } @Override public List> companytypehead(String search, int pagesize) { return null; } @Override public ObjectResult saveCompany(Fd_Company c,String ywdyNos) throws Exception { ObjectResult orr=new ObjectResult(); if(c==null){ orr.setCode(1); orr.setErrmsg("运输公司对象为空,不能保存"); return orr; } if(StringUtils.isEmpty(c.getName())){ orr.setCode(1); orr.setErrmsg("运输公司名字为空,不能保存"); return orr; } String hdpinyin=Pinyinmethod.pinyinhds(c.getName()); String pinyin=Pinyinmethod.pinyins(c.getName()); c.setPinyin(pinyin); c.setHdpinyin(hdpinyin); String id=c.getId(); try { Fd_Company c1= cmper.get(id); if(c1!=null){ Fd_Company fcd=cmper.getByCompanyNo(c.getCompanyNo()); if(fcd!=null&&!fcd.getId().equals(c1.getId())){ orr.setCode(1); orr.setErrmsg("运输公司编号已存在,不能保存,请修改!"); return orr; } c1.setCname(c.getCname()); c1.setHdpinyin(c.getHdpinyin()); c1.setPinyin(c.getPinyin()); c1.setNote(c.getNote()); c1.setCtime(new Date()); c1.setCompanyNo(c.getCompanyNo()); c1.setHasSPR(c.isHasSPR()); c1.setName(c.getName()); cmper.update(c1); this.cmper.delCYnexus(c.getCompanyNo()); orr.setItem(c1); }else{ if(StringUtils.isEmpty(c.getCompanyNo())){ JsonObject pfob=Wxhtmethod.postJsonStr(Contants.YKTgetPrefix_url,PrefixEnum.fd_Company.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){ orr.setCode(1); orr.setErrmsg(pfor.getErrmsg()); return orr; } c.setCompanyNo(pfor.getItem()); }else{ Fd_Company fcd=cmper.getByCompanyNo(c.getCompanyNo()); if(fcd!=null){ orr.setCode(1); orr.setErrmsg("运输公司编号已存在,不能保存,请修改!"); return orr; } } c.setCtime(new Date()); c.setId(UUID.randomUUID()+""); cmper.insert(c); orr.setItem(c); } } catch (Exception e) { throw new Exception(StringUtils.isEmpty(e.getMessage())?"物流公司保存出错!":e.getMessage()); } //修改运输公司和业务单元关联表 if(!StringUtils.isEmpty(ywdyNos)){ for (String ywdyNo : ywdyNos.split(",")) { Ncywdy ywdy=this.zsjper.getNcywdybyCode(ywdyNo); if(ywdy==null) continue; this.cmper.insertCYnexus(c.getCompanyNo(), ywdyNo); } } orr.setCode(0); orr.setItem(c); return orr; } @Override public ObjectResult changeUsed(Fd_Company cp) { return null; } @Override @Transactional(rollbackOn=Exception.class) public ObjectResult delCompany(String companyNo) throws Exception{ ObjectResult prr = new ObjectResult(); if(!StringUtils.isBlank(companyNo)){ try { Fd_Vehicle fdv=this.vhper.selectbyCompanyNo(companyNo); if(fdv!=null){ prr.setCode(1); prr.setErrmsg("请先删除所属的该物流公司的车辆信息!"); return prr; } cmper.delByCompanyNo(companyNo); this.dqper.delbywlgsCode(companyNo); this.cmper.delCYnexus(companyNo); prr.setCode(0); prr.setItem(true); } catch (Exception e) { throw new Exception(StringUtils.isEmpty(e.getMessage())?"物流公司删除程序出错!":e.getMessage()); } prr.setCode(0); prr.setItem(true); }else{ prr.setCode(1); prr.setErrmsg("公司编号不能为空"); } return prr; } @Override public PageResult> getcqList(String companyNo) { PageResult> prr=new PageResult>(); LinkedList> rl=new LinkedList<>(); if(StringUtils.isNotBlank(companyNo)){ List l=this.cmper.getncYwdylist(companyNo); Map map = new HashMap<>(); for (Ncywdy ncy : l) { // 排重 if (map.get(ncy.getCode()) == null) map.put(ncy.getCode(), ncy); } int a = 0; for (Map.Entry entry : map.entrySet()) { String code = entry.getKey(); Map m = null; if(Contants.nc_ywdy_ZZ.equals(code)){ m = new HashMap<>(2); m.put("name","郑州厂区"); m.put("cqval",Contants.nc_ywdy_ZZ); }else if(Contants.nc_ywdy_MS.equals(code)){ m = new HashMap<>(2); m.put("name","明晟厂区"); m.put("cqval",Contants.nc_ywdy_MS); }else if(Contants.nc_ywdy_YR.equals(code)){ m = new HashMap<>(2); m.put("name","义瑞厂区"); m.put("cqval",Contants.nc_ywdy_YR); } else{ if(a==0){ // 防止出现多个科技厂区 m = new HashMap<>(2); m.put("name","科技厂区"); m.put("cqval",Contants.nc_ywdy_KJ); a=1; // 特殊需求 科技要放到第一个 rl.addFirst(m); } // 防止出现空行 continue; } rl.add(m); } prr.setList(rl); }else{ Map m2=new HashMap(); m2.put("name","全部厂区"); m2.put("cqval",""); rl.add(m2); Map m=new HashMap(); m.put("name","科技厂区"); m.put("cqval",Contants.nc_ywdy_KJ); rl.add(m); Map m1=new HashMap(); m1.put("name","郑州厂区"); m1.put("cqval",Contants.nc_ywdy_ZZ); rl.add(m1); Map m3=new HashMap(); m3.put("name","明晟厂区"); m3.put("cqval",Contants.nc_ywdy_MS); rl.add(m3); Map m4=new HashMap(); m4.put("name","义瑞厂区"); m4.put("cqval",Contants.nc_ywdy_YR); rl.add(m4); prr.setList(rl); } prr.setCode(0); return prr; } @Override public PageResult> getFdCqList(String companyNo, Login login) { PageResult> prr=new PageResult>(); LinkedList> rl=new LinkedList<>(); if (!UserType.admin.toString().equals(login.getWorkerno())) { List l= fdMxWlgsMapper.selectChangqu(login.getUsername()); Map map = new HashMap<>(); for (Map map1 : l) { // 排重 if (map1.get(map1.get("fd_cq")) == null) map.put(map1.get("fd_cq").toString(), map1); } int a = 0; for (Map.Entry entry : map.entrySet()) { String code = entry.getKey(); Map m = null; if(Contants.nc_ywdy_ZZ.equals(code)){ m = new HashMap<>(2); m.put("name","郑州厂区"); m.put("cqval",Contants.nc_ywdy_ZZ); }else if(Contants.nc_ywdy_MS.equals(code)){ m = new HashMap<>(2); m.put("name","明晟厂区"); m.put("cqval",Contants.nc_ywdy_MS); }else if(Contants.nc_ywdy_YR.equals(code)){ m = new HashMap<>(2); m.put("name","义瑞厂区"); m.put("cqval",Contants.nc_ywdy_YR); } else{ if(a==0){ // 防止出现多个科技厂区 m = new HashMap<>(2); m.put("name","科技厂区"); m.put("cqval",Contants.nc_ywdy_KJ); a=1; // 特殊需求 科技要放到第一个 rl.addFirst(m); } // 防止出现空行 continue; } rl.add(m); } prr.setList(rl); }else{ Map m2=new HashMap(); m2.put("name","全部厂区"); m2.put("cqval",""); rl.add(m2); Map m=new HashMap(); m.put("name","科技厂区"); m.put("cqval",Contants.nc_ywdy_KJ); rl.add(m); Map m1=new HashMap(); m1.put("name","郑州厂区"); m1.put("cqval",Contants.nc_ywdy_ZZ); rl.add(m1); Map m3=new HashMap(); m3.put("name","明晟厂区"); m3.put("cqval",Contants.nc_ywdy_MS); rl.add(m3); Map m4=new HashMap(); m4.put("name","义瑞厂区"); m4.put("cqval",Contants.nc_ywdy_YR); rl.add(m4); prr.setList(rl); } prr.setCode(0); return prr; } }