KuaishouItemCollectSamplesServiceImpl.java 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682
  1. package com.ruixuan.isc.service.impl;
  2. import com.alibaba.fastjson.JSONArray;
  3. import com.alibaba.fastjson.JSONObject;
  4. import com.google.gson.Gson;
  5. import com.kuaidi100.sdk.api.Subscribe;
  6. import com.kuaidi100.sdk.contant.ApiInfoConstant;
  7. import com.kuaidi100.sdk.core.IBaseClient;
  8. import com.kuaidi100.sdk.request.SubscribeParam;
  9. import com.kuaidi100.sdk.request.SubscribeParameters;
  10. import com.kuaidi100.sdk.request.SubscribeReq;
  11. import com.ruixuan.common.core.domain.entity.SysUser;
  12. import com.ruixuan.common.utils.Check;
  13. import com.ruixuan.common.utils.DateUtils;
  14. import com.ruixuan.common.utils.PageUtils;
  15. import com.ruixuan.isc.entity.KuaishouItemCollectSampleExpress;
  16. import com.ruixuan.isc.entity.KuaishouItemCollectSampleLog;
  17. import com.ruixuan.isc.entity.KuaishouItemCollectSamples;
  18. import com.ruixuan.isc.mapper.KuaishouItemCollectSamplesMapper;
  19. import com.ruixuan.isc.service.IKuaishouItemCollectSamplesService;
  20. import com.ruixuan.isc.service.IKuaishouPromoterService;
  21. import com.ruixuan.isc.service.ISupplyChainService;
  22. import com.ruixuan.system.service.ISysDeptService;
  23. import com.ruixuan.system.service.ISysUserService;
  24. import org.springframework.beans.factory.annotation.Autowired;
  25. import org.springframework.stereotype.Service;
  26. import java.math.BigDecimal;
  27. import java.math.RoundingMode;
  28. import java.util.ArrayList;
  29. import java.util.Arrays;
  30. import java.util.Collections;
  31. import java.util.Comparator;
  32. import java.util.Date;
  33. import java.util.HashMap;
  34. import java.util.List;
  35. import java.util.Map;
  36. /**
  37. * 【请填写功能名称】Service业务层处理
  38. *
  39. * @author ruoyi
  40. * @date 2023-02-08
  41. */
  42. @Service
  43. public class KuaishouItemCollectSamplesServiceImpl implements IKuaishouItemCollectSamplesService {
  44. @Autowired
  45. private KuaishouItemCollectSamplesMapper kuaishouItemCollectSamplesMapper;
  46. @Autowired
  47. private ISysUserService userService;
  48. @Autowired
  49. private IKuaishouPromoterService promoterService;
  50. @Autowired
  51. private ISupplyChainService supplyChainService;
  52. @Autowired
  53. private ISysDeptService deptService;
  54. /**
  55. * 查询【请填写功能名称】
  56. *
  57. * @param id 【请填写功能名称】主键
  58. * @return 【请填写功能名称】
  59. */
  60. @Override
  61. public KuaishouItemCollectSamples selectKuaishouItemCollectSamplesById(Long id) {
  62. return kuaishouItemCollectSamplesMapper.selectKuaishouItemCollectSamplesById(id);
  63. }
  64. /**
  65. * 查询【请填写功能名称】列表
  66. *
  67. * @param requestMap 【请填写功能名称】
  68. * @return 【请填写功能名称】
  69. */
  70. @Override
  71. public List<KuaishouItemCollectSamples> selectKuaishouItemCollectSamplesList(Map<String, Object> requestMap) {
  72. List<KuaishouItemCollectSamples> kuaishouItemCollectSamples = kuaishouItemCollectSamplesMapper.selectKuaishouItemCollectSamplesList(requestMap);
  73. Map<Long, JSONObject> dataMap = new HashMap<>();
  74. if (!Check.isNull(kuaishouItemCollectSamples)) {
  75. Map<Long, Long> map = new HashMap<>();
  76. for (KuaishouItemCollectSamples samples : kuaishouItemCollectSamples) {
  77. map.put(samples.getPromoterId(), samples.getPromoterId());
  78. }
  79. List<JSONObject> promoterList = kuaishouItemCollectSamplesMapper.selectPromoterInfo(map);
  80. for (int i = 0; i < promoterList.size(); i++) {
  81. JSONObject jsonObject = promoterList.get(i);
  82. dataMap.put(jsonObject.getLong("promoterId"), jsonObject);
  83. }
  84. }
  85. if (!Check.isNull(kuaishouItemCollectSamples)) {
  86. for (KuaishouItemCollectSamples samples : kuaishouItemCollectSamples) {
  87. Long promoterId = samples.getPromoterId();
  88. JSONObject jsonObject = dataMap.get(promoterId);
  89. if (!Check.isNull(jsonObject)) {
  90. samples.setValidAmount(jsonObject.getLong("validAmount"));
  91. samples.setValidOrderNum(jsonObject.getLong("validOrderNum"));
  92. }
  93. }
  94. }
  95. return kuaishouItemCollectSamples;
  96. }
  97. /**
  98. * 新增【请填写功能名称】
  99. *
  100. * @param kuaishouItemCollectSamples 【请填写功能名称】
  101. * @return 结果
  102. */
  103. @Override
  104. public int insertKuaishouItemCollectSamples(KuaishouItemCollectSamples kuaishouItemCollectSamples) {
  105. kuaishouItemCollectSamples.setCreateTime(DateUtils.getNowDate());
  106. return kuaishouItemCollectSamplesMapper.insertKuaishouItemCollectSamples(kuaishouItemCollectSamples);
  107. }
  108. /**
  109. * 修改【请填写功能名称】
  110. *
  111. * @param kuaishouItemCollectSamples 【请填写功能名称】
  112. * @return 结果
  113. */
  114. @Override
  115. public int updateKuaishouItemCollectSamples(KuaishouItemCollectSamples kuaishouItemCollectSamples) {
  116. kuaishouItemCollectSamples.setUpdateTime(DateUtils.getNowDate());
  117. return kuaishouItemCollectSamplesMapper.updateKuaishouItemCollectSamples(kuaishouItemCollectSamples);
  118. }
  119. /**
  120. * 批量删除【请填写功能名称】
  121. *
  122. * @param ids 需要删除的【请填写功能名称】主键
  123. * @return 结果
  124. */
  125. @Override
  126. public int deleteKuaishouItemCollectSamplesByIds(Long[] ids) {
  127. return kuaishouItemCollectSamplesMapper.deleteKuaishouItemCollectSamplesByIds(ids);
  128. }
  129. /**
  130. * 删除【请填写功能名称】信息
  131. *
  132. * @param id 【请填写功能名称】主键
  133. * @return 结果
  134. */
  135. @Override
  136. public int deleteKuaishouItemCollectSamplesById(Long id) {
  137. return kuaishouItemCollectSamplesMapper.deleteKuaishouItemCollectSamplesById(id);
  138. }
  139. @Override
  140. public JSONObject preview(Long userId, String nickName, JSONArray itemIds, JSONArray promoterIds) {
  141. JSONObject previewJson = new JSONObject();
  142. List<JSONObject> itemList = kuaishouItemCollectSamplesMapper.getItemList(itemIds);
  143. Map<Long, JSONObject> itemMap = new HashMap<>();
  144. for (int i = 0; i < itemList.size(); i++) {
  145. JSONObject itemJson = itemList.get(i);
  146. itemMap.put(itemJson.getLong("itemId"), itemJson);
  147. }
  148. Map<Long, JSONObject> promoterMap = new HashMap<>();
  149. List<JSONObject> promoterList = kuaishouItemCollectSamplesMapper.promoterList(promoterIds);
  150. for (int i = 0; i < promoterList.size(); i++) {
  151. JSONObject promoterJson = promoterList.get(i);
  152. promoterMap.put(promoterJson.getLong("promoterId"), promoterJson);
  153. }
  154. Integer sampleReceivedCount = 0;
  155. List<JSONObject> samples = new ArrayList<>();
  156. for (int i = 0; i < itemIds.size(); i++) {
  157. Long itemId = itemIds.getLong(i);
  158. JSONObject itemJson = itemMap.get(itemId);
  159. for (int j = 0; j < promoterIds.size(); j++) {
  160. Long promoterId = promoterIds.getLong(j);
  161. Integer count = kuaishouItemCollectSamplesMapper.check(itemId, promoterId);
  162. if (count > 0) {
  163. sampleReceivedCount++;
  164. continue;
  165. }
  166. JSONObject sampleJson = new JSONObject();
  167. sampleJson.put("itemId", itemJson.getLong("itemId"));
  168. sampleJson.put("itemTitle", itemJson.getString("itemTitle"));
  169. sampleJson.put("itemPrice", itemJson.getLong("itemPrice"));
  170. sampleJson.put("itemImgUrl", itemJson.getString("itemImgUrl"));
  171. sampleJson.put("commissionRate", itemJson.getLong("commissionRate"));
  172. sampleJson.put("regimentalPromotion", itemJson.getLong("regimentalPromotion"));
  173. sampleJson.put("itemCreateId", itemJson.getLong("userId"));
  174. sampleJson.put("itemCreateName", itemJson.getString("userName"));
  175. sampleJson.put("collectSampleId", userId);
  176. sampleJson.put("collectSampleName", nickName);
  177. JSONObject promoterJson = promoterMap.get(promoterId);
  178. sampleJson.put("promoterId", promoterJson.getLong("promoterId"));
  179. sampleJson.put("promoterNickName", promoterJson.getString("promoterNickName"));
  180. sampleJson.put("promoterPhone", promoterJson.getString("promoterPhone"));
  181. sampleJson.put("promoterAddress", promoterJson.getString("promoterAddress"));
  182. sampleJson.put("consignee", promoterJson.getString("consignee"));
  183. sampleJson.put("promoterUrl", promoterJson.getString("promoterUrl"));
  184. sampleJson.put("collectSampleStatus", 1);
  185. sampleJson.put("collectSampleDesc", "待招商审核");
  186. samples.add(sampleJson);
  187. }
  188. previewJson.put("sampleReceivedCount", sampleReceivedCount);
  189. previewJson.put("samples", samples);
  190. }
  191. return previewJson;
  192. }
  193. @Override
  194. public void batchAdd(JSONArray temCollectSamples) {
  195. List<KuaishouItemCollectSamples> adds = new ArrayList<>();
  196. for (int i = 0; i < temCollectSamples.size(); i++) {
  197. JSONObject sampleJson = temCollectSamples.getJSONObject(i);
  198. KuaishouItemCollectSamples sample = new KuaishouItemCollectSamples();
  199. sample.setItemId(sampleJson.getLong("itemId"));
  200. sample.setItemTitle(sampleJson.getString("itemTitle"));
  201. sample.setItemPrice(sampleJson.getLong("itemPrice"));
  202. sample.setItemImgUrl(sampleJson.getString("itemImgUrl"));
  203. sample.setItemDesc(sampleJson.getString("itemDesc"));
  204. sample.setCommissionRate(sampleJson.getLong("commissionRate"));
  205. sample.setRegimentalPromotion(sampleJson.getLong("regimentalPromotion"));
  206. sample.setSampleCount(sampleJson.getInteger("sampleCount"));
  207. sample.setSampleRequirement(sampleJson.getString("sampleRequirement"));
  208. sample.setItemCreateId(sampleJson.getLong("itemCreateId"));
  209. sample.setItemCreateName(sampleJson.getString("itemCreateName"));
  210. sample.setCollectSampleId(sampleJson.getLong("collectSampleId"));
  211. sample.setCollectSampleName(sampleJson.getString("collectSampleName"));
  212. sample.setPartnerRecommendText(sampleJson.getString("partnerRecommendText"));
  213. sample.setPromoterId(sampleJson.getLong("promoterId"));
  214. sample.setPromoterPhone(sampleJson.getString("promoterPhone"));
  215. sample.setPromoterAddress(sampleJson.getString("promoterAddress"));
  216. sample.setConsignee(sampleJson.getString("consignee"));
  217. sample.setCollectSampleStatus(sampleJson.getInteger("collectSampleStatus"));
  218. sample.setCollectSampleDesc(sampleJson.getString("collectSampleDesc"));
  219. sample.setPromoterNickName(sampleJson.getString("promoterNickName"));
  220. sample.setPromoterUrl(sampleJson.getString("promoterUrl"));
  221. adds.add(sample);
  222. }
  223. if (!Check.isNull(adds)) {
  224. kuaishouItemCollectSamplesMapper.batchAdd(adds);
  225. }
  226. }
  227. @Override
  228. public void subscribe(Long id, String courierNumber, String code) throws Exception {
  229. KuaishouItemCollectSamples samples = kuaishouItemCollectSamplesMapper.selectKuaishouItemCollectSamplesById(id);
  230. SubscribeParameters subscribeParameters = new SubscribeParameters();
  231. subscribeParameters.setCallbackurl("http://ruixuan.api.tjyourong.com.cn/itemCollectSamples/callback");
  232. subscribeParameters.setPhone(samples.getPromoterPhone());
  233. SubscribeParam subscribeParam = new SubscribeParam();
  234. subscribeParam.setParameters(subscribeParameters);
  235. subscribeParam.setCompany(code);
  236. subscribeParam.setNumber(courierNumber);
  237. subscribeParam.setKey("BPEQUJxB9244");
  238. SubscribeReq subscribeReq = new SubscribeReq();
  239. subscribeReq.setSchema(ApiInfoConstant.SUBSCRIBE_SCHEMA);
  240. subscribeReq.setParam(new Gson().toJson(subscribeParam));
  241. IBaseClient subscribe = new Subscribe();
  242. JSONObject jsonObject = JSONObject.parseObject(subscribe.execute(subscribeReq).getBody());
  243. String returnCode = jsonObject.getString("returnCode");
  244. KuaishouItemCollectSamples updateSample = new KuaishouItemCollectSamples();
  245. updateSample.setId(id);
  246. if ("200".equals(returnCode)) {
  247. updateSample.setCollectSampleDesc("快递推送订阅成功");
  248. } else {
  249. updateSample.setCollectSampleStatus(3);
  250. updateSample.setCollectSampleDesc("快递推送订阅失败,失败原因:" + jsonObject.getString("message"));
  251. }
  252. kuaishouItemCollectSamplesMapper.updateKuaishouItemCollectSamples(updateSample);
  253. }
  254. @Override
  255. public void updateByNum(KuaishouItemCollectSamples updateSample) {
  256. kuaishouItemCollectSamplesMapper.updateByNum(updateSample);
  257. }
  258. @Override
  259. public void replaceExpress(KuaishouItemCollectSampleExpress express) {
  260. kuaishouItemCollectSamplesMapper.replaceExpress(express);
  261. }
  262. @Override
  263. public JSONObject getDetail(Long id) {
  264. JSONObject json = new JSONObject();
  265. KuaishouItemCollectSamples sample = kuaishouItemCollectSamplesMapper.selectKuaishouItemCollectSamplesById(id);
  266. KuaishouItemCollectSampleLog log = new KuaishouItemCollectSampleLog();
  267. log.setCollectSampleStatus(1);
  268. log.setUserId(sample.getCollectSampleId());
  269. log.setUserName(sample.getCollectSampleName());
  270. log.setSampleId(sample.getId());
  271. log.setCreateTime(sample.getCreateTime());
  272. log.setUpdateTime(sample.getCreateTime());
  273. kuaishouItemCollectSamplesMapper.insertLog(log);
  274. json.put("sample", sample);
  275. if (!Check.isNull(sample.getCourierNumber())) {
  276. KuaishouItemCollectSampleExpress express = kuaishouItemCollectSamplesMapper.getExpress(sample.getCourierNumber());
  277. if (!Check.isNull(express)) {
  278. json.put("express", express);
  279. }
  280. }
  281. List<KuaishouItemCollectSampleLog> logs = kuaishouItemCollectSamplesMapper.getLogList(id);
  282. if (!Check.isNull(logs)) {
  283. json.put("logs", logs);
  284. }
  285. return json;
  286. }
  287. @Override
  288. public Integer getStatusByCourierNumbe(String nu) {
  289. return kuaishouItemCollectSamplesMapper.getStatusByCourierNumbe(nu);
  290. }
  291. @Override
  292. public JSONObject courierNumberInfo(String courierNumber) {
  293. return kuaishouItemCollectSamplesMapper.courierNumberInfo(courierNumber);
  294. }
  295. @Override
  296. public void insertLog(KuaishouItemCollectSampleLog sampleLog) {
  297. kuaishouItemCollectSamplesMapper.insertLog(sampleLog);
  298. }
  299. @Override
  300. public void cleanLogs(Long id, Integer status) {
  301. kuaishouItemCollectSamplesMapper.cleanLogs(id, status);
  302. }
  303. @Override
  304. public List<JSONObject> exportCollectSamples(Map<String, Object> requestMap) {
  305. return kuaishouItemCollectSamplesMapper.exportCollectSamples(requestMap);
  306. }
  307. @Override
  308. public JSONObject getCollectSampleInfo(Long itemId) {
  309. return kuaishouItemCollectSamplesMapper.getCollectSampleInfo(itemId);
  310. }
  311. @Override
  312. public JSONObject getCopyInfo(Long promoterId) {
  313. JSONObject returnJson = kuaishouItemCollectSamplesMapper.promoterInfo(promoterId);
  314. return returnJson;
  315. }
  316. @Override
  317. public Long getItemIdById(Long id) {
  318. return kuaishouItemCollectSamplesMapper.getItemIdById(id);
  319. }
  320. @Override
  321. public JSONObject getPersonalInfo(Long userId, String type) {
  322. String roleKey = userService.getRoleKeyByUserId(String.valueOf(userId));
  323. List<Long> userList = null;
  324. if ("courtshipManager".equals(roleKey) || "bdManager".equals(roleKey)) {
  325. Long deptId = deptService.getDeptIdByUserId(userId);
  326. userList = deptService.getDeptUserListByDeptId(deptId);
  327. } else if (roleKey.contains("association") || "supplyChainAdmin".equals(roleKey)) {
  328. //社群查看所有 all
  329. userList = null;
  330. } else {
  331. userList = Arrays.asList(userId);
  332. }
  333. int userType = 1;
  334. if (!"1".equals(type)) {
  335. userType = 2;
  336. }
  337. JSONObject returnJson = new JSONObject();
  338. JSONObject data = new JSONObject();
  339. SysUser sysUser = userService.selectUserById(userId);
  340. if (Check.isNotNull(sysUser)) {
  341. data.put("userName", sysUser.getNickName());
  342. }
  343. Integer status = 1; //待审核领样数
  344. data.put("toBeReviewed", kuaishouItemCollectSamplesMapper.getSampleCountByUserId(userList, userType, status));
  345. status = 3;//待发货领样数
  346. data.put("toBeShipped", kuaishouItemCollectSamplesMapper.getSampleCountByUserId(userList, userType, status));
  347. status = 6;//作业上传成功待招商审核
  348. data.put("toBeUploaded", kuaishouItemCollectSamplesMapper.getSampleCountByUserId(userList, userType, status));
  349. returnJson.put("code", 0);
  350. returnJson.put("message", "调用成功");
  351. returnJson.put("data", data);
  352. return returnJson;
  353. }
  354. @Override
  355. public JSONObject getOrderData(Long userId, String startDate, String endDate, String type) {
  356. String roleKey = userService.getRoleKeyByUserId(String.valueOf(userId));
  357. List<Long> userList = null;
  358. if ("courtshipManager".equals(roleKey) || "bdManager".equals(roleKey)) {
  359. Long deptId = deptService.getDeptIdByUserId(userId);
  360. userList = deptService.getDeptUserListByDeptId(deptId);
  361. } else if (roleKey.contains("association") || "supplyChainAdmin".equals(roleKey)) {
  362. //社群查看所有 all
  363. userList = null;
  364. } else {
  365. userList = Arrays.asList(userId);
  366. }
  367. JSONObject returnJson = new JSONObject();
  368. returnJson.put("code", 0);
  369. returnJson.put("message", "调用成功");
  370. if ("1".equals(type)) {
  371. returnJson.put("data", getgetOrderDataByChannel(userList, startDate, endDate));
  372. } else {
  373. returnJson.put("data", getgetOrderDataByAttractInvestment(userList, startDate, endDate));
  374. }
  375. return returnJson;
  376. }
  377. //渠道数据
  378. private JSONObject getgetOrderDataByChannel(List<Long> userList, String startDate, String endDate) {
  379. JSONObject data = new JSONObject();
  380. List<Long> promoterIds = promoterService.selectPromoterIdList(userList);
  381. if (Check.isNotNull(promoterIds)) {
  382. data.put("promoterCount", promoterIds.size());//达人总数
  383. if (Check.isNull(userList)) {
  384. //社群查所有
  385. promoterIds = null;
  386. }
  387. JSONObject Sample = kuaishouItemCollectSamplesMapper.queryTheNumberOfCompletedJobs(promoterIds, startDate, endDate);
  388. data.put("completeCount", Sample.getInteger("completeCount"));//完成作业数
  389. data.put("completeRate", Sample.getString("completeRate"));//合格作业率
  390. JSONObject order = supplyChainService.selectOrderInfo(promoterIds, startDate, endDate);
  391. data.put("allNumber", order.getInteger("allNumber"));//订单总数
  392. data.put("validOrderNUm", order.getInteger("validOrderNUm"));//有效订单数
  393. data.put("inValidOrderNUm", order.getInteger("allNumber") - order.getInteger("validOrderNUm"));//失效订单数
  394. data.put("orderAmount", order.getInteger("orderAmount"));//订单金额
  395. data.put("regimentalPromotionAmount", order.getInteger("regimentalPromotionAmount"));//预估服务费
  396. data.put("baseAmount", order.getInteger("baseAmount"));//累计货贷基数
  397. //当天
  398. String today = DateUtils.getNowDateStr();
  399. Date date = new Date();
  400. //昨天
  401. String yesterday = DateUtils.getSubtractTime(date, 1);
  402. //上周的同一天(上个近七天的结束日期)
  403. String todayOfLastWeek = DateUtils.getSubtractTime(date, 7);
  404. //当前近七天的开始日期
  405. String start = DateUtils.getSubtractTime(date, 6);
  406. //上个近七天的开始日期
  407. String LastStart = DateUtils.getSubtractTime(date, 13);
  408. //当天数据
  409. JSONObject toData = supplyChainService.selectOrderRate(promoterIds, null, today, today);
  410. //昨日数据
  411. JSONObject yesData = supplyChainService.selectOrderRate(promoterIds, null, yesterday, yesterday);
  412. //上周当日数据
  413. JSONObject weeklyDayData = supplyChainService.selectOrderRate(promoterIds, null, todayOfLastWeek, todayOfLastWeek);
  414. //当前近七天的数据
  415. JSONObject inDayData = supplyChainService.selectOrderRate(promoterIds, null, start, today);
  416. //上个近七天的数据
  417. JSONObject lastDayData = supplyChainService.selectOrderRate(promoterIds, null, LastStart, todayOfLastWeek);
  418. //日环比 (今天-昨天)/昨天*100%
  419. BigDecimal dateOrder = toData.getBigDecimal("allNumber").subtract(yesData.getBigDecimal("allNumber")).divide(yesData.getBigDecimal("allNumber"), 4, RoundingMode.HALF_UP);
  420. BigDecimal dateAmount = toData.getBigDecimal("orderAmount").subtract(yesData.getBigDecimal("orderAmount")).divide(yesData.getBigDecimal("orderAmount"), 4, RoundingMode.HALF_UP);
  421. data.put("dateOrderRate", dateOrder);
  422. data.put("dateAmountRate", dateAmount);
  423. //周同比
  424. BigDecimal monthOrder = toData.getBigDecimal("allNumber").subtract(weeklyDayData.getBigDecimal("allNumber")).divide(weeklyDayData.getBigDecimal("allNumber"), 4, RoundingMode.HALF_UP);
  425. BigDecimal monthAmount = toData.getBigDecimal("orderAmount").subtract(weeklyDayData.getBigDecimal("orderAmount")).divide(weeklyDayData.getBigDecimal("orderAmount"), 4, RoundingMode.HALF_UP);
  426. data.put("monthOrderRate", monthOrder);
  427. data.put("monthAmountRate", monthAmount);
  428. //数据对比
  429. BigDecimal dataOrder = inDayData.getBigDecimal("allNumber").subtract(lastDayData.getBigDecimal("allNumber")).divide(lastDayData.getBigDecimal("allNumber"), 4, RoundingMode.HALF_UP);
  430. BigDecimal dataAmount = inDayData.getBigDecimal("orderAmount").subtract(lastDayData.getBigDecimal("orderAmount")).divide(lastDayData.getBigDecimal("orderAmount"), 4, RoundingMode.HALF_UP);
  431. data.put("dataOrderRate", dataOrder);
  432. data.put("dataAmountRate", dataAmount);
  433. }
  434. return data;
  435. }
  436. //招商数据
  437. private JSONObject getgetOrderDataByAttractInvestment(List<Long> userList, String startDate, String endDate) {
  438. JSONObject data = new JSONObject();
  439. List<Long> promoterIds = promoterService.selectPromoterIdList(null);
  440. if (Check.isNotNull(promoterIds)) {
  441. data.put("promoterCount", promoterIds.size());//达人总数
  442. }
  443. //查询登录人(创建人)的所有商品
  444. List<Long> itemIds = kuaishouItemCollectSamplesMapper.getItemIdsByCreateId(userList);
  445. if (Check.isNotNull(itemIds)) {
  446. JSONObject order = supplyChainService.selectOrderInfoByItemIds(itemIds, startDate, endDate);
  447. data.put("sendCount", order.getInteger("sendCount"));//发货数
  448. data.put("sendRate", order.getString("sendRate"));//订单发货率
  449. data.put("allNumber", order.getInteger("allNumber"));//订单总数
  450. data.put("validOrderNUm", order.getInteger("validOrderNUm"));//有效订单数
  451. data.put("inValidOrderNUm", order.getInteger("allNumber") - order.getInteger("validOrderNUm"));//失效订单数
  452. data.put("orderAmount", order.getInteger("orderAmount"));//订单金额
  453. data.put("regimentalPromotionAmount", order.getInteger("regimentalPromotionAmount"));//预估服务费
  454. data.put("baseAmount", order.getInteger("baseAmount"));//累计货贷基数
  455. //当天
  456. String today = DateUtils.getNowDateStr();
  457. Date date = new Date();
  458. //昨天
  459. String yesterday = DateUtils.getSubtractTime(date, 1);
  460. //上周的同一天(上个近七天的结束日期)
  461. String todayOfLastWeek = DateUtils.getSubtractTime(date, 7);
  462. //当前近七天的开始日期
  463. String start = DateUtils.getSubtractTime(date, 6);
  464. //上个近七天的开始日期
  465. String LastStart = DateUtils.getSubtractTime(date, 13);
  466. //当天数据
  467. JSONObject toData = supplyChainService.selectOrderRate(null, itemIds, today, today);
  468. //昨日数据
  469. JSONObject yesData = supplyChainService.selectOrderRate(null, itemIds, yesterday, yesterday);
  470. //上周当日数据
  471. JSONObject weeklyDayData = supplyChainService.selectOrderRate(null, itemIds, todayOfLastWeek, todayOfLastWeek);
  472. //当前近七天的数据
  473. JSONObject inDayData = supplyChainService.selectOrderRate(null, itemIds, start, today);
  474. //上个近七天的数据
  475. JSONObject lastDayData = supplyChainService.selectOrderRate(null, itemIds, LastStart, todayOfLastWeek);
  476. //日环比 (今天-昨天)/昨天*100%
  477. BigDecimal dateOrder = toData.getBigDecimal("allNumber").subtract(yesData.getBigDecimal("allNumber")).divide(yesData.getBigDecimal("allNumber"), 4, RoundingMode.HALF_UP);
  478. BigDecimal dateAmount = toData.getBigDecimal("orderAmount").subtract(yesData.getBigDecimal("orderAmount")).divide(yesData.getBigDecimal("orderAmount"), 4, RoundingMode.HALF_UP);
  479. data.put("dateOrderRate", dateOrder);
  480. data.put("dateAmountRate", dateAmount);
  481. //周同比
  482. BigDecimal monthOrder = toData.getBigDecimal("allNumber").subtract(weeklyDayData.getBigDecimal("allNumber")).divide(weeklyDayData.getBigDecimal("allNumber"), 4, RoundingMode.HALF_UP);
  483. BigDecimal monthAmount = toData.getBigDecimal("orderAmount").subtract(weeklyDayData.getBigDecimal("orderAmount")).divide(weeklyDayData.getBigDecimal("orderAmount"), 4, RoundingMode.HALF_UP);
  484. data.put("monthOrderRate", monthOrder);
  485. data.put("monthAmountRate", monthAmount);
  486. //数据对比
  487. BigDecimal dataOrder = inDayData.getBigDecimal("allNumber").subtract(lastDayData.getBigDecimal("allNumber")).divide(lastDayData.getBigDecimal("allNumber"), 4, RoundingMode.HALF_UP);
  488. BigDecimal dataAmount = inDayData.getBigDecimal("orderAmount").subtract(lastDayData.getBigDecimal("orderAmount")).divide(lastDayData.getBigDecimal("orderAmount"), 4, RoundingMode.HALF_UP);
  489. data.put("dataOrderRate", dataOrder);
  490. data.put("dataAmountRate", dataAmount);
  491. }
  492. return data;
  493. }
  494. @Override
  495. public JSONObject getDataAnalysis(Long userId, String type) {
  496. String roleKey = userService.getRoleKeyByUserId(String.valueOf(userId));
  497. List<Long> userList = null;
  498. if ("courtshipManager".equals(roleKey) || "bdManager".equals(roleKey)) {
  499. Long deptId = deptService.getDeptIdByUserId(userId);
  500. userList = deptService.getDeptUserListByDeptId(deptId);
  501. } else if (roleKey.contains("association") || "supplyChainAdmin".equals(roleKey)) {
  502. //社群查看所有 all
  503. userList = null;
  504. } else {
  505. userList = Arrays.asList(userId);
  506. }
  507. JSONObject returnJson = new JSONObject();
  508. returnJson.put("code", 0);
  509. returnJson.put("message", "调用成功");
  510. if ("1".equals(type)) {
  511. returnJson.put("data", getDataAnalysisByChannel(userList));
  512. } else {
  513. returnJson.put("data", getDataAnalysisByAttractInvestment(userList));
  514. }
  515. return returnJson;
  516. }
  517. @Override
  518. public List<JSONObject> queryProductList(Map<String, Object> map) {
  519. PageUtils.startPage();
  520. return kuaishouItemCollectSamplesMapper.queryProductList(map);
  521. }
  522. // 渠道—数据分析
  523. private JSONObject getDataAnalysisByChannel(List<Long> userList) {
  524. JSONObject data = new JSONObject();
  525. List<Long> promoterIds = promoterService.selectPromoterIdList(userList);
  526. if (Check.isNotNull(promoterIds)) {
  527. //当天
  528. String end = DateUtils.getNowDateStr();
  529. //当前月份第一天
  530. String start = DateUtils.getFirstDayByMonth();
  531. Map<String, Object> map = new HashMap<>();
  532. map.put("start", start);
  533. map.put("end", end);
  534. map.put("userList", userList);
  535. map.put("promoterIds", promoterIds);
  536. map.put("startLong", DateUtils.getStartLongTime(start));
  537. map.put("endLong", DateUtils.getEndLongTime(end));
  538. List<JSONObject> list = supplyChainService.getMonthOrderAmount(map);
  539. List<String> allDates = DateUtils.getAllDatesOfTwoTimes(start, end);
  540. if (Check.isNull(list) || list.size() != allDates.size()) {
  541. String orderAmountStr = JSONObject.toJSONString(list);
  542. for (String date : allDates) {
  543. if (!orderAmountStr.contains(date)) {
  544. JSONObject obj = new JSONObject();
  545. obj.put("date", date);
  546. obj.put("orderAmount", 0);
  547. obj.put("totalRegimentalSettleAmount", 0);
  548. obj.put("promoterCount", 0);
  549. list.add(obj);
  550. }
  551. }
  552. //排序
  553. Collections.sort(list, new Comparator<JSONObject>() {
  554. @Override
  555. public int compare(JSONObject o1, JSONObject o2) {
  556. return o1.getString("date").compareTo(o2.getString("date"));
  557. }
  558. });
  559. }
  560. data.put("list", list);
  561. map.put("toStartLong", DateUtils.getStartLongTime(end));
  562. JSONObject amount = supplyChainService.getMonthOrderAmountTotal(map);
  563. if (Check.isNotNull(amount)) {
  564. data.put("orderAmount", amount.getInteger("orderAmount"));
  565. data.put("totalRegimentalSettleAmount", amount.getInteger("totalRegimentalSettleAmount"));
  566. data.put("orderAmountRate", amount.getString("orderAmountRate"));
  567. data.put("totalRegimentalSettleAmountRate", amount.getString("totalRegimentalSettleAmountRate"));
  568. }
  569. JSONObject promoter = promoterService.getgetMonthPromoterTotal(userList, start, end);
  570. if (Check.isNotNull(promoter)) {
  571. data.put("promoterCount", promoter.getString("count"));
  572. data.put("promoterRate", promoter.getString("rate"));
  573. }
  574. }
  575. return data;
  576. }
  577. // 招商—数据分析
  578. private JSONObject getDataAnalysisByAttractInvestment(List<Long> userList) {
  579. JSONObject data = new JSONObject();
  580. //查询登录人(创建人)的所有商品
  581. List<Long> itemIds = kuaishouItemCollectSamplesMapper.getItemIdsByCreateId(userList);
  582. if (Check.isNotNull(itemIds)) {
  583. //当天
  584. String end = DateUtils.getNowDateStr();
  585. //当前月份第一天
  586. String start = DateUtils.getFirstDayByMonth();
  587. Map<String, Object> map = new HashMap<>();
  588. map.put("start", start);
  589. map.put("end", end);
  590. map.put("itemIds", itemIds);
  591. map.put("startLong", DateUtils.getStartLongTime(start));
  592. map.put("toStartLong", DateUtils.getStartLongTime(end));
  593. map.put("endLong", DateUtils.getEndLongTime(end));
  594. List<JSONObject> list = supplyChainService.getMonthOrderAmountByItems(map);
  595. List<String> allDates = DateUtils.getAllDatesOfTwoTimes(start, end);
  596. if (Check.isNull(list) || list.size() != allDates.size()) {
  597. String orderAmountStr = JSONObject.toJSONString(list);
  598. for (String date : allDates) {
  599. if (!orderAmountStr.contains(date)) {
  600. JSONObject obj = new JSONObject();
  601. obj.put("date", date);
  602. obj.put("orderAmount", 0);
  603. obj.put("totalRegimentalSettleAmount", 0);
  604. obj.put("orderCount", 0);
  605. list.add(obj);
  606. }
  607. }
  608. //排序
  609. Collections.sort(list, new Comparator<JSONObject>() {
  610. @Override
  611. public int compare(JSONObject o1, JSONObject o2) {
  612. return o1.getString("date").compareTo(o2.getString("date"));
  613. }
  614. });
  615. }
  616. data.put("list", list);
  617. JSONObject amount = supplyChainService.getMonthOrderAmountTotalByItems(map);
  618. if (Check.isNotNull(amount)) {
  619. data.put("orderAmount", amount.getInteger("orderAmount"));
  620. data.put("totalRegimentalSettleAmount", amount.getInteger("totalRegimentalSettleAmount"));
  621. data.put("orderAmountRate", amount.getString("orderAmountRate"));
  622. data.put("totalRegimentalSettleAmountRate", amount.getString("totalRegimentalSettleAmountRate"));
  623. data.put("orderCount", amount.getString("orderCount"));
  624. data.put("orderCountRate", amount.getString("orderCountRate"));
  625. }
  626. }
  627. return data;
  628. }
  629. }