ApiService.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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. //查询出未缴费的车辆
  117. //有业务的车
  118. List<TbBusinessCar> businessCarList = tbBusinessCarService.searchForPayCar(carNo);
  119. //无业务的车
  120. List<TbBusinessCar> noBusinessCarList = tbBusinessCarService.findTheNoBusinessCar(carNo);
  121. businessCarList.addAll(noBusinessCarList);
  122. List<SearchBO> searchBOList = businessCarList.stream().map(tbBusinessCar -> {
  123. SearchBO searchBO = new SearchBO();
  124. searchBO.setId(tbBusinessCar.getId()).setCarNo(tbBusinessCar.getCarNo());
  125. return searchBO;
  126. }).distinct().collect(Collectors.toList());
  127. return searchBOList;
  128. }
  129. public void confirm(List<String> ids) {
  130. tbBusinessService.confirm(ids);
  131. }
  132. public Map<String, Object> getBusinessMoney(String carId, String state) {
  133. return tbBusinessService.getBusinessMoney(carId, state);
  134. }
  135. public Map<String, Object> getPartCarByChannel(String channel) {
  136. Map<String, Object> result = new HashMap<>();
  137. String carNo = RedisUtil.get(channel);
  138. if (StrUtil.isEmpty(carNo)) {
  139. return result;
  140. }
  141. TbBusinessCar tbBusinessCar = tbBusinessCarService.findTheLastRecord(carNo);
  142. if (tbBusinessCar == null) {
  143. return result;
  144. }
  145. Date now = new Date();
  146. Date startTime = tbBusinessCar.getRealInTime();
  147. if (tbBusinessCar.getPayTime() != null) {
  148. startTime = tbBusinessCar.getPayTime();
  149. }
  150. BigDecimal price = tbBusinessService.calculationPartMoney(startTime, now,tbBusinessCar.getCarSize());
  151. Map<String, Object> car = new HashMap<>();
  152. car.put("id", tbBusinessCar.getId());
  153. car.put("carNo", tbBusinessCar.getCarNo());
  154. car.put("price", price);
  155. result.put("total", price);
  156. result.put("cars", Collections.singleton(car));
  157. return result;
  158. }
  159. public void addDeclare(TbDeclare tbDeclare) {
  160. if (StpUserUtil.isLogin() && !StrUtil.equals(UserTypeEnum.PLATFORM_ADMIN.getCustomerId(), StpUserUtil.getCustomerId())) {
  161. String currentCustomerId = StpUserUtil.getCustomerId();
  162. tbDeclare.setCustomerId(currentCustomerId);
  163. }
  164. TbCostomer tbCostomer = tbCostomerService.getById(tbDeclare.getCustomerId());
  165. tbDeclare.setCustomerName(tbCostomer.getName());
  166. String chinaCarNo = tbDeclare.getChinaCarNo();
  167. if (StrUtil.isNotEmpty(chinaCarNo)) {
  168. chinaCarNo = chinaCarNo.toUpperCase();
  169. tbDeclare.setChinaCarNo(chinaCarNo);
  170. }
  171. String carNo = tbDeclare.getCarNo();
  172. if (StrUtil.isNotEmpty(carNo)) {
  173. carNo = carNo.toUpperCase();
  174. tbDeclare.setCarNo(carNo);
  175. }
  176. String nowStr = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss"));
  177. tbDeclare.setCreateTime(new Date()).setDeclareNo(nowStr + RandomUtil.randomNumbers(6));
  178. tbDeclareService.save(tbDeclare);
  179. }
  180. public void addDisinfect(TbDisinfect tbDisinfect) {
  181. if (StpUserUtil.isLogin() && !StrUtil.equals(UserTypeEnum.PLATFORM_ADMIN.getCustomerId(), StpUserUtil.getCustomerId())) {
  182. String currentCustomerId = StpUserUtil.getCustomerId();
  183. tbDisinfect.setCustomerId(currentCustomerId);
  184. }
  185. TbCostomer tbCostomer = tbCostomerService.getById(tbDisinfect.getCustomerId());
  186. tbDisinfect.setCustomerName(tbCostomer.getName()).setApplyUnit(tbCostomer.getName());
  187. String nowStr = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss"));
  188. tbDisinfect.setCode(nowStr + RandomUtil.randomNumbers(6)).setApplyTime(DateUtil.now());
  189. tbDisinfectService.save(tbDisinfect);
  190. }
  191. public List<TbDeclare> getDeclareList(SoMap so) {
  192. return tbDeclareService.getList(so);
  193. }
  194. public TbAccount getAccountInfo(String customerId) {
  195. return accountService.getByCustomerId(customerId);
  196. }
  197. public List<TbDiscount> getDiscountInfo(SoMap so) {
  198. return tbDiscountService.getList(so);
  199. }
  200. public TbBusinessItem getBusinessItem(String id) {
  201. TbBusinessItem item = tbBusinessItemService.getById(id);
  202. TbBusiness business = tbBusinessService.getById(item.getBusinessId());
  203. item.setCardNo(business.getCardNo()).setChinaCarNo(business.getChinaCarNo()).setGoodsName(business.getGoodsName());
  204. return item;
  205. }
  206. public AjaxJson pickBusinessItem(String openid, long id) {
  207. TbBusinessItem tbBusinessItem = tbBusinessItemService.getById(id);
  208. if (tbBusinessItem == null) {
  209. return AjaxJson.getError("业务项目或已被删除");
  210. }
  211. SpAdmin spAdmin = spAdminService.findByOpenid(openid);
  212. if (spAdmin == null) {
  213. return AjaxJson.getError("用户已被删除或解绑");
  214. }
  215. if (tbBusinessItem.getPick() == 1) {
  216. return AjaxJson.getError("您慢了一步,其他用户已接单");
  217. }
  218. String adminId = spAdmin.getId() + "";
  219. String adminName = StrUtil.isNotEmpty(spAdmin.getNickname()) ? spAdmin.getNickname() : spAdmin.getName();
  220. String customerId = spAdmin.getCustomerId();
  221. TbCostomer tbCostomer = tbCostomerService.getById(customerId);
  222. tbBusinessItem.setPick(1).setPickTime(new Date()).setPickCustomerId(tbCostomer.getId()).setPickCustomerName(tbCostomer.getName())
  223. .setPickBy(adminName).setPickByAdminId(adminId);
  224. tbBusinessItemService.updateById(tbBusinessItem);
  225. //更新扣款记录里面的合作伙伴信息
  226. TbDeductionRecord tbDeductionRecord = tbDeductionRecordService.findByBusinessItemId(tbBusinessItem.getId());
  227. if (tbDeductionRecord != null) {
  228. tbDeductionRecord.setPickCustomerName(tbCostomer.getName());
  229. tbDeductionRecordService.updateById(tbDeductionRecord);
  230. }
  231. //todo 通知录入人员 已接单
  232. List<SpAdmin> spAdminList = spAdminService.findByCustomerId(UserTypeEnum.PLATFORM_ADMIN.getCustomerId());
  233. TbBusiness tbBusiness = tbBusinessService.getById(tbBusinessItem.getBusinessId());
  234. MsgDataBO msgDataBO = new MsgDataBO("订单号:" + tbBusinessItem.getNo(), tbCostomer.getName(), DateUtil.now(), tbBusiness.getGoodsName() + "(" + tbBusinessItem.getItemTypeName() + tbBusinessItem.getItemName() + ")");
  235. spAdminList.stream().filter(admin -> StrUtil.isNotEmpty(admin.getOpenid())).forEach(admin -> {
  236. String detailUrl = myConfig.getWebDomain() + "/pages/business-order/business-item?id=" + tbBusiness.getId() + "&itemId=" + tbBusinessItem.getId() + "&openid=" + admin.getOpenid();
  237. log.info("admin confirm business:[{}]", detailUrl);
  238. wxService.sendTemplateMsg(wxConfig.getBusinessPickTemplate(), admin.getOpenid(), msgDataBO, detailUrl);
  239. });
  240. return AjaxJson.getSuccess();
  241. }
  242. public void confirmBusinessItem(long id) {
  243. tbBusinessItemService.confirmBusinessItem(id);
  244. }
  245. public List<TbBusinessItem> getPartnerBusinessItem(SoMap startPage) {
  246. List<TbBusinessItem> list = tbBusinessItemService.getList(startPage);
  247. list.parallelStream().forEach(tbBusinessItem -> {
  248. TbBusiness tbBusiness = tbBusinessService.getById(tbBusinessItem.getBusinessId());
  249. List<TbBusinessCar> businessCars = tbBusinessCarService.findOtherBusinessCar(tbBusiness.getId());
  250. tbBusinessItem.setCardNo(businessCars.stream().map(TbBusinessCar::getCarNo).collect(Collectors.joining("、")));
  251. });
  252. return list;
  253. }
  254. public String handlerDesc(String desc) {
  255. if (desc.getBytes(Charset.forName("utf-8")).length > 128) {
  256. desc = StrUtil.sub(desc, 0, desc.lastIndexOf("-"));
  257. handlerDesc(desc);
  258. }
  259. return desc;
  260. }
  261. }