BackendBookController.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. package com.fuint.module.backendApi.controller;
  2. import com.fuint.common.dto.AccountInfo;
  3. import com.fuint.common.dto.BookDto;
  4. import com.fuint.common.service.BookCateService;
  5. import com.fuint.common.service.BookService;
  6. import com.fuint.common.service.StoreService;
  7. import com.fuint.common.util.TokenUtil;
  8. import com.fuint.framework.web.BaseController;
  9. import com.fuint.framework.web.ResponseObject;
  10. import com.fuint.common.Constants;
  11. import com.fuint.common.enums.StatusEnum;
  12. import com.fuint.common.service.SettingService;
  13. import com.fuint.framework.pagination.PaginationRequest;
  14. import com.fuint.framework.pagination.PaginationResponse;
  15. import com.fuint.framework.exception.BusinessCheckException;
  16. import com.fuint.repository.model.MtBook;
  17. import com.fuint.repository.model.MtBookCate;
  18. import com.fuint.repository.model.MtStore;
  19. import com.fuint.utils.StringUtil;
  20. import io.swagger.annotations.Api;
  21. import io.swagger.annotations.ApiOperation;
  22. import lombok.AllArgsConstructor;
  23. import org.springframework.beans.BeanUtils;
  24. import org.springframework.security.access.prepost.PreAuthorize;
  25. import org.springframework.web.bind.annotation.*;
  26. import javax.servlet.http.HttpServletRequest;
  27. import java.text.ParseException;
  28. import java.util.*;
  29. import java.util.stream.Collectors;
  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/book")
  40. public class BackendBookController extends BaseController {
  41. /**
  42. * 预约服务接口
  43. */
  44. private BookService bookService;
  45. /**
  46. * 系统设置服务接口
  47. * */
  48. private SettingService settingService;
  49. /**
  50. * 店铺服务接口
  51. */
  52. private StoreService storeService;
  53. /**
  54. * 预约分类服务接口
  55. */
  56. private BookCateService bookCateService;
  57. /**
  58. * 预约项目列表查询
  59. *
  60. * @param request HttpServletRequest对象
  61. * @return 预约项目列表
  62. */
  63. @ApiOperation(value = "预约项目列表查询")
  64. @RequestMapping(value = "/list", method = RequestMethod.GET)
  65. @CrossOrigin
  66. @PreAuthorize("@pms.hasPermission('book:index')")
  67. public ResponseObject list(HttpServletRequest request) throws BusinessCheckException {
  68. String token = request.getHeader("Access-Token");
  69. Integer page = request.getParameter("page") == null ? Constants.PAGE_NUMBER : Integer.parseInt(request.getParameter("page"));
  70. Integer pageSize = request.getParameter("pageSize") == null ? Constants.PAGE_SIZE : Integer.parseInt(request.getParameter("pageSize"));
  71. String name = request.getParameter("name");
  72. String cateId = request.getParameter("cateId");
  73. String status = request.getParameter("status");
  74. String searchStoreId = request.getParameter("storeId");
  75. AccountInfo accountInfo = TokenUtil.getAccountInfoByToken(token);
  76. Integer storeId;
  77. if (accountInfo == null) {
  78. return getFailureResult(1001, "请先登录");
  79. } else {
  80. storeId = accountInfo.getStoreId();
  81. }
  82. PaginationRequest paginationRequest = new PaginationRequest();
  83. paginationRequest.setCurrentPage(page);
  84. paginationRequest.setPageSize(pageSize);
  85. Map<String, Object> params = new HashMap<>();
  86. if (accountInfo.getMerchantId() != null && accountInfo.getMerchantId() > 0) {
  87. params.put("merchantId", accountInfo.getMerchantId());
  88. }
  89. if (StringUtil.isNotEmpty(name)) {
  90. params.put("name", name);
  91. }
  92. if (StringUtil.isNotEmpty(cateId)) {
  93. params.put("cateId", cateId);
  94. }
  95. if (StringUtil.isNotEmpty(status)) {
  96. params.put("status", status);
  97. }
  98. if (StringUtil.isNotEmpty(searchStoreId)) {
  99. params.put("storeId", searchStoreId);
  100. }
  101. if (storeId != null && storeId > 0) {
  102. params.put("storeId", storeId);
  103. }
  104. paginationRequest.setSearchParams(params);
  105. PaginationResponse<BookDto> paginationResponse = bookService.queryBookListByPagination(paginationRequest);
  106. Map<String, Object> param = new HashMap<>();
  107. param.put("status", StatusEnum.ENABLED.getKey());
  108. if (accountInfo.getStoreId() != null && accountInfo.getStoreId() > 0) {
  109. param.put("storeId", accountInfo.getStoreId().toString());
  110. }
  111. if (accountInfo.getMerchantId() != null && accountInfo.getMerchantId() > 0) {
  112. param.put("merchantId", accountInfo.getMerchantId());
  113. }
  114. List<MtStore> storeList = storeService.queryStoresByParams(param);
  115. String imagePath = settingService.getUploadBasePath();
  116. List<MtBookCate> cateList = bookCateService.queryBookCateListByParams(param);
  117. Map<String, Object> result = new HashMap<>();
  118. result.put("dataList", paginationResponse);
  119. result.put("imagePath", imagePath);
  120. result.put("storeList", storeList);
  121. result.put("cateList", cateList);
  122. return getSuccessResult(result);
  123. }
  124. /**
  125. * 更新预约项目状态
  126. *
  127. * @return
  128. */
  129. @ApiOperation(value = "更新预约项目状态")
  130. @RequestMapping(value = "/updateStatus", method = RequestMethod.POST)
  131. @CrossOrigin
  132. @PreAuthorize("@pms.hasPermission('book:index')")
  133. public ResponseObject updateStatus(HttpServletRequest request, @RequestBody Map<String, Object> params) throws BusinessCheckException, ParseException {
  134. String token = request.getHeader("Access-Token");
  135. String status = params.get("status") != null ? params.get("status").toString() : StatusEnum.ENABLED.getKey();
  136. Integer id = params.get("id") == null ? 0 : Integer.parseInt(params.get("id").toString());
  137. AccountInfo accountInfo = TokenUtil.getAccountInfoByToken(token);
  138. if (accountInfo == null) {
  139. return getFailureResult(1001, "请先登录");
  140. }
  141. BookDto bookDto = bookService.getBookById(id);
  142. if (bookDto == null) {
  143. return getFailureResult(201);
  144. }
  145. String operator = accountInfo.getAccountName();
  146. MtBook mtBook = new MtBook();
  147. BeanUtils.copyProperties(bookDto, mtBook);
  148. mtBook.setOperator(operator);
  149. mtBook.setStatus(status);
  150. bookService.updateBook(mtBook);
  151. return getSuccessResult(true);
  152. }
  153. /**
  154. * 保存预约项目
  155. *
  156. * @param request HttpServletRequest对象
  157. * @return
  158. */
  159. @ApiOperation(value = "保存预约项目")
  160. @RequestMapping(value = "/save", method = RequestMethod.POST)
  161. @CrossOrigin
  162. @PreAuthorize("@pms.hasPermission('book:index')")
  163. public ResponseObject saveHandler(HttpServletRequest request, @RequestBody Map<String, Object> params) throws BusinessCheckException {
  164. String token = request.getHeader("Access-Token");
  165. String id = params.get("id") == null ? "" : params.get("id").toString();
  166. String cateId = params.get("cateId") == null ? "0" : params.get("cateId").toString();
  167. String name = params.get("name") == null ? "" : params.get("name").toString();
  168. String description = params.get("description") == null ? "" : params.get("description").toString();
  169. String logo = params.get("logo") == null ? "" : params.get("logo").toString();
  170. String status = params.get("status") == null ? "" : params.get("status").toString();
  171. String storeId = (params.get("storeId") == null || StringUtil.isEmpty(params.get("storeId").toString())) ? "0" : params.get("storeId").toString();
  172. String sort = (params.get("sort") == null || StringUtil.isEmpty(params.get("sort").toString())) ? "0" : params.get("sort").toString();
  173. String dates = params.get("dates") == null ? "" : params.get("dates").toString();
  174. List<LinkedHashMap> times = params.get("times") == null ? new ArrayList<>() : (List) params.get("times");
  175. AccountInfo accountInfo = TokenUtil.getAccountInfoByToken(token);
  176. if (accountInfo == null) {
  177. return getFailureResult(1001, "请先登录");
  178. }
  179. if (accountInfo.getMerchantId() == null || accountInfo.getMerchantId() < 1) {
  180. throw new BusinessCheckException("平台方帐号无法执行该操作,请使用商户帐号操作");
  181. }
  182. MtBook mtBook = new MtBook();
  183. mtBook.setName(name);
  184. mtBook.setDescription(description);
  185. mtBook.setLogo(logo);
  186. mtBook.setOperator(accountInfo.getAccountName());
  187. mtBook.setStatus(status);
  188. mtBook.setStoreId(Integer.parseInt(storeId));
  189. mtBook.setSort(Integer.parseInt(sort));
  190. mtBook.setMerchantId(accountInfo.getMerchantId());
  191. mtBook.setServiceDates(dates);
  192. if (StringUtil.isNotEmpty(cateId)) {
  193. mtBook.setCateId(Integer.parseInt(cateId));
  194. }
  195. String timeStr = "";
  196. if (times != null && times.size() > 0) {
  197. List<String> timeArr = new ArrayList<>();
  198. for (LinkedHashMap time : times) {
  199. String startTime = time.get("startTime") == null ? "" : time.get("startTime").toString();
  200. String endTime = time.get("endTime") == null ? "" : time.get("endTime").toString();
  201. String num = time.get("num") == null ? "" : time.get("num").toString();
  202. if (StringUtil.isNotEmpty(startTime) && StringUtil.isNotEmpty(endTime) && StringUtil.isNotEmpty(num)) {
  203. String item = startTime + "-" + endTime + "-" + num;
  204. if (!timeArr.contains(item)) {
  205. timeArr.add(item);
  206. }
  207. }
  208. }
  209. if (timeArr.size() > 0) {
  210. timeStr = timeArr.stream().collect(Collectors.joining(","));
  211. mtBook.setServiceTimes(timeStr);
  212. }
  213. }
  214. mtBook.setServiceTimes(timeStr);
  215. if (StringUtil.isNotEmpty(id)) {
  216. mtBook.setId(Integer.parseInt(id));
  217. bookService.updateBook(mtBook);
  218. } else {
  219. bookService.addBook(mtBook);
  220. }
  221. return getSuccessResult(true);
  222. }
  223. /**
  224. * 获取预约项目详情
  225. *
  226. * @param id 预约项目ID
  227. * @return
  228. */
  229. @ApiOperation(value = "获取预约项目详情")
  230. @RequestMapping(value = "/info/{id}", method = RequestMethod.GET)
  231. @CrossOrigin
  232. @PreAuthorize("@pms.hasPermission('book:index')")
  233. public ResponseObject info(HttpServletRequest request, @PathVariable("id") Integer id) throws BusinessCheckException, ParseException {
  234. String token = request.getHeader("Access-Token");
  235. AccountInfo accountInfo = TokenUtil.getAccountInfoByToken(token);
  236. if (accountInfo == null) {
  237. return getFailureResult(1001, "请先登录");
  238. }
  239. BookDto bookDto = bookService.getBookById(id);
  240. Map<String, Object> result = new HashMap<>();
  241. result.put("bookInfo", bookDto);
  242. return getSuccessResult(result);
  243. }
  244. }