Administrator
2023-06-19 49588f5a462ae7425e7eb030438a35fd80c246fa
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
package com.gk.firework.Service.ServiceImpl;
 
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gk.firework.Domain.CustomerInfo;
import com.gk.firework.Domain.Exception.BusinessException;
import com.gk.firework.Domain.Utils.Properties;
import com.gk.firework.Domain.Utils.StringUtils;
import com.gk.firework.Domain.Utils.UploadUtil;
import com.gk.firework.Mapper.CustomerInfoMapper;
import com.gk.firework.Service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
 
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
 
/**
 * @author : jingjy
 * @date : 2021/3/31 14:19
 */
@Service("CustomerService")
public class CustomerServiceImpl extends ServiceImpl<CustomerInfoMapper, CustomerInfo> implements CustomerService {
    @Autowired
    private CustomerInfoMapper customerInfoMapper;
    @Override
    public CustomerInfo createOrUpdate(JSONObject customer, Integer num, Date salesTime) throws ParseException {
        //TODO:新增或修改顾客信息
        String idCardNum = customer.getString("idCardNumber");
        String workerName = customer.getString("workerName");
        String nation = customer.getString("nation");
//        String gender = customer.getString("sex");
        String gender = userType(idCardNum);
        byte byteGender = 0;
        if (CustomerInfo.GENDER_MALE.equals(gender)){
            byteGender = 1;
        }else if (CustomerInfo.GENDER_FEMALE.equals(gender)){
            byteGender = 2;
        }
 
        String bornDay = customer.getString("csrq");
        DateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
        Date date = new Date();
        Date born = dateFormat.parse(bornDay);
        String address = customer.getString("certAddress");
        CustomerInfo customerInfo = customerInfoMapper.selectCustomerByIdCardNum(idCardNum);
        if (customerInfo == null){
            customerInfo = new CustomerInfo(workerName,byteGender,idCardNum,nation,born,address);
            customerInfo.setFrequency(1);
            customerInfo.setCreateddate(date);
            customerInfo.setLasttime(salesTime);
            customerInfo.setLastnum(num);
            customerInfo.setNum(num);
            customerInfoMapper.insert(customerInfo);
        }else {
            if (customerInfo.getLasttime().compareTo(salesTime) < 0){
                customerInfo.setLasttime(salesTime);
                customerInfo.setLastnum(num);
            }
            customerInfo.setFrequency(customerInfo.getFrequency()+1);
            customerInfo.setNum(customerInfo.getNum() + num);
            customerInfo.setModifieddate(date);
            customerInfoMapper.updateById(customerInfo);
        }
        return customerInfo;
 
    }
 
    //根据身份证号分辨男女
    public static String userType(String idNumber) {
        String userType = "";
        int gender = 0;
        idNumber = idNumber.replaceAll("[\r\n]","").trim();
        if(idNumber.length() == 18){
            //如果身份证号18位,取身份证号倒数第二位
            char c = idNumber.charAt(idNumber.length() - 2);
            gender = Integer.parseInt(String.valueOf(c));
        }else if (idNumber.length() == 15){
            //如果身份证号15位,取身份证号最后一位
            char c = idNumber.charAt(idNumber.length() - 1);
            gender = Integer.parseInt(String.valueOf(c));
        }
        if(gender % 2 == 1){
            userType =  "男";
        }else{
            userType =  "女";
        }
        return userType;
    }
 
    @Override
    public CustomerInfo getCustomerByIdCardNum(String idCardNum) {
        return customerInfoMapper.selectCustomerByIdCardNum(idCardNum);
    }
 
    @Override
    public CustomerInfo getCustomerBySaleOrder(String orderCode) {
        return customerInfoMapper.getCustomerBySaleOrder(orderCode);
    }
 
    @Override
    public void uploadPhoto(String idCard, MultipartFile file) {
 
        if (StringUtils.isBlank(idCard))
            throw new BusinessException("身份证参数传递错误,请联系管理员");
        if (file == null)
            throw new BusinessException("请上传文件");
        CustomerInfo customer = getCustomerByIdCardNum(idCard);
        if (customer == null)
            throw new BusinessException("身份证信息不存在,请联系管理员");
 
        try {
            String name = UploadUtil.uploadFile(file, Properties.customerPath);
            customer.setPath(Properties.customer + name);
        } catch (Exception e) {
            throw new BusinessException("上传文件失败");
        }
        this.updateById(customer);
 
    }
 
    @Override
    public void createOrUpdateCard(String idCardNum, Integer num, Date createdat) throws ParseException {
        //TODO:新增或修改顾客信息
        String gender = userType(idCardNum);
        byte byteGender = 0;
        if (CustomerInfo.GENDER_MALE.equals(gender)){
            byteGender = 1;
        }else if (CustomerInfo.GENDER_FEMALE.equals(gender)){
            byteGender = 2;
        }
 
        String bornDay = idCardNum.substring(6,14);
        DateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
        Date date = new Date();
        Date born = dateFormat.parse(bornDay);
 
        CustomerInfo customerInfo = customerInfoMapper.selectCustomerByIdCardNum(idCardNum);
        if (customerInfo == null){
            customerInfo = new CustomerInfo("",byteGender,idCardNum,"",born,"");
            customerInfo.setFrequency(1);
            customerInfo.setCreateddate(date);
            customerInfo.setLasttime(createdat);
            customerInfo.setLastnum(num);
            customerInfo.setNum(num);
            customerInfoMapper.insert(customerInfo);
        }else {
            if (customerInfo.getLasttime().compareTo(createdat) < 0){
                customerInfo.setLasttime(createdat);
                customerInfo.setLastnum(num);
            }
            customerInfo.setFrequency(customerInfo.getFrequency()+1);
            customerInfo.setNum(customerInfo.getNum() + num);
            customerInfo.setModifieddate(date);
            customerInfoMapper.updateById(customerInfo);
        }
    }
}