TJsController.java 13 KB

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