ApiService.java 12 KB

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