ApiService.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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. return list;
  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. TbBusinessCar tbBusinessCar = tbBusinessCarService.findInBuNoOutAndNotPay(carNo);
  138. if (tbBusinessCar == null) {
  139. return result;
  140. }
  141. Date now = new Date();
  142. BigDecimal price = tbBusinessService.calculationPartMoney(tbBusinessCar.getRealInTime(), now);
  143. BigDecimal dif = price.subtract(tbBusinessCar.getMoney());
  144. Map<String, Object> car = new HashMap<>();
  145. car.put("id", tbBusinessCar.getId());
  146. car.put("carNo", tbBusinessCar.getCarNo());
  147. car.put("price", dif);
  148. result.put("total", dif);
  149. result.put("cars", Collections.singleton(car));
  150. return result;
  151. }
  152. public void addDeclare(TbDeclare tbDeclare) {
  153. if (StpUserUtil.isLogin() && !StrUtil.equals(UserTypeEnum.PLATFORM_ADMIN.getCustomerId(), StpUserUtil.getCustomerId())) {
  154. String currentCustomerId = StpUserUtil.getCustomerId();
  155. tbDeclare.setCustomerId(currentCustomerId);
  156. }
  157. TbCostomer tbCostomer = tbCostomerService.getById(tbDeclare.getCustomerId());
  158. tbDeclare.setCustomerName(tbCostomer.getName());
  159. String chinaCarNo = tbDeclare.getChinaCarNo();
  160. if (StrUtil.isNotEmpty(chinaCarNo)) {
  161. chinaCarNo = chinaCarNo.toUpperCase();
  162. tbDeclare.setChinaCarNo(chinaCarNo);
  163. }
  164. String carNo = tbDeclare.getCarNo();
  165. if (StrUtil.isNotEmpty(carNo)) {
  166. carNo = carNo.toUpperCase();
  167. tbDeclare.setCarNo(carNo);
  168. }
  169. String nowStr = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss"));
  170. tbDeclare.setCreateTime(new Date()).setDeclareNo(nowStr + RandomUtil.randomNumbers(6));
  171. tbDeclareService.save(tbDeclare);
  172. }
  173. public void addDisinfect(TbDisinfect tbDisinfect) {
  174. if (StpUserUtil.isLogin() && !StrUtil.equals(UserTypeEnum.PLATFORM_ADMIN.getCustomerId(), StpUserUtil.getCustomerId())) {
  175. String currentCustomerId = StpUserUtil.getCustomerId();
  176. tbDisinfect.setCustomerId(currentCustomerId);
  177. }
  178. TbCostomer tbCostomer = tbCostomerService.getById(tbDisinfect.getCustomerId());
  179. tbDisinfect.setCustomerName(tbCostomer.getName()).setApplyUnit(tbCostomer.getName());
  180. String nowStr = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss"));
  181. tbDisinfect.setCode(nowStr + RandomUtil.randomNumbers(6)).setApplyTime(DateUtil.now());
  182. tbDisinfectService.save(tbDisinfect);
  183. }
  184. public List<TbDeclare> getDeclareList(SoMap so) {
  185. return tbDeclareService.getList(so);
  186. }
  187. public TbAccount getAccountInfo(String customerId) {
  188. return accountService.getByCustomerId(customerId);
  189. }
  190. public List<TbDiscount> getDiscountInfo(SoMap so) {
  191. return tbDiscountService.getList(so);
  192. }
  193. public TbBusinessItem getBusinessItem(String id) {
  194. TbBusinessItem item = tbBusinessItemService.getById(id);
  195. TbBusiness business = tbBusinessService.getById(item.getBusinessId());
  196. item.setCardNo(business.getCardNo()).setChinaCarNo(business.getChinaCarNo()).setGoodsName(business.getGoodsName());
  197. return item;
  198. }
  199. public AjaxJson pickBusinessItem(String openid, long id) {
  200. TbBusinessItem tbBusinessItem = tbBusinessItemService.getById(id);
  201. if (tbBusinessItem == null) {
  202. return AjaxJson.getError("业务项目或已被删除");
  203. }
  204. SpAdmin spAdmin = spAdminService.findByOpenid(openid);
  205. if (spAdmin == null) {
  206. return AjaxJson.getError("用户已被删除或解绑");
  207. }
  208. if (tbBusinessItem.getPick() == 1) {
  209. return AjaxJson.getError("您慢了一步,其他用户已接单");
  210. }
  211. String customerId = spAdmin.getCustomerId();
  212. TbCostomer tbCostomer = tbCostomerService.getById(customerId);
  213. tbBusinessItem.setPick(1).setPickTime(new Date()).setPickCustomerId(tbCostomer.getId()).setPickCustomerName(tbCostomer.getName());
  214. tbBusinessItemService.updateById(tbBusinessItem);
  215. //todo 通知录入人员 已接单
  216. List<SpAdmin> spAdminList = spAdminService.findByCustomerId(UserTypeEnum.PLATFORM_ADMIN.getCustomerId());
  217. TbBusiness tbBusiness = tbBusinessService.getById(tbBusinessItem.getBusinessId());
  218. MsgDataBO msgDataBO = new MsgDataBO("订单号:" + tbBusinessItem.getNo(), tbCostomer.getName(), DateUtil.now(), tbBusiness.getGoodsName() + "(" + tbBusinessItem.getItemTypeName() + tbBusinessItem.getItemName() + ")");
  219. spAdminList.stream().filter(admin -> StrUtil.isNotEmpty(admin.getOpenid())).forEach(admin -> {
  220. String detailUrl = myConfig.getWebDomain() + "/pages/business-order/business-item?id=" + tbBusiness.getId() + "&itemId=" + tbBusinessItem.getId() + "&openid=" + openid;
  221. log.info("admin confirm business:[{}]", detailUrl);
  222. wxService.sendTemplateMsg(wxConfig.getBusinessPickTemplate(), admin.getOpenid(), msgDataBO, detailUrl);
  223. });
  224. return AjaxJson.getSuccess();
  225. }
  226. public void confirmBusinessItem(long id) {
  227. tbBusinessItemService.confirmBusinessItem(id);
  228. }
  229. public List<TbBusinessItem> getPartnerBusinessItem(SoMap startPage) {
  230. return tbBusinessItemService.getList(startPage);
  231. }
  232. public void addCarDisinfect(String carNo, String itemJson, String carType) {
  233. carNo = carNo.toUpperCase();
  234. TbBusinessCar db = tbBusinessCarService.checkCar(carNo);
  235. if (db != null) {
  236. throw new BusinessException("该车辆有未完成作业");
  237. }
  238. db = new TbBusinessCar();
  239. db.setCarNo(carNo).setIsLock(0);
  240. TbBusiness tbBusiness = new TbBusiness();
  241. if (StpUserUtil.isLogin()) {
  242. String customerId = StpUserUtil.getCustomerId();
  243. db.setCustomerId(customerId);
  244. TbCostomer tbCostomer = tbCostomerService.getById(customerId);
  245. tbBusiness.setCustomerId(customerId).setCustomerName(tbCostomer.getName());
  246. }
  247. db.setPay(0).setCarSize(12D);
  248. List<TbItem> tbItems = JSONUtil.toList(itemJson, TbItem.class);
  249. BigDecimal price = new BigDecimal("0");
  250. List<TbBusinessItem> itemList = new ArrayList<>();
  251. String no = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmm")) + RandomUtil.randomNumbers(4);
  252. int index = 1;
  253. for (TbItem tbItem : tbItems) {
  254. TbBusinessItem item = new TbBusinessItem();
  255. item.setNo(no + "0" + index)
  256. .setBusinessType(TbCostomer.CustomerEnum.DISINFECT_TYPE.getType());
  257. item.setItemCode(tbItem.getItemCode()).setNum("1")
  258. .setItemName(tbItem.getItemName()).setItemPrice(tbItem.getPrice())
  259. .setItemTypeId(tbItem.getTypeId()).setItemTypeName(tbItem.getTypeName())
  260. .setUnit(tbItem.getUnit()).setTotal(tbItem.getPrice()).setCreateTime(new Date());
  261. price = price.add(item.getItemPrice());
  262. itemList.add(item);
  263. index++;
  264. }
  265. BigDecimal initCarPartMoney = partConfig.getBasePrice();
  266. tbBusiness.setPartMoney(initCarPartMoney)
  267. .setNo(no).setGoodsName(carType)
  268. .setCreateTime(new Date()).setBusinessType(TbBusiness.BusinessType.CAR_DISINCLE.getCode())
  269. .setItemPrice(price)
  270. .setTotalMoney(price.add(initCarPartMoney));
  271. String prefix = carNo.substring(0, 1);
  272. if (CAR_LIST.contains(prefix)) {
  273. tbBusiness.setChinaCarNo(carNo);
  274. } else {
  275. tbBusiness.setCardNo(carNo);
  276. }
  277. tbBusinessService.save(tbBusiness);
  278. db.setBusinessId(tbBusiness.getId());
  279. tbBusinessCarService.save(db);
  280. itemList.forEach(tbBusinessItem -> tbBusinessItem.setBusinessId(tbBusiness.getId()));
  281. tbBusinessItemService.saveBatch(itemList);
  282. }
  283. public void editCarDisinfect(CarDisincle carDisincle, String itemJson, String type) {
  284. String carId = carDisincle.getBusinessCarId();
  285. TbBusinessCar businessCar = tbBusinessCarService.getById(carId);
  286. if (businessCar == null) {
  287. throw new BusinessException("数据不存在");
  288. }
  289. List<TbItem> tbItems = JSONUtil.toList(itemJson, TbItem.class);
  290. tbItems.forEach(tbItem -> {
  291. });
  292. }
  293. }