BackendCashierController.java 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. package com.fuint.module.backendApi.controller;
  2. import com.fuint.common.Constants;
  3. import com.fuint.common.dto.*;
  4. import com.fuint.common.enums.OrderModeEnum;
  5. import com.fuint.common.enums.PlatformTypeEnum;
  6. import com.fuint.common.enums.StatusEnum;
  7. import com.fuint.common.enums.YesOrNoEnum;
  8. import com.fuint.common.service.*;
  9. import com.fuint.common.util.DateUtil;
  10. import com.fuint.common.util.PhoneFormatCheckUtils;
  11. import com.fuint.common.util.TokenUtil;
  12. import com.fuint.framework.exception.BusinessCheckException;
  13. import com.fuint.framework.web.BaseController;
  14. import com.fuint.framework.web.ResponseObject;
  15. import com.fuint.repository.model.*;
  16. import com.fuint.utils.StringUtil;
  17. import io.swagger.annotations.Api;
  18. import io.swagger.annotations.ApiOperation;
  19. import lombok.AllArgsConstructor;
  20. import org.springframework.beans.BeanUtils;
  21. import org.springframework.security.access.prepost.PreAuthorize;
  22. import org.springframework.web.bind.annotation.*;
  23. import javax.servlet.http.HttpServletRequest;
  24. import java.lang.reflect.InvocationTargetException;
  25. import java.math.BigDecimal;
  26. import java.util.ArrayList;
  27. import java.util.HashMap;
  28. import java.util.List;
  29. import java.util.Map;
  30. /**
  31. * 收银管理controller
  32. *
  33. * Created by FSQ
  34. * CopyRight https://www.fuint.cn
  35. */
  36. @Api(tags="管理端-收银台相关接口")
  37. @RestController
  38. @AllArgsConstructor
  39. @RequestMapping(value = "/backendApi/cashier")
  40. public class BackendCashierController extends BaseController {
  41. /**
  42. * 后台账户服务接口
  43. */
  44. private AccountService accountService;
  45. /**
  46. * 商品类别服务接口
  47. */
  48. private CateService cateService;
  49. /**
  50. * 购物车服务接口
  51. * */
  52. private CartService cartService;
  53. /**
  54. * 订单服务接口
  55. * */
  56. private OrderService orderService;
  57. /**
  58. * 商品服务接口
  59. */
  60. private GoodsService goodsService;
  61. /**
  62. * 店铺服务接口
  63. */
  64. private StoreService storeService;
  65. /**
  66. * 系统设置服务接口
  67. */
  68. private SettingService settingService;
  69. /**
  70. * 会员服务接口
  71. */
  72. private MemberService memberService;
  73. /**
  74. * 商户接口
  75. */
  76. private MerchantService merchantService;
  77. /**
  78. * 收银台初始化
  79. *
  80. * @param request HttpServletRequest对象
  81. * @return
  82. */
  83. @ApiOperation(value = "收银台初始化")
  84. @RequestMapping(value = "/init/{userId}", method = RequestMethod.GET)
  85. @CrossOrigin
  86. @PreAuthorize("@pms.hasPermission('cashier:index')")
  87. public ResponseObject init(HttpServletRequest request, @PathVariable("userId") Integer userId) throws BusinessCheckException {
  88. String token = request.getHeader("Access-Token");
  89. Integer page = request.getParameter("page") == null ? Constants.PAGE_NUMBER : Integer.parseInt(request.getParameter("page"));
  90. Integer pageSize = request.getParameter("pageSize") == null ? Constants.PAGE_SIZE : Integer.parseInt(request.getParameter("pageSize"));
  91. Integer cateId = request.getParameter("cateId") == null ? 0 : Integer.parseInt(request.getParameter("cateId"));
  92. AccountInfo accountDto = TokenUtil.getAccountInfoByToken(token);
  93. if (accountDto == null) {
  94. return getFailureResult(1001, "请先登录");
  95. }
  96. TAccount accountInfo = accountService.getAccountInfoById(accountDto.getId());
  97. Integer storeId = (accountInfo.getStoreId() == null || accountInfo.getStoreId() < 1) ? 0 : accountInfo.getStoreId();
  98. MtStore storeInfo = null;
  99. if (storeId == null || storeId < 1) {
  100. MtMerchant mtMerchant = merchantService.queryMerchantById(accountInfo.getMerchantId());
  101. if (mtMerchant != null) {
  102. storeInfo = storeService.getDefaultStore(mtMerchant.getNo());
  103. }
  104. } else {
  105. storeInfo = storeService.queryStoreById(storeId);
  106. }
  107. if (storeInfo != null) {
  108. storeId = storeInfo.getId();
  109. }
  110. MtUser memberInfo = null;
  111. if (userId != null && userId > 0) {
  112. memberInfo = memberService.queryMemberById(userId);
  113. }
  114. Map<String, Object> param = new HashMap<>();
  115. param.put("status", StatusEnum.ENABLED.getKey());
  116. if (accountInfo.getMerchantId() != null && accountInfo.getMerchantId() > 0) {
  117. param.put("merchantId", accountInfo.getMerchantId());
  118. } else {
  119. param.put("merchantId", 0);
  120. }
  121. if (storeId > 0) {
  122. param.put("storeId", storeId);
  123. } else {
  124. param.put("storeId", 0);
  125. }
  126. List<MtGoodsCate> cateList = cateService.queryCateListByParams(param);
  127. param.put("status", StatusEnum.ENABLED.getKey());
  128. Map<String, Object> goodsData = goodsService.getStoreGoodsList(storeId, "", cateId, page, pageSize);
  129. String imagePath = settingService.getUploadBasePath();
  130. Map<String, Object> result = new HashMap<>();
  131. result.put("imagePath", imagePath);
  132. result.put("storeInfo", storeInfo);
  133. result.put("memberInfo", memberInfo);
  134. result.put("accountInfo", accountDto);
  135. result.put("goodsList", goodsData.get("goodsList"));
  136. result.put("totalGoods", goodsData.get("total"));
  137. result.put("cateList", cateList);
  138. return getSuccessResult(result);
  139. }
  140. /**
  141. * 查询商品列表
  142. *
  143. * @param request
  144. * @param param
  145. * @return
  146. */
  147. @ApiOperation(value = "查询商品列表")
  148. @RequestMapping(value = "/searchGoods", method = RequestMethod.POST)
  149. @CrossOrigin
  150. @PreAuthorize("@pms.hasPermission('cashier:index')")
  151. public ResponseObject searchGoods(HttpServletRequest request, @RequestBody Map<String, Object> param) throws BusinessCheckException {
  152. String token = request.getHeader("Access-Token");
  153. String keyword = param.get("keyword") == null ? "" : param.get("keyword").toString();
  154. AccountInfo accountDto = TokenUtil.getAccountInfoByToken(token);
  155. if (accountDto == null) {
  156. return getFailureResult(1001, "请先登录");
  157. }
  158. TAccount accountInfo = accountService.getAccountInfoById(accountDto.getId());
  159. Integer storeId = accountInfo.getStoreId();
  160. if (storeId == null || storeId < 1) {
  161. MtMerchant mtMerchant = merchantService.queryMerchantById(accountInfo.getMerchantId());
  162. if (mtMerchant != null) {
  163. MtStore storeInfo = storeService.getDefaultStore(mtMerchant.getNo());
  164. if (storeInfo != null) {
  165. storeId = storeInfo.getId();
  166. }
  167. }
  168. }
  169. Map<String, Object> goodsData = goodsService.getStoreGoodsList(storeId, keyword, 0, 1, 100);
  170. return getSuccessResult(goodsData.get("goodsList"));
  171. }
  172. /**
  173. * 获取商品详情
  174. *
  175. * @param request
  176. * @param goodsId
  177. * @return
  178. */
  179. @ApiOperation(value = "获取商品详情")
  180. @RequestMapping(value = "/getGoodsInfo/{id}", method = RequestMethod.GET)
  181. @CrossOrigin
  182. @PreAuthorize("@pms.hasPermission('cashier:index')")
  183. public ResponseObject getGoodsInfo(HttpServletRequest request, @PathVariable("id") Integer goodsId) throws InvocationTargetException, IllegalAccessException {
  184. String token = request.getHeader("Access-Token");
  185. AccountInfo accountInfo = TokenUtil.getAccountInfoByToken(token);
  186. if (accountInfo == null) {
  187. return getFailureResult(1001, "请先登录");
  188. }
  189. GoodsDto goodsInfo = goodsService.getGoodsDetail(goodsId, false);
  190. Map<String, Object> result = new HashMap<>();
  191. result.put("goodsInfo", goodsInfo);
  192. // 商品规格列表
  193. List<String> specNameArr = new ArrayList<>();
  194. List<Integer> specIdArr = new ArrayList<>();
  195. List<GoodsSpecItemDto> specArr = new ArrayList<>();
  196. // sku列表
  197. List<GoodsSkuDto> skuArr = new ArrayList<>();
  198. if (goodsInfo != null) {
  199. // 处理规格列表
  200. for (MtGoodsSpec mtGoodsSpec : goodsInfo.getSpecList()) {
  201. if (!specNameArr.contains(mtGoodsSpec.getName())) {
  202. specNameArr.add(mtGoodsSpec.getName());
  203. specIdArr.add(mtGoodsSpec.getId());
  204. }
  205. }
  206. for (int i = 0; i < specNameArr.size(); i++) {
  207. GoodsSpecItemDto item = new GoodsSpecItemDto();
  208. List<GoodsSpecChildDto> child = new ArrayList<>();
  209. Integer specId = specIdArr.get(i) == null ? (i + 1) : specIdArr.get(i);
  210. String name = specNameArr.get(i);
  211. for (MtGoodsSpec mtGoodsSpec : goodsInfo.getSpecList()) {
  212. if (mtGoodsSpec.getName().equals(name)) {
  213. GoodsSpecChildDto e = new GoodsSpecChildDto();
  214. e.setId(mtGoodsSpec.getId());
  215. e.setName(mtGoodsSpec.getValue());
  216. e.setChecked(true);
  217. child.add(e);
  218. }
  219. }
  220. item.setId(specId);
  221. item.setName(name);
  222. item.setChild(child);
  223. specArr.add(item);
  224. }
  225. // 处理sku列表
  226. for (MtGoodsSku mtGoodsSku : goodsInfo.getSkuList()) {
  227. GoodsSkuDto skuDto = new GoodsSkuDto();
  228. BeanUtils.copyProperties(mtGoodsSku, skuDto);
  229. List<MtGoodsSpec> specList = new ArrayList<>();
  230. String[] specIds = skuDto.getSpecIds().split("-");
  231. for (String specId : specIds) {
  232. MtGoodsSpec spec = goodsService.getSpecDetail(Integer.parseInt(specId));
  233. if (spec != null) {
  234. specList.add(spec);
  235. }
  236. }
  237. skuDto.setSpecList(specList);
  238. skuArr.add(skuDto);
  239. }
  240. }
  241. result.put("specList", specArr);
  242. result.put("skuList", skuArr);
  243. return getSuccessResult(result);
  244. }
  245. /**
  246. * 搜索会员信息
  247. */
  248. @ApiOperation(value = "搜索会员信息")
  249. @RequestMapping(value = "/getMemberInfo", method = RequestMethod.POST)
  250. @CrossOrigin
  251. @PreAuthorize("@pms.hasPermission('cashier:index')")
  252. public ResponseObject getMemberInfo(HttpServletRequest request, @RequestBody Map<String, Object> param) throws BusinessCheckException {
  253. String token = request.getHeader("Access-Token");
  254. String keyword = param.get("keyword") == null ? "" : param.get("keyword").toString();
  255. AccountInfo accountInfo = TokenUtil.getAccountInfoByToken(token);
  256. if (accountInfo == null) {
  257. return getFailureResult(1001, "请先登录");
  258. }
  259. if (StringUtil.isEmpty(keyword)) {
  260. return getFailureResult(201);
  261. }
  262. MtUser userInfo;
  263. if (PhoneFormatCheckUtils.isChinaPhoneLegal(keyword)) {
  264. userInfo = memberService.queryMemberByMobile(accountInfo.getMerchantId(), keyword);
  265. } else {
  266. userInfo = memberService.queryMemberByName(accountInfo.getMerchantId(), keyword);
  267. if (userInfo == null) {
  268. userInfo = memberService.queryMemberByUserNo(accountInfo.getMerchantId(), keyword);
  269. }
  270. }
  271. Map<String, Object> result = new HashMap<>();
  272. result.put("memberInfo", userInfo);
  273. return getSuccessResult(result);
  274. }
  275. /**
  276. * 获取会员信息
  277. */
  278. @ApiOperation(value = "获取会员信息")
  279. @RequestMapping(value = "/getMemberInfoById/{userId}", method = RequestMethod.GET)
  280. @CrossOrigin
  281. @PreAuthorize("@pms.hasPermission('cashier:index')")
  282. public ResponseObject getMemberInfoById(HttpServletRequest request, @PathVariable("userId") String userId) throws BusinessCheckException {
  283. String token = request.getHeader("Access-Token");
  284. AccountInfo accountInfo = TokenUtil.getAccountInfoByToken(token);
  285. if (accountInfo == null) {
  286. return getFailureResult(1001, "请先登录");
  287. }
  288. if (StringUtil.isEmpty(userId)) {
  289. return getFailureResult(201);
  290. }
  291. MtUser userInfo = memberService.queryMemberById(Integer.parseInt(userId));
  292. Map<String, Object> result = new HashMap<>();
  293. result.put("memberInfo", userInfo);
  294. return getSuccessResult(result);
  295. }
  296. /**
  297. * 执行挂单
  298. */
  299. @ApiOperation(value = "执行挂单")
  300. @RequestMapping(value = "/doHangUp", method = RequestMethod.POST)
  301. @CrossOrigin
  302. @PreAuthorize("@pms.hasPermission('cashier:index')")
  303. public ResponseObject doHangUp(HttpServletRequest request, @RequestBody Map<String, Object> param) {
  304. String token = request.getHeader("Access-Token");
  305. String cartIds = param.get("cartIds") == null ? "" : param.get("cartIds").toString();
  306. String hangNo = param.get("hangNo") == null ? "" : param.get("hangNo").toString();
  307. String userId = param.get("userId") == null ? "" : param.get("userId").toString();
  308. AccountInfo accountInfo = TokenUtil.getAccountInfoByToken(token);
  309. if (accountInfo == null) {
  310. return getFailureResult(1001, "请先登录");
  311. }
  312. String isVisitor = YesOrNoEnum.NO.getKey();
  313. if (StringUtil.isEmpty(userId)) {
  314. isVisitor = YesOrNoEnum.YES.getKey();
  315. }
  316. try {
  317. if (StringUtil.isNotEmpty(cartIds)) {
  318. String[] ids = cartIds.split(",");
  319. if (ids.length > 0) {
  320. for (int i = 0; i < ids.length; i++) {
  321. cartService.setHangNo(Integer.parseInt(ids[i]), hangNo, isVisitor);
  322. }
  323. }
  324. }
  325. } catch (BusinessCheckException e) {
  326. return getFailureResult(201, "挂单失败");
  327. }
  328. return getSuccessResult(true);
  329. }
  330. /**
  331. * 获取挂单列表
  332. */
  333. @ApiOperation(value = "获取挂单列表")
  334. @RequestMapping(value = "/getHangUpList", method = RequestMethod.GET)
  335. @CrossOrigin
  336. @PreAuthorize("@pms.hasPermission('cashier:index')")
  337. public ResponseObject getHangUpList(HttpServletRequest request) throws BusinessCheckException {
  338. String token = request.getHeader("Access-Token");
  339. AccountInfo accountInfo = TokenUtil.getAccountInfoByToken(token);
  340. if (accountInfo == null) {
  341. return getFailureResult(1001, "请先登录");
  342. }
  343. List<HangUpDto> dataList = new ArrayList<>();
  344. for (int i = 0; i < 20; i++) {
  345. String hangNo = "#0" + (i+1);
  346. Map<String, Object> param = new HashMap<>();
  347. param.put("hangNo", hangNo);
  348. param.put("merchantId", accountInfo.getMerchantId());
  349. List<MtCart> cartList = cartService.queryCartListByParams(param);
  350. HangUpDto dto = new HangUpDto();
  351. dto.setIsEmpty(true);
  352. if (cartList.size() > 0) {
  353. Integer userId = cartList.get(0).getUserId();
  354. String isVisitor = cartList.get(0).getIsVisitor();
  355. Map<String, Object> cartInfo = orderService.calculateCartGoods(accountInfo.getMerchantId(), userId, cartList, 0, false, PlatformTypeEnum.PC.getCode(), OrderModeEnum.ONESELF.getKey());
  356. dto.setNum(Integer.parseInt(cartInfo.get("totalNum").toString()));
  357. dto.setAmount(new BigDecimal(cartInfo.get("totalPrice").toString()));
  358. if (isVisitor.equals(YesOrNoEnum.NO.getKey())) {
  359. MtUser userInfo = memberService.queryMemberById(userId);
  360. dto.setMemberInfo(userInfo);
  361. }
  362. String dateTime = DateUtil.formatDate(cartList.get(0).getUpdateTime(), "yyyy-MM-dd HH:mm:ss");
  363. dto.setDateTime(dateTime);
  364. dto.setIsEmpty(false);
  365. }
  366. dto.setHangNo(hangNo);
  367. dataList.add(dto);
  368. }
  369. return getSuccessResult(dataList);
  370. }
  371. }