BookServiceImpl.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. package com.fuint.common.service.impl;
  2. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  3. import com.baomidou.mybatisplus.core.toolkit.Wrappers;
  4. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  5. import com.fuint.common.dto.BookDto;
  6. import com.fuint.common.dto.DayDto;
  7. import com.fuint.common.dto.TimeDto;
  8. import com.fuint.common.service.BookService;
  9. import com.fuint.common.service.StoreService;
  10. import com.fuint.common.util.DateUtil;
  11. import com.fuint.framework.annoation.OperationServiceLog;
  12. import com.fuint.framework.exception.BusinessCheckException;
  13. import com.fuint.framework.pagination.PaginationRequest;
  14. import com.fuint.framework.pagination.PaginationResponse;
  15. import com.fuint.repository.mapper.MtBookMapper;
  16. import com.fuint.repository.model.MtBanner;
  17. import com.fuint.common.service.SettingService;
  18. import com.fuint.common.enums.StatusEnum;
  19. import com.fuint.repository.model.MtBook;
  20. import com.fuint.repository.model.MtStore;
  21. import com.fuint.utils.StringUtil;
  22. import com.github.pagehelper.PageHelper;
  23. import lombok.AllArgsConstructor;
  24. import org.apache.commons.lang.StringUtils;
  25. import org.slf4j.Logger;
  26. import org.slf4j.LoggerFactory;
  27. import com.github.pagehelper.Page;
  28. import org.springframework.beans.BeanUtils;
  29. import org.springframework.data.domain.PageImpl;
  30. import org.springframework.data.domain.PageRequest;
  31. import org.springframework.stereotype.Service;
  32. import org.springframework.transaction.annotation.Transactional;
  33. import java.text.ParseException;
  34. import java.text.SimpleDateFormat;
  35. import java.util.*;
  36. /**
  37. * 预约服务接口
  38. *
  39. * Created by FSQ
  40. * CopyRight https://www.fuint.cn
  41. */
  42. @Service
  43. @AllArgsConstructor
  44. public class BookServiceImpl extends ServiceImpl<MtBookMapper, MtBook> implements BookService {
  45. private static final Logger logger = LoggerFactory.getLogger(BookServiceImpl.class);
  46. private MtBookMapper mtBookMapper;
  47. /**
  48. * 系统设置服务接口
  49. * */
  50. private SettingService settingService;
  51. /**
  52. * 店铺接口
  53. */
  54. private StoreService storeService;
  55. /**
  56. * 分页查询预约列表
  57. *
  58. * @param paginationRequest
  59. * @return
  60. */
  61. @Override
  62. public PaginationResponse<BookDto> queryBookListByPagination(PaginationRequest paginationRequest) {
  63. Page<MtBanner> pageHelper = PageHelper.startPage(paginationRequest.getCurrentPage(), paginationRequest.getPageSize());
  64. LambdaQueryWrapper<MtBook> lambdaQueryWrapper = Wrappers.lambdaQuery();
  65. lambdaQueryWrapper.ne(MtBook::getStatus, StatusEnum.DISABLE.getKey());
  66. String name = paginationRequest.getSearchParams().get("name") == null ? "" : paginationRequest.getSearchParams().get("name").toString();
  67. if (StringUtils.isNotBlank(name)) {
  68. lambdaQueryWrapper.like(MtBook::getName, name);
  69. }
  70. String cateId = paginationRequest.getSearchParams().get("cateId") == null ? "" : paginationRequest.getSearchParams().get("cateId").toString();
  71. if (StringUtils.isNotBlank(cateId)) {
  72. lambdaQueryWrapper.like(MtBook::getCateId, cateId);
  73. }
  74. String status = paginationRequest.getSearchParams().get("status") == null ? "" : paginationRequest.getSearchParams().get("status").toString();
  75. if (StringUtils.isNotBlank(status)) {
  76. lambdaQueryWrapper.eq(MtBook::getStatus, status);
  77. }
  78. String merchantId = paginationRequest.getSearchParams().get("merchantId") == null ? "" : paginationRequest.getSearchParams().get("merchantId").toString();
  79. if (StringUtils.isNotBlank(merchantId)) {
  80. lambdaQueryWrapper.eq(MtBook::getMerchantId, merchantId);
  81. }
  82. String storeId = paginationRequest.getSearchParams().get("storeId") == null ? "" : paginationRequest.getSearchParams().get("storeId").toString();
  83. if (StringUtils.isNotBlank(storeId)) {
  84. lambdaQueryWrapper.eq(MtBook::getStoreId, storeId);
  85. }
  86. lambdaQueryWrapper.orderByAsc(MtBook::getSort);
  87. List<MtBook> bookList = mtBookMapper.selectList(lambdaQueryWrapper);
  88. List<BookDto> dataList = new ArrayList<>();
  89. String baseImage = settingService.getUploadBasePath();
  90. if (bookList != null && bookList.size() > 0) {
  91. for (MtBook mtBook : bookList) {
  92. BookDto bookDto = new BookDto();
  93. BeanUtils.copyProperties(mtBook, bookDto);
  94. bookDto.setLogo(baseImage + mtBook.getLogo());
  95. dataList.add(bookDto);
  96. }
  97. }
  98. PageRequest pageRequest = PageRequest.of(paginationRequest.getCurrentPage(), paginationRequest.getPageSize());
  99. PageImpl pageImpl = new PageImpl(dataList, pageRequest, pageHelper.getTotal());
  100. PaginationResponse<BookDto> paginationResponse = new PaginationResponse(pageImpl, BookDto.class);
  101. paginationResponse.setTotalPages(pageHelper.getPages());
  102. paginationResponse.setTotalElements(pageHelper.getTotal());
  103. paginationResponse.setContent(dataList);
  104. return paginationResponse;
  105. }
  106. /**
  107. * 添加预约项目
  108. *
  109. * @param mtBook 预约信息
  110. * @return
  111. */
  112. @Override
  113. @OperationServiceLog(description = "添加预约项目")
  114. public MtBook addBook(MtBook mtBook) throws BusinessCheckException {
  115. Integer storeId = mtBook.getStoreId() == null ? 0 : mtBook.getStoreId();
  116. if (mtBook.getMerchantId() == null || mtBook.getMerchantId() <= 0) {
  117. MtStore mtStore = storeService.queryStoreById(storeId);
  118. if (mtStore != null) {
  119. mtBook.setMerchantId(mtStore.getMerchantId());
  120. }
  121. }
  122. if (mtBook.getMerchantId() == null || mtBook.getMerchantId() <= 0) {
  123. throw new BusinessCheckException("新增预约失败:所属商户不能为空!");
  124. }
  125. mtBook.setStoreId(storeId);
  126. mtBook.setStatus(StatusEnum.ENABLED.getKey());
  127. mtBook.setUpdateTime(new Date());
  128. mtBook.setCreateTime(new Date());
  129. Integer id = mtBookMapper.insert(mtBook);
  130. if (id > 0) {
  131. return mtBook;
  132. } else {
  133. logger.error("新增预约失败.");
  134. throw new BusinessCheckException("抱歉,新增预约失败!");
  135. }
  136. }
  137. /**
  138. * 根据ID获取预约项目信息
  139. *
  140. * @param id 预约项目ID
  141. * @return
  142. */
  143. @Override
  144. public BookDto getBookById(Integer id) throws ParseException {
  145. BookDto bookDto = new BookDto();
  146. MtBook mtBook = mtBookMapper.selectById(id);
  147. if (mtBook == null) {
  148. return null;
  149. }
  150. BeanUtils.copyProperties(mtBook, bookDto);
  151. List<DayDto> dateList = new ArrayList<>();
  152. String serviceDates = mtBook.getServiceDates();
  153. if (StringUtil.isNotEmpty(serviceDates)) {
  154. List<String> dates = Arrays.asList(serviceDates.split(",").clone());
  155. if (dates.size() > 0) {
  156. for (String date : dates) {
  157. Date currentDate = DateUtil.parseDate(date);
  158. SimpleDateFormat format = new SimpleDateFormat("EEEE");
  159. String week = format.format(currentDate);
  160. DayDto day = new DayDto();
  161. day.setWeek(week);
  162. day.setDate(DateUtil.formatDate(currentDate, "MM-dd"));
  163. day.setEnable(true);
  164. dateList.add(day);
  165. }
  166. }
  167. }
  168. bookDto.setDateList(dateList);
  169. List<TimeDto> timeList = new ArrayList<>();
  170. String serviceTimes = mtBook.getServiceTimes();
  171. if (StringUtil.isNotEmpty(serviceTimes)) {
  172. List<String> times = Arrays.asList(serviceTimes.split(",").clone());
  173. if (times.size() > 0) {
  174. for (String time : times) {
  175. TimeDto timeDto = new TimeDto();
  176. timeDto.setTime(time);
  177. timeDto.setEnable(true);
  178. timeList.add(timeDto);
  179. }
  180. }
  181. }
  182. bookDto.setTimeList(timeList);
  183. return bookDto;
  184. }
  185. /**
  186. * 修改预约项目
  187. *
  188. * @param mtBook
  189. * @throws BusinessCheckException
  190. * @return
  191. */
  192. @Override
  193. @Transactional(rollbackFor = Exception.class)
  194. @OperationServiceLog(description = "修改预约项目")
  195. public MtBook updateBook(MtBook mtBook) throws BusinessCheckException {
  196. MtBook book = mtBookMapper.selectById(mtBook.getId());
  197. if (book == null) {
  198. throw new BusinessCheckException("该预约项目状态异常");
  199. }
  200. book.setId(book.getId());
  201. if (mtBook.getLogo() != null) {
  202. book.setLogo(mtBook.getLogo());
  203. }
  204. if (mtBook.getCateId() != null) {
  205. book.setCateId(mtBook.getCateId());
  206. }
  207. if (book.getName() != null) {
  208. book.setName(mtBook.getName());
  209. }
  210. if (mtBook.getStoreId() != null) {
  211. book.setStoreId(mtBook.getStoreId());
  212. }
  213. if (mtBook.getDescription() != null) {
  214. book.setDescription(mtBook.getDescription());
  215. }
  216. if (mtBook.getOperator() != null) {
  217. book.setOperator(mtBook.getOperator());
  218. }
  219. if (mtBook.getStatus() != null) {
  220. book.setStatus(mtBook.getStatus());
  221. }
  222. if (mtBook.getGoodsId() != null) {
  223. book.setGoodsId(mtBook.getGoodsId());
  224. }
  225. if (mtBook.getSort() != null) {
  226. book.setSort(mtBook.getSort());
  227. }
  228. if (mtBook.getServiceDates() != null) {
  229. book.setServiceDates(mtBook.getServiceDates());
  230. }
  231. if (mtBook.getServiceTimes() != null) {
  232. book.setServiceTimes(mtBook.getServiceTimes());
  233. }
  234. if (mtBook.getServiceStaffIds() != null) {
  235. book.setServiceStaffIds(mtBook.getServiceStaffIds());
  236. }
  237. book.setUpdateTime(new Date());
  238. mtBookMapper.updateById(book);
  239. return book;
  240. }
  241. /**
  242. * 根据条件搜索预约项目
  243. *
  244. * @param params 查询参数
  245. * @throws BusinessCheckException
  246. * @return
  247. * */
  248. @Override
  249. public List<MtBook> queryBookListByParams(Map<String, Object> params) {
  250. String status = params.get("status") == null ? StatusEnum.ENABLED.getKey(): params.get("status").toString();
  251. String storeId = params.get("storeId") == null ? "" : params.get("storeId").toString();
  252. String merchantId = params.get("merchantId") == null ? "" : params.get("merchantId").toString();
  253. String name = params.get("name") == null ? "" : params.get("name").toString();
  254. LambdaQueryWrapper<MtBook> lambdaQueryWrapper = Wrappers.lambdaQuery();
  255. if (StringUtils.isNotBlank(name)) {
  256. lambdaQueryWrapper.like(MtBook::getName, name);
  257. }
  258. if (StringUtils.isNotBlank(status)) {
  259. lambdaQueryWrapper.eq(MtBook::getStatus, status);
  260. }
  261. if (StringUtils.isNotBlank(merchantId)) {
  262. lambdaQueryWrapper.eq(MtBook::getMerchantId, merchantId);
  263. }
  264. if (StringUtils.isNotBlank(storeId)) {
  265. lambdaQueryWrapper.eq(MtBook::getStoreId, storeId);
  266. }
  267. lambdaQueryWrapper.orderByAsc(MtBook::getSort);
  268. List<MtBook> dataList = mtBookMapper.selectList(lambdaQueryWrapper);
  269. String baseImage = settingService.getUploadBasePath();
  270. if (dataList.size() > 0) {
  271. for (MtBook book : dataList) {
  272. book.setLogo(baseImage + book.getLogo());
  273. }
  274. }
  275. return dataList;
  276. }
  277. }