InquiryServiceImpl.java 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983
  1. package com.uas.ps.inquiry.service.impl;
  2. import com.alibaba.fastjson.JSONObject;
  3. import com.uas.ps.core.util.CollectionUtils;
  4. import com.uas.ps.core.util.ContextUtils;
  5. import com.uas.ps.entity.Product;
  6. import com.uas.ps.entity.Status;
  7. import com.uas.ps.inquiry.AccessConfiguration;
  8. import com.uas.ps.inquiry.dao.*;
  9. import com.uas.ps.inquiry.entity.*;
  10. import com.uas.ps.inquiry.model.*;
  11. import com.uas.ps.inquiry.page.PageInfo;
  12. import com.uas.ps.inquiry.page.SearchFilter;
  13. import com.uas.ps.inquiry.page.criteria.CriterionExpression;
  14. import com.uas.ps.inquiry.page.criteria.LogicalExpression;
  15. import com.uas.ps.inquiry.page.criteria.PredicateUtils;
  16. import com.uas.ps.inquiry.page.criteria.SimpleExpression;
  17. import com.uas.ps.inquiry.page.exception.IllegalOperatorException;
  18. import com.uas.ps.inquiry.service.InquiryService;
  19. import com.uas.ps.inquiry.service.PublicInquiryService;
  20. import com.uas.ps.inquiry.util.FlexJsonUtils;
  21. import com.uas.ps.inquiry.util.HttpUtil;
  22. import com.uas.ps.inquiry.util.StringUtil;
  23. import com.uas.ps.inquiry.util.ThreadUtils;
  24. import javassist.NotFoundException;
  25. import org.slf4j.Logger;
  26. import org.slf4j.LoggerFactory;
  27. import org.springframework.beans.factory.annotation.Autowired;
  28. import org.springframework.data.domain.Page;
  29. import org.springframework.data.domain.Sort;
  30. import org.springframework.data.jpa.domain.Specification;
  31. import org.springframework.jdbc.core.BeanPropertyRowMapper;
  32. import org.springframework.jdbc.core.JdbcTemplate;
  33. import org.springframework.stereotype.Service;
  34. import org.springframework.transaction.annotation.Transactional;
  35. import org.springframework.util.StringUtils;
  36. import javax.persistence.criteria.CriteriaBuilder;
  37. import javax.persistence.criteria.CriteriaQuery;
  38. import javax.persistence.criteria.Predicate;
  39. import javax.persistence.criteria.Root;
  40. import java.net.URLEncoder;
  41. import java.text.ParseException;
  42. import java.text.SimpleDateFormat;
  43. import java.util.*;
  44. /**
  45. * 针对转询价报价单的数据查询操作
  46. *
  47. * Created by hejq on 2018-01-17.
  48. */
  49. @Service
  50. @Transactional
  51. public class InquiryServiceImpl implements InquiryService {
  52. /**
  53. * 消息类型 (在B2B消息中拼接跳转单据详情url请求时需要)
  54. */
  55. private final String INQUIRY_TYPE = "公共询价";
  56. /**
  57. * 消息推送短信模板id
  58. */
  59. private final String SMS_TEMP_ID = "e6320a3c-89ac-4c77-a75f-62a727bce654";
  60. /**
  61. * 消息推送方式:邮件、短信、im
  62. */
  63. private final String SMS_TYPE = "MAIL_AND_SM_AND_IM";
  64. /**
  65. * 消费类型: 多个,MULTI
  66. */
  67. private final String CUST_TYPE = "MULTI";
  68. /**
  69. * 应用来源,主要是为了平台公共询价做处理
  70. */
  71. private String SOURCEAPP_MALL = "MALL";
  72. /**
  73. * 接收应用
  74. */
  75. private final String CONSUMERAPP_HIDE = "HIDE";
  76. /**
  77. * 消息类型 (在MALL消息中拼接求购询价待报价)
  78. */
  79. private final String INQUIRY_TYPE_SELLER_MALL = "MALL跳转卖家待报价页面";
  80. /**
  81. * 公共消息访问地址
  82. */
  83. private final String PS_MESSAGE_URL = ContextUtils.getBean(AccessConfiguration.class).getPsMessageUrl();
  84. /**
  85. * 询价统计通知发送人UU
  86. */
  87. private final Long MESSAGE_SENDERUU = ContextUtils.getBean(AccessConfiguration.class).getSenderuu();
  88. /**
  89. * 询价统计通知发送企业UU
  90. */
  91. private final Long MESSAGE_SENDERENUU = ContextUtils.getBean(AccessConfiguration.class).getSenderEnuu();
  92. @Autowired
  93. private PublicInquiryItemDao itemDao;
  94. @Autowired
  95. private PurcInquiryItemInfoDao inquiryItemDao;
  96. @Autowired
  97. private PurcInquiryDao purcInquiryDao;
  98. @Autowired
  99. private PublicInquiryDao inquiryDao;
  100. @Autowired
  101. private ProductDao productDao;
  102. @Autowired
  103. private PurcInquiryItemDao purcInquiryItemDao;
  104. @Autowired
  105. private PublicInquiryService inquiryService;
  106. @Autowired
  107. private InquiryRemindDao inquiryRemindDao;
  108. @Autowired
  109. private JdbcTemplate jdbcTemplate;
  110. @Autowired
  111. private EnterpriseDao enterpriseDao;
  112. @Autowired
  113. private PublicInquiryServiceImpl service;
  114. @Autowired
  115. private InquiryEnRemindDao inquiryEnRemindDao;
  116. /**
  117. * 公共物料访问地址
  118. */
  119. private final String PS_PRODUCT_URL = ContextUtils.getBean(AccessConfiguration.class).getPsProductUrl();
  120. /**
  121. * 日志
  122. */
  123. private static final Logger log = LoggerFactory.getLogger(InquiryServiceImpl.class);
  124. /**
  125. * 查询公共询价列表信息
  126. *
  127. * @param info 分页新
  128. * @param filter 过滤条件
  129. * @param state 过滤状态
  130. * @param overdue 是否过期 1、已过期;0、未过期
  131. * @return
  132. */
  133. @Override
  134. public Page<PurcInquiryItemInfo> findTodoByPageInfo(final PageInfo info, SearchFilter filter, String state, Integer overdue) {
  135. Sort sort = new Sort(Sort.Direction.DESC, "date");
  136. if (info.getOffset() == 0) {
  137. info.setOffset(info.getPageSize() * (info.getPageNumber() - 1));
  138. }
  139. info.setSort(sort);
  140. if (null != filter) {
  141. if (null != filter.getUserUU()) {
  142. info.filter("userUU", filter.getUserUU());
  143. info.expression(PredicateUtils.isNull("inquiry.enUU"));
  144. } else if (null != filter.getEnUU()) {
  145. info.filter("inquiry.enUU", filter.getEnUU());
  146. } else {
  147. throw new IllegalAccessError("非法访问");
  148. }
  149. if (StringUtils.hasText(filter.getKeyword())) {
  150. SimpleExpression cmpCode = new SimpleExpression("cmpCode", filter.getKeyword(), CriterionExpression.Operator.LIKE);
  151. SimpleExpression brand = new SimpleExpression("inbrand", filter.getKeyword(), CriterionExpression.Operator.LIKE);
  152. SimpleExpression prodTitle = new SimpleExpression("prodTitle", filter.getKeyword(), CriterionExpression.Operator.LIKE);
  153. SimpleExpression[] simpleExpressions = new SimpleExpression[]{cmpCode, brand, prodTitle};
  154. LogicalExpression logicalExpression = PredicateUtils.or(simpleExpressions);
  155. info.expression(logicalExpression);
  156. }
  157. if (filter.getFromDate() != null) {
  158. info.expression(PredicateUtils.gte("date", new Date(filter.getFromDate()), false));
  159. }
  160. if (filter.getEndDate() != null) {
  161. info.expression(PredicateUtils.lte("date", new Date(filter.getEndDate()), false));
  162. }
  163. }
  164. if (null != state) {
  165. // 待报价
  166. if (state.equals(OrderStatus.todo.name())) {
  167. SimpleExpression amount = new SimpleExpression("offerAmount", Constant.NO, CriterionExpression.Operator.EQ);
  168. SimpleExpression[] simpleExpressions = new SimpleExpression[]{amount};
  169. LogicalExpression logicalExpression = PredicateUtils.and(simpleExpressions);
  170. info.expression(logicalExpression);
  171. }
  172. // 已报价
  173. if (state.equals(OrderStatus.done.name())) {
  174. SimpleExpression amount = new SimpleExpression("offerAmount", Constant.YES, CriterionExpression.Operator.GTE);
  175. SimpleExpression[] simpleExpressions = new SimpleExpression[]{amount};
  176. LogicalExpression logicalExpression = PredicateUtils.and(simpleExpressions);
  177. info.expression(logicalExpression);
  178. }
  179. // 已超过截止日期
  180. if (state.equals(OrderStatus.end.name())) {
  181. SimpleExpression date = new SimpleExpression("endDate", new Date(System.currentTimeMillis()), CriterionExpression.Operator.LT);
  182. SimpleExpression[] simpleExpressions = new SimpleExpression[]{date};
  183. LogicalExpression logicalExpression = PredicateUtils.and(simpleExpressions);
  184. info.expression(logicalExpression);
  185. }
  186. }
  187. if (null != overdue) {
  188. // 已超过截止日期
  189. if (overdue.equals((int)Constant.YES)) {
  190. SimpleExpression date = new SimpleExpression("endDate", new Date(System.currentTimeMillis()), CriterionExpression.Operator.LT);
  191. SimpleExpression[] simpleExpressions = new SimpleExpression[]{date};
  192. LogicalExpression logicalExpression = PredicateUtils.and(simpleExpressions);
  193. info.expression(logicalExpression);
  194. } else if (overdue.equals((int)Constant.NO)) {
  195. info.expression(PredicateUtils.gte("endDate", new Date(System.currentTimeMillis()), false));
  196. }
  197. }
  198. return inquiryItemDao.findAll(new Specification<PurcInquiryItemInfo>() {
  199. @Override
  200. public Predicate toPredicate(Root<PurcInquiryItemInfo> root, CriteriaQuery<?> query, CriteriaBuilder builder) {
  201. query.where(info.getPredicates(root, query, builder));
  202. return null;
  203. }
  204. }, info);
  205. }
  206. /**
  207. * 保存公共询价
  208. *
  209. * @param currentInquiry 询价信息
  210. */
  211. @Override
  212. public PurcInquiry saveInquiry(PurcInquiry currentInquiry) throws NotFoundException {
  213. // amount,code,date设置
  214. if (StringUtils.isEmpty(currentInquiry.getAmount())) {
  215. currentInquiry.setAmount(currentInquiry.getInquiryItems().size());
  216. }
  217. if (StringUtils.isEmpty(currentInquiry.getCode())) {
  218. SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddsss");
  219. try {
  220. currentInquiry.setCode("MALL" + sdf.parse(sdf.format(new Date())).getTime() + StringUtil.getRandomNumber(3) + StringUtil.getRandomNumber(3));
  221. } catch (ParseException e) {
  222. e.printStackTrace();
  223. }
  224. }
  225. //通过流水号和企业号找到公共询价单
  226. List<PurcInquiry> existInquiryList = purcInquiryDao.findByCodeAndEnUU(currentInquiry.getCode(), currentInquiry.getEnUU());
  227. if (!CollectionUtils.isEmpty(existInquiryList)) {
  228. throw new IllegalOperatorException("单号重复");
  229. } else {
  230. if (null != currentInquiry.getEnUU()) {
  231. Enterprise e = enterpriseDao.findOne(currentInquiry.getEnUU());
  232. if (null != e) {
  233. currentInquiry.setEnName(e.getEnName());
  234. } else {
  235. throw new NotFoundException("询价企业不存在");
  236. }
  237. }
  238. //保存询价单
  239. PurcInquiry inquiry = purcInquiryDao.save(currentInquiry);
  240. //判断询价明细单是否为空
  241. if (!CollectionUtils.isEmpty(currentInquiry.getInquiryItems())) {
  242. List<PurcInquiryItem> items = new ArrayList<PurcInquiryItem>();
  243. //给询价明细单属性设置初始值
  244. for (PurcInquiryItem item : currentInquiry.getInquiryItems()) {
  245. item.setInquiry(inquiry);
  246. item.setValid(1);
  247. item.setOfferAmount(0);
  248. item.setStatus((short) Status.NOT_REPLY.value());
  249. item.setIsOpen(Constant.YES);
  250. item.setEnuu(inquiry.getEnUU());
  251. if (null == item.getDate()) {
  252. //设置提交时间
  253. item.setDate(new Date(System.currentTimeMillis()));
  254. }
  255. // 这里设置物料信息的冗余字段
  256. if (null != item.getProduct()) {
  257. item.setProdTitle(item.getProduct().getTitle());
  258. item.setProdCode(item.getProduct().getCode());
  259. item.setSpec(item.getProduct().getSpec());
  260. item.setInbrand(item.getProduct().getBrand());
  261. item.setCmpCode(item.getProduct().getCmpCode());
  262. }
  263. if (null == item.getCmpCode() || item.getCmpCode().equals("无")) {
  264. item.setCmpCode(item.getSpec());
  265. }
  266. if (null == item.getEndDate()) {
  267. item.setEndDate(inquiry.getEndDate());
  268. }
  269. items.add(item);
  270. }
  271. //保存询价单明细表
  272. items = purcInquiryItemDao.save(items);
  273. final List<PurcInquiryItem> purcInquiryItems = items;
  274. final String sourceapp = inquiry.getSourceapp();
  275. final Long enuu = inquiry.getEnUU();
  276. if (null != inquiry.getSourceapp()) {
  277. ThreadUtils.task(new Runnable() {
  278. @Override
  279. public void run() {
  280. try {
  281. //生成推荐询价表
  282. inquiryService.notifyMessage(purcInquiryItems, sourceapp);
  283. if (!StringUtils.isEmpty(enuu)) {
  284. saveProduct(purcInquiryItems, sourceapp);
  285. }
  286. } catch (Exception e) {
  287. e.printStackTrace();
  288. }
  289. }
  290. });
  291. }
  292. }
  293. return inquiry;
  294. }
  295. }
  296. /**
  297. * 可配置的定时任务
  298. */
  299. @Override
  300. public List<InquiryRemind> testMessage(Date startTime, Date endTime, Long useruu, Long enuu){
  301. log.info("公共询价,9点定时统计询价单总数服务开启");
  302. List<InquiryRemind> list = inquiryRemindDao.findTestInfo(startTime,endTime,useruu,enuu);
  303. log.info("公共询价,9点定时统计询价单总数服务结束");
  304. return list;
  305. }
  306. /**
  307. * 发布询价成功后加入当前用户的个人物料库
  308. * @param inquiryItems
  309. * @param sourceApp
  310. */
  311. public void saveProduct(List<PurcInquiryItem> inquiryItems, String sourceApp) {
  312. List<Product> products = new ArrayList<>();
  313. for (PurcInquiryItem item : inquiryItems) {
  314. Product product = new Product();
  315. // 需要哪些字段
  316. product.setEnUU(item.getInquiry().getEnUU());
  317. product.setUserUU(item.getInquiry().getRecorderUU());
  318. product.setCmpCode(item.getCmpCode());
  319. product.setBrand(item.getInbrand());
  320. product.setpCmpCode(item.getCmpCode());
  321. product.setpBrandEn(item.getInbrand());
  322. product.setStandard((short) 0);
  323. product.setCreateTime(new Date());
  324. product.setSourceApp(sourceApp);
  325. product.setSpec(StringUtils.isEmpty(item.getSpec()) ? item.getCmpCode() : item.getSpec());
  326. product.setKind(item.getProdTitle());
  327. products.add(product);
  328. }
  329. // 调用公共物料服务的接口
  330. try {
  331. String url = PS_PRODUCT_URL + "/product/save/inquiry/batch";
  332. long start = System.currentTimeMillis();
  333. String res = HttpUtil.doPost(url, FlexJsonUtils.toJsonDeep(products));
  334. log.info("/product/save/inquiry/batch 耗时:{},物料数:{}", (System.currentTimeMillis() - start), products.size());
  335. JSONObject result = JSONObject.parseObject(res);
  336. Boolean success = (Boolean) result.get("success");
  337. if (success) {
  338. log.info("保存询价单后,批量保存物料成功:{}", products.size());
  339. } else {
  340. log.info("保存询价单后,批量保存物料失败,返回:{}", result.get("message"));
  341. }
  342. } catch (Exception e) {
  343. e.printStackTrace();
  344. }
  345. }
  346. /**
  347. * 通过报价明细id对供应商报价进行相关审核操作
  348. *
  349. * @param id 报价明细id
  350. * @param status 状态
  351. */
  352. @Override
  353. public void decideQuote(Long id, Short status) {
  354. PublicInquiryItem item = itemDao.findOne(id);
  355. // 验证是否重复操作
  356. validateInquiry(item);
  357. item.setAgreed(status);
  358. item.setDecideDownStatus((short) Status.NOT_UPLOAD.value());
  359. itemDao.save(item);
  360. // 更新询价推荐表相关信息
  361. if (status.equals(Constant.YES)) {
  362. inquiryRemindDao.updateStatus(item.getSourceId(), Status.ALLOW.value(), item.getVendUU());
  363. inquiryEnRemindDao.updateStatus(item.getSourceId(), Status.ALLOW.value(), item.getVendUU());
  364. updatePurcInquiryItemAgreed(item.getSourceId(), status);
  365. } else if (status.equals(Constant.NO)) {
  366. inquiryRemindDao.updateStatus(item.getSourceId(), Status.NOTALLOW.value(), item.getVendUU());
  367. inquiryEnRemindDao.updateStatus(item.getSourceId(), Status.NOTALLOW.value(), item.getVendUU());
  368. }
  369. // 更新原公共询价单该条明细,设置为不可报价
  370. String sql = "update purc$puinquiryitems set id_overdue = 1 where id_id = " + item.getSourceId();
  371. jdbcTemplate.update(sql);
  372. inquiryService.notifyDecide(item, InquirySource.B2B.name());
  373. }
  374. /**
  375. * 商城通过报价明细id采纳对供应商报价
  376. *
  377. * @param id 报价明细id
  378. * @param status 状态
  379. */
  380. @Override
  381. public void adpotQuote(Long id, Short status) {
  382. PublicInquiryItem item = itemDao.findOne(id);
  383. // 验证是否重复操作
  384. validateInquiry(item);
  385. item.setAgreed(status);
  386. item.setDecideDownStatus((short) Status.NOT_UPLOAD.value());
  387. itemDao.save(item);
  388. // 更新询价推荐表相关信息
  389. if (status.equals(Constant.YES)) {
  390. inquiryRemindDao.updateStatus(item.getSourceId(), Status.ALLOW.value(), item.getVendUU());
  391. inquiryEnRemindDao.updateStatus(item.getSourceId(), Status.ALLOW.value(), item.getVendUU());
  392. // 修改询价明细agreed字段为1,表示存在已采纳报价明细
  393. updatePurcInquiryItemAgreed(item.getSourceId(), status);
  394. } else if (status.equals(Constant.NO)) {
  395. inquiryRemindDao.updateStatus(item.getSourceId(), Status.NOTALLOW.value(), item.getVendUU());
  396. inquiryEnRemindDao.updateStatus(item.getSourceId(), Status.NOTALLOW.value(), item.getVendUU());
  397. }
  398. // 更新原公共询价单该条明细,设置为不可报价
  399. String sql = "update purc$puinquiryitems set id_overdue = 1 where id_id = " + item.getSourceId();
  400. jdbcTemplate.update(sql);
  401. // 如果是替代料报价的采纳,添加替代料到询价方对应物料
  402. if (status.equals(Constant.YES)) {
  403. addProductReplace(item);
  404. }
  405. inquiryService.adoptMessage(item, InquirySource.MALL.name());
  406. }
  407. /**
  408. * 更新询价单明细采纳状态
  409. * @param id 询价单明细id
  410. * @param agreed 采纳参数 1 或 0
  411. */
  412. private void updatePurcInquiryItemAgreed(Long id, short agreed) {
  413. PurcInquiryItem inquiryItem = purcInquiryItemDao.findOne(id);
  414. inquiryItem.setAgreed(agreed);
  415. purcInquiryItemDao.save(inquiryItem);
  416. }
  417. /**
  418. * 替代料报价采纳之后,添加替代料到询价企业对应物料
  419. * @param item 报价明细
  420. */
  421. private void addProductReplace(PublicInquiryItem item) {
  422. if (null != item.getIsReplace() && Constant.YES == item.getIsReplace() && !StringUtils.isEmpty(item.getReplaceCmpCode())
  423. && !StringUtils.isEmpty(item.getReplaceBrand())) {
  424. try {
  425. String url = PS_PRODUCT_URL + "/product/save/inquiry?replaceCmpCode=" + URLEncoder.encode(item.getReplaceCmpCode(), "UTF-8")
  426. + "&replaceBrand=" + URLEncoder.encode(item.getReplaceBrand(), "UTF-8");
  427. Product product;
  428. if (null != item.getProductId()) {
  429. product = productDao.findOne(item.getProductId());
  430. product.setUserUU(item.getInquiry().getRecorderUU());
  431. } else {
  432. product = new Product();
  433. // 需要哪些字段
  434. product.setEnUU(item.getInquiry().getEnUU());
  435. product.setUserUU(item.getInquiry().getRecorderUU());
  436. product.setCmpCode(item.getCmpCode());
  437. product.setBrand(item.getInbrand());
  438. product.setpCmpCode(item.getCmpCode());
  439. product.setpBrandEn(item.getInbrand());
  440. product.setStandard((short) 0);
  441. product.setCreateTime(new Date());
  442. product.setSourceApp(item.getInquiry().getSourceApp());
  443. product.setSpec(StringUtils.isEmpty(item.getSpec()) ? item.getCmpCode() : item.getSpec());
  444. product.setKind(item.getProdTitle());
  445. }
  446. long start = System.currentTimeMillis();
  447. String res = HttpUtil.doPost(url, FlexJsonUtils.toJsonDeep(product));
  448. log.info("/product/save/inquiry 耗时:{}", (System.currentTimeMillis() - start));
  449. log.info("采纳替代物料报价,添加替代物料返回{}", res);
  450. } catch (Exception e) {
  451. log.info("/product/save/inquiry 替代料报价采纳之后保存到个人物料库中 出错:{}", e.getMessage());
  452. e.printStackTrace();
  453. }
  454. }
  455. }
  456. /**
  457. * 验证是否已采纳或拒绝
  458. * @param item 报价单
  459. */
  460. private void validateInquiry(PublicInquiryItem item) {
  461. if (null != item.getAgreed()) {
  462. if (item.getAgreed().equals(Constant.YES)) {
  463. throw new IllegalOperatorException("该报价已被采纳,请勿重复处理");
  464. } else if (item.getAgreed().equals(Constant.NO)) {
  465. throw new IllegalOperatorException("该报价已被拒绝,请勿重复处理");
  466. }
  467. }
  468. }
  469. /**
  470. * 商城通过报价明细id拒绝对供应商报价
  471. *
  472. * @param id 报价明细id
  473. * @param status 状态
  474. */
  475. @Override
  476. public void refuseQuote(Long id, Short status,String refusereason) {
  477. PublicInquiryItem item = itemDao.findOne(id);
  478. // 验证是否重复操作
  479. validateInquiry(item);
  480. item.setAgreed(status);
  481. item.setRefusereason(refusereason);
  482. item.setDecideDownStatus((short) Status.NOT_UPLOAD.value());
  483. itemDao.save(item);
  484. if (status.equals(Constant.YES)) {
  485. inquiryRemindDao.updateStatus(item.getSourceId(), Status.ALLOW.value(), item.getVendUU());
  486. inquiryEnRemindDao.updateStatus(item.getSourceId(), Status.ALLOW.value(), item.getVendUU());
  487. updatePurcInquiryItemAgreed(item.getSourceId(), status);
  488. } else if (status.equals(Constant.NO)) {
  489. inquiryRemindDao.updateStatus(item.getSourceId(), Status.NOTALLOW.value(), item.getVendUU());
  490. inquiryEnRemindDao.updateStatus(item.getSourceId(), Status.NOTALLOW.value(), item.getVendUU());
  491. }
  492. inquiryService.refuseMessage(item,InquirySource.MALL.name());
  493. }
  494. /**
  495. * 针对客户,查询供应商报价详情
  496. *
  497. * @param id 主表id
  498. * @param enuu 企业UU
  499. * @return
  500. */
  501. @Override
  502. public InquiryDetailInfo findById(Long id, Long enuu) {
  503. PublicInquiry inquiry = inquiryDao.findOne(id);
  504. InquiryDetailInfo inquiryInfo = new InquiryDetailInfo();
  505. if (inquiry != null) {
  506. inquiryInfo.setAttachs(inquiry.getAttachs());
  507. inquiryInfo.setAuditor(inquiry.getAuditor());
  508. inquiryInfo.setCheck(inquiry.getCheck());
  509. inquiryInfo.setCode(inquiry.getCode());
  510. inquiryInfo.setDate(inquiry.getDate());
  511. inquiryInfo.setEndDate(inquiry.getEndDate());
  512. inquiryInfo.setEnterprise(inquiry.getEnterprise());
  513. inquiryInfo.setEnUU(inquiry.getEnUU());
  514. inquiryInfo.setEnvironment(inquiry.getEnvironment());
  515. inquiryInfo.setId(inquiry.getId());
  516. inquiryInfo.setIsOpen(inquiry.getIsOpen());
  517. inquiryInfo.setOverdue(inquiry.getOverdue());
  518. inquiryInfo.setPriceType(inquiry.getPriceType());
  519. inquiryInfo.setRecorder(inquiry.getRecorder());
  520. inquiryInfo.setRecorderUU(inquiry.getRecorderUU());
  521. inquiryInfo.setRemark(inquiry.getRemark());
  522. inquiryInfo.setSourceApp(inquiry.getSourceApp());
  523. inquiryInfo.setSourceId(inquiry.getSourceId());
  524. inquiryInfo.setShip(inquiry.getShip());
  525. inquiryInfo.setInvoice(inquiry.getInvoice());
  526. inquiryInfo.setInquirytype(inquiry.getInquirytype());
  527. Set<Long> ids = new HashSet<Long>();
  528. List<Long> idList = new ArrayList<>();
  529. Set<InquiryProductInfo> products = new HashSet<InquiryProductInfo>();
  530. if (!CollectionUtils.isEmpty(inquiry.getInquiryItems())) {
  531. for (PublicInquiryItem item : inquiry.getInquiryItems()) {
  532. if (item.getProductId() != null) {
  533. idList.add(item.getProductId());
  534. ids.addAll(idList);
  535. } else {
  536. InquiryProductInfo productInfo = new InquiryProductInfo();
  537. Set<PublicInquiryItem> items = new HashSet<PublicInquiryItem>();
  538. productInfo.setBrand(item.getInbrand());
  539. productInfo.setSpec(item.getSpec());
  540. productInfo.setCode(item.getProdCode());
  541. productInfo.setTitle(item.getProdTitle());
  542. productInfo.setUnit(item.getUnit());
  543. productInfo.setCmpCode(item.getCmpCode());
  544. products.add(productInfo);
  545. for (PublicInquiryItem item1 : inquiry.getInquiryItems()) {
  546. if (item1.getCmpCode() != null && item1.getCmpCode().equals(productInfo.getCmpCode())) {
  547. String sql = "select at_path,at_name from attachs a left join public$inquiryitems$attach p on a.at_id = p.at_id " +
  548. "left join public$inquiryitems i on p.id_id = i.id_id where i.id_id = " + item1.getId();
  549. List<Map<String, Object>> maps = jdbcTemplate.queryForList(sql);
  550. if (!org.springframework.util.CollectionUtils.isEmpty(maps)) {
  551. Map<String, Object> map = maps.get(0);
  552. item1.setAttachUrl(map.get("at_path").toString());
  553. item1.setAttachName(map.get("at_name").toString());
  554. }
  555. items.add(item1);
  556. }
  557. }
  558. productInfo.setInquiryItems(items);
  559. inquiryInfo.setProducts(products);
  560. }
  561. }
  562. }
  563. if (!CollectionUtils.isEmpty(ids)) {
  564. for (Long idInfo : ids) {
  565. InquiryProductInfo productInfo = new InquiryProductInfo();
  566. Set<PublicInquiryItem> items = new HashSet<PublicInquiryItem>();
  567. Product product = productDao.findOne(idInfo);
  568. productInfo.setBrand(product.getBrand());
  569. productInfo.setId(product.getId());
  570. productInfo.setSpec(product.getSpec());
  571. productInfo.setCode(product.getCode());
  572. productInfo.setTitle(product.getTitle());
  573. productInfo.setUnit(product.getUnit());
  574. productInfo.setCmpCode(product.getCmpCode());
  575. for (PublicInquiryItem item : inquiry.getInquiryItems()) {
  576. if (item.getProductId().equals(idInfo)) {
  577. String sql = "select at_path,at_name from attachs a left join public$inquiryitems$attach p on a.at_id = p.at_id " +
  578. "left join public$inquiryitems i on p.id_id = i.id_id where i.id_id = " + item.getId();
  579. List<Map<String, Object>> maps = jdbcTemplate.queryForList(sql);
  580. if (!org.springframework.util.CollectionUtils.isEmpty(maps)) {
  581. Map<String, Object> map = maps.get(0);
  582. item.setAttachUrl(map.get("at_path").toString());
  583. item.setAttachName(map.get("at_name").toString());
  584. }
  585. items.add(item);
  586. }
  587. }
  588. productInfo.setInquiryItems(items);
  589. products.add(productInfo);
  590. inquiryInfo.setProducts(products);
  591. }
  592. }
  593. }
  594. return inquiryInfo;
  595. }
  596. /**
  597. * 通过企业UU和分页信息等查询已发布信息
  598. *
  599. * @param info 分页信息
  600. * @param filter 过滤条件
  601. * @return
  602. */
  603. @Override
  604. public Page<PurcInquiry> findByPageInfo(final PageInfo info, SearchFilter filter) {
  605. if (info.getOffset() == 0) {
  606. info.setOffset(info.getPageSize() * (info.getPageNumber() - 1));
  607. }
  608. Sort sort = new Sort(Sort.Direction.DESC, "date");
  609. if (info.getPageNumber() == 0) {
  610. info.setPageNumber(1);
  611. }
  612. if (info.getPageSize() == 0) {
  613. info.setPageSize(5);
  614. }
  615. info.setSort(sort);
  616. if (null == filter) {
  617. throw new IllegalOperatorException("未传入过滤条件");
  618. }
  619. if (null == filter.getUserUU() && null == filter.getEnUU()) {
  620. throw new IllegalAccessError("非法访问");
  621. }
  622. if (null != filter.getUserUU()) {
  623. info.expression(PredicateUtils.eq("recorderUU", filter.getUserUU(), false));
  624. }
  625. if (null != filter.getEnUU()) {
  626. info.expression(PredicateUtils.eq("enUU", filter.getEnUU(), false));
  627. } else {
  628. info.expression(PredicateUtils.isNull("enUU"));
  629. }
  630. if (!StringUtils.isEmpty(filter.getKeyword())) {
  631. SimpleExpression code = new SimpleExpression("remark", filter.getKeyword(), CriterionExpression.Operator.LIKE);
  632. SimpleExpression[] simpleExpressions = new SimpleExpression[]{code};
  633. LogicalExpression logicalExpression = PredicateUtils.or(simpleExpressions);
  634. info.expression(logicalExpression);
  635. }
  636. if (filter.getFromDate() != null) {
  637. info.expression(PredicateUtils.gte("date", new Date(filter.getFromDate()), false));
  638. }
  639. if (filter.getEndDate() != null) {
  640. info.expression(PredicateUtils.lte("date", new Date(filter.getEndDate()), false));
  641. }
  642. SimpleExpression amount = new SimpleExpression("amount", Constant.YES, CriterionExpression.Operator.GT);
  643. SimpleExpression[] simpleExpressions = new SimpleExpression[]{amount};
  644. LogicalExpression logicalExpression = PredicateUtils.and(simpleExpressions);
  645. info.expression(logicalExpression);
  646. return purcInquiryDao.findAll(new Specification<PurcInquiry>() {
  647. @Override
  648. public Predicate toPredicate(Root<PurcInquiry> root, CriteriaQuery<?> query, CriteriaBuilder builder) {
  649. query.where(info.getPredicates(root, query, builder));
  650. return null;
  651. }
  652. }, info);
  653. }
  654. /**
  655. * 针对客户,查询单个物料的报价情况
  656. *
  657. * @param id 明细id
  658. * @param enuu 当前企业UU
  659. * @return
  660. */
  661. @Override
  662. public InquiryProductInfo findInquiryDetailById(Long id, Long enuu) {
  663. List<PublicInquiryItem> items = itemDao.findBySourceId(id);
  664. InquiryProductInfo productInfo = new InquiryProductInfo();
  665. if (!CollectionUtils.isEmpty(items)) {
  666. Long prId = items.get(0).getProductId();
  667. if (null != prId) {
  668. Product product = productDao.findOne(prId);
  669. productInfo.setBrand(product.getBrand());
  670. productInfo.setCode(product.getCode());
  671. productInfo.setTitle(product.getTitle());
  672. productInfo.setSpec(product.getSpec());
  673. productInfo.setUnit(product.getUnit());
  674. productInfo.setCmpCode(product.getCmpCode());
  675. } else {
  676. productInfo.setCmpCode(items.get(0).getCmpCode());
  677. productInfo.setBrand(items.get(0).getInbrand());
  678. }
  679. productInfo.setInquiryItems(new HashSet<PublicInquiryItem>(items));
  680. }
  681. return productInfo;
  682. }
  683. /**
  684. * 针对客户查询供应商报价信息
  685. *
  686. * @param pageInfo 分页信息
  687. * @param filter 过滤条件
  688. * @return
  689. */
  690. @Override
  691. public Page<PurcInquiryItemInfo> findQuotationsByPage(final PageInfo pageInfo, final SearchFilter filter, Long enUU, Long userUU, Short overdue) {
  692. if (enUU == null && userUU == null) {
  693. throw new IllegalAccessError("非法访问");
  694. }
  695. Sort sort = new Sort(Sort.Direction.DESC, "date");
  696. if (pageInfo.getOffset() == 0) {
  697. pageInfo.setOffset(pageInfo.getPageSize() * (pageInfo.getPageNumber() - 1));
  698. }
  699. pageInfo.setSort(sort);
  700. if (enUU != null) {
  701. pageInfo.filter("inquiry.enUU", enUU);
  702. } else {
  703. if (userUU != null) {
  704. pageInfo.filter("inquiry.recorderUU", userUU);
  705. pageInfo.expression(PredicateUtils.isNull("inquiry.enUU"));
  706. }
  707. }
  708. if (StringUtils.hasText(filter.getKeyword())) {
  709. SimpleExpression cmpCode = new SimpleExpression("cmpCode", filter.getKeyword(), CriterionExpression.Operator.LIKE);
  710. SimpleExpression inbrand = new SimpleExpression("inbrand", filter.getKeyword(), CriterionExpression.Operator.LIKE);
  711. SimpleExpression[] simpleExpressions = new SimpleExpression[]{inbrand, cmpCode};
  712. LogicalExpression logicalExpression = PredicateUtils.or(simpleExpressions);
  713. pageInfo.expression(logicalExpression);
  714. }
  715. if (filter.getFromDate() != null) {
  716. pageInfo.expression(PredicateUtils.gte("date", new Date(filter.getFromDate()), false));
  717. }
  718. if (filter.getEndDate() != null) {
  719. pageInfo.expression(PredicateUtils.lte("date", new Date(filter.getEndDate()), false));
  720. }
  721. if (null != overdue) {
  722. // 已超过截止日期
  723. if (overdue.equals(Constant.YES)) {
  724. pageInfo.expression(PredicateUtils.lt("endDate", new Date(System.currentTimeMillis()), false));
  725. } else if (overdue.equals(Constant.NO)) {
  726. pageInfo.expression(PredicateUtils.gte("endDate", new Date(System.currentTimeMillis()), false));
  727. }
  728. }
  729. pageInfo.expression(PredicateUtils.gt("offerAmount", Constant.NO, false));
  730. Page<PurcInquiryItemInfo> items = inquiryItemDao.findAll(new Specification<PurcInquiryItemInfo>() {
  731. @Override
  732. public Predicate toPredicate(Root<PurcInquiryItemInfo> root, CriteriaQuery<?> query, CriteriaBuilder builder) {
  733. query.where(pageInfo.getPredicates(root, query, builder));
  734. return null;
  735. }
  736. }, pageInfo);
  737. if (!CollectionUtils.isEmpty(items.getContent())) {
  738. for (PurcInquiryItemInfo itemInfo : items.getContent()) {
  739. List<PublicInquiryItem> itemList = itemDao.findBySourceIdOrderByOfferTimeDesc(itemInfo.getId());
  740. itemInfo.setQutations(itemList);
  741. itemInfo.setQuotation(itemList.get(0));
  742. itemInfo.setAgreed(Constant.NO);
  743. if (!CollectionUtils.isEmpty(itemList)) {
  744. for (PublicInquiryItem i : itemList) {
  745. if (i.getAgreed() != null && i.getAgreed().equals(Constant.YES)) {
  746. itemInfo.setAgreed(Constant.YES);
  747. break;
  748. }
  749. }
  750. }
  751. if (null != itemInfo.getInquiry().getEndDate()) {
  752. itemInfo.setRemainingTime(itemInfo.getInquiry().getEndDate().getTime() - System.currentTimeMillis());
  753. }
  754. if (null == itemInfo.getProductId()) {
  755. Product product = new Product();
  756. product.setTitle(itemInfo.getProdTitle());
  757. product.setCmpCode(itemInfo.getCmpCode());
  758. product.setBrand(itemInfo.getInbrand());
  759. product.setSpec(itemInfo.getSpec());
  760. itemInfo.setProduct(product);
  761. }
  762. }
  763. }
  764. return items;
  765. }
  766. /**
  767. * 针对客户单个公共询价,查询供应商报价信息
  768. *
  769. * @param id
  770. * @return
  771. */
  772. @Override
  773. public PurcInquiryItemInfo findQuotationById(Long id) {
  774. PurcInquiryItemInfo itemInfo = inquiryItemDao.findOne(id);
  775. List<PublicInquiryItem> itemList = itemDao.findBySourceId(itemInfo.getId());
  776. itemInfo.setQutations(itemList);
  777. itemInfo.setAgreed(Constant.NO);
  778. if (!CollectionUtils.isEmpty(itemList)) {
  779. for (PublicInquiryItem i : itemList) {
  780. if (i.getAgreed() != null && i.getAgreed().equals(Constant.YES)) {
  781. itemInfo.setAgreed(Constant.YES);
  782. break;
  783. }
  784. }
  785. }
  786. if (null != itemInfo.getInquiry().getEndDate()) {
  787. itemInfo.setRemainingTime(itemInfo.getInquiry().getEndDate().getTime() - System.currentTimeMillis());
  788. }
  789. if (null == itemInfo.getProductId()) {
  790. Product product = new Product();
  791. product.setTitle(itemInfo.getProdTitle());
  792. product.setCmpCode(itemInfo.getCmpCode());
  793. product.setBrand(itemInfo.getInbrand());
  794. product.setSpec(itemInfo.getSpec());
  795. itemInfo.setProduct(product);
  796. }
  797. return itemInfo;
  798. }
  799. /**
  800. * 发送消息推送
  801. *
  802. */
  803. public void sendMessage(final Integer count, final List<InquiryRemind> reminds) {
  804. ThreadUtils.task(new Runnable() {
  805. @Override
  806. public void run() {
  807. try {
  808. List<MessageModel> models = new ArrayList<>();
  809. MessageModel model = new MessageModel();
  810. model.setType(INQUIRY_TYPE);
  811. model.setType(INQUIRY_TYPE_SELLER_MALL);
  812. model.setProducerApp("MALl");
  813. model.setConsumerType(CUST_TYPE);
  814. model.setConsumerApp(CONSUMERAPP_HIDE);
  815. model.setRemark(String.valueOf(count));
  816. for(InquiryRemind remind : reminds){
  817. model.setReceiverEnuu(remind.getVendUU());
  818. model.setReceiverUu(remind.getVendUserUU());
  819. model.setSenderEnuu(MESSAGE_SENDERENUU);
  820. model.setSenderUu(MESSAGE_SENDERUU);
  821. }
  822. String company = "";
  823. Enterprise enterprise = enterpriseDao.findOne(reminds.get(0).getVendUU());
  824. if(null != enterprise){
  825. company = enterprise.getEnName();
  826. }
  827. String content = company + "新增了"+ count +"张公共询价单,快登录优软商城查看详情吧!https://www.usoftmall.com/vendor#/seekPurchase";
  828. model.setContent(content);
  829. model.setSmsType(SMS_TYPE);
  830. model.setSmTemplate(SMS_TEMP_ID);
  831. models.add(model);
  832. long start = System.currentTimeMillis();
  833. HttpUtil.doPost(PS_MESSAGE_URL + "/messages", FlexJsonUtils.toJsonDeep(models));
  834. log.info("/message 耗时:{},消息数:{}", (System.currentTimeMillis() - start), models.size());
  835. log.info("公共询价", "此次{}公司新增{}张公共询价", company, count);
  836. } catch (Exception e) {
  837. e.printStackTrace();
  838. }
  839. }
  840. });
  841. }
  842. /**
  843. * 发送消息推送 (定时任务使用)
  844. *
  845. */
  846. @Override
  847. public void sendMessage(final List<InquiryRemind> reminds) {
  848. ThreadUtils.task(new Runnable() {
  849. @Override
  850. public void run() {
  851. try {
  852. log.info("发送消息开始");
  853. List<MessageModel> models = new ArrayList<>();
  854. for (InquiryRemind remind : reminds) {
  855. if (null != remind.getVendUU() && null != remind.getVendUserUU()) {
  856. Integer count = remind.getCounts();
  857. MessageModel model = new MessageModel();
  858. model.setType(INQUIRY_TYPE);
  859. model.setType(INQUIRY_TYPE_SELLER_MALL);
  860. model.setProducerApp(SOURCEAPP_MALL);
  861. model.setConsumerType(CUST_TYPE);
  862. model.setConsumerApp(CONSUMERAPP_HIDE);
  863. model.setRemark(String.valueOf(count));
  864. model.setReceiverEnuu(remind.getVendUU());
  865. model.setReceiverUu(remind.getVendUserUU());
  866. model.setSenderEnuu(MESSAGE_SENDERENUU);
  867. model.setSenderUu(MESSAGE_SENDERUU);
  868. String company = "";
  869. Enterprise enterprise = enterpriseDao.findOne(remind.getVendUU());
  870. if(null != enterprise){
  871. company = enterprise.getEnName();
  872. }
  873. String content = company + "新增了"+ count +"张公共询价单,快登录优软商城查看详情吧!https://www.usoftmall.com/vendor#/seekPurchase";
  874. model.setContent(content);
  875. model.setSmsType(SMS_TYPE);
  876. model.setSmTemplate(SMS_TEMP_ID);
  877. models.add(model);
  878. log.info("此次{}公司新增{}张公共询价(发送信息前),接收人UU:{}", company, remind.getCounts(), model.getReceiverUu());
  879. }
  880. if (models.size() >= 500) {
  881. long start = System.currentTimeMillis();
  882. String res = HttpUtil.doPost(PS_MESSAGE_URL + "/messages", FlexJsonUtils.toJsonDeep(models));
  883. log.info("消息中心生成消息");
  884. log.info("发送消息{},耗时:{}", models.size(), (System.currentTimeMillis() - start));
  885. models = new ArrayList<>();
  886. }
  887. }
  888. if (!CollectionUtils.isEmpty(models)) {
  889. long start = System.currentTimeMillis();
  890. String res = HttpUtil.doPost(PS_MESSAGE_URL + "/messages", FlexJsonUtils.toJsonDeep(models));
  891. log.info("消息中心生成消息");
  892. log.info("发送消息{},耗时:{}", models.size(), (System.currentTimeMillis() - start));
  893. }
  894. log.info("发送消息全部完成");
  895. } catch (Exception e) {
  896. e.printStackTrace();
  897. }
  898. }
  899. });
  900. }
  901. /**
  902. * 以供应商企业UU和用户UU分组查询商机
  903. * @param hours 查之前多少小时内的单据
  904. * @return 商机List
  905. */
  906. @Override
  907. public List<InquiryRemind> findInquiryRemindGroupByVendUUAndVendUserUU(int hours) {
  908. String sql = "select ir_venduu vendUU,ir_venduseruu vendUserUU, count(1) counts from purc$inquiry$remind where ir_date between date_sub(now(), interval "
  909. + hours + " hour) and now() group by ir_venduu,ir_venduseruu";
  910. return jdbcTemplate.query(sql, new BeanPropertyRowMapper<InquiryRemind>(InquiryRemind.class));
  911. }
  912. /**
  913. * 讲报价产品数设置进page
  914. *
  915. * @param page 询价单page
  916. * @return 询价单pege
  917. */
  918. @Override
  919. public Page<PurcInquiry> setQuotedAmount(Page<PurcInquiry> page) {
  920. for (PurcInquiry inquiry : page.getContent()) {
  921. List<Long> itemIds = new ArrayList<>();
  922. Long[] itemIdArray = new Long[inquiry.getInquiryItems().size()];
  923. for (PurcInquiryItem item : inquiry.getInquiryItems()) {
  924. itemIds.add(item.getId());
  925. }
  926. Integer size = itemDao.getQuotedAmountBySourceId(itemIds.toArray(itemIdArray));
  927. inquiry.setQuotedAmount(size);
  928. }
  929. return page;
  930. }
  931. }