ApiService.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. package com.pj.api.service;
  2. import cn.hutool.core.bean.BeanUtil;
  3. import cn.hutool.core.date.DateUtil;
  4. import cn.hutool.core.util.RandomUtil;
  5. import cn.hutool.core.util.StrUtil;
  6. import com.pj.api.bo.InOutRecordBO;
  7. import com.pj.api.bo.SearchBO;
  8. import com.pj.api.wx.bo.MsgDataBO;
  9. import com.pj.api.wx.service.WxService;
  10. import com.pj.constants.UserTypeEnum;
  11. import com.pj.current.config.MyConfig;
  12. import com.pj.current.config.WxConfig;
  13. import com.pj.current.satoken.StpUserUtil;
  14. import com.pj.project.tb_account.TbAccount;
  15. import com.pj.project.tb_account.TbAccountService;
  16. import com.pj.project.tb_business.TbBusiness;
  17. import com.pj.project.tb_business.TbBusinessService;
  18. import com.pj.project.tb_business_car.TbBusinessCar;
  19. import com.pj.project.tb_business_car.TbBusinessCarService;
  20. import com.pj.project.tb_business_item.TbBusinessItem;
  21. import com.pj.project.tb_business_item.TbBusinessItemService;
  22. import com.pj.project.tb_costomer.TbCostomer;
  23. import com.pj.project.tb_costomer.TbCostomerService;
  24. import com.pj.project.tb_declare.TbDeclare;
  25. import com.pj.project.tb_declare.TbDeclareService;
  26. import com.pj.project.tb_deduction_record.TbDeductionRecord;
  27. import com.pj.project.tb_deduction_record.TbDeductionRecordService;
  28. import com.pj.project.tb_discount.TbDiscount;
  29. import com.pj.project.tb_discount.TbDiscountService;
  30. import com.pj.project.tb_disinfect.TbDisinfect;
  31. import com.pj.project.tb_disinfect.TbDisinfectService;
  32. import com.pj.project.tb_pass_record.TbPassRecord;
  33. import com.pj.project.tb_pass_record.TbPassRecordService;
  34. import com.pj.project4sp.admin.SpAdmin;
  35. import com.pj.project4sp.admin.SpAdminService;
  36. import com.pj.project4sp.global.BusinessException;
  37. import com.pj.utils.cache.RedisUtil;
  38. import com.pj.utils.sg.AjaxJson;
  39. import com.pj.utils.so.SoMap;
  40. import lombok.extern.slf4j.Slf4j;
  41. import org.springframework.context.annotation.Lazy;
  42. import org.springframework.stereotype.Service;
  43. import org.springframework.transaction.annotation.Transactional;
  44. import javax.annotation.Resource;
  45. import java.math.BigDecimal;
  46. import java.nio.charset.Charset;
  47. import java.time.LocalDateTime;
  48. import java.time.format.DateTimeFormatter;
  49. import java.util.*;
  50. import java.util.stream.Collectors;
  51. /**
  52. *
  53. */
  54. @Service
  55. @Transactional(rollbackFor = Exception.class)
  56. @Slf4j
  57. public class ApiService {
  58. @Resource
  59. TbCostomerService tbCostomerService;
  60. @Resource
  61. @Lazy
  62. TbBusinessService tbBusinessService;
  63. @Resource
  64. TbPassRecordService tbPassRecordService;
  65. @Resource
  66. @Lazy
  67. TbBusinessCarService tbBusinessCarService;
  68. @Resource
  69. private TbDeclareService tbDeclareService;
  70. @Resource
  71. @Lazy
  72. private TbDisinfectService tbDisinfectService;
  73. @Resource
  74. private TbAccountService accountService;
  75. @Resource
  76. private TbDiscountService tbDiscountService;
  77. @Resource
  78. private TbBusinessItemService tbBusinessItemService;
  79. @Resource
  80. private SpAdminService spAdminService;
  81. @Resource
  82. private WxService wxService;
  83. @Resource
  84. private WxConfig wxConfig;
  85. @Resource
  86. private MyConfig myConfig;
  87. @Resource
  88. private TbDeductionRecordService tbDeductionRecordService;
  89. public List<InOutRecordBO> getInOutRecord(SoMap so) {
  90. List<TbPassRecord> passRecords = tbPassRecordService.getList(so);
  91. List<InOutRecordBO> recordList = new ArrayList<>();
  92. for (TbPassRecord passRecord : passRecords) {
  93. InOutRecordBO record = new InOutRecordBO();
  94. BeanUtil.copyProperties(passRecord, record);
  95. TbCostomer costomer = tbCostomerService.getById(passRecord.getCustomerId());
  96. record.setCustomerContact(costomer.getPhone()).setDutyPeople(costomer.getDutyPeople());
  97. recordList.add(record);
  98. }
  99. return recordList;
  100. }
  101. public List<TbCostomer> getCustomerList(SoMap so) {
  102. List<TbCostomer> list = tbCostomerService.getList(so);
  103. return list;
  104. }
  105. public void confirmCustomer(Long customerId, String judgeContent) {
  106. TbCostomer costomer = tbCostomerService.getById(customerId);
  107. if (costomer != null && costomer.getJudgeStatus() == 1) {
  108. tbCostomerService.judge(customerId + "", 2, judgeContent);
  109. }
  110. }
  111. public List<SearchBO> searchPartCar(String carNo) {
  112. if (StrUtil.isEmpty(carNo) || StrUtil.length(carNo) < 3) {
  113. return Collections.emptyList();
  114. }
  115. carNo = carNo.toUpperCase();
  116. List<TbBusinessCar> businessCarList = tbBusinessCarService.findNotPayByCarNo(carNo);
  117. return businessCarList.stream().map(tbBusinessCar -> {
  118. SearchBO searchBO = new SearchBO();
  119. searchBO.setId(tbBusinessCar.getId()).setCarNo(tbBusinessCar.getCarNo());
  120. return searchBO;
  121. }).distinct().collect(Collectors.toList());
  122. }
  123. public void confirm(List<String> ids) {
  124. tbBusinessService.confirm(ids);
  125. }
  126. public Map<String, Object> getBusinessMoney(String carId, String state) {
  127. return tbBusinessService.getBusinessMoney(carId, state);
  128. }
  129. public Map<String, Object> getPartCarByChannel(String channel) {
  130. Map<String, Object> result = new HashMap<>();
  131. String carNo = RedisUtil.get(channel);
  132. if (StrUtil.isEmpty(carNo)) {
  133. return result;
  134. }
  135. TbBusinessCar tbBusinessCar = tbBusinessCarService.findTheLastRecord(carNo);
  136. if (tbBusinessCar == null) {
  137. return result;
  138. }
  139. Date now = new Date();
  140. Date startTime = tbBusinessCar.getRealInTime();
  141. if (tbBusinessCar.getPayTime() != null) {
  142. startTime = tbBusinessCar.getPayTime();
  143. }
  144. BigDecimal price = tbBusinessService.calculationPartMoney(startTime, now, tbBusinessCar.getCarSize());
  145. Map<String, Object> car = new HashMap<>();
  146. car.put("id", tbBusinessCar.getId());
  147. car.put("carNo", tbBusinessCar.getCarNo());
  148. car.put("price", price);
  149. result.put("total", price);
  150. result.put("cars", Collections.singleton(car));
  151. return result;
  152. }
  153. public void addDeclare(TbDeclare tbDeclare) {
  154. if (StpUserUtil.isLogin() && !StrUtil.equals(UserTypeEnum.PLATFORM_ADMIN.getCustomerId(), StpUserUtil.getCustomerId())) {
  155. String currentCustomerId = StpUserUtil.getCustomerId();
  156. tbDeclare.setCustomerId(currentCustomerId);
  157. }
  158. TbCostomer tbCostomer = tbCostomerService.getById(tbDeclare.getCustomerId());
  159. tbDeclare.setCustomerName(tbCostomer.getName());
  160. String chinaCarNo = tbDeclare.getChinaCarNo();
  161. if (StrUtil.isNotEmpty(chinaCarNo)) {
  162. chinaCarNo = chinaCarNo.toUpperCase();
  163. tbDeclare.setChinaCarNo(chinaCarNo);
  164. }
  165. String carNo = tbDeclare.getCarNo();
  166. if (StrUtil.isNotEmpty(carNo)) {
  167. carNo = carNo.toUpperCase();
  168. tbDeclare.setCarNo(carNo);
  169. }
  170. String nowStr = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss"));
  171. tbDeclare.setCreateTime(new Date()).setDeclareNo(nowStr + RandomUtil.randomNumbers(6));
  172. tbDeclareService.save(tbDeclare);
  173. }
  174. public void addDisinfect(TbDisinfect tbDisinfect) {
  175. if (StpUserUtil.isLogin() && !StrUtil.equals(UserTypeEnum.PLATFORM_ADMIN.getCustomerId(), StpUserUtil.getCustomerId())) {
  176. String currentCustomerId = StpUserUtil.getCustomerId();
  177. tbDisinfect.setCustomerId(currentCustomerId);
  178. }
  179. TbCostomer tbCostomer = tbCostomerService.getById(tbDisinfect.getCustomerId());
  180. tbDisinfect.setCustomerName(tbCostomer.getName()).setApplyUnit(tbCostomer.getName());
  181. String nowStr = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss"));
  182. tbDisinfect.setCode(nowStr + RandomUtil.randomNumbers(6)).setApplyTime(DateUtil.now());
  183. tbDisinfectService.save(tbDisinfect);
  184. }
  185. public List<TbDeclare> getDeclareList(SoMap so) {
  186. return tbDeclareService.getList(so);
  187. }
  188. public TbAccount getAccountInfo(String customerId) {
  189. return accountService.getByCustomerId(customerId);
  190. }
  191. public List<TbDiscount> getDiscountInfo(SoMap so) {
  192. return tbDiscountService.getList(so);
  193. }
  194. public TbBusinessItem getBusinessItem(String id) {
  195. TbBusinessItem item = tbBusinessItemService.getById(id);
  196. TbBusiness business = tbBusinessService.getById(item.getBusinessId());
  197. item.setCardNo(business.getCardNo()).setChinaCarNo(business.getChinaCarNo()).setGoodsName(business.getGoodsName());
  198. return item;
  199. }
  200. public AjaxJson pickBusinessItem(String openid, long id) {
  201. TbBusinessItem tbBusinessItem = tbBusinessItemService.getById(id);
  202. if (tbBusinessItem == null) {
  203. return AjaxJson.getError("业务项目或已被删除");
  204. }
  205. SpAdmin spAdmin = spAdminService.findByOpenid(openid);
  206. if (spAdmin == null) {
  207. return AjaxJson.getError("用户已被删除或解绑");
  208. }
  209. if (tbBusinessItem.getPick() == 1) {
  210. return AjaxJson.getError("您慢了一步,其他用户已接单");
  211. }
  212. String adminId = spAdmin.getId() + "";
  213. String adminName = StrUtil.isNotEmpty(spAdmin.getNickname()) ? spAdmin.getNickname() : spAdmin.getName();
  214. String customerId = spAdmin.getCustomerId();
  215. TbCostomer tbCostomer = tbCostomerService.getById(customerId);
  216. tbBusinessItem.setPick(1).setPickTime(new Date()).setPickCustomerId(tbCostomer.getId()).setPickCustomerName(tbCostomer.getName())
  217. .setPickBy(adminName).setPickByAdminId(adminId);
  218. tbBusinessItemService.updateById(tbBusinessItem);
  219. //更新扣款记录里面的合作伙伴信息
  220. TbDeductionRecord tbDeductionRecord = tbDeductionRecordService.findByBusinessItemId(tbBusinessItem.getId());
  221. if (tbDeductionRecord != null) {
  222. tbDeductionRecord.setPickCustomerName(tbCostomer.getName());
  223. tbDeductionRecordService.updateById(tbDeductionRecord);
  224. }
  225. //todo 通知录入人员 已接单
  226. List<SpAdmin> spAdminList = spAdminService.findByCustomerId(UserTypeEnum.PLATFORM_ADMIN.getCustomerId());
  227. TbBusiness tbBusiness = tbBusinessService.getById(tbBusinessItem.getBusinessId());
  228. MsgDataBO msgDataBO = new MsgDataBO("订单号:" + tbBusinessItem.getNo(), tbCostomer.getName(), DateUtil.now(), tbBusiness.getGoodsName() + "(" + tbBusinessItem.getItemTypeName() + tbBusinessItem.getItemName() + ")");
  229. spAdminList.stream().filter(admin -> StrUtil.isNotEmpty(admin.getOpenid())).forEach(admin -> {
  230. String detailUrl = myConfig.getWebDomain() + "/pages/business-order/business-item?id=" + tbBusiness.getId() + "&itemId=" + tbBusinessItem.getId() + "&openid=" + admin.getOpenid();
  231. log.info("admin confirm business:[{}]", detailUrl);
  232. wxService.sendTemplateMsg(wxConfig.getBusinessPickTemplate(), admin.getOpenid(), msgDataBO, detailUrl);
  233. });
  234. return AjaxJson.getSuccess();
  235. }
  236. public void confirmBusinessItem(long id) {
  237. tbBusinessItemService.confirmBusinessItem(id);
  238. }
  239. public List<TbBusinessItem> getPartnerBusinessItem(SoMap startPage) {
  240. List<TbBusinessItem> list = tbBusinessItemService.getList(startPage);
  241. list.parallelStream().forEach(tbBusinessItem -> {
  242. TbBusiness tbBusiness = tbBusinessService.getById(tbBusinessItem.getBusinessId());
  243. List<TbBusinessCar> businessCars = tbBusinessCarService.findOtherBusinessCar(tbBusiness.getId());
  244. tbBusinessItem.setCardNo(businessCars.stream().map(TbBusinessCar::getCarNo).collect(Collectors.joining("、")));
  245. });
  246. return list;
  247. }
  248. public String handlerDesc(String desc) {
  249. if (desc.getBytes(Charset.forName("utf-8")).length > 128) {
  250. desc = StrUtil.sub(desc, 0, desc.lastIndexOf("-"));
  251. handlerDesc(desc);
  252. }
  253. return desc;
  254. }
  255. }