ApiService.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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.io.unit.DataUnit;
  5. import cn.hutool.core.util.NumberUtil;
  6. import cn.hutool.core.util.RandomUtil;
  7. import cn.hutool.core.util.StrUtil;
  8. import cn.hutool.json.JSONUtil;
  9. import com.pj.api.bo.InOutRecordBO;
  10. import com.pj.api.wx.bo.MsgDataBO;
  11. import com.pj.api.wx.service.WxService;
  12. import com.pj.constants.UserTypeEnum;
  13. import com.pj.current.config.MyConfig;
  14. import com.pj.current.config.PartConfig;
  15. import com.pj.current.config.WxConfig;
  16. import com.pj.current.satoken.StpUserUtil;
  17. import com.pj.project.tb_account.TbAccount;
  18. import com.pj.project.tb_account.TbAccountService;
  19. import com.pj.project.tb_business.CarDisincle;
  20. import com.pj.project.tb_business.TbBusiness;
  21. import com.pj.project.tb_business.TbBusinessService;
  22. import com.pj.project.tb_business_car.TbBusinessCar;
  23. import com.pj.project.tb_business_car.TbBusinessCarService;
  24. import com.pj.project.tb_business_item.TbBusinessItem;
  25. import com.pj.project.tb_business_item.TbBusinessItemService;
  26. import com.pj.project.tb_car.TbCar;
  27. import com.pj.project.tb_car.TbCarService;
  28. import com.pj.project.tb_costomer.TbCostomer;
  29. import com.pj.project.tb_costomer.TbCostomerService;
  30. import com.pj.project.tb_declare.TbDeclare;
  31. import com.pj.project.tb_declare.TbDeclareService;
  32. import com.pj.project.tb_discount.TbDiscount;
  33. import com.pj.project.tb_discount.TbDiscountService;
  34. import com.pj.project.tb_disinfect.TbDisinfect;
  35. import com.pj.project.tb_disinfect.TbDisinfectService;
  36. import com.pj.project.tb_district.TbDistrict;
  37. import com.pj.project.tb_district.TbDistrictService;
  38. import com.pj.project.tb_item.TbItem;
  39. import com.pj.project.tb_pass_record.TbPassRecord;
  40. import com.pj.project.tb_pass_record.TbPassRecordService;
  41. import com.pj.project4sp.admin.SpAdmin;
  42. import com.pj.project4sp.admin.SpAdminService;
  43. import com.pj.project4sp.global.BusinessException;
  44. import com.pj.utils.cache.RedisUtil;
  45. import com.pj.utils.sg.AjaxJson;
  46. import com.pj.utils.so.SoMap;
  47. import lombok.extern.slf4j.Slf4j;
  48. import org.springframework.context.annotation.Lazy;
  49. import org.springframework.stereotype.Service;
  50. import org.springframework.transaction.annotation.Transactional;
  51. import javax.annotation.Resource;
  52. import java.math.BigDecimal;
  53. import java.time.LocalDateTime;
  54. import java.time.format.DateTimeFormatter;
  55. import java.util.*;
  56. import java.util.stream.Collectors;
  57. /**
  58. *
  59. */
  60. @Service
  61. @Transactional(rollbackFor = Exception.class)
  62. @Slf4j
  63. public class ApiService {
  64. @Resource
  65. TbCostomerService tbCostomerService;
  66. @Resource
  67. @Lazy
  68. TbBusinessService tbBusinessService;
  69. @Resource
  70. TbPassRecordService tbPassRecordService;
  71. @Resource
  72. @Lazy
  73. TbBusinessCarService tbBusinessCarService;
  74. @Resource
  75. private TbDeclareService tbDeclareService;
  76. @Resource
  77. @Lazy
  78. private TbDisinfectService tbDisinfectService;
  79. @Resource
  80. private TbAccountService accountService;
  81. @Resource
  82. private TbDiscountService tbDiscountService;
  83. @Resource
  84. private TbBusinessItemService tbBusinessItemService;
  85. @Resource
  86. private SpAdminService spAdminService;
  87. @Resource
  88. private WxService wxService;
  89. @Resource
  90. private WxConfig wxConfig;
  91. @Resource
  92. private MyConfig myConfig;
  93. @Resource
  94. private TbCarService tbCarService;
  95. private final List<String> CAR_LIST = StrUtil.splitTrim("浙,粤,京,津,冀,晋,蒙,辽,黑,沪,吉,苏,皖,赣,鲁,豫,鄂,湘,桂,琼,渝,川,贵,云,藏, 陕, 甘, 青, 宁", ",");
  96. public List<InOutRecordBO> getInOutRecord(SoMap so) {
  97. List<TbPassRecord> passRecords = tbPassRecordService.getList(so);
  98. List<InOutRecordBO> recordList = new ArrayList<>();
  99. for (TbPassRecord passRecord : passRecords) {
  100. InOutRecordBO record = new InOutRecordBO();
  101. BeanUtil.copyProperties(passRecord, record);
  102. TbCostomer costomer = tbCostomerService.getById(passRecord.getCustomerId());
  103. record.setCustomerContact(costomer.getPhone()).setDutyPeople(costomer.getDutyPeople());
  104. recordList.add(record);
  105. }
  106. return recordList;
  107. }
  108. public List<TbCostomer> getCustomerList(SoMap so) {
  109. List<TbCostomer> list = tbCostomerService.getList(so);
  110. return list;
  111. }
  112. public void confirmCustomer(Long customerId, String judgeContent) {
  113. TbCostomer costomer = tbCostomerService.getById(customerId);
  114. if (costomer != null && costomer.getJudgeStatus() == 1) {
  115. tbCostomerService.judge(customerId + "", 2, judgeContent);
  116. }
  117. }
  118. public List<TbBusinessCar> searchPartCar(String carNo) {
  119. List<TbBusinessCar> list = tbBusinessCarService.searchPartCar(carNo);
  120. list = list.stream().filter(tbBusinessCar -> {
  121. String businessId = tbBusinessCar.getBusinessId();
  122. if (StrUtil.isEmpty(businessId)) {
  123. return false;
  124. }
  125. TbBusiness tbBusiness = tbBusinessService.getById(businessId);
  126. return tbBusiness.getAdminConfirmInput() == 1;
  127. }).collect(Collectors.toList());
  128. List<TbBusinessCar> cars = tbBusinessCarService.findTheNoBusinessCar(carNo);
  129. //过滤掉不用缴费的车
  130. list.addAll(cars);
  131. return list;
  132. }
  133. public void confirm(List<String> ids) {
  134. tbBusinessService.confirm(ids);
  135. }
  136. public Map<String, Object> getBusinessMoney(String carId, String state) {
  137. return tbBusinessService.getBusinessMoney(carId, state);
  138. }
  139. public Map<String, Object> getPartCarByChannel(String channel) {
  140. Map<String, Object> result = new HashMap<>();
  141. String carNo = RedisUtil.get(channel);
  142. if (StrUtil.isEmpty(carNo)) {
  143. return result;
  144. }
  145. TbBusinessCar tbBusinessCar = tbBusinessCarService.findTheLastRecord(carNo);
  146. if (tbBusinessCar == null) {
  147. return result;
  148. }
  149. Date now = new Date();
  150. Date startTime = tbBusinessCar.getRealInTime();
  151. if (tbBusinessCar.getPayTime() != null) {
  152. startTime = tbBusinessCar.getPayTime();
  153. }
  154. BigDecimal price = tbBusinessService.calculationPartMoney(startTime, now);
  155. Map<String, Object> car = new HashMap<>();
  156. car.put("id", tbBusinessCar.getId());
  157. car.put("carNo", tbBusinessCar.getCarNo());
  158. car.put("price", price);
  159. result.put("total", price);
  160. result.put("cars", Collections.singleton(car));
  161. return result;
  162. }
  163. public void addDeclare(TbDeclare tbDeclare) {
  164. if (StpUserUtil.isLogin() && !StrUtil.equals(UserTypeEnum.PLATFORM_ADMIN.getCustomerId(), StpUserUtil.getCustomerId())) {
  165. String currentCustomerId = StpUserUtil.getCustomerId();
  166. tbDeclare.setCustomerId(currentCustomerId);
  167. }
  168. TbCostomer tbCostomer = tbCostomerService.getById(tbDeclare.getCustomerId());
  169. tbDeclare.setCustomerName(tbCostomer.getName());
  170. String chinaCarNo = tbDeclare.getChinaCarNo();
  171. if (StrUtil.isNotEmpty(chinaCarNo)) {
  172. chinaCarNo = chinaCarNo.toUpperCase();
  173. tbDeclare.setChinaCarNo(chinaCarNo);
  174. }
  175. String carNo = tbDeclare.getCarNo();
  176. if (StrUtil.isNotEmpty(carNo)) {
  177. carNo = carNo.toUpperCase();
  178. tbDeclare.setCarNo(carNo);
  179. }
  180. String nowStr = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss"));
  181. tbDeclare.setCreateTime(new Date()).setDeclareNo(nowStr + RandomUtil.randomNumbers(6));
  182. tbDeclareService.save(tbDeclare);
  183. }
  184. public void addDisinfect(TbDisinfect tbDisinfect) {
  185. if (StpUserUtil.isLogin() && !StrUtil.equals(UserTypeEnum.PLATFORM_ADMIN.getCustomerId(), StpUserUtil.getCustomerId())) {
  186. String currentCustomerId = StpUserUtil.getCustomerId();
  187. tbDisinfect.setCustomerId(currentCustomerId);
  188. }
  189. TbCostomer tbCostomer = tbCostomerService.getById(tbDisinfect.getCustomerId());
  190. tbDisinfect.setCustomerName(tbCostomer.getName()).setApplyUnit(tbCostomer.getName());
  191. String nowStr = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss"));
  192. tbDisinfect.setCode(nowStr + RandomUtil.randomNumbers(6)).setApplyTime(DateUtil.now());
  193. tbDisinfectService.save(tbDisinfect);
  194. }
  195. public List<TbDeclare> getDeclareList(SoMap so) {
  196. return tbDeclareService.getList(so);
  197. }
  198. public TbAccount getAccountInfo(String customerId) {
  199. return accountService.getByCustomerId(customerId);
  200. }
  201. public List<TbDiscount> getDiscountInfo(SoMap so) {
  202. return tbDiscountService.getList(so);
  203. }
  204. public TbBusinessItem getBusinessItem(String id) {
  205. TbBusinessItem item = tbBusinessItemService.getById(id);
  206. TbBusiness business = tbBusinessService.getById(item.getBusinessId());
  207. item.setCardNo(business.getCardNo()).setChinaCarNo(business.getChinaCarNo()).setGoodsName(business.getGoodsName());
  208. return item;
  209. }
  210. public AjaxJson pickBusinessItem(String openid, long id) {
  211. TbBusinessItem tbBusinessItem = tbBusinessItemService.getById(id);
  212. if (tbBusinessItem == null) {
  213. return AjaxJson.getError("业务项目或已被删除");
  214. }
  215. SpAdmin spAdmin = spAdminService.findByOpenid(openid);
  216. if (spAdmin == null) {
  217. return AjaxJson.getError("用户已被删除或解绑");
  218. }
  219. if (tbBusinessItem.getPick() == 1) {
  220. return AjaxJson.getError("您慢了一步,其他用户已接单");
  221. }
  222. String customerId = spAdmin.getCustomerId();
  223. TbCostomer tbCostomer = tbCostomerService.getById(customerId);
  224. tbBusinessItem.setPick(1).setPickTime(new Date()).setPickCustomerId(tbCostomer.getId()).setPickCustomerName(tbCostomer.getName());
  225. tbBusinessItemService.updateById(tbBusinessItem);
  226. //todo 通知录入人员 已接单
  227. List<SpAdmin> spAdminList = spAdminService.findByCustomerId(UserTypeEnum.PLATFORM_ADMIN.getCustomerId());
  228. TbBusiness tbBusiness = tbBusinessService.getById(tbBusinessItem.getBusinessId());
  229. MsgDataBO msgDataBO = new MsgDataBO("订单号:" + tbBusinessItem.getNo(), tbCostomer.getName(), DateUtil.now(), tbBusiness.getGoodsName() + "(" + tbBusinessItem.getItemTypeName() + tbBusinessItem.getItemName() + ")");
  230. spAdminList.stream().filter(admin -> StrUtil.isNotEmpty(admin.getOpenid())).forEach(admin -> {
  231. String detailUrl = myConfig.getWebDomain() + "/pages/business-order/business-item?id=" + tbBusiness.getId() + "&itemId=" + tbBusinessItem.getId() + "&openid=" + openid;
  232. log.info("admin confirm business:[{}]", detailUrl);
  233. wxService.sendTemplateMsg(wxConfig.getBusinessPickTemplate(), admin.getOpenid(), msgDataBO, detailUrl);
  234. });
  235. return AjaxJson.getSuccess();
  236. }
  237. public void confirmBusinessItem(long id) {
  238. tbBusinessItemService.confirmBusinessItem(id);
  239. }
  240. public List<TbBusinessItem> getPartnerBusinessItem(SoMap startPage) {
  241. return tbBusinessItemService.getList(startPage);
  242. }
  243. public void addCarDisinfect(String carNo, String itemJson, String carType) {
  244. carNo = carNo.toUpperCase();
  245. TbBusinessCar db = tbBusinessCarService.checkCar(carNo);
  246. if (db != null) {
  247. throw new BusinessException("该车辆有未完成作业");
  248. }
  249. db = new TbBusinessCar();
  250. TbCar tbCar = tbCarService.findByCardNo(carNo);
  251. db.setCarNo(carNo).setIsLock(0).setCarCompany(tbCar != null ? tbCar.getCustomerName() : "临时");
  252. TbBusiness tbBusiness = new TbBusiness();
  253. if (StpUserUtil.isLogin()) {
  254. String customerId = StpUserUtil.getCustomerId();
  255. db.setCustomerId(customerId);
  256. TbCostomer tbCostomer = tbCostomerService.getById(customerId);
  257. tbBusiness.setCustomerId(customerId).setCustomerName(tbCostomer.getName());
  258. }
  259. db.setPay(0).setCarSize(12D);
  260. List<TbItem> tbItems = JSONUtil.toList(itemJson, TbItem.class);
  261. BigDecimal price = new BigDecimal("0");
  262. List<TbBusinessItem> itemList = new ArrayList<>();
  263. String no = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmm")) + RandomUtil.randomNumbers(4);
  264. int index = 1;
  265. for (TbItem tbItem : tbItems) {
  266. TbBusinessItem item = new TbBusinessItem();
  267. item.setNo(no + "0" + index)
  268. .setBusinessType(TbCostomer.CustomerEnum.DISINFECT_TYPE.getType());
  269. item.setItemCode(tbItem.getItemCode()).setNum("1")
  270. .setItemName(tbItem.getItemName()).setItemPrice(tbItem.getPrice())
  271. .setItemTypeId(tbItem.getTypeId()).setItemTypeName(tbItem.getTypeName())
  272. .setUnit(tbItem.getUnit()).setTotal(tbItem.getPrice()).setCreateTime(new Date());
  273. price = price.add(item.getItemPrice());
  274. itemList.add(item);
  275. index++;
  276. }
  277. // BigDecimal initCarPartMoney = partConfig.getBasePrice();
  278. tbBusiness
  279. .setNo(no).setGoodsName(carType)
  280. .setCreateTime(new Date()).setBusinessType(TbBusiness.BusinessType.CAR_DISINCLE.getCode())
  281. .setItemPrice(price);
  282. String prefix = carNo.substring(0, 1);
  283. if (CAR_LIST.contains(prefix)) {
  284. tbBusiness.setChinaCarNo(carNo);
  285. } else {
  286. tbBusiness.setCardNo(carNo);
  287. }
  288. tbBusinessService.save(tbBusiness);
  289. db.setBusinessId(tbBusiness.getId())
  290. .setNo(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmm")) + RandomUtil.randomNumbers(4));
  291. tbBusinessCarService.save(db);
  292. itemList.forEach(tbBusinessItem -> tbBusinessItem.setBusinessId(tbBusiness.getId()));
  293. tbBusinessItemService.saveBatch(itemList);
  294. }
  295. public void editCarDisinfect(CarDisincle carDisincle, String itemJson, String type) {
  296. String carId = carDisincle.getBusinessCarId();
  297. TbBusinessCar businessCar = tbBusinessCarService.getById(carId);
  298. if (businessCar == null) {
  299. throw new BusinessException("数据不存在");
  300. }
  301. List<TbItem> tbItems = JSONUtil.toList(itemJson, TbItem.class);
  302. tbItems.forEach(tbItem -> {
  303. });
  304. }
  305. }