HsCanteenOrderServiceImpl.java 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. package com.willalp.canteen.service.impl;
  2. import cn.hutool.core.date.DateUtil;
  3. import cn.hutool.core.util.ObjectUtil;
  4. import cn.hutool.core.util.StrUtil;
  5. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  6. import com.baomidou.mybatisplus.core.toolkit.IdWorker;
  7. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  8. import com.willalp.canteen.domain.*;
  9. import com.willalp.canteen.domain.resp.CustomGetOrderResp;
  10. import com.willalp.canteen.domain.vo.CustomGetOrderVo;
  11. import com.willalp.canteen.domain.vo.HsCanteenOrderInfoVo;
  12. import com.willalp.canteen.domain.vo.MenuCountVo;
  13. import com.willalp.canteen.mapper.HsCanteenOrderMapper;
  14. import com.willalp.canteen.service.IHsCanteenDiningPlaceService;
  15. import com.willalp.canteen.service.IHsCanteenMenuService;
  16. import com.willalp.canteen.service.IHsCanteenOrderDetailService;
  17. import com.willalp.canteen.service.IHsCanteenOrderService;
  18. import com.willalp.common.core.domain.AjaxResult;
  19. import com.willalp.common.enums.ConsumeChangeEnum;
  20. import com.willalp.common.enums.NumberRuleEnum;
  21. import com.willalp.common.enums.SysOrgCodeEnum;
  22. import com.willalp.common.exception.base.BaseException;
  23. import com.willalp.common.utils.DateUtils;
  24. import com.willalp.common.utils.SecurityUtils;
  25. import com.willalp.common.utils.StringUtils;
  26. import com.willalp.common.utils.bean.BeanUtils;
  27. import com.willalp.integral.domain.Integral;
  28. import com.willalp.integral.service.IIntegralService;
  29. import com.willalp.number.domain.req.NumberRuleReq;
  30. import com.willalp.number.service.IHsSysNumberRuleService;
  31. import com.willalp.system.domain.SysUserOrganization;
  32. import com.willalp.system.service.ISysUserOrganizationService;
  33. import com.willalp.system.service.ISysUserService;
  34. import org.springframework.beans.factory.annotation.Autowired;
  35. import org.springframework.stereotype.Service;
  36. import org.springframework.transaction.annotation.Transactional;
  37. import javax.annotation.Resource;
  38. import java.math.BigDecimal;
  39. import java.util.*;
  40. import java.util.stream.Collectors;
  41. /**
  42. * 食堂订单Service业务层处理
  43. *
  44. * @author songyu
  45. * @date 2021-12-03
  46. */
  47. @Service
  48. public class HsCanteenOrderServiceImpl extends ServiceImpl<HsCanteenOrderMapper, HsCanteenOrder> implements IHsCanteenOrderService {
  49. @Autowired
  50. private HsCanteenOrderMapper hsCanteenOrderMapper;
  51. @Resource
  52. private IHsCanteenOrderDetailService codService;
  53. @Autowired
  54. ISysUserService sysUserService;
  55. @Autowired
  56. IHsSysNumberRuleService hsSysNumberRuleService;
  57. @Autowired
  58. IIntegralService integralService;
  59. @Autowired
  60. ISysUserOrganizationService sysUserOrganizationService;
  61. @Resource
  62. private IHsCanteenMenuService menuService;
  63. @Resource
  64. private IHsCanteenDiningPlaceService diningPlaceService;
  65. @Resource
  66. private ISysUserOrganizationService userOrganizationService;
  67. @Transactional(rollbackFor = Exception.class)
  68. @Override
  69. public boolean payMiwp(HsCanteenOrder order) {
  70. if (ObjectUtil.isEmpty(order)) {
  71. throw new BaseException("没有订单信息");
  72. }
  73. List<String> loginName = order.getLoginName();
  74. List<HsCanteenCuisine> list = order.getList();
  75. String menuNumber = order.getMenuNumber();
  76. String placeNumber = order.getPlaceNumber();
  77. BigDecimal orderTotal = order.getOrderTotal();
  78. if (StringUtils.isBlank(menuNumber)) {
  79. throw new BaseException("请选择菜单");
  80. }
  81. if (0 == list.size()) {
  82. throw new BaseException("请选择菜品");
  83. }
  84. if (0 == loginName.size()) {
  85. throw new BaseException("请选择用户");
  86. }
  87. if (StringUtils.isBlank(placeNumber)) {
  88. throw new BaseException("请选择就餐地");
  89. }
  90. Date date = new Date();
  91. List<HsCanteenOrder> oSaveList = new ArrayList<>();
  92. List<HsCanteenOrderDetail> oDSaveList = new ArrayList<>();
  93. loginName.forEach(item -> {
  94. String jgid = order.getJgid();
  95. SysUserOrganization uo = new SysUserOrganization();
  96. String[] split = item.split(",");
  97. if (0 < split.length && split.length <= 2) {
  98. uo.setLoginName(split[0]);
  99. } else {
  100. throw new BaseException("用户异常请重新选择");
  101. }
  102. uo.setOrganizationCode(jgid);
  103. List<SysUserOrganization> uoDB = sysUserOrganizationService.selectSysUserOrganizationList(uo);
  104. SysUserOrganization sysUserOrganization = uoDB.get(0);
  105. BigDecimal integral = sysUserOrganization.getIntegral();
  106. if (integral.compareTo(orderTotal) > 0) {
  107. BigDecimal subtract = integral.subtract(orderTotal);
  108. sysUserOrganization.setIntegral(subtract);
  109. if (!sysUserOrganizationService.updateById(sysUserOrganization)) {
  110. throw new BaseException("积分更新失败,请重试");
  111. }
  112. } else {
  113. throw new BaseException(split[1] + " - 积分不足");
  114. }
  115. HsCanteenOrder canteenOrder = new HsCanteenOrder();
  116. //支付时间
  117. canteenOrder.setPaytime(date);
  118. //获取编号
  119. NumberRuleReq numberRuleReq = new NumberRuleReq();
  120. numberRuleReq.setRuleId(NumberRuleEnum.ORDER.getValue());
  121. numberRuleReq.setInput(String.valueOf(DateUtil.year(canteenOrder.getPaytime())).substring(2, 4));
  122. String number = hsSysNumberRuleService.getNumber(numberRuleReq);
  123. canteenOrder.setOrderNumber(number);
  124. canteenOrder.setUserNumber(split[0]);
  125. canteenOrder.setOrderTotal(orderTotal);
  126. canteenOrder.setMenuNumber(menuNumber);
  127. canteenOrder.setPlaceNumber(placeNumber);
  128. canteenOrder.setPlaceName(order.getPlaceName());
  129. canteenOrder.setZfbz(0);
  130. canteenOrder.setConsumeSign(0);
  131. canteenOrder.setNickName(order.getNickName());
  132. canteenOrder.setJgid(jgid);
  133. oSaveList.add(canteenOrder);
  134. list.forEach(cItem -> {
  135. HsCanteenOrderDetail cod = new HsCanteenOrderDetail();
  136. BeanUtils.copyProperties(cItem, cod);
  137. cod.setMenuNumber(menuNumber);
  138. cod.setOrderNumber(number);
  139. cod.setPrice(cItem.getCuisinePrice());
  140. cod.setAmount(1);
  141. cod.setTotal(orderTotal);
  142. oDSaveList.add(cod);
  143. });
  144. });
  145. if (0 != oSaveList.size()) {
  146. if (!saveBatch(oSaveList)) {
  147. throw new BaseException("订单生成失败");
  148. }
  149. }
  150. if (0 != oDSaveList.size()) {
  151. if (!codService.saveBatch(oDSaveList)) {
  152. throw new BaseException("订单详情生成失败");
  153. }
  154. }
  155. return true;
  156. }
  157. @Override
  158. public Object jcdStat(String menuCode) {
  159. if (StringUtils.isBlank(menuCode)) {
  160. throw new BaseException("菜单编号不能为空");
  161. }
  162. Map<String, Object> map = new HashMap<>();
  163. HsCanteenMenu canteenMenu = menuService.getBaseMapper().selectOne(new QueryWrapper<HsCanteenMenu>().eq("menu_number", menuCode));
  164. map.put("menuName", canteenMenu.getMenuName());
  165. List<HsCanteenOrder> orders = baseMapper.selectList(new QueryWrapper<HsCanteenOrder>().eq("menu_number", menuCode));
  166. return jcdTj(map, orders);
  167. }
  168. @Override
  169. public Object jcdMonthStat(int year, int month) {
  170. Map<String, Object> map = new HashMap<>();
  171. List<HsCanteenOrder> orders = baseMapper.selectList(new QueryWrapper<HsCanteenOrder>()
  172. .eq("YEAR(paytime) ", year)
  173. .eq("MONTH(paytime)", month));
  174. return jcdTj(map, orders);
  175. }
  176. private Map<String, Object> jcdTj(Map<String, Object> map, List<HsCanteenOrder> orders) {
  177. if (0 == orders.size()) {
  178. throw new BaseException("暂无统计数据");
  179. }
  180. List<HsCanteenDiningPlace> hsCanteenDiningPlaces = diningPlaceService.getBaseMapper().selectList(
  181. new QueryWrapper<HsCanteenDiningPlace>()
  182. .eq("jgid", SysOrgCodeEnum.MLHuiZhuLi.getCode())
  183. .notIn("parent_id", 0));
  184. if (0 == hsCanteenDiningPlaces.size()) {
  185. throw new BaseException("暂无统计数据");
  186. }
  187. List<SysUserOrganization> sysUserOrganizations = userOrganizationService.getBaseMapper().selectList(
  188. new QueryWrapper<SysUserOrganization>().eq("organization_code", SysOrgCodeEnum.MLHuiZhuLi.getCode()));
  189. if (0 == sysUserOrganizations.size()) {
  190. throw new BaseException("暂无统计数据");
  191. }
  192. List<Object> list = new ArrayList<>();
  193. for (HsCanteenDiningPlace hsCanteenDiningPlace : hsCanteenDiningPlaces) {
  194. Map<String, Object> mapJcd = new HashMap<String, Object>();
  195. mapJcd.put("placeName", hsCanteenDiningPlace.getPlaceName());
  196. //就餐地总数
  197. List<HsCanteenOrder> jdcSumList = orders.stream().filter(o ->
  198. null != o.getPlaceNumber() &&
  199. o.getZfbz() != 1 &&
  200. o.getPlaceNumber().equals(hsCanteenDiningPlace.getPlaceNumber())).collect(Collectors.toList());
  201. mapJcd.put("count", jdcSumList.size());
  202. //用户类型计算
  203. int sybq = 0;
  204. int fsybq = 0;
  205. int dsflwwb = 0;
  206. for (HsCanteenOrder hsCanteenOrder : jdcSumList) {
  207. for (SysUserOrganization userOrganization : sysUserOrganizations) {
  208. if (hsCanteenOrder.getUserNumber().equals(userOrganization.getLoginName()) &&
  209. hsCanteenOrder.getZfbz() != 1) {
  210. switch (userOrganization.getUserType()) {
  211. case "1":
  212. //事业编群
  213. sybq++;
  214. break;
  215. case "2":
  216. //非事业编群
  217. fsybq++;
  218. break;
  219. case "3":
  220. case "4":
  221. //第三方劳务外包
  222. dsflwwb++;
  223. break;
  224. default:
  225. break;
  226. }
  227. }
  228. }
  229. }
  230. mapJcd.put("sybq", sybq);
  231. mapJcd.put("fsybq", fsybq);
  232. mapJcd.put("dsflwwb", dsflwwb);
  233. list.add(mapJcd);
  234. }
  235. map.put("place", list);
  236. return map;
  237. }
  238. @Override
  239. public List<MenuCountVo> getTheMenuItemsAndQuantitiesPerPerson(String menuCode) {
  240. if (StringUtils.isBlank(menuCode)) {
  241. throw new BaseException("菜单编号不能为空");
  242. }
  243. List<HsCanteenOrderInfoVo> orderDataBasedOnMenu = hsCanteenOrderMapper.getOrderDataBasedOnMenu(menuCode);
  244. if (0 == orderDataBasedOnMenu.size()) {
  245. throw new BaseException("该菜单下没有可用订单");
  246. }
  247. //提取用户 使用map分组
  248. Map<String, List<HsCanteenOrderInfoVo>> mapOs = new HashMap<>();
  249. orderDataBasedOnMenu.forEach(o -> {
  250. String userNumber = o.getUserNumber();
  251. if (StringUtils.isNotBlank(userNumber) && null == mapOs.get(userNumber)) {
  252. mapOs.put(userNumber, new ArrayList<>());
  253. }
  254. });
  255. //寻找每个用户的菜品
  256. mapOs.forEach((k, v) -> v.addAll(orderDataBasedOnMenu.stream().filter(o -> k.equals(o.getUserNumber())).collect(Collectors.toList())));
  257. List<MenuCountVo> mvs = new ArrayList<>();
  258. //拼接菜品信息
  259. mapOs.forEach((k, v) -> {
  260. StringBuilder foodInformation = new StringBuilder();
  261. for (HsCanteenOrderInfoVo xv : v) {
  262. String cuisineName = xv.getCuisineName();
  263. if (foodInformation.toString().contains(cuisineName)) {
  264. break;
  265. }
  266. //获取每个菜品的数量
  267. int i = (int) v.stream().filter(xv1 -> StringUtils.isNotBlank(cuisineName) && cuisineName.contentEquals(xv1.getCuisineName())).count();
  268. //拼接单个菜品信息
  269. foodInformation.append("[ ").append(cuisineName).append(" * ").append(i).append(" ] ");
  270. }
  271. MenuCountVo mv = new MenuCountVo(v.get(0).getNickName(), k, foodInformation.toString(), v.get(0).getPlaceName());
  272. mvs.add(mv);
  273. });
  274. return mvs;
  275. }
  276. /**
  277. * 查询食堂订单
  278. *
  279. * @param orderId 食堂订单主键
  280. * @return 食堂订单
  281. */
  282. @Override
  283. public HsCanteenOrder selectHsCanteenOrderByOrderId(String orderId) {
  284. return hsCanteenOrderMapper.selectHsCanteenOrderByOrderId(orderId);
  285. }
  286. /**
  287. * 查询食堂订单列表
  288. *
  289. * @param hsCanteenOrder 食堂订单
  290. * @return 食堂订单
  291. */
  292. @Override
  293. public List<HsCanteenOrder> selectHsCanteenOrderList(HsCanteenOrder hsCanteenOrder) {
  294. return hsCanteenOrderMapper.selectHsCanteenOrderList(hsCanteenOrder);
  295. }
  296. /**
  297. * 作废食堂订单
  298. *
  299. * @param hsCanteenOrder
  300. * @return
  301. */
  302. @Override
  303. @Transactional(rollbackFor = Exception.class)
  304. public AjaxResult getBack(HsCanteenOrder hsCanteenOrder) {
  305. BigDecimal total = hsCanteenOrder.getOrderTotal();
  306. if (hsCanteenOrder.getZfbz() == 1) {
  307. return AjaxResult.error("该订单已作废,不可重复作废");
  308. }
  309. if (StrUtil.isBlank(hsCanteenOrder.getUserNumber())) {
  310. return AjaxResult.error("用户编号为空,请联系管理员");
  311. }
  312. if (hsCanteenOrder.getZfbz() == 3) {
  313. return AjaxResult.error("因错退还订单不可作废");
  314. }
  315. try {
  316. SysUserOrganization user = new SysUserOrganization();
  317. user.setLoginName(hsCanteenOrder.getUserNumber());
  318. user.setOrganizationCode(hsCanteenOrder.getJgid());
  319. List<SysUserOrganization> userList = sysUserOrganizationService.selectSysUserOrganizationList(user);
  320. if (userList.size() == 0) {
  321. return AjaxResult.error("用户信息不存在");
  322. }
  323. user = userList.get(0);
  324. //消费记录
  325. Integral integral = new Integral();
  326. integral.setId(IdWorker.getIdStr());//积分主键
  327. integral.setMoneyType(Integer.valueOf(ConsumeChangeEnum.REFUND.getValue()));//退还
  328. integral.setRechargeEquipmentCode(hsCanteenOrder.getDeviceNumber());//设备编号
  329. integral.setRechargeEquipmentName(hsCanteenOrder.getDeviceName());//设备名称
  330. integral.setLoginName(hsCanteenOrder.getUserNumber());//账号编号
  331. integral.setRechargeIntegral(hsCanteenOrder.getOrderTotal());//积分(负数)
  332. integral.setIsDelete(0);//有效标志
  333. integral.setNickName(user.getNickName());
  334. integral.setOrganizationCode(hsCanteenOrder.getJgid());//机构代码
  335. integral.setOrderNumber(hsCanteenOrder.getOrderNumber());
  336. integral.setRechargeId(SecurityUtils.getLoginUser().getUserId());
  337. integral.setRechargeTime(DateUtils.getNowDate());
  338. if (hsCanteenOrder.getConsumeSign() == 0) {//积分
  339. user.setIntegral(user.getIntegral().add(hsCanteenOrder.getOrderTotal()));
  340. } else {//记账额度
  341. user.setQuota(user.getQuota().add(hsCanteenOrder.getOrderTotal()));
  342. }
  343. integralService.save(integral);
  344. sysUserOrganizationService.updateById(user);
  345. hsCanteenOrderMapper.updateCanteenOrderZfpb(hsCanteenOrder.getJgid(), hsCanteenOrder.getOrderId());//更新订单主表作废判别
  346. } catch (Exception e) {
  347. throw new BaseException("作废失败");
  348. }
  349. return AjaxResult.success();
  350. }
  351. /**
  352. * 待取餐订单
  353. *
  354. * @param customGetOrderVo
  355. * @return
  356. */
  357. @Override
  358. public List<CustomGetOrderResp> getOrderNow(CustomGetOrderVo customGetOrderVo) {
  359. return hsCanteenOrderMapper.getOrderNow(customGetOrderVo);
  360. }
  361. @Override
  362. public CustomGetOrderResp getMenuNow(CustomGetOrderVo customGetOrderVo) {
  363. return hsCanteenOrderMapper.getMenuNow(customGetOrderVo);
  364. }
  365. /**
  366. * 近一月订单量
  367. *
  368. * @param hsCanteenOrder
  369. * @return
  370. */
  371. @Override
  372. public List<HsCanteenOrder> selectHsCanteenOrderLists(HsCanteenOrder hsCanteenOrder) {
  373. return hsCanteenOrderMapper.selectHsCanteenOrderLists(hsCanteenOrder);
  374. }
  375. /**
  376. * 首页金额总额
  377. *
  378. * @param hsCanteenOrder
  379. * @return
  380. */
  381. @Override
  382. public String getSumpay(HsCanteenOrder hsCanteenOrder) {
  383. return hsCanteenOrderMapper.getSumpay(hsCanteenOrder);
  384. }
  385. /**
  386. * 首页订单总数
  387. *
  388. * @param hsCanteenOrder
  389. * @return
  390. */
  391. @Override
  392. public String getSumOrderNum(HsCanteenOrder hsCanteenOrder) {
  393. return hsCanteenOrderMapper.getSumOrderNum(hsCanteenOrder);
  394. }
  395. @Override
  396. public void checkOrderTotal(HsCanteenOrder hsCanteenOrder) {
  397. if (hsCanteenOrder.getZfbz() == 1) {
  398. throw new BaseException("该订单已作废,不可退还");
  399. } else if (hsCanteenOrder.getZfbz() == 3) {
  400. throw new BaseException("该订单为因错退还,不可再次退还");
  401. }
  402. List<Map<String, Object>> zforderlist = hsCanteenOrderMapper.checkSfzf(hsCanteenOrder);
  403. BigDecimal totalpay = new BigDecimal(zforderlist.get(0).get("order_total") + "");
  404. BigDecimal partpay = BigDecimal.ZERO;
  405. if (zforderlist.get(1).size() > 0) {
  406. partpay = new BigDecimal(zforderlist.get(1).get("order_total") + "").add(hsCanteenOrder.getOrderTotal());
  407. }
  408. if (totalpay.compareTo(partpay) < 0) {
  409. throw new BaseException("已超出最大退还金额,不可退还");
  410. }
  411. //积分退还
  412. SysUserOrganization user = new SysUserOrganization();
  413. user.setLoginName(hsCanteenOrder.getUserNumber());
  414. user.setOrganizationCode(hsCanteenOrder.getJgid());
  415. List<SysUserOrganization> userList = sysUserOrganizationService.selectSysUserOrganizationList(user);
  416. if (userList.size() == 0) {
  417. throw new BaseException("用户信息不存在");
  418. }
  419. user = userList.get(0);
  420. user.setIntegral(user.getIntegral().add(hsCanteenOrder.getOrderTotal()));
  421. sysUserOrganizationService.updateById(user);
  422. }
  423. @Override
  424. public List<Map<String, Object>> getConsumeReport(Map<String, Object> params) {
  425. return hsCanteenOrderMapper.getConsumeReport(params);
  426. }
  427. @Override
  428. public List<HsCanteenOrder> todayNotOrderUser() {
  429. return hsCanteenOrderMapper.todayNotOrderUser();
  430. }
  431. }