TJsServiceImpl.java 14 KB

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