package com.doumee.service.business.impl;
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
import com.doumee.core.annotation.excel.ExcelImporter;
|
import com.doumee.core.constants.ResponseStatus;
|
import com.doumee.core.exception.BusinessException;
|
import com.doumee.core.utils.Constants;
|
import com.doumee.core.utils.ScientificNotationTUtil;
|
import com.doumee.dao.admin.request.CarsImport;
|
import com.doumee.dao.admin.request.JkLineImport;
|
import com.doumee.dao.business.CarsMapper;
|
import com.doumee.dao.business.CategoryMapper;
|
import com.doumee.dao.business.model.*;
|
import com.doumee.service.business.third.model.LoginUserInfo;
|
import com.doumee.service.business.third.model.PageData;
|
import com.doumee.service.business.third.model.PageWrap;
|
import com.doumee.core.utils.Utils;
|
import com.doumee.dao.business.JkLineMapper;
|
import com.doumee.service.business.JkLineService;
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
|
import com.baomidou.mybatisplus.core.metadata.IPage;
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
import com.github.yulichang.wrapper.MPJLambdaWrapper;
|
import org.apache.commons.lang3.StringUtils;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.data.redis.core.RedisTemplate;
|
import org.springframework.stereotype.Service;
|
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.util.CollectionUtils;
|
import org.springframework.web.multipart.MultipartFile;
|
|
import java.math.BigDecimal;
|
import java.util.ArrayList;
|
import java.util.Date;
|
import java.util.List;
|
|
/**
|
* 交控-线路信息表Service实现
|
* @author 江蹄蹄
|
* @date 2025/09/28 09:01
|
*/
|
@Service
|
public class JkLineServiceImpl implements JkLineService {
|
|
@Autowired
|
private RedisTemplate<String, Object> redisTemplate;
|
@Autowired
|
private JkLineMapper jkLineMapper;
|
@Autowired
|
private CategoryMapper categoryMapper;
|
@Autowired
|
private CarsMapper carsMapper;
|
|
@Override
|
public Integer create(JkLine jkLine) {
|
if(jkLineMapper.selectCount(new QueryWrapper<JkLine>().lambda()
|
.eq(JkLine::getIsdeleted,Constants.ZERO)
|
.eq(JkLine::getName,jkLine.getName()) )>0){
|
throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"对不起,线路名称已存在,请返回刷新页面查看!");
|
}
|
if(categoryMapper.selectOne(new QueryWrapper<Category>().lambda()
|
.eq(Category::getId,jkLine.getCategoryId())
|
.eq(Category::getType,Constants.FOUR)
|
.eq(Category::getIsdeleted,Constants.ZERO)) ==null){
|
throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"对不起,所属主线路不存在,请返回刷新页面查看!");
|
}
|
jkLine.setStatus(Constants.formatIntegerNum(jkLine.getStatus()));
|
jkLine.setIsdeleted(Constants.ZERO);
|
jkLine.setCreator(jkLine.getLoginUserInfo().getId());
|
jkLine.setCreateDate(new Date());
|
jkLine.setEditDate(jkLine.getCreateDate());
|
jkLine.setEditor(jkLine.getCreator());
|
jkLineMapper.insert(jkLine);
|
return jkLine.getId();
|
}
|
|
@Override
|
public void deleteById(Integer id, LoginUserInfo user) {
|
jkLineMapper.update(null,new UpdateWrapper<JkLine>().lambda()
|
.set(JkLine::getIsdeleted,Constants.ONE)
|
.set(JkLine::getEditor,user.getId())
|
.set(JkLine::getEditDate,new Date())
|
.eq(JkLine::getId,id)
|
);
|
}
|
|
@Override
|
public void delete(JkLine jkLine) {
|
UpdateWrapper<JkLine> deleteWrapper = new UpdateWrapper<>(jkLine);
|
jkLineMapper.delete(deleteWrapper);
|
}
|
|
@Override
|
public void deleteByIdInBatch(List<Integer> ids, LoginUserInfo user) {
|
if (CollectionUtils.isEmpty(ids)) {
|
return;
|
}
|
jkLineMapper.update(null,new UpdateWrapper<JkLine>().lambda()
|
.set(JkLine::getIsdeleted,Constants.ONE)
|
.set(JkLine::getEditor,user.getId())
|
.set(JkLine::getEditDate,new Date())
|
.in(JkLine::getId,ids)
|
);
|
}
|
|
@Override
|
public void updateById(JkLine jkLine) {
|
if(jkLineMapper.selectCount(new QueryWrapper<JkLine>().lambda()
|
.ne(JkLine::getId,jkLine.getId())
|
.eq(JkLine::getIsdeleted,Constants.ZERO)
|
.eq(JkLine::getName,jkLine.getName()) )>0){
|
throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"对不起,线路名称已存在,请返回刷新页面查看!");
|
}
|
if(categoryMapper.selectOne(new QueryWrapper<Category>().lambda()
|
.eq(Category::getId,jkLine.getCategoryId())
|
.eq(Category::getType,Constants.FOUR)
|
.eq(Category::getIsdeleted,Constants.ZERO)) ==null){
|
throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"对不起,所属主线路不存在,请返回刷新页面查看!");
|
}
|
jkLine.setEditor(jkLine.getLoginUserInfo().getId());
|
jkLine.setEditDate(new Date());
|
jkLineMapper.updateById(jkLine);
|
}
|
|
@Override
|
public void updateByIdInBatch(List<JkLine> jkLines) {
|
if (CollectionUtils.isEmpty(jkLines)) {
|
return;
|
}
|
for (JkLine jkLine: jkLines) {
|
this.updateById(jkLine);
|
}
|
}
|
|
@Override
|
public JkLine findById(Integer id) {
|
return jkLineMapper.selectById(id);
|
}
|
|
@Override
|
public JkLine findOne(JkLine jkLine) {
|
QueryWrapper<JkLine> wrapper = new QueryWrapper<>(jkLine);
|
return jkLineMapper.selectOne(wrapper);
|
}
|
|
@Override
|
public List<JkLine> findList(JkLine jkLine) {
|
jkLine.setIsdeleted(Constants.ZERO);
|
QueryWrapper<JkLine> wrapper = new QueryWrapper<>(jkLine);
|
wrapper.lambda().orderByAsc(JkLine::getCode);
|
return jkLineMapper.selectList(wrapper);
|
}
|
|
@Override
|
public PageData<JkLine> findPage(PageWrap<JkLine> pageWrap) {
|
IPage<JkLine> page = new Page<>(pageWrap.getPage(), pageWrap.getCapacity());
|
MPJLambdaWrapper<JkLine> queryWrapper = new MPJLambdaWrapper<>();
|
Utils.MP.blankToNull(pageWrap.getModel());
|
pageWrap.getModel().setIsdeleted(Constants.ZERO);
|
queryWrapper.selectAll(JkLine.class )
|
.selectAs(Cars::getCode,JkLine::getCarCode)
|
.selectAs(Category::getName,JkLine::getCategoryName)
|
.leftJoin(Category.class,Category::getId,JkLine::getCategoryId )
|
.leftJoin(Cars.class,Cars::getId,JkLine::getCarId );
|
queryWrapper.like(StringUtils.isNotBlank(pageWrap.getModel().getCarCode()),Cars::getCode, pageWrap.getModel().getCarCode());
|
queryWrapper.like(StringUtils.isNotBlank(pageWrap.getModel().getCategoryName()),Category::getName, pageWrap.getModel().getCategoryName());
|
queryWrapper.like(StringUtils.isNotBlank(pageWrap.getModel().getCode()),JkLine::getCode, pageWrap.getModel().getCode());
|
queryWrapper.eq(StringUtils.isNotBlank(pageWrap.getModel().getWeeks()),JkLine::getWeeks, pageWrap.getModel().getWeeks());
|
|
if (pageWrap.getModel().getId() != null) {
|
queryWrapper.eq(JkLine::getId, pageWrap.getModel().getId());
|
}
|
if (pageWrap.getModel().getCreator() != null) {
|
queryWrapper.eq(JkLine::getCreator, pageWrap.getModel().getCreator());
|
}
|
if (pageWrap.getModel().getCreateDate() != null) {
|
queryWrapper.ge(JkLine::getCreateDate, Utils.Date.getStart(pageWrap.getModel().getCreateDate()));
|
queryWrapper.le(JkLine::getCreateDate, Utils.Date.getEnd(pageWrap.getModel().getCreateDate()));
|
}
|
if (pageWrap.getModel().getEditor() != null) {
|
queryWrapper.eq(JkLine::getEditor, pageWrap.getModel().getEditor());
|
}
|
if (pageWrap.getModel().getEditDate() != null) {
|
queryWrapper.ge(JkLine::getEditDate, Utils.Date.getStart(pageWrap.getModel().getEditDate()));
|
queryWrapper.le(JkLine::getEditDate, Utils.Date.getEnd(pageWrap.getModel().getEditDate()));
|
}
|
if (pageWrap.getModel().getIsdeleted() != null) {
|
queryWrapper.eq(JkLine::getIsdeleted, pageWrap.getModel().getIsdeleted());
|
}
|
if (pageWrap.getModel().getInfo() != null) {
|
queryWrapper.eq(JkLine::getInfo, pageWrap.getModel().getInfo());
|
}
|
if (pageWrap.getModel().getName() != null) {
|
queryWrapper.like(JkLine::getName, pageWrap.getModel().getName());
|
}
|
if (pageWrap.getModel().getCategoryId() != null) {
|
queryWrapper.eq(JkLine::getCategoryId, pageWrap.getModel().getCategoryId());
|
}
|
if (pageWrap.getModel().getCarId() != null) {
|
queryWrapper.eq(JkLine::getCarId, pageWrap.getModel().getCarId());
|
}
|
if (pageWrap.getModel().getMaxCustomer() != null) {
|
queryWrapper.eq(JkLine::getMaxCustomer, pageWrap.getModel().getMaxCustomer());
|
}
|
if (pageWrap.getModel().getMaxOrder() != null) {
|
queryWrapper.eq(JkLine::getMaxOrder, pageWrap.getModel().getMaxOrder());
|
}
|
if (pageWrap.getModel().getStatus() != null) {
|
queryWrapper.eq(JkLine::getStatus, pageWrap.getModel().getStatus());
|
}
|
if (pageWrap.getModel().getSortnum() != null) {
|
queryWrapper.eq(JkLine::getSortnum, pageWrap.getModel().getSortnum());
|
}
|
queryWrapper.orderByAsc(JkLine::getCode);
|
IPage<JkLine> result = jkLineMapper.selectJoinPage(page, JkLine.class,queryWrapper);
|
return PageData.from(result);
|
}
|
|
@Override
|
public long count(JkLine jkLine) {
|
QueryWrapper<JkLine> wrapper = new QueryWrapper<>(jkLine);
|
return jkLineMapper.selectCount(wrapper);
|
}
|
|
@Override
|
@Transactional(rollbackFor = {BusinessException.class,Exception.class})
|
public List<JkLine> importBatch(MultipartFile file, LoginUserInfo loginUserInfo){
|
Boolean importing = (Boolean) redisTemplate.opsForValue().get(Constants.RedisKeys.IMPORTING_JKLINE);
|
if(importing!=null && importing){
|
throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"对不起,已存在导入任务正在执行中,请稍后再试!");
|
}
|
redisTemplate.opsForValue().set(Constants.RedisKeys.IMPORTING_JKLINE,true);
|
try {
|
ExcelImporter ie = null;
|
List<JkLineImport> dataList =null;
|
try {
|
ie = new ExcelImporter(file,1,0);
|
dataList = ie.getDataList(JkLineImport.class,null);
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
if(dataList == null || dataList.size() ==0){
|
throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"对不起,录入数据为空!");
|
}
|
//当前所有线路
|
List<JkLine> allList = jkLineMapper.selectJoinList(JkLine.class,new MPJLambdaWrapper<JkLine>()
|
.selectAll(JkLine.class)
|
.eq(JkLine::getIsdeleted,Constants.ZERO)
|
);
|
//所有主线路
|
List<Category> categoryList = categoryMapper.selectJoinList(Category.class,new MPJLambdaWrapper<Category>()
|
.selectAll(Category.class)
|
.eq(Category::getIsdeleted,Constants.ZERO)
|
.eq(Category::getType,Constants.FOUR)
|
);
|
//所有物流车车辆
|
List<Cars> cars = carsMapper.selectJoinList(Cars.class,new MPJLambdaWrapper<Cars>()
|
.selectAll(Cars.class)
|
.eq(Cars::getIsdeleted,Constants.ZERO)
|
// .eq(Cars::getType,Constants.ONE)
|
);
|
|
|
List<JkLine> newList = new ArrayList<>();
|
List<JkLine> updateList = new ArrayList<>();
|
for(int i=0;i<dataList.size();i++){
|
JkLineImport model = dataList.get(i);
|
if(StringUtils.isBlank(model.getName())
|
&&StringUtils.isBlank(model.getCategoryName())
|
&&StringUtils.isBlank(model.getCarCode())
|
&&StringUtils.isBlank(model.getWeeks())
|
&&StringUtils.isBlank(model.getCode())
|
&&StringUtils.isBlank(model.getMaxCustomer())
|
&&StringUtils.isBlank(model.getMaxOrder()) ){
|
continue;
|
}
|
checkModelParam(model,newList,updateList,i,loginUserInfo,allList,categoryList,cars);
|
}
|
if((newList == null || newList.size() ==0) && updateList.size() == 0){
|
throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"对不起,录入有效数据为空!");
|
}
|
if(newList.size()>0){
|
jkLineMapper.insert(newList);
|
}
|
if(updateList.size()>0){
|
for (JkLine c : updateList){
|
jkLineMapper.updateById(c);
|
}
|
}
|
newList.addAll(updateList);
|
return newList;
|
}catch (BusinessException e){
|
throw e;
|
}catch (Exception e){
|
e.printStackTrace();
|
throw new BusinessException(ResponseStatus.SERVER_ERROR.getCode(),"信息导入失败,请稍后重试");
|
}finally {
|
redisTemplate.delete(Constants.RedisKeys.IMPORTING_JKLINE);
|
}
|
|
}
|
|
|
private JkLine checkModelParam(JkLineImport model, List<JkLine> newList
|
, List<JkLine> updateList
|
,int index
|
,LoginUserInfo loginUserInfo
|
,List<JkLine> allList
|
,List<Category> categoryList
|
, List<Cars> carsList) {
|
if(StringUtils.isBlank(model.getName())
|
||StringUtils.isBlank(model.getCategoryName()) ){
|
throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"对不起,第"+(index+3)+"行线路信息不完整,请检查表格内容!");
|
}
|
for(JkLine param: newList){
|
if(StringUtils.isNotBlank(model.getName())&&StringUtils.isNotBlank(param.getName())) {
|
if (StringUtils.equals(model.getName(), param.getName())) {
|
throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(), "对不起,第" + (index + 3) + "行线路名称【" + model.getName() + "】重复出现,请检查表格内容!");
|
}
|
}
|
}
|
Category cate = findCategoryFromListByName(model.getCategoryName(),categoryList);
|
if(cate == null){
|
throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(), "对不起,第" + (index + 3) + "行所属主线路【" + model.getCategoryName() + "】不存在,请检查表格内容!");
|
}
|
Cars car = findCarFromListByName(model.getCarCode(),carsList);
|
if(car == null){
|
throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(), "对不起,第" + (index + 3) + "行送车车辆【" + model.getCarCode() + "】不存在,请检查表格内容!");
|
}
|
JkLine line = findModelFromList(model.getName(),allList);
|
if(line == null){
|
line = new JkLine();
|
line.setCreator(loginUserInfo.getId());
|
line.setCreateDate(new Date());
|
line.setIsnew(Constants.ONE);
|
newList.add(line);
|
}else{
|
line.setIsnew(Constants.ZERO);
|
updateList.add(line);
|
}
|
line.setName(model.getName());
|
line.setCode(model.getCode());
|
line.setCategoryId(cate.getId());
|
line.setCarId(car.getId());
|
line.setWeeks(model.getWeeks());
|
line.setMaxCustomer(getIntegerByVal(model.getMaxCustomer()));
|
line.setMaxOrder(getIntegerByVal(model.getMaxOrder()));
|
|
line.setEditDate(new Date());
|
line.setEditor(loginUserInfo.getId());
|
line.setIsdeleted(Constants.ZERO);
|
return line;
|
}
|
|
private Integer getIntegerByVal(String maxCustomer) {
|
try {
|
return Integer.parseInt(maxCustomer);
|
}catch (Exception e){
|
|
}
|
return null;
|
}
|
|
private Company findCompanyFromList(String companyName, List<Company> companyList) {
|
if(companyList !=null){
|
for(Company company : companyList){
|
if(StringUtils.equals(companyName,company.getCompanyNamePath())){
|
return company;
|
}
|
}
|
}
|
return null;
|
}
|
private JkLine findModelFromList(String name, List<JkLine> list) {
|
if(list !=null){
|
for(JkLine model : list){
|
if(StringUtils.equals(name,model.getName())){
|
return model;
|
}
|
}
|
}
|
return null;
|
}
|
private Category findCategoryFromListByName(String name, List<Category> list) {
|
if(list !=null){
|
for(Category model : list){
|
if(StringUtils.equals(name,model.getName())){
|
return model;
|
}
|
}
|
}
|
return null;
|
}
|
private Cars findCarFromListByName(String name, List<Cars> list) {
|
if(list !=null){
|
for(Cars model : list){
|
if(StringUtils.equals(name,model.getCode())){
|
return model;
|
}
|
}
|
}
|
return null;
|
}
|
}
|