ApiService.java 14 KB

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