TJsController.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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.ArrayList;
  32. import java.util.List;
  33. import java.util.Map;
  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. } catch (Exception e) {
  74. log.error(e.toString());
  75. return R.fail("系统异常");
  76. }
  77. }
  78. /**
  79. * 更新技师信息
  80. *
  81. * @param js
  82. * @return
  83. */
  84. @Log(title = "技师管理", businessType = BusinessType.UPDATE)
  85. @RequestMapping(value = "wx/update", method = RequestMethod.POST)
  86. @ApiOperation("更新技师信息")
  87. public R wxUpdate(@RequestBody TJs js) {
  88. try {
  89. return R.ok(jsService.wxUpdateJs(js));
  90. } catch (ServiceException s) {
  91. log.error(s.toString());
  92. return R.fail(s.getMessage());
  93. } catch (Exception e) {
  94. log.error(e.toString());
  95. return R.fail("系统异常");
  96. }
  97. }
  98. @Log(title = "实时更新技师位置", businessType = BusinessType.UPDATE)
  99. @RequestMapping(value = "wx/updateLocation", method = RequestMethod.POST)
  100. @ApiOperation("实时更新技师位置")
  101. public R updateLocation(@RequestBody TJs js) {
  102. try {
  103. return R.ok(jsService.updateLocation(js));
  104. } catch (ServiceException s) {
  105. log.error(s.toString());
  106. return R.fail(s.getMessage());
  107. } catch (Exception e) {
  108. log.error(e.toString());
  109. return R.fail("系统异常");
  110. }
  111. }
  112. /**
  113. * 微信查询
  114. *
  115. * @param
  116. * @return
  117. */
  118. @RequestMapping(value = "wx/select", method = RequestMethod.GET)
  119. @ApiOperation("微信查询")
  120. public R wxSelect(Page<TJs> page, TJsVo js) {
  121. try {
  122. if (js.getLatitude() != null && js.getLongitude() != null) {
  123. // 检查经纬度是否在合理范围内
  124. Location location = new Location();
  125. location.setLatitude(js.getLatitude().doubleValue());
  126. location.setLongitude(js.getLongitude().doubleValue());
  127. location.setLimit(100L); // 考虑将此值作为参数或配置项
  128. location.setRadius(1000.00);
  129. List<TJs> nearbyTechnicians = locationUtil.dis(location);
  130. if (nearbyTechnicians != null) {
  131. Map<String, BigDecimal> collect = nearbyTechnicians.stream().collect(Collectors.toMap(TJs::getcOpenId, TJs::getDistance));
  132. js.setIds(nearbyTechnicians.stream().map(TJs::getcOpenId).collect(Collectors.toList()));
  133. int size = (int) page.getSize();
  134. int pageNum = (int) page.getCurrent();
  135. page.setSize(nearbyTechnicians.size());
  136. page.setCurrent(1);
  137. Page<TJs> all = jsService.getAll(page, js);
  138. all.getRecords().forEach(item -> {
  139. item.setDistance(collect.get(item.getcOpenId()).setScale(2, RoundingMode.DOWN));
  140. });
  141. List<TJs> objects = (List<TJs>) ListUtils.subList(all.getRecords(), size, pageNum);
  142. page.setRecords(objects);
  143. page.setSize(size);
  144. return R.ok(page);
  145. }
  146. }
  147. return R.ok(jsService.getAll(page, js));
  148. } catch (Exception e) {
  149. // 异常处理
  150. e.printStackTrace();
  151. return R.fail("操作失败");
  152. }
  153. }
  154. /**
  155. * 分页查询数据
  156. */
  157. @RequestMapping(value = "/select", method = RequestMethod.GET)
  158. @ApiOperation("分页查询数据")
  159. public Page<TJs> selectSp(Page<TJs> page, TJs js) {
  160. LambdaQueryWrapper<TJs> mhCompanyLambdaQueryWrapper = new LambdaQueryWrapper<>();
  161. mhCompanyLambdaQueryWrapper.eq(null != js.getnTong(), TJs::getnTong, js.getnTong()).
  162. like(StringUtils.isNotBlank(js.getcName()), TJs::getcName, js.getcName()).
  163. like(StringUtils.isNotBlank(js.getcNickName()), TJs::getcNickName, js.getcNickName()).
  164. like(StringUtils.isNotBlank(js.getcPhone()), TJs::getcPhone, js.getcPhone()).
  165. eq(js.getnStatus() != null, TJs::getnStatus, js.getnStatus()).
  166. eq(js.getnSex() != null, TJs::getnSex, js.getnSex()).
  167. orderByDesc(TJs::getDtCreateTime);
  168. return jsService.page(page, mhCompanyLambdaQueryWrapper);
  169. }
  170. /**
  171. * 删除
  172. */
  173. @Log(title = "技师管理", businessType = BusinessType.DELETE)
  174. @RequestMapping(value = "/del", method = RequestMethod.POST)
  175. @ApiOperation("删除")
  176. public Boolean del(@RequestBody TJs js) {
  177. return jsService.removeById(js);
  178. }
  179. @ApiOperation("根据id查询技师")
  180. @RequestMapping(value = "/wx/getByid", method = RequestMethod.POST)
  181. public R getById(@RequestBody TJs js) {
  182. try {
  183. return R.ok(jsService.getByJsId(js.getId(), js.getcOpenId()));
  184. } catch (ServiceException s) {
  185. log.error(s.getMessage());
  186. return R.fail(s.getMessage());
  187. } catch (Exception e) {
  188. log.error(e.getMessage());
  189. return R.fail("系统异常");
  190. }
  191. }
  192. @ApiOperation("根据OpenId查询技师")
  193. @RequestMapping(value = "/wx/getByOpenId", method = RequestMethod.POST)
  194. public R getByOpenId(@RequestBody TJs js) {
  195. try {
  196. LambdaQueryWrapper<TJs> mhCompanyLambdaQueryWrapper = new LambdaQueryWrapper<>();
  197. mhCompanyLambdaQueryWrapper.eq(TJs::getcOpenId, js.getcOpenId());
  198. return R.ok(jsService.getOne(mhCompanyLambdaQueryWrapper));
  199. } catch (ServiceException s) {
  200. log.error(s.getMessage());
  201. return R.fail(s.getMessage());
  202. } catch (Exception e) {
  203. log.error(e.getMessage());
  204. return R.fail("系统异常");
  205. }
  206. }
  207. @ApiOperation("审核")
  208. @RequestMapping(value = "/auditing", method = RequestMethod.POST)
  209. public R auditing(@RequestBody TJs js) {
  210. try {
  211. return R.ok(jsService.auditing(js));
  212. } catch (ServiceException s) {
  213. log.error(s.getMessage());
  214. return R.fail(s.getMessage());
  215. } catch (Exception e) {
  216. log.error(e.getMessage());
  217. return R.fail("系统异常");
  218. }
  219. }
  220. @ApiOperation("退回")
  221. @RequestMapping(value = "/repulse", method = RequestMethod.POST)
  222. public R repulse(@RequestBody TJs js) {
  223. try {
  224. LambdaQueryWrapper<TJs> mhCompanyLambdaQueryWrapper = new LambdaQueryWrapper<>();
  225. mhCompanyLambdaQueryWrapper.eq(TJs::getId, js.getId()).eq(TJs::getnTong, JsStatusEnum.JS_NOT_PASS.getCode());
  226. js.setnTong(JsStatusEnum.JS_RETURN.getCode());
  227. return R.ok(jsService.update(js, mhCompanyLambdaQueryWrapper));
  228. } catch (ServiceException s) {
  229. log.error(s.getMessage());
  230. return R.fail(s.getMessage());
  231. } catch (Exception e) {
  232. log.error(e.getMessage());
  233. return R.fail("系统异常");
  234. }
  235. }
  236. @ApiOperation("通知")
  237. @RequestMapping(value = "/tongzhi", method = RequestMethod.GET)
  238. public boolean sendSubscribeMessage(String openId) {
  239. String page="pages/index/index"; // 应该是消息点击后跳转页面
  240. WxMaSubscribeMessage message = new WxMaSubscribeMessage();
  241. message.setTemplateId(msgTemplateId1);
  242. message.setToUser(openId);
  243. message.setPage(page);
  244. List<WxMaSubscribeMessage.MsgData> subscribeDataList = new ArrayList<>();
  245. WxMaSubscribeMessage.MsgData subscribeData = new WxMaSubscribeMessage.MsgData();
  246. subscribeData.setName("key1"); // 你在小程序自定义的key(比如“商家名称”)
  247. subscribeData.setValue("夜来香"); //key对应的内容
  248. WxMaSubscribeMessage.MsgData msgData = new WxMaSubscribeMessage.MsgData();
  249. msgData.setName("key2");
  250. msgData.setValue("");
  251. //添加你配置要且要展示的内容
  252. subscribeDataList.add(subscribeData);
  253. message.setData(subscribeDataList);
  254. try {
  255. wxMaService.getMsgService().sendSubscribeMsg(message);
  256. return true;
  257. } catch (WxErrorException e) {
  258. log.error("发送小程序订阅消息失败,errCode:{},errMsg:{}", e.getError().getErrorCode(), e.getError().getErrorMsg());
  259. }
  260. return false;
  261. }
  262. }