package com.qtzl.alterSales.manager.service; import cn.com.taiji.common.manager.net.http.ServiceHandleException; import cn.hutool.core.bean.BeanUtil; import com.qtzl.alterSales.dao.entity.second.AflSignReboundInfo; import com.qtzl.alterSales.dao.repo.jpa.second.AflSignReboundInfoRepo; import com.qtzl.alterSales.manager.enums.IsDeleteEnum; import com.qtzl.alterSales.manager.enums.StatusEnum; import com.qtzl.alterSales.manager.model.protocol.UcServiceError; import com.qtzl.alterSales.manager.model.protocol.sales.*; import com.qtzl.alterSales.manager.service.third.ConstantConfig; import com.qtzl.alterSales.manager.service.third.FmsService; import com.qtzl.alterSales.manager.tools.ExcelUtils; import com.qtzl.alterSales.manager.vo.AflSignReboundInfoVo; import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.springframework.beans.BeanUtils; import org.springframework.stereotype.Service; import javax.annotation.Resource; import java.io.File; import java.util.Date; import java.util.List; @Service public class AflSignReboundInfoServiceImpl implements AflSignReboundInfoService{ @Resource AflSignReboundInfoRepo aflSignReboundInfoRepo; @Resource FmsService fmsService; @Resource ConstantConfig constantConfig; @Override public void save(AflSignReboundInfoSaveOrUpdateRequest request) throws ServiceHandleException { verify(request); AflSignReboundInfo aflSignReboundInfo = aflSignReboundInfoRepo.findByChannelCode(request.getChannelCode()); if (null != aflSignReboundInfo) { throw UcServiceError.BUSINESS_VALIDATE_ERR.toHandleException("已存在该渠道的签约回跳配置,不允许重复,请核实"); } aflSignReboundInfo = new AflSignReboundInfo(); BeanUtils.copyProperties(request, aflSignReboundInfo); aflSignReboundInfo.setStatus(StatusEnum.ENABLE.getCode()); aflSignReboundInfo.setIsDelete(IsDeleteEnum.NO.getCode()); aflSignReboundInfo.setInsertTime(new Date()); aflSignReboundInfoRepo.save(aflSignReboundInfo); } @Override public void update(AflSignReboundInfoSaveOrUpdateRequest request) throws ServiceHandleException { if (StringUtils.isEmpty(request.getUpdator())){ throw UcServiceError.BUSINESS_VALIDATE_ERR.toHandleException("修改人不能为空"); } AflSignReboundInfo aflSignReboundInfo = findById(request.getId()); if (!aflSignReboundInfo.getChannelCode().equals(request.getChannelCode())){ AflSignReboundInfo byProductCode = aflSignReboundInfoRepo.findByChannelCode(request.getChannelCode()); if (byProductCode!=null){ throw UcServiceError.BUSINESS_VALIDATE_ERR.toHandleException("已存在该渠道的签约回跳配置,不允许重复,请核实"); } } BeanUtils.copyProperties(request,aflSignReboundInfo); aflSignReboundInfo.setUpdator(request.getUpdator()); aflSignReboundInfo.setUpdatedTime(new Date()); aflSignReboundInfoRepo.save(aflSignReboundInfo); } @Override public void delete(AflSignReboundInfoDeleteRequest request) throws ServiceHandleException { if (StringUtils.isEmpty(request.getUpdator())){ throw UcServiceError.BUSINESS_VALIDATE_ERR.toHandleException("修改人不能为空"); } AflSignReboundInfo aflSignReboundInfo = findById(request.getId()); aflSignReboundInfo.setIsDelete(IsDeleteEnum.YES.getCode()); aflSignReboundInfo.setUpdator(request.getUpdator()); aflSignReboundInfo.setUpdatedTime(new Date()); aflSignReboundInfoRepo.save(aflSignReboundInfo); } @Override public void updateStatus(AflSignReboundInfoUpdateStatusRequest request) throws ServiceHandleException { AflSignReboundInfo aflSignReboundInfo = findById(request.getId()); if (aflSignReboundInfo.getStatus()==request.getStatus()||request.getStatus()==null) { throw UcServiceError.BUSINESS_VALIDATE_ERR.toHandleException("状态没有发生改变"); } aflSignReboundInfo.setStatus(request.getStatus()); aflSignReboundInfo.setUpdator(request.getUpdator()); aflSignReboundInfo.setUpdatedTime(new Date()); aflSignReboundInfoRepo.save(aflSignReboundInfo); } public AflSignReboundInfo findById(String id) throws ServiceHandleException { if (StringUtils.isEmpty(id)) { throw UcServiceError.BUSINESS_VALIDATE_ERR.toHandleException("请指定签约回跳配置编号"); } AflSignReboundInfo aflSignReboundInfo = aflSignReboundInfoRepo.findByIdAndIsDelete(id); if (null == aflSignReboundInfo) { throw UcServiceError.BUSINESS_VALIDATE_ERR.toHandleException("接入签约回跳配置不存在"); } return aflSignReboundInfo; } private void verify(AflSignReboundInfoSaveOrUpdateRequest request) throws ServiceHandleException { if (StringUtils.isEmpty(request.getChannelCode())) { throw UcServiceError.BUSINESS_VALIDATE_ERR.toHandleException("渠道编码不能为空"); } if (StringUtils.isEmpty(request.getCallbackUrl())) { throw UcServiceError.BUSINESS_VALIDATE_ERR.toHandleException("回跳地址不能为空"); } if (StringUtils.isEmpty(request.getVehicleBrand())) { throw UcServiceError.BUSINESS_VALIDATE_ERR.toHandleException("车辆品牌不能为空"); } if (StringUtils.isEmpty(request.getCreator())) { throw UcServiceError.BUSINESS_VALIDATE_ERR.toHandleException("创建人不能为空"); } } @Override public String export(AflSignReboundInfoFindPageViewRequest request, Logger logger) throws ServiceHandleException { String pathFile =null; try { AflSignReboundInfoFindPageRequest queryRequest = new AflSignReboundInfoFindPageRequest(); BeanUtil.copyProperties(request, queryRequest); List signReboundInfoVos = aflSignReboundInfoRepo.list(queryRequest); if (signReboundInfoVos==null||signReboundInfoVos.size()==0) { pathFile = ExcelUtils.export(null, "签约回跳",constantConfig.getFilePath() ,null, AflSignReboundInfoVo.class); } else { logger.info("选装-签约回跳管理导出返回参数:{}",signReboundInfoVos.toString()); pathFile = ExcelUtils.export(null, "签约回跳",constantConfig.getFilePath(), signReboundInfoVos, AflSignReboundInfoVo.class); } if (StringUtils.isEmpty(pathFile)){ throw UcServiceError.BUSINESS_VALIDATE_ERR.toHandleException("签约回跳管理导出失败"); } String s = null; try { s = fmsService.uploadFile(constantConfig.getUploadFile(),pathFile, 90000); } catch (ServiceHandleException e) { logger.error("选装-签约回跳管理导出失败:{}", e.getMessage()); throw UcServiceError.BUSINESS_VALIDATE_ERR.toHandleException(e.getMessage()); } if (StringUtils.isEmpty(s)){ throw UcServiceError.BUSINESS_VALIDATE_ERR.toHandleException("签约回跳管理导出失败"); } return s; } catch (Exception e) { if (e instanceof ServiceHandleException) { throw e; } logger.error("选装-签约回跳管理导出失败:{}", e.getMessage()); throw UcServiceError.BUSINESS_VALIDATE_ERR.toHandleException("签约回跳管理导出失败"); }finally { File file = null; try { file = new File(pathFile); } catch (Exception e) { } if (file!=null){ file.delete(); } } } }