package com.ylx.web.controller.massage; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.ylx.common.annotation.Log; import com.ylx.common.core.controller.BaseController; import com.ylx.common.core.domain.R; import com.ylx.common.core.domain.model.WxLoginUser; import com.ylx.common.enums.BusinessType; import com.ylx.common.exception.ServiceException; import com.ylx.common.utils.ListUtils; import com.ylx.massage.domain.Location; import com.ylx.massage.domain.TJs; import com.ylx.massage.domain.vo.TJsVo; import com.ylx.massage.service.TJsService; import com.ylx.massage.utils.LocationUtil; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; import javax.annotation.Resource; import java.math.BigDecimal; import java.util.List; import java.util.Map; import java.util.stream.Collectors; /** * 技师Controller * * @author ylx * @date 2024-03-22 */ @RestController @RequestMapping("api/js/v1") @Api(tags = {"技师管理"}) @Slf4j public class TJsController extends BaseController { @Resource private TJsService jsService; @Resource private LocationUtil locationUtil; /** * 添加技师申请 * * @param js * @return */ @Log(title = "技师管理", businessType = BusinessType.INSERT) @RequestMapping(value = "wx/add", method = RequestMethod.POST) @ApiOperation("添加技师申请") public R add(@RequestBody TJs js) { try { WxLoginUser wxLoginUser = getWxLoginUser(); js.setcOpenId(wxLoginUser.getCOpenid()); return R.ok(jsService.addJs(js)); } catch (ServiceException s) { log.error(s.toString()); return R.fail(s.getMessage()); } catch (Exception e) { log.error(e.toString()); return R.fail("系统异常"); } } /** * 更新技师信息 * * @param js * @return */ @Log(title = "技师管理", businessType = BusinessType.UPDATE) @RequestMapping(value = "wx/update", method = RequestMethod.POST) @ApiOperation("更新技师信息") public R wxUpdate(@RequestBody TJs js) { try { return R.ok(jsService.wxUpdateJs(js)); } catch (ServiceException s) { log.error(s.toString()); return R.fail(s.getMessage()); } catch (Exception e) { log.error(e.toString()); return R.fail("系统异常"); } } /** * 微信查询 * * @param * @return */ @RequestMapping(value = "wx/select", method = RequestMethod.GET) @ApiOperation("微信查询") public R wxSelect(Page page, TJsVo js) { try { if (js.getLatitude() != null && js.getLongitude() != null) { // 检查经纬度是否在合理范围内 Location location = new Location(); location.setLatitude(js.getLatitude().doubleValue()); location.setLongitude(js.getLongitude().doubleValue()); location.setLimit(100L); // 考虑将此值作为参数或配置项 location.setRadius(1000.00); List nearbyTechnicians = locationUtil.dis(location); if (nearbyTechnicians != null) { Map collect = nearbyTechnicians.stream().collect(Collectors.toMap(TJs::getcOpenId, TJs::getDistance)); js.setIds(nearbyTechnicians.stream().map(TJs::getcOpenId).collect(Collectors.toList())); int size = (int) page.getSize(); int pageNum = (int) page.getCurrent(); page.setSize(nearbyTechnicians.size()); page.setCurrent(1); Page all = jsService.getAll(page, js); all.getRecords().forEach(item -> { item.setDistance(collect.get(item.getcOpenId())); }); List objects = (List) ListUtils.subList(all.getRecords(), size, pageNum); page.setRecords(objects); page.setSize(size); } } return R.ok(jsService.getAll(page, js)); } catch (Exception e) { // 异常处理 e.printStackTrace(); return R.fail("操作失败"); } } /** * 分页查询数据 */ @RequestMapping(value = "/select", method = RequestMethod.GET) @ApiOperation("分页查询数据") public Page selectSp(Page page, TJs js) { LambdaQueryWrapper mhCompanyLambdaQueryWrapper = new LambdaQueryWrapper<>(); mhCompanyLambdaQueryWrapper.eq(null != js.getnTong(), TJs::getnTong, js.getnTong()). like(StringUtils.isNotBlank(js.getcName()), TJs::getcName, js.getcName()). orderByDesc(TJs::getDtCreateTime); return jsService.page(page, mhCompanyLambdaQueryWrapper); } /** * 删除 */ @Log(title = "技师管理", businessType = BusinessType.DELETE) @RequestMapping(value = "/del", method = RequestMethod.POST) @ApiOperation("删除") public Boolean del(@RequestBody TJs js) { return jsService.removeById(js); } @ApiOperation("根据id查询技师") @RequestMapping(value = "/wx/getByid", method = RequestMethod.POST) public R getById(@RequestBody TJs js) { try { return R.ok(jsService.getByJsId(js.getId())); } catch (ServiceException s) { log.error(s.getMessage()); return R.fail(s.getMessage()); } catch (Exception e) { log.error(e.getMessage()); return R.fail("系统异常"); } } @ApiOperation("根据OpenId查询技师") @RequestMapping(value = "/wx/getByOpenId", method = RequestMethod.POST) public R getByOpenId(@RequestBody TJs js) { try { LambdaQueryWrapper mhCompanyLambdaQueryWrapper = new LambdaQueryWrapper<>(); mhCompanyLambdaQueryWrapper.eq(TJs::getcOpenId, js.getcOpenId()); return R.ok(jsService.getOne(mhCompanyLambdaQueryWrapper)); } catch (ServiceException s) { log.error(s.getMessage()); return R.fail(s.getMessage()); } catch (Exception e) { log.error(e.getMessage()); return R.fail("系统异常"); } } }