郑永安
2023-06-19 2fcd97552d16718cc7997629fd637a73a5a4483f
src/main/java/com/gk/firework/Service/ServiceImpl/CustomerServiceImpl.java
对比新文件
@@ -0,0 +1,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);
        }
    }
}