mitao
2025-02-21 31573d6180d15ef65ed0df9c2732495f40b12663
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
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<ConvenientElevatingPointDAO, ConvenientElevatingPointDO> 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<ConvenientElevatingPointVO> 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<ConvenientElevatingPointDO>().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<ConvenientElevatingPointDO>().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<Object, Object> 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<ConvenientElevatingPointDO> convenientElevatingPointDOS = this.baseMapper.selectList(
                new LambdaQueryWrapper<ConvenientElevatingPointDO>()
                        .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<ConvenientElevatingPointVO> 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<Object, Object> 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<ConvenientElevatingPointDO> convenientElevatingPointDOS = this.baseMapper.selectList(
                new LambdaQueryWrapper<ConvenientElevatingPointDO>()
                        .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<ConvenientElevatingPointVO> 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());
    }
}