TJsController.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. package com.ylx.web.controller.massage;
  2. import cn.binarywang.wx.miniapp.api.WxMaService;
  3. import cn.binarywang.wx.miniapp.bean.WxMaSubscribeMessage;
  4. import cn.hutool.core.collection.CollectionUtil;
  5. import cn.hutool.core.stream.CollectorUtil;
  6. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  7. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  8. import com.ylx.common.annotation.Log;
  9. import com.ylx.common.core.controller.BaseController;
  10. import com.ylx.common.core.domain.R;
  11. import com.ylx.common.core.domain.model.WxLoginUser;
  12. import com.ylx.common.enums.BusinessType;
  13. import com.ylx.common.exception.ServiceException;
  14. import com.ylx.common.utils.ListUtils;
  15. import com.ylx.massage.domain.Location;
  16. import com.ylx.massage.domain.TJs;
  17. import com.ylx.massage.domain.vo.TJsVo;
  18. import com.ylx.massage.enums.JsStatusEnum;
  19. import com.ylx.massage.service.TJsService;
  20. import com.ylx.massage.service.TWxUserService;
  21. import com.ylx.massage.utils.LocationUtil;
  22. import io.swagger.annotations.Api;
  23. import io.swagger.annotations.ApiOperation;
  24. import lombok.extern.slf4j.Slf4j;
  25. import me.chanjar.weixin.common.error.WxErrorException;
  26. import org.apache.commons.lang3.StringUtils;
  27. import org.springframework.beans.factory.annotation.Autowired;
  28. import org.springframework.beans.factory.annotation.Value;
  29. import org.springframework.web.bind.annotation.*;
  30. import javax.annotation.Resource;
  31. import java.math.BigDecimal;
  32. import java.math.RoundingMode;
  33. import java.util.*;
  34. import java.util.stream.Collectors;
  35. /**
  36. * 技师Controller
  37. *
  38. * @author ylx
  39. * @date 2024-03-22
  40. */
  41. @RestController
  42. @RequestMapping("api/js/v1")
  43. @Api(tags = {"技师管理"})
  44. @Slf4j
  45. public class TJsController extends BaseController {
  46. @Resource
  47. private TJsService jsService;
  48. @Resource
  49. private LocationUtil locationUtil;
  50. @Resource
  51. private TWxUserService wxUserService;
  52. @Autowired
  53. private WxMaService wxMaService;
  54. @Value("${wx.template_id_1}")
  55. private String msgTemplateId1;
  56. /**
  57. * 添加技师申请
  58. *
  59. * @param js
  60. * @return
  61. */
  62. @Log(title = "技师管理", businessType = BusinessType.INSERT)
  63. @RequestMapping(value = "wx/add", method = RequestMethod.POST)
  64. @ApiOperation("添加技师申请")
  65. public R add(@RequestBody TJs js) {
  66. try {
  67. WxLoginUser wxLoginUser = getWxLoginUser();
  68. js.setcOpenId(wxLoginUser.getCOpenid());
  69. return R.ok(jsService.addJs(js));
  70. } catch (ServiceException s) {
  71. log.error(s.toString());
  72. return R.fail(s.getMessage());
  73. }
  74. }
  75. /**
  76. * 更新技师信息
  77. *
  78. * @param js
  79. * @return
  80. */
  81. @Log(title = "技师管理", businessType = BusinessType.UPDATE)
  82. @RequestMapping(value = "wx/update", method = RequestMethod.POST)
  83. @ApiOperation("更新技师信息")
  84. public R wxUpdate(@RequestBody TJs js) {
  85. try {
  86. return R.ok(jsService.wxUpdateJs(js));
  87. } catch (ServiceException s) {
  88. log.error(s.toString());
  89. return R.fail(s.getMessage());
  90. } catch (Exception e) {
  91. log.error(e.toString());
  92. return R.fail("系统异常");
  93. }
  94. }
  95. @Log(title = "实时更新技师位置", businessType = BusinessType.UPDATE)
  96. @RequestMapping(value = "wx/updateLocation", method = RequestMethod.POST)
  97. @ApiOperation("实时更新技师位置")
  98. public R updateLocation(@RequestBody TJs js) {
  99. try {
  100. return R.ok(jsService.updateLocation(js));
  101. } catch (ServiceException s) {
  102. log.error(s.toString());
  103. return R.fail(s.getMessage());
  104. } catch (Exception e) {
  105. log.error(e.toString());
  106. return R.fail("系统异常");
  107. }
  108. }
  109. @Log(title = "免车费设置", businessType = BusinessType.UPDATE)
  110. @RequestMapping(value = "wx/freeFare", method = RequestMethod.POST)
  111. @ApiOperation("免车费设置")
  112. public R freeFare(@RequestBody TJs js) {
  113. try {
  114. return R.ok(jsService.updateById(js));
  115. } catch (ServiceException s) {
  116. log.error(s.toString());
  117. return R.fail(s.getMessage());
  118. } catch (Exception e) {
  119. log.error(e.toString());
  120. return R.fail("系统异常");
  121. }
  122. }
  123. @Log(title = "上岗/下岗", businessType = BusinessType.UPDATE)
  124. @RequestMapping(value = "wx/post", method = RequestMethod.POST)
  125. @ApiOperation("上岗/下岗")
  126. public R upPost(@RequestBody TJs js) {
  127. try {
  128. return R.ok(jsService.upPost(js));
  129. } catch (ServiceException s) {
  130. log.error(s.toString());
  131. return R.fail(s.getMessage());
  132. } catch (Exception e) {
  133. log.error(e.toString());
  134. return R.fail("系统异常");
  135. }
  136. }
  137. /**
  138. * 微信查询
  139. *
  140. * @param
  141. * @return
  142. */
  143. @RequestMapping(value = "wx/select", method = RequestMethod.GET)
  144. @Log(title = "微信查询技师列表", businessType = BusinessType.OTHER)
  145. @ApiOperation("微信查询")
  146. public R wxSelect(Page<TJs> page, TJsVo js) {
  147. try {
  148. if (js.getLatitude() != null && js.getLongitude() != null) {
  149. // 检查经纬度是否在合理范围内
  150. Location location = new Location();
  151. location.setLatitude(js.getLatitude().doubleValue());
  152. location.setLongitude(js.getLongitude().doubleValue());
  153. location.setLimit(100L); // 考虑将此值作为参数或配置项
  154. location.setRadius(1000.00);
  155. List<TJs> nearbyTechnicians = locationUtil.dis(location);
  156. if (nearbyTechnicians != null) {
  157. Map<String, BigDecimal> collect = nearbyTechnicians.stream().collect(Collectors.toMap(TJs::getcOpenId, TJs::getDistance));
  158. js.setIds(nearbyTechnicians.stream().map(TJs::getcOpenId).collect(Collectors.toList()));
  159. int size = (int) page.getSize();
  160. int pageNum = (int) page.getCurrent();
  161. page.setSize(nearbyTechnicians.size());
  162. page.setCurrent(1);
  163. Page<TJs> all = jsService.getAll(page, js);
  164. all.getRecords().forEach(item -> {
  165. item.setDistance(collect.get(item.getcOpenId()).setScale(2, RoundingMode.DOWN));
  166. });
  167. List<TJs> objects = (List<TJs>) ListUtils.subList(all.getRecords(), size, pageNum);
  168. page.setRecords(objects);
  169. page.setSize(size);
  170. return R.ok(page);
  171. }
  172. }
  173. return R.ok(jsService.getAll(page, js));
  174. } catch (Exception e) {
  175. // 异常处理
  176. e.printStackTrace();
  177. return R.fail("操作失败");
  178. }
  179. }
  180. /**
  181. * 分页查询数据
  182. */
  183. @RequestMapping(value = "/select", method = RequestMethod.GET)
  184. @ApiOperation("分页查询数据")
  185. public Page<TJs> selectSp(Page<TJs> page, TJs js) {
  186. LambdaQueryWrapper<TJs> mhCompanyLambdaQueryWrapper = new LambdaQueryWrapper<>();
  187. mhCompanyLambdaQueryWrapper.eq(null != js.getnTong(), TJs::getnTong, js.getnTong()).
  188. like(StringUtils.isNotBlank(js.getcName()), TJs::getcName, js.getcName()).
  189. like(StringUtils.isNotBlank(js.getcNickName()), TJs::getcNickName, js.getcNickName()).
  190. like(StringUtils.isNotBlank(js.getcPhone()), TJs::getcPhone, js.getcPhone()).
  191. eq(js.getnStatus() != null, TJs::getnStatus, js.getnStatus()).
  192. eq(js.getnSex() != null, TJs::getnSex, js.getnSex()).
  193. orderByDesc(TJs::getDtCreateTime);
  194. if (js.getPageType() == 1) {
  195. mhCompanyLambdaQueryWrapper.eq(TJs::getnTong, JsStatusEnum.JS_PASS.getCode());
  196. } else {
  197. mhCompanyLambdaQueryWrapper.in(TJs::getnTong, Arrays.asList(JsStatusEnum.JS_RETURN.getCode(), JsStatusEnum.JS_NOT_PASS.getCode()));
  198. }
  199. return jsService.page(page, mhCompanyLambdaQueryWrapper);
  200. }
  201. /**
  202. * 删除
  203. */
  204. @Log(title = "技师管理", businessType = BusinessType.DELETE)
  205. @RequestMapping(value = "/del", method = RequestMethod.POST)
  206. @ApiOperation("删除")
  207. public Boolean del(@RequestBody TJs js) {
  208. return jsService.removeById(js);
  209. }
  210. @ApiOperation("根据id查询技师")
  211. @RequestMapping(value = "/wx/getByid", method = RequestMethod.POST)
  212. public R getById(@RequestBody TJs js) {
  213. try {
  214. return R.ok(jsService.getByJsId(js.getId(), js.getcOpenId()));
  215. } catch (ServiceException s) {
  216. log.error(s.getMessage());
  217. return R.fail(s.getMessage());
  218. } catch (Exception e) {
  219. log.error(e.getMessage());
  220. return R.fail("系统异常");
  221. }
  222. }
  223. @ApiOperation("根据OpenId查询技师")
  224. @RequestMapping(value = "/wx/getByOpenId", method = RequestMethod.POST)
  225. public R getByOpenId(@RequestBody TJs js) {
  226. try {
  227. LambdaQueryWrapper<TJs> mhCompanyLambdaQueryWrapper = new LambdaQueryWrapper<>();
  228. mhCompanyLambdaQueryWrapper.eq(TJs::getcOpenId, js.getcOpenId());
  229. return R.ok(jsService.getOne(mhCompanyLambdaQueryWrapper));
  230. } catch (ServiceException s) {
  231. log.error(s.getMessage());
  232. return R.fail(s.getMessage());
  233. } catch (Exception e) {
  234. log.error(e.getMessage());
  235. return R.fail("系统异常");
  236. }
  237. }
  238. @ApiOperation("审核")
  239. @RequestMapping(value = "/auditing", method = RequestMethod.POST)
  240. public R auditing(@RequestBody TJs js) {
  241. try {
  242. return R.ok(jsService.auditing(js));
  243. } catch (ServiceException s) {
  244. log.error(s.getMessage());
  245. return R.fail(s.getMessage());
  246. } catch (Exception e) {
  247. log.error(e.getMessage());
  248. return R.fail("系统异常");
  249. }
  250. }
  251. @ApiOperation("退回")
  252. @RequestMapping(value = "/repulse", method = RequestMethod.POST)
  253. public R repulse(@RequestBody TJs js) {
  254. try {
  255. TJs js1 = jsService.getById(js.getId());
  256. LambdaQueryWrapper<TJs> mhCompanyLambdaQueryWrapper = new LambdaQueryWrapper<>();
  257. mhCompanyLambdaQueryWrapper.eq(TJs::getId, js.getId()).eq(TJs::getnTong, JsStatusEnum.JS_NOT_PASS.getCode());
  258. js.setnTong(JsStatusEnum.JS_RETURN.getCode());
  259. boolean update = jsService.update(js, mhCompanyLambdaQueryWrapper);
  260. js.setcOpenId(js1.getcOpenId());
  261. jsService.jsNotification(js);
  262. return R.ok(update);
  263. } catch (ServiceException s) {
  264. log.error(s.getMessage());
  265. return R.fail(s.getMessage());
  266. } catch (Exception e) {
  267. log.error(e.getMessage());
  268. return R.fail("系统异常");
  269. }
  270. }
  271. @ApiOperation("拉黑")
  272. @RequestMapping(value = "/block", method = RequestMethod.POST)
  273. public R block(@RequestBody TJs js) {
  274. try {
  275. return R.ok(jsService.block(js));
  276. } catch (ServiceException s) {
  277. log.error(s.getMessage());
  278. return R.fail(s.getMessage());
  279. } catch (Exception e) {
  280. log.error(e.getMessage());
  281. return R.fail("系统异常");
  282. }
  283. }
  284. @ApiOperation("小程序通知")
  285. @RequestMapping(value = "/tongzhi", method = RequestMethod.GET)
  286. public boolean sendSubscribeMessage(String openId) {
  287. String page="pages/index/index"; // 应该是消息点击后跳转页面
  288. WxMaSubscribeMessage message = new WxMaSubscribeMessage();
  289. message.setTemplateId(msgTemplateId1);
  290. message.setToUser(openId);
  291. message.setPage(page);
  292. List<WxMaSubscribeMessage.MsgData> subscribeDataList = new ArrayList<>();
  293. WxMaSubscribeMessage.MsgData subscribeData = new WxMaSubscribeMessage.MsgData();
  294. subscribeData.setName("key1"); // 你在小程序自定义的key(比如“商家名称”)
  295. subscribeData.setValue("夜来香"); //key对应的内容
  296. WxMaSubscribeMessage.MsgData msgData = new WxMaSubscribeMessage.MsgData();
  297. msgData.setName("key2");
  298. msgData.setValue("");
  299. //添加你配置要且要展示的内容
  300. subscribeDataList.add(subscribeData);
  301. message.setData(subscribeDataList);
  302. try {
  303. wxMaService.getMsgService().sendSubscribeMsg(message);
  304. return true;
  305. } catch (WxErrorException e) {
  306. log.error("发送小程序订阅消息失败,errCode:{},errMsg:{}", e.getError().getErrorCode(), e.getError().getErrorMsg());
  307. }
  308. return false;
  309. }
  310. @ApiOperation("公众号新订单处理通知")
  311. @RequestMapping(value = "/notification", method = RequestMethod.GET)
  312. public boolean newOrderNotification(String openId) {
  313. jsService.newOrderNotification(openId);
  314. return true;
  315. }
  316. }