package com.panzhihua.service_community.service.impl; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.panzhihua.common.model.dtos.community.convenient.ConvenientElevatingPointDTO; import com.panzhihua.common.model.dtos.community.convenient.PageConvenientElevatingPointDTO; import com.panzhihua.common.model.vos.R; import com.panzhihua.common.model.vos.community.convenient.ConvenientElevatingPointVO; import com.panzhihua.service_community.dao.ComActDAO; import com.panzhihua.service_community.dao.ConvenientElevatingPointDAO; import com.panzhihua.service_community.dao.ConvenientMerchantDAO; import com.panzhihua.service_community.model.dos.ComActDO; import com.panzhihua.service_community.model.dos.ConvenientElevatingPointDO; import com.panzhihua.service_community.model.dos.ConvenientMerchantDO; import com.panzhihua.service_community.service.ConvenientElevatingPointService; import com.panzhihua.service_community.util.MapDistance; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.BeanUtils; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import javax.annotation.Resource; import java.util.*; import static java.util.Objects.isNull; import static java.util.Objects.nonNull; /** * @ClassName: ConvenientElevatingPointServiceImpl * @Author: yh * @Date: 2022/11/8 10:34 * @Description: 自提点 */ @Service @Slf4j public class ConvenientElevatingPointServiceImpl extends ServiceImpl implements ConvenientElevatingPointService { @Resource private ComActDAO comActDAO; @Resource private ConvenientMerchantDAO convenientMerchantDAO; /** * 分页查询 * * @param pageConvenientElevatingPointDTO * @return */ @Override public R pagePoint(PageConvenientElevatingPointDTO pageConvenientElevatingPointDTO) { Page page = new Page<>(); page.setSize(pageConvenientElevatingPointDTO.getPageSize()); page.setCurrent(pageConvenientElevatingPointDTO.getPageNum()); IPage iPage = this.baseMapper.page(page, pageConvenientElevatingPointDTO); return R.ok(iPage); } /** * 新增 * * @param convenientElevatingPointDTO * @return */ @Override @Transactional(rollbackFor = Exception.class) public R addPoint(ConvenientElevatingPointDTO convenientElevatingPointDTO) { ConvenientElevatingPointDO convenientElevatingPointDODB = this.baseMapper.selectOne(new LambdaQueryWrapper().eq(ConvenientElevatingPointDO::getWechatAccount, convenientElevatingPointDTO.getWechatAccount())); if (!Objects.isNull(convenientElevatingPointDODB)){ return R.fail("该微信手机号已使用,请更换"); } ConvenientElevatingPointDO convenientElevatingPointDO = new ConvenientElevatingPointDO(); BeanUtils.copyProperties(convenientElevatingPointDTO, convenientElevatingPointDO); String communityId = convenientElevatingPointDTO.getCommunityId(); if (nonNull(communityId)) { ComActDO comActDO = comActDAO.selectById(Long.parseLong(communityId.substring(communityId.lastIndexOf(",") + 1))); if (comActDO != null) { convenientElevatingPointDO.setCommunityName(comActDO.getName()); } } this.baseMapper.insert(convenientElevatingPointDO); return R.ok(); } /** * 修改 * * @param convenientElevatingPointDTO * @return */ @Override @Transactional(rollbackFor = Exception.class) public R putPoint(ConvenientElevatingPointDTO convenientElevatingPointDTO) { ConvenientElevatingPointDO convenientElevatingPointDODB = this.baseMapper.selectOne(new LambdaQueryWrapper().eq(ConvenientElevatingPointDO::getWechatAccount, convenientElevatingPointDTO.getWechatAccount())); Long id = convenientElevatingPointDTO.getId(); ConvenientElevatingPointDO convenientElevatingPointDO = this.baseMapper.selectById(id); if (isNull(convenientElevatingPointDO)) { return R.fail("自提点不存在"); } if (nonNull(convenientElevatingPointDODB) && !convenientElevatingPointDODB.getId().equals(convenientElevatingPointDO.getId())) { R.fail("该微信手机号:" + convenientElevatingPointDTO.getWechatAccount() + "已被使用!请更换"); } String communityId = convenientElevatingPointDTO.getCommunityId(); if (nonNull(communityId)) { ComActDO comActDO = comActDAO.selectById(Long.parseLong(communityId.substring(communityId.lastIndexOf(",") + 1))); if (comActDO != null) { convenientElevatingPointDO.setCommunityName(comActDO.getName()); } } BeanUtils.copyProperties(convenientElevatingPointDTO, convenientElevatingPointDO); this.baseMapper.updateById(convenientElevatingPointDO); return R.ok(); } @Override public R deletePoint(Long pointId, Long operator) { ConvenientElevatingPointDO convenientElevatingPointDO = this.baseMapper.selectById(pointId); if (convenientElevatingPointDO.getPrepareGoodsNum() > 0) { return R.fail("该提货点暂时无法删除!若想删除需保证待提货数量为0"); } int result = this.baseMapper.deletePointById(pointId, operator); if (result > 0) { return R.ok(); } return R.fail("删除失败"); } @Override public R detailPoint(Long pointId) { ConvenientElevatingPointDO convenientElevatingPointDO = this.baseMapper.selectById(pointId); ConvenientElevatingPointVO convenientElevatingPointVO = new ConvenientElevatingPointVO(); BeanUtils.copyProperties(convenientElevatingPointDO,convenientElevatingPointVO); return R.ok(convenientElevatingPointVO); } public R findPointByMerchantId(Long merchantId, String lat, String lng) { ConvenientMerchantDO convenientMerchantDO = convenientMerchantDAO.selectById(merchantId); //服务最远距离 km Integer serviceRange = convenientMerchantDO.getServiceRange(); Map around = MapDistance.getAround(convenientMerchantDO.getLat(), convenientMerchantDO.getLon(), serviceRange * 1000d); String maxLat = around.get("maxLat").toString(); String minLat = around.get("minLat").toString(); String minLng = around.get("minLng").toString(); String maxLng = around.get("maxLng").toString(); List convenientElevatingPointDOS = this.baseMapper.selectList( new LambdaQueryWrapper() .ge(ConvenientElevatingPointDO::getLat, Double.parseDouble(minLng)) .le(ConvenientElevatingPointDO::getLat, Double.parseDouble(maxLng)) .ge(ConvenientElevatingPointDO::getLon, Double.parseDouble(minLat)) .le(ConvenientElevatingPointDO::getLon, Double.parseDouble(maxLat)) .eq(ConvenientElevatingPointDO::getBusinessStatus,1)); List convenientElevatingPointVOS = new ArrayList<>(); convenientElevatingPointDOS.forEach(f -> { ConvenientElevatingPointVO convenientElevatingPointVO = new ConvenientElevatingPointVO(); BeanUtils.copyProperties(f, convenientElevatingPointVO); Double distance = MapDistance.distanceOfTwoPoints(lat, lng, f.getLon(), f.getLat()); convenientElevatingPointVO.setDistance(distance); convenientElevatingPointVOS.add(convenientElevatingPointVO); }); Collections.sort(convenientElevatingPointVOS); return R.ok(convenientElevatingPointVOS); } @Override public R findPointNumByDistance(Integer distance, String lat, String lng) { Map around = MapDistance.getAround(lat, lng, distance * 1000d); String maxLat = around.get("maxLat").toString(); String minLat = around.get("minLat").toString(); String minLng = around.get("minLng").toString(); String maxLng = around.get("maxLng").toString(); List convenientElevatingPointDOS = this.baseMapper.selectList( new LambdaQueryWrapper() .ge(ConvenientElevatingPointDO::getLat, Double.parseDouble(minLng)) .le(ConvenientElevatingPointDO::getLat, Double.parseDouble(maxLng)) .ge(ConvenientElevatingPointDO::getLon, Double.parseDouble(minLat)) .le(ConvenientElevatingPointDO::getLon, Double.parseDouble(maxLat)) .eq(ConvenientElevatingPointDO::getBusinessStatus,1)); List convenientElevatingPointVOS = new ArrayList<>(); convenientElevatingPointDOS.forEach(f -> { ConvenientElevatingPointVO convenientElevatingPointVO = new ConvenientElevatingPointVO(); BeanUtils.copyProperties(f, convenientElevatingPointVO); Double distancem = MapDistance.distanceOfTwoPoints(lat, lng, f.getLon(), f.getLat()); convenientElevatingPointVO.setDistance(distancem); convenientElevatingPointVOS.add(convenientElevatingPointVO); }); Collections.sort(convenientElevatingPointVOS); return R.ok(convenientElevatingPointVOS.size()); } }