TJsServiceImpl.java 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. package com.ylx.massage.service.impl;
  2. import cn.hutool.core.collection.CollectionUtil;
  3. import cn.hutool.json.JSONException;
  4. import cn.hutool.json.JSONUtil;
  5. import com.alibaba.fastjson2.JSONObject;
  6. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  7. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  8. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  9. import com.ylx.common.config.WechatAccountConfig;
  10. import com.ylx.common.constant.MassageConstants;
  11. import com.ylx.common.exception.ServiceException;
  12. import com.ylx.common.utils.StringUtils;
  13. import com.ylx.massage.domain.*;
  14. import com.ylx.massage.domain.vo.TJsVo;
  15. import com.ylx.massage.enums.JsStatusEnum;
  16. import com.ylx.massage.mapper.TJsMapper;
  17. import com.ylx.massage.service.*;
  18. import com.ylx.massage.utils.DateTimeUtils;
  19. import com.ylx.massage.utils.LocationUtil;
  20. import com.ylx.massage.utils.WeChatUtil;
  21. import lombok.extern.slf4j.Slf4j;
  22. import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
  23. import org.springframework.stereotype.Service;
  24. import org.springframework.transaction.annotation.Transactional;
  25. import javax.annotation.Resource;
  26. import java.time.LocalDateTime;
  27. import java.util.*;
  28. import java.util.stream.Collectors;
  29. /**
  30. * 技师表 服务实现类
  31. */
  32. @Service
  33. @Slf4j
  34. public class TJsServiceImpl extends ServiceImpl<TJsMapper, TJs> implements TJsService {
  35. @Resource
  36. private TJsMapper mapper;
  37. @Resource
  38. private WechatAccountConfig wxPayProperties;
  39. @Resource
  40. private LocationUtil locationUtil;
  41. @Resource
  42. private TCollectService collectService;
  43. @Resource
  44. private TXiangmuService xiangmuService;
  45. @Resource
  46. private TCommentService commentService;
  47. @Resource
  48. private TWxUserService wxUserService;
  49. @Resource
  50. private WeChatUtil weChatUtil;
  51. @Resource
  52. private TOrderService orderService;
  53. @Resource
  54. private TSignService signService;
  55. @Resource(name = "commonAsyncExecutor")
  56. private ThreadPoolTaskExecutor threadPoolTaskExecutor;
  57. @Override
  58. public Page<TJs> getAll(Page<TJs> page, TJsVo param) {
  59. return mapper.getAll(page, param);
  60. }
  61. @Override
  62. public boolean addJs(TJs js) {
  63. LambdaQueryWrapper<TJs> queryWrapper = new LambdaQueryWrapper<>();
  64. queryWrapper.eq(TJs::getcOpenId, js.getcOpenId());
  65. TJs cjs = this.getOne(queryWrapper);
  66. if (cjs != null) {
  67. throw new ServiceException("请勿重复申请");
  68. }
  69. // cacheAddress(js);
  70. initialization(js);
  71. return this.save(js);
  72. }
  73. private void cacheAddress(TJs js) {
  74. if (StringUtils.isBlank(js.getcPhone())) {
  75. throw new ServiceException("手机号不能为空");
  76. }
  77. if (StringUtils.isBlank(js.getName())) {
  78. throw new ServiceException("地址不能为空");
  79. }
  80. if (StringUtils.isNotBlank(js.getId())) {
  81. TJs oldJs = this.getById(js.getId());
  82. if (oldJs.getnTong().equals(JsStatusEnum.JS_RETURN.getCode())) {
  83. js.setnTong(JsStatusEnum.JS_NOT_PASS.getCode());
  84. }
  85. }
  86. if (StringUtils.isNotBlank(js.getName())) {
  87. this.updateLocation(js);
  88. }
  89. // log.info("js:{}", js);
  90. // JSONObject jsonObject = new JSONObject(js.getcAddress());
  91. // Object latitude = jsonObject.get("latitude");
  92. // Object longitude = jsonObject.get("longitude");
  93. // locationUtil.geoAdd(LocationUtil.GEO_KEY,js.getcOpenId(), Double.parseDouble(js.getLongitude().toString()), Double.parseDouble(js.getLatitude().toString()));
  94. }
  95. @Override
  96. public boolean wxUpdateJs(TJs js) {
  97. cacheAddress(js);
  98. return this.updateById(js);
  99. }
  100. @Override
  101. @Transactional(rollbackFor = Exception.class)
  102. public boolean auditing(TJs js) {
  103. TJs js1 = this.getById(js.getId());
  104. LambdaQueryWrapper<TJs> jsLambdaQueryWrapper = new LambdaQueryWrapper<>();
  105. jsLambdaQueryWrapper.eq(TJs::getId, js.getId()).eq(TJs::getnTong, JsStatusEnum.JS_NOT_PASS.getCode());
  106. js.setnTong(JsStatusEnum.JS_PASS.getCode());
  107. js.setnB2(1);
  108. js.setcPhone(js1.getcPhone());
  109. js.setcTime(new Date());
  110. LambdaQueryWrapper<TWxUser> wxLambdaQueryWrapper = new LambdaQueryWrapper<>();
  111. wxLambdaQueryWrapper.eq(TWxUser::getcOpenid, js1.getcOpenId());
  112. TWxUser tWxUser = new TWxUser();
  113. //角色状态修改
  114. tWxUser.setRole(1);
  115. wxUserService.update(tWxUser, wxLambdaQueryWrapper);
  116. //修改审核状态
  117. boolean update = this.update(js, jsLambdaQueryWrapper);
  118. //修改新技师标识
  119. js.setcOpenId(js1.getcOpenId());
  120. // 发送审核结果通知
  121. //this.jsNotification(js);
  122. return update;
  123. }
  124. @Override
  125. public void jsNotification(TJs js) {
  126. try {
  127. cn.hutool.json.JSONObject param = JSONUtil.createObj();
  128. //是否审核通过
  129. if (JsStatusEnum.JS_PASS.getCode().equals(js.getnTong())) {
  130. param.set("const1", JSONUtil.createObj().set("value", "审核通过可以接单"));
  131. } else {
  132. param.set("const1", JSONUtil.createObj().set("value", "审核驳回请重新提交"));
  133. }
  134. //审核时间
  135. param.set("time2", JSONUtil.createObj().set("value", DateTimeUtils.formatDate(new Date(), DateTimeUtils.DATE_NUMBER_YEAR_MONTH_FORMAT)));
  136. //账号
  137. param.set("character_string3", JSONUtil.createObj().set("value", js.getcPhone()));
  138. //
  139. weChatUtil.notification(js.getcOpenId(), wxPayProperties.getTemplateId2(), param);
  140. } catch (JSONException e) {
  141. e.printStackTrace();
  142. throw new RuntimeException(e);
  143. }
  144. }
  145. @Override
  146. public boolean updateLocation(TJs js) {
  147. if (StringUtils.isBlank(js.getId())) {
  148. throw new ServiceException("ID不能为空");
  149. }
  150. if (StringUtils.isBlank(js.getName())) {
  151. throw new ServiceException("地址不能为空");
  152. }
  153. // JSONObject jsonObject = new JSONObject(js.getcAddress());
  154. // Object latitude = jsonObject.get("latitude");
  155. // Object longitude = jsonObject.get("longitude");
  156. locationUtil.geoAdd(LocationUtil.GEO_KEY, js.getcPhone(), Double.parseDouble(js.getLongitude().toString()), Double.parseDouble(js.getLatitude().toString()));
  157. return this.updateById(js);
  158. }
  159. @Override
  160. public void newOrderNotification(String openId) {
  161. // 订单号 23010000010101
  162. // 用户电话 15055556666
  163. // 用户名称 李四
  164. // 时间 2022年11月11日22:22
  165. // 服务地址 湖南长沙岳麓
  166. /*cn.hutool.json.JSONObject param = JSONUtil.createObj();
  167. //订单号
  168. param.set("character_string9", JSONUtil.createObj().set("value", "23010000010101"));
  169. //点话
  170. param.set("phone_number14", JSONUtil.createObj().set("value", "18360233903"));
  171. param.set("thing18", JSONUtil.createObj().set("value", "李四"));
  172. param.set("time6", JSONUtil.createObj().set("value", "2022年11月11日 22:22"));
  173. param.set("thing27", JSONUtil.createObj().set("value", "湖南长沙岳麓"));*/
  174. cn.hutool.json.JSONObject param = JSONUtil.createObj();
  175. /**
  176. * 订单未接单提醒
  177. */
  178. //设置订单号
  179. param.set("character_string9", JSONUtil.createObj().set("value", "123456"));
  180. //设置项目
  181. param.set("thing10", JSONUtil.createObj().set("value", "按摩"));
  182. //设置预约时间
  183. param.set("time6", JSONUtil.createObj().set("value", "2022年11月11日 22:22"));
  184. //设置用户电话
  185. param.set("phone_number3", JSONUtil.createObj().set("value", "18360233903"));
  186. //设置下单地址
  187. param.set("thing13", JSONUtil.createObj().set("value", "信达国际"));
  188. //weChatUtil.notification(openId, wxPayProperties.get(), param);
  189. }
  190. @Override
  191. public boolean upPost(TJs js) {
  192. if (StringUtils.isBlank(js.getId())) {
  193. throw new ServiceException("技师ID不能为空");
  194. }
  195. if (js.getnStatus2() == null) {
  196. throw new ServiceException("岗位状态不能为空");
  197. }
  198. TJs js2 = this.getById(js.getId());
  199. TSign tSign = new TSign();
  200. //上岗
  201. if (Objects.equals(js.getnStatus2(), JsStatusEnum.POST_ON_DUTY.getCode())) {
  202. //岗位状态为1时,设置nStatus为1
  203. js.setnStatus(JsStatusEnum.JS_SERVICEABLE.getCode());
  204. //记录签到信息
  205. tSign.setSingTime(new Date());
  206. tSign.setOpenId(js2.getcOpenId());
  207. tSign.setJsId(js2.getId());
  208. tSign.setName(js2.getcName());
  209. tSign.setDeptId(js2.getDeptId());
  210. tSign.setDeptName(js2.getCity());
  211. signService.save(tSign);
  212. }
  213. if (Objects.equals(js.getnStatus2(), JsStatusEnum.POST_NOT_ON_DUTY.getCode())) {
  214. //判断该技师是否有服务中的订单
  215. TJs js1 = this.getById(js.getId());
  216. if (js1.getnStatus().equals(JsStatusEnum.JS_SERVICE.getCode())) {
  217. throw new ServiceException("您有服务中的订单,不能下岗");
  218. }
  219. js.setnStatus(JsStatusEnum.JS_NO_SERVICE.getCode());
  220. LambdaQueryWrapper<TSign> signLambdaQueryWrapper = new LambdaQueryWrapper<>();
  221. signLambdaQueryWrapper.eq(TSign::getJsId, js.getId()).isNull(TSign::getLayoffTime).orderByAsc(TSign::getSingTime).last("LIMIT 1");
  222. TSign singn = signService.getOne(signLambdaQueryWrapper);
  223. if (null != singn) {
  224. singn.setLayoffTime(new Date());
  225. Long time = getaTime(singn);
  226. singn.setOnlineTime(time.intValue());
  227. signService.updateById(singn);
  228. }
  229. // else {
  230. // tSign.setSingTime(DateTimeUtils.getStartDate(new Date()));
  231. // tSign.setLayoffTime(new Date());
  232. // tSign.setOpenId(js2.getcOpenId());
  233. // tSign.setJsId(js2.getId());
  234. // tSign.setName(js2.getcName());
  235. // Long time = getaTime(tSign);
  236. // tSign.setDeptId(js2.getDeptId());
  237. // tSign.setDeptName(js2.getCity());
  238. // tSign.setOnlineTime(time.intValue());
  239. // signService.save(tSign);
  240. // }
  241. }
  242. return this.updateById(js);
  243. }
  244. private Long getaTime(TSign tSign) {
  245. Long layoff = DateTimeUtils.dateToStamp(tSign.getLayoffTime());
  246. Long sing = DateTimeUtils.dateToStamp(tSign.getSingTime());
  247. Long time = layoff - sing;
  248. time = time / 1000;
  249. time = time / 60;
  250. return time;
  251. }
  252. @Override
  253. public boolean block(TJs js) {
  254. if (StringUtils.isBlank(js.getId())) {
  255. throw new ServiceException("技师ID不能为空");
  256. }
  257. if (js.getnTong() == null) {
  258. throw new ServiceException("拉黑状态不能为空");
  259. }
  260. //恢复
  261. if (Objects.equals(js.getnTong(), JsStatusEnum.JS_NOT_PASS.getCode())) {
  262. //岗位状态为1时,设置nStatus为1
  263. js.setnStatus(JsStatusEnum.JS_NO_SERVICE.getCode());
  264. }
  265. if (Objects.equals(js.getnTong(), JsStatusEnum.BLOCKED.getCode())) {
  266. //判断该技师是否有服务中的订单
  267. TJs js1 = this.getById(js.getId());
  268. if (js1.getnStatus().equals(JsStatusEnum.JS_SERVICE.getCode())) {
  269. throw new ServiceException("有服务中的订单,暂时不能拉黑");
  270. }
  271. js.setnStatus(JsStatusEnum.JS_NO_SERVICE.getCode());
  272. js.setnStatus2(JsStatusEnum.POST_NOT_ON_DUTY.getCode());
  273. }
  274. return this.updateById(js);
  275. }
  276. @Override
  277. public boolean pcAddJs(TJs js) {
  278. /*if (js.getCity() == null) {
  279. throw new ServiceException("所在城市不能为空");
  280. }*/
  281. //判断虚拟订单数量不能为空
  282. if (js.getVirtualOrderNumber() == null) {
  283. throw new ServiceException("虚拟订单数量不能为空");
  284. }
  285. // 评分默认最高
  286. js.setnStar(MassageConstants.INTEGER_FIVE);
  287. // 已服务数量 0
  288. js.setnNum(MassageConstants.INTEGER_ZERO);
  289. // 佣金比例 10
  290. js.setnBili(MassageConstants.INTEGER_TEN);
  291. // 服务状态
  292. js.setnStatus(JsStatusEnum.JS_NO_SERVICE.getCode());
  293. // 上岗状态
  294. js.setnStatus2(JsStatusEnum.POST_NOT_ON_DUTY.getCode());
  295. // 审核状态
  296. js.setnTong(JsStatusEnum.JS_PASS.getCode());
  297. js.setnB1(MassageConstants.INTEGER_ZERO);
  298. js.setnB2(MassageConstants.INTEGER_ZERO);
  299. js.setnB3(MassageConstants.INTEGER_ZERO);
  300. js.setDtCreateTime(LocalDateTime.now());
  301. this.save(js);
  302. /*String token = weChatUtil.getToken();
  303. //获取的二维码ticket
  304. Map<?, ?> jsTicket = weChatUtil.getJsTicket(token, js.getId());
  305. js.setTicket(jsTicket.get("ticket").toString());*/
  306. return this.updateById(js);
  307. }
  308. @Override
  309. public TJs getByJsId(String jsId, String openId) {
  310. if (jsId == null || jsId.trim().isEmpty()) {
  311. // 处理空或空白的jsId
  312. throw new ServiceException("Id为空");
  313. }
  314. TJs js = this.getById(jsId);
  315. if (js == null) {
  316. // 处理getById返回null的情况
  317. return null; // 或者返回一个新的TJs实例,具体看业务需求
  318. }
  319. if (StringUtils.isBlank(js.getcBhList())) {
  320. // 处理js.getcBhList()返回null或空列表的情况
  321. js.setProjects(new ArrayList<>()); // 设置空列表,避免后续调用空指针
  322. return js;
  323. }
  324. //查询项目
  325. List<String> projectIds = Arrays.stream(js.getcBhList().split(",")).collect(Collectors.toList());
  326. LambdaQueryWrapper<TXiangmu> xiangmuLambdaQueryWrapper = new LambdaQueryWrapper<>();
  327. xiangmuLambdaQueryWrapper.in(TXiangmu::getcId, projectIds).orderByAsc(TXiangmu::getdPrice);
  328. List<TXiangmu> projects = xiangmuService.list(xiangmuLambdaQueryWrapper);
  329. js.setProjects(projects);
  330. //查询评论
  331. LambdaQueryWrapper<TComment> commentLambdaQueryWrapper = new LambdaQueryWrapper<>();
  332. commentLambdaQueryWrapper.eq(TComment::getcJsid, jsId);
  333. List<TComment> list = commentService.list(commentLambdaQueryWrapper);
  334. if (CollectionUtil.isNotEmpty(list)) {
  335. js.setComments(list);
  336. }
  337. //是否收藏
  338. LambdaQueryWrapper<TCollect> objectLambdaQueryWrapper = new LambdaQueryWrapper<>();
  339. objectLambdaQueryWrapper.eq(TCollect::getcJsId, jsId).eq(TCollect::getcWxOpenId, openId);
  340. TCollect collect = collectService.getOne(objectLambdaQueryWrapper);
  341. if (collect != null) {
  342. js.setIsCollection(true);
  343. } else {
  344. js.setIsCollection(false);
  345. }
  346. return js;
  347. }
  348. private static void initialization(TJs js) {
  349. // 评分默认最高 5
  350. js.setnStar(MassageConstants.INTEGER_FIVE);
  351. // 已服务数量 0
  352. js.setnNum(MassageConstants.INTEGER_ZERO);
  353. // 佣金比例 10
  354. js.setnBili(MassageConstants.INTEGER_TEN);
  355. // 服务状态
  356. js.setnStatus(JsStatusEnum.JS_NO_SERVICE.getCode());
  357. // 上岗状态
  358. js.setnStatus2(JsStatusEnum.POST_NOT_ON_DUTY.getCode());
  359. // 审核状态
  360. js.setnTong(JsStatusEnum.JS_NOT_PASS.getCode());
  361. js.setnB1(MassageConstants.INTEGER_ZERO);
  362. js.setnB2(MassageConstants.INTEGER_ZERO);
  363. js.setnB3(MassageConstants.INTEGER_ZERO);
  364. js.setDtCreateTime(LocalDateTime.now());
  365. }
  366. }