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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
package com.mzl.flower.service.customer;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mzl.flower.config.exception.ValidationException;
import com.mzl.flower.config.security.SecurityUtils;
import com.mzl.flower.constant.Constants;
import com.mzl.flower.dto.request.customer.BindPartnerDTO;
import com.mzl.flower.dto.request.customer.ChangePartnerDTO;
import com.mzl.flower.dto.request.customer.QueryCustomerDTO;
import com.mzl.flower.dto.request.customer.UpdateCustomerDTO;
import com.mzl.flower.dto.response.customer.CustomerDTO;
import com.mzl.flower.dto.response.partner.PartnerDTO;
import com.mzl.flower.entity.customer.Customer;
import com.mzl.flower.entity.partner.Partner;
import com.mzl.flower.enums.TrueOrFalseEnum;
import com.mzl.flower.mapper.customer.CustomerMapper;
import com.mzl.flower.mapper.partner.PartnerMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.util.List;
 
@Service
@Transactional
@Slf4j
public class CustomerService {
 
 
    private final CustomerMapper customerMapper;
 
    private final PartnerMapper partnerMapper;
 
    public CustomerService(CustomerMapper customerMapper, PartnerMapper partnerMapper) {
        this.customerMapper = customerMapper;
        this.partnerMapper = partnerMapper;
    }
 
    public CustomerDTO getCurrentCustomer() {
        return customerMapper.getCurrentCustomer(SecurityUtils.getUserId());
    }
 
    public PartnerDTO getCurrentBindPartner() {
        CustomerDTO cus = customerMapper.getCurrentCustomer(SecurityUtils.getUserId());
        if (cus != null && StringUtils.isNotBlank(cus.getPartnerId())) {
            return partnerMapper.getCurrentPartner(cus.getPartnerUserId());
        }
        return null;
    }
 
    public void addOrUpdateCustomer(UpdateCustomerDTO dto) {
 
        Customer customer;
        if (dto.getId() == null) { //注册
            if (checkExist(dto.getUserId())) {
                throw new ValidationException("商户信息已注册");
            }
            customer = new Customer();
            BeanUtils.copyProperties(dto, customer, "id");
            Partner byPartnerId = getByPartnerId(dto.getPartnerUserId());
            if (byPartnerId != null) {
                customer.setPartnerUserId(byPartnerId.getUserId());
                customer.setPartnerId(byPartnerId.getId());
            }
 
            customer.create(SecurityUtils.getUserId());
            customerMapper.insert(customer);
        } else {//重新修改
            customer = customerMapper.selectById(dto.getId());
            if (customer == null) {
                throw new ValidationException("商户信息不存在");
            }
            BeanUtils.copyProperties(dto, customer, "id");
//            customer.setPartnerUserId(getByPartnerId(dto.getPartnerUserId()));
            Partner byPartnerId = getByPartnerId(dto.getPartnerUserId());
            if (byPartnerId != null) {
                customer.setPartnerUserId(byPartnerId.getUserId());
                customer.setPartnerId(byPartnerId.getId());
            }
            customer.update(SecurityUtils.getUserId());
            customerMapper.updateById(customer);
        }
 
 
    }
 
    private Partner getByPartnerId(String partnerId) {
        if (StringUtils.isNotBlank(partnerId)) {
            Partner partnerByUserId = partnerMapper.selectOne(new QueryWrapper<Partner>().eq("user_id", partnerId));
            if (partnerByUserId != null) {
                return partnerByUserId;
            }
            Partner partner = partnerMapper.selectById(partnerId);
            if (partner == null) {
                throw new ValidationException("合伙人不存在");
            } else {
                //判断id和userid是否相同,避免string被转为int
                if (!partner.getUserId().equals(partnerId) && !partner.getId().toString().equals(partnerId)) {
                    throw new ValidationException("合伙人不存在");
                }
            }
//            return partner.getUserId();
            return partner;
        }
        return null;
 
    }
 
    private boolean checkExist(String userId) {
        return customerMapper.selectCount(new LambdaQueryWrapper<Customer>().eq(Customer::getUserId, userId)) > 0;
    }
 
    public Page<CustomerDTO> queryCustomer(QueryCustomerDTO dto, Page page) {
        List<CustomerDTO> list = customerMapper.queryCustomer(dto, page);
        page.setRecords(list);
        return page;
    }
 
    public CustomerDTO findDetail(Long id) {
        Customer customer = customerMapper.selectById(id);
        if (customer == null) {
            return null;
        }
        if (Constants.USER_TYPE.admin.name().equals(SecurityUtils.getUserType())) {
            CustomerDTO customerDTO = new CustomerDTO();
            BeanUtils.copyProperties(customer, customerDTO);
            return customerDTO;
        } else if (Constants.USER_TYPE.partner.name().equals(SecurityUtils.getUserType())) {
            if (customer.getPartnerUserId().equals(SecurityUtils.getUserId())) {
                CustomerDTO customerDTO = new CustomerDTO();
                BeanUtils.copyProperties(customer, customerDTO);
                return customerDTO;
            }
        }
        return null;
    }
 
    public void changeCustomerPartner(ChangePartnerDTO dto) {
        Customer customer = customerMapper.selectById(dto.getId());
        if (customer == null) {
            throw new ValidationException("商户信息不存在");
        }
        if (dto.getPartnerId() == null || dto.getPartnerId() <= 0) {
            customer.setPartnerUserId(null);
            customer.setPartnerId(null);
        } else {
            Partner partner = partnerMapper.selectById(dto.getPartnerId());
            if (partner == null) {
                throw new ValidationException("合伙人不存在");
            }
            if (!"P".equals(partner.getStatus())) {
                throw new ValidationException("合伙人信息未审核通过");
            }
            customer.setPartnerUserId(partner.getUserId());
            customer.setPartnerId(partner.getId());
        }
        customerMapper.updateById(customer);
    }
 
    public void bindPartner(BindPartnerDTO dto) {
        CustomerDTO c = getCurrentCustomer();
        if (c == null) {
            throw new ValidationException("商户信息不存在");
        }
 
        if (StringUtils.isNotBlank(c.getPartnerId()) || StringUtils.isNotBlank(c.getPartnerUserId())) {
            throw new ValidationException("商户已绑定合伙人,请联系客服人员进行解绑后再进行绑定");
        }
        Partner partner;
        try {
            long id = Long.parseLong(dto.getPartnerUserId());
            partner = partnerMapper.selectById(id);
        } catch (Exception e) {
            partner =partnerMapper.selectOne(new QueryWrapper<Partner>().eq("user_id", dto.getPartnerUserId()));
        }
        if (partner == null) {
            throw new ValidationException("合伙人不存在");
        }
        if (!"P".equals(partner.getStatus())) {
            throw new ValidationException("合伙人信息未审核通过,请联系客服人员");
        }
 
        customerMapper.bindPartner(c.getId(), partner.getId(), partner.getUserId());
    }
 
    public String getPartnerName(String partnerUserId) {
        if (StringUtils.isNotBlank(partnerUserId)) {
            Partner partner;
            try {
                long id = Long.parseLong(partnerUserId);
                partner = partnerMapper.selectById(id);
            } catch (Exception e) {
                partner =partnerMapper.selectOne(new QueryWrapper<Partner>().eq("user_id", partnerUserId));
            }
            if (partner == null) {
                throw new ValidationException("合伙人不存在");
            }
            return partner.getName();
        }
        return null;
    }
 
    /**
     * 根据会员等级获取等级下的customer信息
     * @param levelId
     * @return
     */
    public List<Customer> getCustomerListByLevelId(Integer levelId){
        if(null != levelId){
            QueryWrapper<Customer> customerQueryWrapper=new QueryWrapper<>();
            customerQueryWrapper.lambda()
                    .eq(Customer::getDeleted, TrueOrFalseEnum.FALSE.isFlag())
                    .eq(Customer::getLevelId,levelId);
            return customerMapper.selectList(customerQueryWrapper);
        }
        return null;
    }
 
}