xiaoyong931011
2023-02-23 e3d7ec787c2a2b03af577fa151cf78f951a6ae66
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
package cc.mrbird.febs.mall.service.impl;
 
import cc.mrbird.febs.common.entity.FebsResponse;
import cc.mrbird.febs.common.exception.FebsException;
import cc.mrbird.febs.common.utils.LoginUserUtil;
import cc.mrbird.febs.common.utils.RedisUtils;
import cc.mrbird.febs.mall.conversion.MallAddressInfoConversion;
import cc.mrbird.febs.mall.dto.AddressInfoDto;
import cc.mrbird.febs.mall.dto.ApiIdentifyAddressDto;
import cc.mrbird.febs.mall.entity.MallAddressInfo;
import cc.mrbird.febs.mall.entity.MallMember;
import cc.mrbird.febs.mall.mapper.MallAddressInfoMapper;
import cc.mrbird.febs.mall.service.IApiMallAddressInfoService;
import cc.mrbird.febs.mall.vo.AddressInfoVo;
import cc.mrbird.febs.pay.util.WechatConfigure;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import okhttp3.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
 
/**
 * @author wzy
 * @date 2021-09-18
 **/
@Slf4j
@Service
@RequiredArgsConstructor
public class ApiMallAddressInfoServiceImpl extends ServiceImpl<MallAddressInfoMapper, MallAddressInfo> implements IApiMallAddressInfoService {
 
    @Autowired
    private RedisUtils redisUtils;
 
    @Override
    public List<AddressInfoVo> findAddressInfoList() {
        Long memberId = LoginUserUtil.getLoginUser().getId();
        List<MallAddressInfo> addressInfos = this.baseMapper.selectAddressInfoListByMemberId(memberId);
 
        return MallAddressInfoConversion.INSTANCE.entityListToVos(addressInfos);
    }
 
    @Override
    public FebsResponse addAddress(AddressInfoDto addressInfoDto) {
        MallAddressInfo addressInfo = MallAddressInfoConversion.INSTANCE.dtoToEntity(addressInfoDto);
 
        MallMember member = LoginUserUtil.getLoginUser();
        Long memberId = member.getId();
        List<MallAddressInfo> existAddress = this.baseMapper.selectAddressInfoListByMemberId(memberId);
        if (CollUtil.isEmpty(existAddress)) {
            addressInfo.setIsDefault(MallAddressInfo.IS_DEFAULT_Y);
        } else {
            if (MallAddressInfo.IS_DEFAULT_Y.equals(addressInfoDto.getIsDefault())) {
                this.baseMapper.updateIsDefault(MallAddressInfo.IS_DEFAULT_N, memberId, null);
            }
        }
 
        addressInfo.setMemberId(memberId);
        addressInfo.setCreatedBy(member.getPhone());
        addressInfo.setUpdatedBy(member.getPhone());
        this.baseMapper.insert(addressInfo);
 
        return new FebsResponse().success().data(addressInfo.getId());
    }
 
    @Override
    public void modifyAddress(AddressInfoDto addressInfoDto) {
        MallAddressInfo existAddress = this.baseMapper.selectById(addressInfoDto.getId());
        if (existAddress == null) {
            throw new FebsException("地址不存在");
        }
 
        Long memberId = LoginUserUtil.getLoginUser().getId();
        MallAddressInfo addressInfo = MallAddressInfoConversion.INSTANCE.dtoToEntity(addressInfoDto);
        if (MallAddressInfo.IS_DEFAULT_Y.equals(addressInfoDto.getIsDefault())) {
            this.baseMapper.updateIsDefault(MallAddressInfo.IS_DEFAULT_N, memberId, null);
        }
 
        addressInfo.setUpdatedTime(new Date());
        this.baseMapper.updateById(addressInfo);
    }
 
    @Override
    public void setDefaultAddress(Long id) {
        MallAddressInfo addressInfo = this.baseMapper.selectById(id);
        if (addressInfo == null) {
            throw new FebsException("地址不存在");
        }
 
        MallMember member = LoginUserUtil.getLoginUser();
        this.baseMapper.updateIsDefault(MallAddressInfo.IS_DEFAULT_N, member.getId(), null);
 
        this.baseMapper.updateIsDefault(MallAddressInfo.IS_DEFAULT_Y, member.getId(), id);
    }
 
    static final OkHttpClient HTTP_CLIENT = new OkHttpClient().newBuilder().build();
 
    @Override
    public FebsResponse identifyAddress(ApiIdentifyAddressDto identifyAddressDto) {
        MediaType mediaType = MediaType.parse("application/json");
        RequestBody body = RequestBody.create(mediaType, JSONUtil.toJsonStr(identifyAddressDto));
        Request request = new Request.Builder()
                .url("https://aip.baidubce.com/rpc/2.0/nlp/v1/address?access_token=" + redisUtils.get(WechatConfigure.BAIDU_ACCESS_TOKEN_REDIS_KEY).toString())
                .method("POST", body)
                .addHeader("Content-Type", "application/json")
                .addHeader("Accept", "application/json")
                .build();
        String data = "";
        try {
            Response response = HTTP_CLIENT.newCall(request).execute();
            data = response.body().string();
            if (data.contains("error_code")) {
                JSONObject jsonObject = JSONUtil.parseObj(data);
                return new FebsResponse().fail().message("地址识别错误编码:"+jsonObject.get("error_code").toString());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return new FebsResponse().success().data(data);
    }
}