车辆档案信息

package com.reachauto.vsp.portal.controller;

import com.reachauto.vsp.portal.bean.dto.ArchivesCarStatusDTO;
import com.reachauto.vsp.portal.bean.param.*;
import com.reachauto.vsp.portal.constant.ErrorConstants;
import com.reachauto.vsp.portal.service.ArchivesCarInformationService;
import com.reachauto.vsp.portal.util.AliyunOSSUtil;
import com.reachauto.vsp.portal.util.BeanParseException;
import com.reachauto.vsp.portal.util.ExcelUtil;
import com.reachauto.vsp.portal.util.WordUtils;
import com.reachauto.vspcloud.common.authorization.bean.GrantRoleApi;
import com.reachauto.vspcloud.common.authorization.utils.PortalTokenUtils;
import com.reachauto.vspcloud.common.response.Response;
import com.reachauto.vspcloud.common.response.ResponseHelper;
import com.reachauto.vspcloud.common.utils.StringUtils;
import com.reachauto.vspcloud.common.utils.WebUtils;
import io.swagger.annotations.ApiOperation;
import jdk.nashorn.internal.ir.CallNode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * 车辆档案信息
 */
@Slf4j
@RestController
@RequestMapping("/portal/archivesVehicleInformation")
public class ArchivesVehicleInformationController {

    @Autowired
    private ArchivesCarInformationService archivesCarInformationService;
    @Autowired
    private PortalTokenUtils portalTokenUtils;

    @ApiOperation(value = "返回车辆状态下拉列表", notes = "返回车辆状态下拉列表")
    @RequestMapping(
            value = "/getCarStatusList/",
            method = RequestMethod.POST,
            produces = {MediaType.APPLICATION_JSON_UTF8_VALUE})
    public Response getCarStatusList(Integer id) {
        //车辆状态 1 待销售 2 已销售 3 已绑定
        List<ArchivesCarStatusDTO> list = new ArrayList<>(16);
        ArchivesCarStatusDTO archivesCarStatusDTO1 = new ArchivesCarStatusDTO();
        archivesCarStatusDTO1.setValue(1);
        archivesCarStatusDTO1.setName("待销售");
        ArchivesCarStatusDTO archivesCarStatusDTO2 = new ArchivesCarStatusDTO();
        archivesCarStatusDTO2.setValue(2);
        archivesCarStatusDTO2.setName("已销售");
        ArchivesCarStatusDTO archivesCarStatusDTO3 = new ArchivesCarStatusDTO();
        archivesCarStatusDTO3.setValue(3);
        archivesCarStatusDTO3.setName("已绑定");
        list.add(archivesCarStatusDTO1);
        list.add(archivesCarStatusDTO2);
        list.add(archivesCarStatusDTO3);
        return ResponseHelper.createSuccessResponse(
                list);
    }

    @ApiOperation(value = "车辆档案条件查询", notes = "车辆档案条件查询")
    @RequestMapping(value = "/queryArchivesVehicle", method = RequestMethod.GET)
    public Response queryArchivesVehicle(ArchivesVehicleParameter parameter) {
        return ResponseHelper.createSuccessResponse(archivesCarInformationService.queryArchivesCar(parameter));
    }

    @ApiOperation(value = "车辆档案删除", notes = "车辆档案删除")
    @RequestMapping(value = "/ArchivesVehicle/{id}", method = RequestMethod.DELETE)
    public Response delArchivesVehicle(@PathVariable("id") Integer id, HttpServletRequest request) {
        log.info("车辆档案删除 id:" + id);
        if (id == null || "".equals(id)) {
            return ResponseHelper.createResponse(
                    StringUtils.toString(ErrorConstants.ERR_CODE_PARAM_IS_MISSING, ""),
                    ErrorConstants.MSG_PARAM_IS_MISSSING);
        }

        Response response = archivesCarInformationService.delArchivesVehicle(id);
        return ResponseHelper.createResponse(response.getCode(), response.getDescription());
    }

    @ApiOperation(value = "车辆档案详情查询", notes = "车辆档案详情查询")
    @RequestMapping(value = "/archivesVehicleDetail/{id}", method = RequestMethod.GET)
    public Response queryArchivesVehicleDetail(@PathVariable("id") Long id, HttpServletRequest request) {
        return ResponseHelper.createSuccessResponse(archivesCarInformationService.queryArchivesVehicleDetail(id));
    }

    /**
     * 车辆信息_导入模板
     * @param request
     * @param response
     */
    @RequestMapping(value = "/downloadCarInfoTemplate", method = RequestMethod.POST)
    public Response<Object> export(HttpServletRequest request, HttpServletResponse response) {
        String url = "";
        try {
            url = AliyunOSSUtil.downloadExcel("车辆信息_导入模板.xlsx");
        } catch (Exception e) {
            log.error("/downloadExcel/downloadExcelCarInfo", e);
        }
        return ResponseHelper.createSuccessResponse(
                url);
    }

    /**
     * 销售信息_导入模板
     * @param request
     * @param response
     */
    @RequestMapping(value = "/downloadSaleInfoTemplate", method = RequestMethod.POST)
    public Response<Object> downloadSaleInfoTemplate(HttpServletRequest request, HttpServletResponse response) {
        String url = "";
        try {
            url = AliyunOSSUtil.downloadExcel("销售信息_导入模板.xlsx");
        } catch (Exception e) {
            log.error("/downloadExcel/downloadExcelCarInfo", e);
        }
        return ResponseHelper.createSuccessResponse(
                url);
    }

    /**
     * 车辆信息导入
     * @param file
     * @return
     * @throws IOException
     */
    @ApiOperation(value = "车辆信息导入", notes = "车辆信息导入 ")
    @RequestMapping(path = "/batchImportCarInfo", method = RequestMethod.POST)
    public Response<Object> batchImportCarInfo(@RequestParam("fileUpload") MultipartFile file) throws IOException {
        Response response = new Response();
        try {
            if(file == null){
                response.setCode("000010");
                response.setDescription("上传文件为空");
                return response;
            }
            if(!file.getOriginalFilename().toLowerCase().endsWith(".xlsx")){
                response.setCode("000020");
                response.setDescription("文件格式错误,请确认");
                return response;
            }
            String[][] strings = ExcelUtil.readFromFile(file, 20, 2000);
            response = archivesCarInformationService.batchImportCarInfo(strings);
            return response;
        } catch (BeanParseException e) {
            response.setCode("000030");
            response.setDescription("文件解析失败,请确认");
            return response;
        }
    }
}

package com.reachauto.vsp.portal.service.impl;


import com.reachauto.vsp.portal.bean.param.*;
import com.reachauto.vsp.portal.bean.vo.*;
import com.reachauto.vsp.portal.constant.CarStatusEnum;
import com.reachauto.vsp.portal.mapper.*;
import com.reachauto.vsp.portal.service.ArchivesCarInformationService;
import com.reachauto.vsp.portal.service.BasicInformationEquipmentService;
import com.reachauto.vsp.portal.service.BasicVehicleInformationService;
import com.reachauto.vsp.portal.util.StrictDetectIdCard;
import com.reachauto.vsp.portal.util.UuidUtil;
import com.reachauto.vspcloud.common.response.Response;
import com.reachauto.vspcloud.common.response.ResponseHelper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class ArchivesCarInformationServiceImpl implements ArchivesCarInformationService {
    @Resource
    private ArchivesCarBaseinfoMapper archivesCarBaseinfoMapper;
    @Resource
    private ArchivesCarExtendinfoMapper archivesCarExtendinfoMapper;
    @Resource
    private ArchivesCityMapper archivesCityMapper;
    @Resource
    private ArchivesSaleInfoMapper archivesSaleInfoMapper;
    @Resource
    private ArchivesUserMapper archivesUserMapper;
    @Autowired
    private VehicleModelQueryMapper vehicleModelQueryMapper;
    @Autowired
    private BasicInformationEquipmentService basicInformationEquipmentService;
    @Resource
    private ArchivesCarExtendinfoImportMapper archivesCarExtendinfoImportMapper;
    @Autowired
    private ArchivesSaleInfoImportMapper archivesSaleInfoImportMapper;


    /**
     * 车辆档案条件查询
     * @param parameter
     * @return
     */
    @Override
    public PageArchivesCarVO queryArchivesCar(ArchivesVehicleParameter parameter) {
        if(parameter.getPageNum() == null){
            parameter.setPageNum(1);
        }
        if(parameter.getPageSize() == null){
            parameter.setPageSize(20);
        }
        PageArchivesCarVO pageArchivesCarVO = new PageArchivesCarVO();
        int start = (parameter.getPageNum()*parameter.getPageSize()-20);
        int end = 20;
        parameter.setStartData(start);
        parameter.setEndData(end);
        List<ArchivesCarBaseinfo> list = archivesCarBaseinfoMapper.queryArchivesCar(parameter);
        pageArchivesCarVO.setRecords(list);
        pageArchivesCarVO.setTotal(archivesCarBaseinfoMapper.countArchivesCarAll(parameter));
        return pageArchivesCarVO;
    }

    @Override
    public Response delArchivesVehicle(Integer id) {
        Response response = new Response();
        ArchivesCarBaseinfo archivesCarBaseinfo = archivesCarBaseinfoMapper.selectByPrimaryKey(Long.valueOf(id));
        if(archivesCarBaseinfo == null){
            response.setCode("000030");
            response.setDescription("数据不存在");
            return response;
        }
        if(CarStatusEnum.BIND.getValue().equals(archivesCarBaseinfo.getCarStatus())){
            response.setCode("000040");
            response.setDescription("该车辆已被用户绑定,无法删除");
            return response;
        }
        int delete = archivesCarBaseinfoMapper.delArchivesVehicleById(id);
        if(delete == 1){
            response.setCode("000000");
            response.setDescription("删除成功");
            return response;
        }else {
            response.setCode("000020");
            response.setDescription("删除失败");
            return response;
        }
    }

    /**
     * 查询车辆档案详情
     * @param id
     * @return
     */
    @Override
    public ArchivesVehicleDetailVo queryArchivesVehicleDetail(Long id) {
        ArchivesCarBaseinfo archivesCarBaseinfo = archivesCarBaseinfoMapper.selectByPrimaryKey(id);
        ArchivesCarExtendinfo archivesCarExtendinfo = archivesCarExtendinfoMapper.selectByVin(archivesCarBaseinfo.getVin());
        ArchivesSaleInfo archivesSaleInfo = archivesSaleInfoMapper.selectByVin(archivesCarBaseinfo.getVin());
        if(archivesSaleInfo != null){
            ArchivesCity archivesCity = archivesCityMapper.selectByCityCode(archivesSaleInfo.getCityCode());
            if(archivesCity != null){
                archivesSaleInfo.setCityName(archivesCity.getCityName()+archivesCity.getDistrictName());
            }
        }
        List<ArchivesUser> archivesUsers = archivesUserMapper.selectByVin(archivesCarBaseinfo.getVin());
        List<ServiceInfoVO> serviceInfoVOList = new ArrayList<>();
        buildServiceInfoVOList(serviceInfoVOList,archivesSaleInfo);

        ArchivesVehicleDetailVo archivesVehicleDetailVo = new ArchivesVehicleDetailVo();
        archivesVehicleDetailVo.setArchivesCarBaseinfo(archivesCarBaseinfo);
        archivesVehicleDetailVo.setArchivesCarExtendinfo(archivesCarExtendinfo);
        archivesVehicleDetailVo.setArchivesSaleInfo(archivesSaleInfo);
        archivesVehicleDetailVo.setArchivesUsers(archivesUsers);
        archivesVehicleDetailVo.setServiceInfoVOList(serviceInfoVOList);

        return archivesVehicleDetailVo;
    }

    /**
     * 导入车辆信息
     * @param strings
     */
    @Override
    @Transactional
    public Response batchImportCarInfo(String[][] strings) {
        String batchImportNumber = UuidUtil.uuid();
        List<ArchivesCarExtendinfoImport> failList = new ArrayList<>();
        List<ArchivesCarBaseinfo> successList = new ArrayList<>();
        List<ArchivesCarExtendinfo> successList2 = new ArrayList<>();
        List<String> vinList = new ArrayList<>();
        Response response = new Response();
        boolean flag = false;
        for (int r = 2; r < strings.length; r++) {
            String vin = strings[r][1];
            if(!StringUtils.isBlank(vin)){
                flag = true;
            }
            vinList.add(vin);
        }
        if(!flag){
            response.setCode("000040");
            response.setDescription("文件不包含车辆信息,请确认");
            return response;
        }
        if(strings.length > 1000){
            response.setCode("000050");
            response.setDescription("一次最多支持上传1000条,请确认");
            return response;
        }
        for (int r = 2; r < strings.length; r++) {
            String deriveCode = strings[r][0];
            String vin = strings[r][1];
            String color = strings[r][2];
            String tsuDeviceModel = strings[r][3];
            String tsuPartModel = strings[r][4];
            String tsuSerialNumber = strings[r][5];
            String iccid = strings[r][6];
            String sim = strings[r][7];
            String tsuHardVersion = strings[r][8];
            String tsuSoftVersion = strings[r][9];
            String daDeviceModel = strings[r][10];
            String daPartModel = strings[r][11];
            String daSerialModel = strings[r][12];
            String daHardVersion = strings[r][13];
            String daSoftVersion = strings[r][14];

            //通过设备id查询车辆型号id和车辆品牌id
            VehicleModelVO vehicleModelVO = vehicleModelQueryMapper.queryVehicleModelByDeriveCode(deriveCode);
            // 组装车辆基础信息对象
            ArchivesCarBaseinfo archivesCarBaseinfo = new ArchivesCarBaseinfo();
            if(vehicleModelVO != null){
                archivesCarBaseinfo.setBrandCode(String.valueOf(vehicleModelVO.getBrandId()));
                archivesCarBaseinfo.setModelCode(String.valueOf(vehicleModelVO.getModelParentId()));
                archivesCarBaseinfo.setDeriveCode(String.valueOf(vehicleModelVO.getId()));
            }
            archivesCarBaseinfo.setBrandName("");
            archivesCarBaseinfo.setModelName("");
            archivesCarBaseinfo.setDeriveName("");
            archivesCarBaseinfo.setVin(vin);
            archivesCarBaseinfo.setCarStatus(1);
            archivesCarBaseinfo.setPlate("");
            archivesCarBaseinfo.setPhone("");
            archivesCarBaseinfo.setCreater("");
            //取当前时间
            Date nowdate=new Date();
            //转换时间格式
            SimpleDateFormat simpleDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            archivesCarBaseinfo.setCreateTime(Timestamp.valueOf(simpleDate.format(nowdate)));
            archivesCarBaseinfo.setUpdater("");
            archivesCarBaseinfo.setUpdateTime(String.valueOf(Timestamp.valueOf(simpleDate.format(nowdate))));
            archivesCarBaseinfo.setIsDeleted(0);
            //组装车辆扩展信息对象
            ArchivesCarExtendinfo archivesCarExtendinfo = new ArchivesCarExtendinfo();
            archivesCarExtendinfo.setDeriveCode(deriveCode);
            archivesCarExtendinfo.setVin(vin);
            archivesCarExtendinfo.setColor(color);
            archivesCarExtendinfo.setTsuDeviceModel(tsuDeviceModel);
            archivesCarExtendinfo.setTsuPartModel(tsuPartModel);
            archivesCarExtendinfo.setTsuSerialNumber(tsuSerialNumber);
            archivesCarExtendinfo.setIccid(iccid);
            archivesCarExtendinfo.setSim(sim);
            archivesCarExtendinfo.setTsuHardVersion(tsuHardVersion);
            archivesCarExtendinfo.setTsuSoftVersion(tsuSoftVersion);
            archivesCarExtendinfo.setDaDeviceModel(daDeviceModel);
            archivesCarExtendinfo.setDaPartModel(daPartModel);
            archivesCarExtendinfo.setDaSerialModel(daSerialModel);
            archivesCarExtendinfo.setDaHardVersion(daHardVersion);
            archivesCarExtendinfo.setDaSoftVersion(daSoftVersion);
            archivesCarExtendinfo.setCreater("");
            archivesCarExtendinfo.setCreateTime(Timestamp.valueOf(simpleDate.format(nowdate)));
            archivesCarExtendinfo.setUpdater("");
            archivesCarExtendinfo.setUpdateTime(Timestamp.valueOf(simpleDate.format(nowdate)));
            archivesCarExtendinfo.setIsDeleted(0);
            //组装车辆中间表信息对象
            ArchivesCarExtendinfoImport archivesCarExtendinfoImport = new ArchivesCarExtendinfoImport();
            archivesCarExtendinfoImport.setDeriveCode(deriveCode);
            archivesCarExtendinfoImport.setVin(vin);
            archivesCarExtendinfoImport.setColor(color);
            archivesCarExtendinfoImport.setTsuDeviceModel(tsuDeviceModel);
            archivesCarExtendinfoImport.setTsuPartModel(tsuPartModel);
            archivesCarExtendinfoImport.setTsuSerialNumber(tsuSerialNumber);
            archivesCarExtendinfoImport.setIccid(iccid);
            archivesCarExtendinfoImport.setSim(sim);
            archivesCarExtendinfoImport.setTsuHardVersion(tsuHardVersion);
            archivesCarExtendinfoImport.setTsuSoftVersion(tsuSoftVersion);
            archivesCarExtendinfoImport.setDaDeviceModel(daDeviceModel);
            archivesCarExtendinfoImport.setDaPartModel(daPartModel);
            archivesCarExtendinfoImport.setDaSerialModel(daSerialModel);
            archivesCarExtendinfoImport.setDaHardVersion(daHardVersion);
            archivesCarExtendinfoImport.setDaSoftVersion(daSoftVersion);
            archivesCarExtendinfoImport.setBatchNumber(batchImportNumber);
            archivesCarExtendinfoImport.setImportFailReason("");
            archivesCarExtendinfoImport.setCreater("");
            archivesCarExtendinfoImport.setCreateTime(Timestamp.valueOf(simpleDate.format(nowdate)));
            archivesCarExtendinfoImport.setUpdater("");
            archivesCarExtendinfoImport.setUpdateTime(Timestamp.valueOf(simpleDate.format(nowdate)));
            archivesCarExtendinfoImport.setIsDeleted(0);

            //校验数据,有问题的放到这个failList集合中,没问题的放到这个successList集合中
            //当未填写必填项时,显示“未填写必填信息”
            if (StringUtils.isBlank(archivesCarExtendinfoImport.getVin()) || StringUtils.isBlank(archivesCarExtendinfoImport.getTsuSerialNumber())
                    || StringUtils.isBlank(archivesCarExtendinfoImport.getTsuDeviceModel()) || StringUtils.isBlank(archivesCarExtendinfoImport.getTsuPartModel())
                    || StringUtils.isBlank(archivesCarExtendinfoImport.getDaDeviceModel()) || StringUtils.isBlank(archivesCarExtendinfoImport.getDaPartModel())
                    || StringUtils.isBlank(archivesCarExtendinfoImport.getDaSerialModel())) {
                archivesCarExtendinfoImport.setImportFailReason("未填写必填信息");
                failList.add(archivesCarExtendinfoImport);
                continue;
            }
            //当数据大于等于255字符时,显示“包含超过长度的数据”
            if(archivesCarExtendinfoImport.getDeriveCode().length()>255||
                    archivesCarExtendinfoImport.getVin().length()>255 ||
                    archivesCarExtendinfoImport.getColor().length()>255 ||
                    archivesCarExtendinfoImport.getTsuDeviceModel().length()>255 ||
                    archivesCarExtendinfoImport.getTsuPartModel().length()>255 ||
                    archivesCarExtendinfoImport.getTsuSerialNumber().length()>255 ||
                    archivesCarExtendinfoImport.getIccid().length()>255 ||
                    archivesCarExtendinfoImport.getSim().length()>255 ||
                    archivesCarExtendinfoImport.getTsuHardVersion().length()>255 ||
                    archivesCarExtendinfoImport.getTsuSoftVersion().length()>255 ||
                    archivesCarExtendinfoImport.getDaDeviceModel().length()>255 ||
                    archivesCarExtendinfoImport.getDaPartModel().length()>255 ||
                    archivesCarExtendinfoImport.getDaSerialModel().length()>255 ||
                    archivesCarExtendinfoImport.getDaHardVersion().length()>255 ||
                    archivesCarExtendinfoImport.getDaSoftVersion().length()>255){
                archivesCarExtendinfoImport.setImportFailReason("包含超过长度的数据");
                failList.add(archivesCarExtendinfoImport);
                continue;
            }
            //当填写的派生编码在系统中不存在时,显示”包含系统不存在的派生编码“
            VehicleModelParameter vehicleModelParameter = new VehicleModelParameter();
            vehicleModelParameter.setModelCode(archivesCarExtendinfoImport.getDeriveCode());
            List<VehicleModelVO> vehicleModelVOS = vehicleModelQueryMapper.queryVehicleModel(vehicleModelParameter);
            if(vehicleModelVOS == null || vehicleModelVOS.size() != 1){
                archivesCarExtendinfoImport.setImportFailReason("包含系统不存在的派生编码");
                failList.add(archivesCarExtendinfoImport);
                continue;
            }
            //当填写的设备型号在系统中不存在时,显示”包含系统不存在的设备型号“
            boolean tsuDeviceModelExist = basicInformationEquipmentService.deviceModelDoesExist(archivesCarExtendinfoImport.getTsuDeviceModel());
            boolean daDeviceModelExist = basicInformationEquipmentService.deviceModelDoesExist(archivesCarExtendinfoImport.getDaDeviceModel());
            if(!tsuDeviceModelExist){
                archivesCarExtendinfoImport.setImportFailReason("包含系统不存在的设备型号");
                failList.add(archivesCarExtendinfoImport);
                continue;
            }
            if(!daDeviceModelExist){
                archivesCarExtendinfoImport.setImportFailReason("包含系统不存在的设备型号");
                failList.add(archivesCarExtendinfoImport);
                continue;
            }
            //当VIN码不是17位字母数字组合时,显示“包含不合法的VIN信息”
            String reg="^[a-zA-Z0-9]{17}$";
            boolean checkVin = archivesCarExtendinfoImport.getVin().matches(reg);
            if(!checkVin){
                archivesCarExtendinfoImport.setImportFailReason("包含不合法的VIN信息");
                failList.add(archivesCarExtendinfoImport);
                continue;
            }
            //当列表里出现重复VIN时,所有重复行都显示“包含重复的VIN信息”
            int count = 0;
            for (String vinIn : vinList) {
                if(archivesCarExtendinfoImport.getVin().equals(vinIn)){
                    count++;
                }
            }
            if (count > 1) {
                archivesCarExtendinfoImport.setImportFailReason("包含重复的VIN信息");
                failList.add(archivesCarExtendinfoImport);
                continue;
            }
            //当ICCID非20位时,显示“包含不合法的ICCID”
            if(StringUtils.isNotBlank(archivesCarExtendinfoImport.getIccid())){
                if(archivesCarExtendinfoImport.getIccid().length() != 20){
                    archivesCarExtendinfoImport.setImportFailReason("包含不合法的ICCID");
                    failList.add(archivesCarExtendinfoImport);
                    continue;
                }
            }
            //当SIM卡号非13位时,显示“包含不合法的SIM卡号”
            if(StringUtils.isNotBlank(archivesCarExtendinfoImport.getSim())){
                if(archivesCarExtendinfoImport.getSim().length() != 13){
                    archivesCarExtendinfoImport.setImportFailReason("包含不合法的SIM卡号");
                    failList.add(archivesCarExtendinfoImport);
                    continue;
                }
            }
            //当上传软件版本信息不符合规定格式时,显示“包含不合法的软件版本”  (.*)(\.(0[0,1]))(\.([0-9A-HJ-NP-Z]{1}))([0-9A-F]{2})(\.(\d{3}))(\.(.*))
            if(StringUtils.isNotBlank(archivesCarExtendinfoImport.getTsuSoftVersion())){
                String regTsuSoftVersion="(.*)(\\.(0[0,1]))(\\.([0-9A-HJ-NP-Z]{1}))([0-9A-F]{2})(\\.(\\d{3}))(\\.(.*))";
                boolean checkTsuSoftVersion = archivesCarExtendinfoImport.getTsuSoftVersion().matches(regTsuSoftVersion);
                if (!checkTsuSoftVersion){
                    archivesCarExtendinfoImport.setImportFailReason("包含不合法的软件版本");
                    failList.add(archivesCarExtendinfoImport);
                    continue;
                }
            }
            if(StringUtils.isNotBlank(archivesCarExtendinfoImport.getDaSoftVersion())){
                String regDaSoftVersion="(.*)(\\.(0[0,1]))(\\.([0-9A-HJ-NP-Z]{1}))([0-9A-F]{2})(\\.(\\d{3}))(\\.(.*))";
                boolean checkDaSoftVersion = archivesCarExtendinfoImport.getDaSoftVersion().matches(regDaSoftVersion);
                if (!checkDaSoftVersion){
                    archivesCarExtendinfoImport.setImportFailReason("包含不合法的软件版本");
                    failList.add(archivesCarExtendinfoImport);
                    continue;
                }
            }
            successList.add(archivesCarBaseinfo);
            successList2.add(archivesCarExtendinfo);
        }
        //上传系统中已存在的数据时,将覆盖现有数据,如有未填写项,保留原有数据。
        for (ArchivesCarBaseinfo archivesCarBaseinfo : successList) {
            List<ArchivesCarBaseinfo> archivesCarBaseinfos = archivesCarBaseinfoMapper.queryArchivesCarByVin(archivesCarBaseinfo.getVin());
            if(archivesCarBaseinfos == null || archivesCarBaseinfos.size() == 0){
                // 数据库不存在,直接插入数据
                int insertArchivesCarBaseinfo = archivesCarBaseinfoMapper.insert(archivesCarBaseinfo);
                if(insertArchivesCarBaseinfo != 1){
                    throw new RuntimeException("保存车辆基础信息影响行数不唯一");
                }
            }else{
                // 更新车辆基础信息数据
                ArchivesCarBaseinfo archivesCarBaseinfoDataBase = archivesCarBaseinfos.get(0);
                archivesCarBaseinfo.setId(archivesCarBaseinfoDataBase.getId());
                int updateArchivesCarBaseinfo = archivesCarBaseinfoMapper.updateByPrimaryKey(archivesCarBaseinfo);
                if(updateArchivesCarBaseinfo != 1){
                    throw new RuntimeException("更新车辆基础信息影响行数不唯一");
                }
            }
        }
        for (ArchivesCarExtendinfo archivesCarExtendinfo : successList2) {
            ArchivesCarExtendinfo archivesCarExtendinfos = archivesCarExtendinfoMapper.selectByVin(archivesCarExtendinfo.getVin());
            if(archivesCarExtendinfos == null){
                // 数据库不存在,直接插入数据
                int insertArchivesCarExtendinfo = archivesCarExtendinfoMapper.insert(archivesCarExtendinfo);
                if(insertArchivesCarExtendinfo != 1){
                    throw new RuntimeException("保存车辆扩展信息影响行数不唯一");
                }
            }else{
                // 更新车辆扩展信息数据
                archivesCarExtendinfo.setId(archivesCarExtendinfos.getId());
                int updateArchivesCarExtendinfo = archivesCarExtendinfoMapper.updateByPrimaryKey(archivesCarExtendinfo);
                if(updateArchivesCarExtendinfo != 1){
                    throw new RuntimeException("更新车辆扩展信息影响行数不唯一");
                }
            }
        }
        for (ArchivesCarExtendinfoImport archivesCarExtendinfoImport : failList) {
            int insertArchivesCarExtendinfoImport = archivesCarExtendinfoImportMapper.insert(archivesCarExtendinfoImport);
            if(insertArchivesCarExtendinfoImport != 1){
                throw new RuntimeException("保存车辆扩展信息中间表影响行数不唯一");
            }
        }
        ArchivesCarInportResultVo archivesCarInportResultVo = new ArchivesCarInportResultVo();
        archivesCarInportResultVo.setBatchImportNumber(batchImportNumber);
        archivesCarInportResultVo.setFailSize(failList.size());
        archivesCarInportResultVo.setSucessSize(successList.size());
        return ResponseHelper.createSuccessResponse(
                archivesCarInportResultVo);
    }

    /**
     * 导入销售信息
     * @param strings
     * @return
     */
    @Override
    @Transactional
    public Response batchImportSaleInfo(String[][] strings) {
        String batchImportNumber = UuidUtil.uuid();
        List<ArchivesSaleInfoImport> failList = new ArrayList<>();
        List<ArchivesSaleInfo> successList = new ArrayList<>();
        List<String> vinList = new ArrayList<>();
        Response response = new Response();
        boolean flag = false;
        for (int r = 2; r < strings.length; r++) {
            String vin = strings[r][0];
            if(!StringUtils.isBlank(vin)){
                flag = true;
            }
            vinList.add(vin);
        }
        if(!flag){
            response.setCode("000040");
            response.setDescription("文件不包含车辆信息,请确认");
            return response;
        }
        if(strings.length > 1000){
            response.setCode("000050");
            response.setDescription("一次最多支持上传1000条,请确认");
            return response;
        }
        for (int r = 2; r < strings.length; r++) {
            String vin = strings[r][0];
            String cityCode = strings[r][1];
            String sellCode = strings[r][2];
            String sellName = strings[r][3];
            String sellAddress = strings[r][4];
            String sellPhone = strings[r][5];
            String sellFax = strings[r][6];
            String salespersonName = strings[r][7];
            String salespersonPhone = strings[r][8];
            String userType = strings[r][9];
            String userName = strings[r][10];
            String userPhone = strings[r][11];
            String userIdCardNo = strings[r][12];
            String plate = strings[r][13];
            String serviceStartTime = strings[r][14];
            String serviceEndTime = strings[r][15];
            //组装销售信息对象
            ArchivesSaleInfo archivesSaleInfo = new ArchivesSaleInfo();
            archivesSaleInfo.setVin(vin);
            archivesSaleInfo.setCityCode(cityCode);
            archivesSaleInfo.setCityName("");
            archivesSaleInfo.setSellCode(sellCode);
            archivesSaleInfo.setSellName(sellName);
            archivesSaleInfo.setSellAddress(sellAddress);
            archivesSaleInfo.setSellPhone(sellPhone);
            archivesSaleInfo.setSellFax(sellFax);
            archivesSaleInfo.setSalespersonName(salespersonName);
            archivesSaleInfo.setSalespersonPhone(salespersonPhone);
            archivesSaleInfo.setUserType(userType);
            archivesSaleInfo.setUserName(userName);
            archivesSaleInfo.setUserPhone(userPhone);
            archivesSaleInfo.setUserIdCardNo(userIdCardNo);
            archivesSaleInfo.setPlate(plate);
            archivesSaleInfo.setCreater("");
            //取当前时间
            Date nowdate=new Date();
            //转换时间格式
            SimpleDateFormat simpleDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            archivesSaleInfo.setCreateTime(Timestamp.valueOf(simpleDate.format(nowdate)));
            archivesSaleInfo.setUpdater("");
            archivesSaleInfo.setUpdateTime(Timestamp.valueOf(simpleDate.format(nowdate)));
            archivesSaleInfo.setIsDeleted(0);
            SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            try {
                if(StringUtils.isNotBlank(serviceStartTime)){
                    Date serviceStartTimeHandle = sdf2.parse(serviceStartTime);
                    archivesSaleInfo.setServiceStartTime(serviceStartTimeHandle);

                }
                if(StringUtils.isNotBlank(serviceEndTime)){
                    Date serviceEndTimeHandle = sdf2.parse(serviceEndTime);
                    archivesSaleInfo.setServiceEndTime(serviceEndTimeHandle);
                }
            } catch (ParseException e) {
                e.printStackTrace();
            }
            //组装销售信息中间表对象
            ArchivesSaleInfoImport archivesSaleInfoImport = new ArchivesSaleInfoImport();
            archivesSaleInfoImport.setVin(vin);
            archivesSaleInfoImport.setCityCode(cityCode);
            archivesSaleInfoImport.setSellCode(sellCode);
            archivesSaleInfoImport.setSellName(sellName);
            archivesSaleInfoImport.setSellAddress(sellAddress);
            archivesSaleInfoImport.setSellPhone(sellPhone);
            archivesSaleInfoImport.setSellFax(sellFax);
            archivesSaleInfoImport.setSalespersonName(salespersonName);
            archivesSaleInfoImport.setSalespersonPhone(salespersonPhone);
            archivesSaleInfoImport.setUserType(userType);
            archivesSaleInfoImport.setUserName(userName);
            archivesSaleInfoImport.setUserPhone(userPhone);
            archivesSaleInfoImport.setUserIdCardNo(userIdCardNo);
            archivesSaleInfoImport.setPlate(plate);
            try {
                if(StringUtils.isNotBlank(serviceStartTime)){
                    Date serviceStartTimeHandle = sdf2.parse(serviceStartTime);
                    archivesSaleInfoImport.setServiceStartTime(serviceStartTimeHandle);
                }
                if(StringUtils.isNotBlank(serviceEndTime)){
                    Date serviceEndTimeHandle = sdf2.parse(serviceEndTime);
                    archivesSaleInfoImport.setServiceEndTime(serviceEndTimeHandle);
                }
            } catch (ParseException e) {
                e.printStackTrace();
            }
            archivesSaleInfoImport.setBatchNumber(batchImportNumber);
            archivesSaleInfoImport.setImportFailReason("");
            archivesSaleInfoImport.setCreater("");
            archivesSaleInfoImport.setCreateTime(Timestamp.valueOf(simpleDate.format(nowdate)));
            archivesSaleInfoImport.setUpdater("");
            archivesSaleInfoImport.setUpdateTime(Timestamp.valueOf(simpleDate.format(nowdate)));
            archivesSaleInfoImport.setIsDeleted(0);
            //校验数据,有问题的放到这个failList集合中,没问题的放到这个successList集合中
            //当未填写必填项时,显示“未填写必填信息”
            if (StringUtils.isBlank(archivesSaleInfoImport.getVin()) || StringUtils.isBlank(archivesSaleInfoImport.getCityCode())
                    || StringUtils.isBlank(archivesSaleInfoImport.getUserType()) || StringUtils.isBlank(archivesSaleInfoImport.getUserName())
                    || StringUtils.isBlank(archivesSaleInfoImport.getUserPhone())) {
                archivesSaleInfoImport.setImportFailReason("未填写必填信息");
                failList.add(archivesSaleInfoImport);
                continue;
            }
            if(archivesSaleInfoImport.getVin().length()>255 ||
                    archivesSaleInfoImport.getCityCode().length()>255 ||
                    archivesSaleInfoImport.getSellCode().length()>255 ||
                    archivesSaleInfoImport.getSellName().length()>255 ||
                    archivesSaleInfoImport.getSellAddress().length()>255 ||
                    archivesSaleInfoImport.getSellPhone().length()>255 ||
                    archivesSaleInfoImport.getSellFax().length()>255 ||
                    archivesSaleInfoImport.getSalespersonName().length()>255 ||
                    archivesSaleInfoImport.getSalespersonPhone().length()>255 ||
                    archivesSaleInfoImport.getUserType().length()>255 ||
                    archivesSaleInfoImport.getUserName().length()>255 ||
                    archivesSaleInfoImport.getUserPhone().length()>255 ||
                    archivesSaleInfoImport.getUserIdCardNo().length()>255 ||
                    archivesSaleInfoImport.getPlate().length()>255){
                archivesSaleInfoImport.setImportFailReason("包含超过长度的数据");
                failList.add(archivesSaleInfoImport);
                continue;
            }
            //当VIN码不是17位字母数字组合时,显示“包含不合法的VIN信息”
            String reg="^[a-zA-Z0-9]{17}$";
            boolean checkVin = archivesSaleInfoImport.getVin().matches(reg);
            if(!checkVin){
                archivesSaleInfoImport.setImportFailReason("包含不合法的VIN信息");
                failList.add(archivesSaleInfoImport);
                continue;
            }
            //当包含系统中不存在的VIN时,显示“包含系统不存在的VIN信息”
            List<ArchivesCarBaseinfo> archivesCarBaseinfos = archivesCarBaseinfoMapper.queryArchivesCarByVin(archivesSaleInfoImport.getVin());
            if(archivesCarBaseinfos == null || archivesCarBaseinfos.size() == 0){
                archivesSaleInfoImport.setImportFailReason("包含系统不存在的VIN信息");
                failList.add(archivesSaleInfoImport);
                continue;
            }
            //当列表里出现重复VIN时,所有重复行都显示“包含重复的VIN信息”
            int count = 0;
            for (String vinIn : vinList) {
                if(archivesSaleInfoImport.getVin().equals(vinIn)){
                    count++;
                }
            }
            if (count > 1) {
                archivesSaleInfoImport.setImportFailReason("包含重复的VIN信息");
                failList.add(archivesSaleInfoImport);
                continue;
            }
            //当销售城市编码不合法时,显示“包含不合法的销售城市编码”
            ArchivesCity archivesCity = archivesCityMapper.selectByCityCode(archivesSaleInfoImport.getCityCode());
            if(archivesCity == null){
                archivesSaleInfoImport.setImportFailReason("包含不合法的销售城市编码");
                failList.add(archivesSaleInfoImport);
                continue;
            }
            //当用户手机号不合法时,显示“包含不合法的用户手机号”
            String phoneReg="^1[3456789]\\d{9}$";
            boolean checkPhone = archivesSaleInfoImport.getVin().matches(reg);
            if(!checkPhone){
                archivesSaleInfoImport.setImportFailReason("包含不合法的用户手机号");
                failList.add(archivesSaleInfoImport);
                continue;
            }
            //当已绑定车辆的用户手机号与系统最新一条用户手机号不同时,显示“与已绑定用户手机号不匹配”
            ArchivesUser archivesUser = archivesUserMapper.queryLatestUserInfoByVin(archivesSaleInfoImport.getVin());
            if(!archivesSaleInfoImport.getUserPhone().equals(archivesUser.getPhone())){
                archivesSaleInfoImport.setImportFailReason("与已绑定用户手机号不匹配");
                failList.add(archivesSaleInfoImport);
                continue;
            }
            //选填项不为空时校验case如下:

            //当用户身份证号不合法时,显示“包含不合法的用户身份证号信息”
            if (!StrictDetectIdCard.isValidIdNo(archivesSaleInfoImport.getUserIdCardNo())) {
                archivesSaleInfoImport.setImportFailReason("包含不合法的用户身份证号信息");
                failList.add(archivesSaleInfoImport);
                continue;
            }
            //当只填写一个服务时间时,显示“包含不完整的服务期限”
            if(archivesSaleInfoImport.getServiceStartTime() != null){
                if(archivesSaleInfoImport.getServiceEndTime() == null){
                    archivesSaleInfoImport.setImportFailReason("包含不完整的服务期限");
                    failList.add(archivesSaleInfoImport);
                    continue;
                }
            }
            if(archivesSaleInfoImport.getServiceStartTime() == null){
                if(archivesSaleInfoImport.getServiceEndTime() != null){
                    archivesSaleInfoImport.setImportFailReason("包含不完整的服务期限");
                    failList.add(archivesSaleInfoImport);
                    continue;
                }
            }
            //当服务到期时间早于开始时间时,显示“包含不合法的服务期限”
            if(archivesSaleInfoImport.getServiceStartTime() != null && archivesSaleInfoImport.getServiceEndTime() != null){
                if(archivesSaleInfoImport.getServiceEndTime().before(archivesSaleInfoImport.getServiceStartTime())){
                    archivesSaleInfoImport.setImportFailReason("包含不合法的服务期限");
                    failList.add(archivesSaleInfoImport);
                    continue;
                }
            }
            successList.add(archivesSaleInfo);
        }
        // 入库成功列表
        for (ArchivesSaleInfo archivesSaleInfo : successList) {
            ArchivesSaleInfo archivesSaleInfoDataBase = archivesSaleInfoMapper.selectByVin(archivesSaleInfo.getVin());
            if(archivesSaleInfoDataBase == null){
                int insertArchivesSaleInfo = archivesSaleInfoMapper.insert(archivesSaleInfo);
                if(insertArchivesSaleInfo != 1){
                    throw new RuntimeException("保存销售信息影响行数不唯一");
                }
                //更新车辆状态
                int updateArchivesCarBaseinfo = archivesCarBaseinfoMapper.updateByVinSelective(archivesSaleInfo.getVin(), 2);
                if(updateArchivesCarBaseinfo != 1){
                    throw new RuntimeException("更新车辆状态行数不唯一");
                }
            }else{
                archivesSaleInfo.setId(archivesSaleInfoDataBase.getId());
                int insertArchivesSaleInfo = archivesSaleInfoMapper.updateByPrimaryKey(archivesSaleInfo);
                if(insertArchivesSaleInfo != 1){
                    throw new RuntimeException("更新销售信息影响行数不唯一");
                }
                int updateArchivesCarBaseinfo = archivesCarBaseinfoMapper.updateByVinSelective(archivesSaleInfo.getVin(), 2);
                if(updateArchivesCarBaseinfo != 1){
                    throw new RuntimeException("更新车辆状态行数不唯一");
                }
            }
        }
        // 入库失败列表
        for (ArchivesSaleInfoImport archivesSaleInfoImport : failList) {
            int insertArchivesSaleInfoImport = archivesSaleInfoImportMapper.insert(archivesSaleInfoImport);
            if(insertArchivesSaleInfoImport != 1){
                throw new RuntimeException("保存销售信息中间表影响行数不唯一");
            }
        }

        ArchivesCarInportResultVo archivesCarInportResultVo = new ArchivesCarInportResultVo();
        archivesCarInportResultVo.setBatchImportNumber(batchImportNumber);
        archivesCarInportResultVo.setFailSize(failList.size());
        archivesCarInportResultVo.setSucessSize(successList.size());
        return ResponseHelper.createSuccessResponse(
                archivesCarInportResultVo);
    }

    private void buildServiceInfoVOList(List<ServiceInfoVO> serviceInfoVOList, ArchivesSaleInfo archivesSaleInfo) {
        if(archivesSaleInfo != null){
            ServiceInfoVO serviceInfoVO1 = new ServiceInfoVO();
            serviceInfoVO1.setServiceEndTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(archivesSaleInfo.getServiceEndTime()));
            serviceInfoVO1.setServiceStartTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(archivesSaleInfo.getServiceStartTime()));
            serviceInfoVO1.setName("天猫精灵");
            serviceInfoVO1.setServiceStatus(compareTime(archivesSaleInfo.getServiceStartTime(), archivesSaleInfo.getServiceEndTime()));

            ServiceInfoVO serviceInfoVO2 = new ServiceInfoVO();
            serviceInfoVO2.setServiceEndTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(archivesSaleInfo.getServiceEndTime()));
            serviceInfoVO2.setServiceStartTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(archivesSaleInfo.getServiceStartTime()));
            serviceInfoVO2.setName("蓝牙钥匙");
            serviceInfoVO2.setServiceStatus(compareTime(archivesSaleInfo.getServiceStartTime(), archivesSaleInfo.getServiceEndTime()));
            serviceInfoVOList.add(serviceInfoVO1);
            serviceInfoVOList.add(serviceInfoVO2);
        }

    }


    private Integer compareTime(Date startTimeStr, Date endTimeStr) {
        //1未开通/2正常/3已过期
        Date date = new Date();
        if(date.before(startTimeStr)){
            return 1;
        }
        if(date.after(startTimeStr) && date.before(endTimeStr)){
            return 2;
        }
        if(date.after(endTimeStr)){
            return 3;
        }
        return 0;
    }
}


你可能感兴趣的:(java,mysql,java)