MrShi
2024-11-28 c9ad1f34a86e54b1c690c623ba661cd4131a3d71
server/visits/dmvisit_service/src/main/java/com/doumee/service/business/impl/AreasServiceImpl.java
¶Ô±ÈÐÂÎļþ
@@ -0,0 +1,460 @@
package com.doumee.service.business.impl;
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.doumee.core.constants.ResponseStatus;
import com.doumee.core.exception.BusinessException;
import com.doumee.core.model.LoginUserInfo;
import com.doumee.core.model.PageData;
import com.doumee.core.model.PageWrap;
import com.doumee.core.utils.Constants;
import com.doumee.core.utils.Utils;
import com.doumee.dao.business.AreasMapper;
import com.doumee.dao.business.model.Areas;
import com.doumee.service.business.AreasService;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
/**
 * çœå¸‚区信息表Service实现
 * @author æ±Ÿè¹„蹄
 * @date 2023/02/15 08:55
 */
@Service
public class AreasServiceImpl implements AreasService {
    public static   List<Areas> ALL_AREA_LIST;
    public static   List<Areas> PROVINCE_LIST;
    public static   List<Areas> CITY_LIST;
    public static   List<Areas> AREA_LIST;
    public static   List<Areas> ALL_AREA_TREE;
    @Autowired
    private AreasMapper areasMapper;
    @Override
    public Integer create(Areas areas) {
        LoginUserInfo user = areas.getLoginUserInfo();
/*
        if (Objects.isNull(areas.getParentId())){
            areas.setType(Constants.ZERO);
        }else {
            Areas parentArea = areasMapper.selectById(areas.getParentId());
            if (Objects.isNull(parentArea)){
                areas.setType(Constants.ZERO);
            }else {
                areas.setType(parentArea.getType()+Constants.ONE);
            }
        }*/
        areas.setCreateDate(new Date());
        areas.setEditDate(new Date());
        areas.setCreator(user.getId());
        areas.setEditor(user.getId());
        areas.setIsdeleted(Constants.ZERO);
        areasMapper.insert(areas);
        //刷新缓存数据
        cacheData();
        return areas.getId();
    }
    @Override
    public void deleteById(Integer id) {
        areasMapper.deleteById(id);
        //刷新缓存数据
        cacheData();
    }
    @Override
    public void delete(Areas areas) {
        UpdateWrapper<Areas> deleteWrapper = new UpdateWrapper<>(areas);
        areasMapper.delete(deleteWrapper);
        //刷新缓存数据
        cacheData();
    }
    @Override
    public void deleteByIdInBatch(List<Integer> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return;
        }
        areasMapper.deleteBatchIds(ids);
        //刷新缓存数据
        cacheData();
    }
    @Override
    public void updateById(Areas areas) {
        UpdateWrapper<Areas> wrapper = new UpdateWrapper<>();
        wrapper.lambda().eq(Areas::getId,areas.getId());
        Areas update = new Areas();
        update.setName(areas.getName());
        update.setSortnum(areas.getSortnum());
        areasMapper.update(update,wrapper);
        //刷新缓存数据
        cacheData();
    }
    @Override
    public void updateByIdInBatch(List<Areas> areass) {
        if (CollectionUtils.isEmpty(areass)) {
            return;
        }
        for (Areas areas: areass) {
            this.updateById(areas);
        }
        //刷新缓存数据
        cacheData();
    }
    @Override
    public Areas findById(Integer id) {
        if(ALL_AREA_LIST!=null){
            for(Areas a : ALL_AREA_LIST){
                if(Constants.equalsInteger(a.getId(),id)){
                    return a;
                }
            }
        }
   //     return areasMapper.selectById(id);
        return  null;
    }
    @Override
    public Areas findById(Integer id,Integer type) {
        List<Areas> list = null;
        if(type == null){
            list = ALL_AREA_LIST;
        }else if(Constants.equalsInteger(type,Constants.ZERO)){
            list = PROVINCE_LIST;
        }else  if(Constants.equalsInteger(type,Constants.ONE)){
            list = CITY_LIST;
        } else if (Constants.equalsInteger(type,Constants.TWO)){
            list =  AREA_LIST;
        }
        if(list!=null){
            for(Areas a : list){
                if(Constants.equalsInteger(a.getId(),id)){
                    return a;
                }
            }
        }
        return null;
    }
    @Override
    public Areas findByName(String name,Integer type){
        List<Areas> list = null;
        if(type == null){
            list = ALL_AREA_LIST;
        }else if(Constants.equalsInteger(type,Constants.ZERO)){
            list = PROVINCE_LIST;
        }else  if(Constants.equalsInteger(type,Constants.ONE)){
            list = CITY_LIST;
        } else if (Constants.equalsInteger(type,Constants.TWO)){
            list =  AREA_LIST;
        }
        if(list!=null){
            for(Areas a : list){
                if(StringUtils.equals(name,a.getName())){
                    return a;
                }
            }
        }
        return null;
    }
    @Override
    public Areas findByNameAndParentId(String name,Integer type,Integer parentId){
        List<Areas> list = null;
        if(type == null){
            list = ALL_AREA_LIST;
        }else if(Constants.equalsInteger(type,Constants.ZERO)){
            list = PROVINCE_LIST;
        }else  if(Constants.equalsInteger(type,Constants.ONE)){
            list = CITY_LIST;
        } else if (Constants.equalsInteger(type,Constants.TWO)){
            list =  AREA_LIST;
        }
        if(list!=null){
            for(Areas a : list){
                if(StringUtils.equals(name,a.getName()) && Constants.equalsInteger(parentId,a.getParentId())){
                    return a;
                }
            }
        }
        return null;
    }
    @Override
    public List<Areas> findByParentId(Integer type, Integer parentId) {
        List<Areas> list = null;
        if(type == null){
            list = ALL_AREA_LIST;
        }else if(Constants.equalsInteger(type,Constants.ZERO)){
            list = PROVINCE_LIST;
        }else  if(Constants.equalsInteger(type,Constants.ONE)){
            list = CITY_LIST;
        } else if (Constants.equalsInteger(type,Constants.TWO)){
            list =  AREA_LIST;
        }
        if(list!=null){
            return list.stream().filter(s->Constants.equalsInteger(s.getParentId(),parentId)).collect(Collectors.toList());
        }
        return null;
    }
    @Override
    public List<Areas> findChildByParentId(Integer id,List<Areas> list){
        List<Areas> result =null;
        if(list == null){
            list = ALL_AREA_LIST;
        }
        if(list!=null && list.size()>0){
            for(Areas model :list){
                if(Constants.equalsInteger(id,model.getParentId())){
                    if(result == null){
                        result = new ArrayList<>();
                    }
                    result.add(model );
                }
            }
        }
        return result;
    }
    @Override
    public boolean isAreaValid(String proName,String cityName,String areaName){
        Areas pro = findByName(proName,Constants.ZERO);
        if(pro == null){
            return  false;
        }
        Areas city = findByName(proName,Constants.ONE);
        if(city == null && !Constants.equalsInteger(city.getParentId(),pro.getId())){
            return  false;
        }
        Areas area = findByName(proName,Constants.TWO);
        if(area == null && !Constants.equalsInteger(area.getParentId(),city.getId())){
            return  false;
        }
        return false;
    }
    @Override
    public Areas findOne(Areas areas) {
        QueryWrapper<Areas> wrapper = new QueryWrapper<>(areas);
        return areasMapper.selectOne(wrapper);
    }
    @Override
    public List<Areas> findList(Areas areas) {
        List<Areas> list = null;
        Integer type =areas.getType();
        if(type == null){
            list = ALL_AREA_LIST;
        }else if(Constants.equalsInteger(type,Constants.ZERO)){
            list = PROVINCE_LIST;
        }else  if(Constants.equalsInteger(type,Constants.ONE)){
            list = CITY_LIST;
        } else if (Constants.equalsInteger(type,Constants.TWO)){
            list =  AREA_LIST;
        }
        List<Areas> result = null;
        if(StringUtils.isNotBlank(areas.getName())){
            for(Areas a : list){
                if(StringUtils.contains(a.getName(),areas.getName())){
                    if(result == null){
                        result = new ArrayList<>();
                    }
                    result.add(a);
                }
            }
            return  result;
        }
        return list;
    }
    @Override
    public  List<Areas> listByParentId(Areas areas) {
        List<Areas> list = null;
        Integer type =areas.getType();
        if(type == null){
            list = ALL_AREA_LIST;
        }else if(Constants.equalsInteger(type,Constants.ZERO)){
            list = PROVINCE_LIST;
        }else  if(Constants.equalsInteger(type,Constants.ONE)){
            list = CITY_LIST;
        } else if (Constants.equalsInteger(type,Constants.TWO)){
            list =  AREA_LIST;
        }
        List<Areas> result = null;
        if(list!=null){
            for(Areas a : list){
                if(result == null){
                    result = new ArrayList<>();
                }
                if( areas.getParentId() == null
                        ||(areas.getParentId() !=null) && Constants.equalsInteger(a.getParentId(),areas.getParentId())){
                    Areas t = new Areas();
                    BeanUtils.copyProperties(a,t);
                    t.setChildList(null);
                    result.add(t);
                }
            }
        }
        return result;
    }
    @Override
    @PostConstruct
    public  void cacheData() {
       Areas a = new Areas();
       a.setIsdeleted(Constants.ZERO);
       ALL_AREA_LIST = null;
       PROVINCE_LIST =null;
       CITY_LIST=null;
       AREA_LIST = null;
       ALL_AREA_TREE = null;
       ALL_AREA_LIST =  areasMapper.selectList(new QueryWrapper<>(a).lambda().orderByDesc(Areas::getSortnum));
       if(ALL_AREA_LIST!=null){
           for(Areas model : ALL_AREA_LIST){
                if(Constants.equalsInteger(model.getType(),Constants.ZERO)){
                    if(PROVINCE_LIST == null){
                        PROVINCE_LIST = new ArrayList<>();
                    }
                    PROVINCE_LIST.add(model);
                }else if(Constants.equalsInteger(model.getType(),Constants.ONE)){
                    if(CITY_LIST == null){
                        CITY_LIST = new ArrayList<>();
                    }
                    CITY_LIST.add(model);
                    if(model.getParentId() != null){
                        Areas p = findById(model.getParentId());
                        if(p!=null  ){
                            model.setProvinceId(p.getId());
                            model.setProvinceName(p.getName());
                        }
                    }
                }else if(Constants.equalsInteger(model.getType(),Constants.TWO)){
                    if(AREA_LIST == null){
                        AREA_LIST = new ArrayList<>();
                    }
                    AREA_LIST.add(model);
                    Areas city = findById(model.getParentId());
                    if(city!=null  ){
                        model.setCityId(city.getId());
                        model.setCityName(city.getName());
                        if(city!=null && city.getParentId()!=null){
                            Areas p = findById(city.getParentId());
                            if(p!=null  ){
                                model.setProvinceId(p.getId());
                                model.setProvinceName(p.getName());
                            }
                        }
                    }
                }
           }
       }
       if(CITY_LIST!=null){
           for(Areas aa : CITY_LIST){
               aa.setChildList(findChildByParentId(aa.getId(), AREA_LIST));
           }
       }
       if(PROVINCE_LIST!=null){
           for(Areas aa : PROVINCE_LIST){
               aa.setChildList(findChildByParentId(aa.getId(),CITY_LIST));
           }
       }
       System.out.println("=================");
    }
    @Override
    public PageData<Areas> findPage(PageWrap<Areas> pageWrap) {
        IPage<Areas> page = new Page<>(pageWrap.getPage(), pageWrap.getCapacity());
        QueryWrapper<Areas> queryWrapper = new QueryWrapper<>();
        Utils.MP.blankToNull(pageWrap.getModel());
        if (pageWrap.getModel().getId() != null) {
            queryWrapper.lambda().eq(Areas::getId, pageWrap.getModel().getId());
        }
        if (pageWrap.getModel().getCreator() != null) {
            queryWrapper.lambda().eq(Areas::getCreator, pageWrap.getModel().getCreator());
        }
        if (pageWrap.getModel().getCreateDate() != null) {
            queryWrapper.lambda().ge(Areas::getCreateDate, Utils.Date.getStart(pageWrap.getModel().getCreateDate()));
            queryWrapper.lambda().le(Areas::getCreateDate, Utils.Date.getEnd(pageWrap.getModel().getCreateDate()));
        }
        if (pageWrap.getModel().getEditor() != null) {
            queryWrapper.lambda().eq(Areas::getEditor, pageWrap.getModel().getEditor());
        }
        if (pageWrap.getModel().getEditDate() != null) {
            queryWrapper.lambda().ge(Areas::getEditDate, Utils.Date.getStart(pageWrap.getModel().getEditDate()));
            queryWrapper.lambda().le(Areas::getEditDate, Utils.Date.getEnd(pageWrap.getModel().getEditDate()));
        }
        if (pageWrap.getModel().getIsdeleted() != null) {
            queryWrapper.lambda().eq(Areas::getIsdeleted, pageWrap.getModel().getIsdeleted());
        }
        if (pageWrap.getModel().getName() != null) {
            queryWrapper.lambda().eq(Areas::getName, pageWrap.getModel().getName());
        }
        if (pageWrap.getModel().getInfo() != null) {
            queryWrapper.lambda().eq(Areas::getInfo, pageWrap.getModel().getInfo());
        }
        if (pageWrap.getModel().getCode() != null) {
            queryWrapper.lambda().eq(Areas::getCode, pageWrap.getModel().getCode());
        }
        if (pageWrap.getModel().getParentId() != null) {
            queryWrapper.lambda().eq(Areas::getParentId, pageWrap.getModel().getParentId());
        }
        if (pageWrap.getModel().getType() != null) {
            queryWrapper.lambda().eq(Areas::getType, pageWrap.getModel().getType());
        }
        for(PageWrap.SortData sortData: pageWrap.getSorts()) {
            if (sortData.getDirection().equalsIgnoreCase(PageWrap.DESC)) {
                queryWrapper.orderByDesc(sortData.getProperty());
            } else {
                queryWrapper.orderByAsc(sortData.getProperty());
            }
        }
        return PageData.from(areasMapper.selectPage(page, queryWrapper));
    }
    @Override
    public long count(Areas areas) {
        QueryWrapper<Areas> wrapper = new QueryWrapper<>(areas);
        return areasMapper.selectCount(wrapper);
    }
    @Override
    public Areas findByCityAndArea(String cityName, String areasName) {
        Areas city = findByName(cityName,Constants.ONE);
        if(city !=null){
           return  findByNameAndParentId(areasName,Constants.TWO,city.getId());
        }
        return null;
    }
    @Override
    public String getAddress(Integer cityId,Integer areaId){
        Areas cityAreas = findById(cityId, Constants.ONE);
        Areas areas = findById(areaId, Constants.TWO);
        String cityName = Optional.ofNullable(cityAreas)
                .map(s -> s.getProvinceName() + s.getName())
                .orElseThrow(() -> new BusinessException(ResponseStatus.BAD_REQUEST));
        String areaName = Optional.ofNullable(areas).map(s -> s.getName()).orElse("");
        return cityName+areaName;
    }
}