ApiService.java 15 KB

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