BackendCashierController.java 14 KB

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