content: package jnpf.base.service.impl;
  
  
  import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  import jnpf.base.mapper.DictionaryDataMapper;
  import jnpf.base.service.DictionaryDataService;
  import jnpf.util.DateUtil;
  import jnpf.util.PinYinUtil;
  import jnpf.util.RandomUtil;
  import jnpf.util.StringUtil;
  import jnpf.base.entity.DictionaryDataEntity;
  import jnpf.util.UserProvider;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.stereotype.Service;
  import org.springframework.transaction.annotation.Transactional;
  
  import java.util.ArrayList;
  import java.util.List;
  
  /**
   * 字典数据
   *
   * @author JNPF开发平台组
   * @version V3.1.0
   * @copyright 引迈信息技术有限公司（https://www.jnpfsoft.com）
   * @date 2019年9月27日 上午9:18
   */
  @Service
  public class DictionaryDataServiceImpl extends ServiceImpl<DictionaryDataMapper, DictionaryDataEntity> implements DictionaryDataService {
  
      @Autowired
      private UserProvider userProvider;
  
      @Override
      public List<DictionaryDataEntity> getList() {
          QueryWrapper<DictionaryDataEntity> queryWrapper = new QueryWrapper<>();
          queryWrapper.lambda().orderByDesc(DictionaryDataEntity::getSortCode);
          return this.list(queryWrapper);
      }
  
      @Override
      public List<DictionaryDataEntity> getList(String dictionaryTypeId) {
          QueryWrapper<DictionaryDataEntity> queryWrapper = new QueryWrapper<>();
          queryWrapper.lambda().eq(DictionaryDataEntity::getDictionaryTypeId, dictionaryTypeId);
          queryWrapper.lambda().orderByAsc(DictionaryDataEntity::getSortCode);
          return this.list(queryWrapper);
      }
  
      @Override
      public DictionaryDataEntity getInfo(String id) {
          QueryWrapper<DictionaryDataEntity> queryWrapper = new QueryWrapper<>();
          queryWrapper.lambda().eq(DictionaryDataEntity::getId, id);
          return this.getOne(queryWrapper);
      }
  
      @Override
      public boolean isExistByFullName(String dictionaryTypeId, String fullName, String id) {
          QueryWrapper<DictionaryDataEntity> queryWrapper = new QueryWrapper<>();
          queryWrapper.lambda().eq(DictionaryDataEntity::getFullName, fullName).eq(DictionaryDataEntity::getDictionaryTypeId, dictionaryTypeId);
          if (!StringUtil.isEmpty(id)) {
              queryWrapper.lambda().ne(DictionaryDataEntity::getId, id);
          }
          return this.count(queryWrapper) > 0 ? true : false;
      }
  
      @Override
      public boolean isExistByEnCode(String dictionaryTypeId, String enCode, String id) {
          QueryWrapper<DictionaryDataEntity> queryWrapper = new QueryWrapper<>();
          queryWrapper.lambda().eq(DictionaryDataEntity::getEnCode, enCode).eq(DictionaryDataEntity::getDictionaryTypeId, dictionaryTypeId);
          if (!StringUtil.isEmpty(id)) {
              queryWrapper.lambda().ne(DictionaryDataEntity::getId, id);
          }
          return this.count(queryWrapper) > 0 ? true : false;
      }
  
      @Override
      public void delete(DictionaryDataEntity entity) {
          this.removeById(entity.getId());
      }
  
      @Override
      public void create(DictionaryDataEntity entity) {
          entity.setId(RandomUtil.uuId());
          entity.setSimpleSpelling(PinYinUtil.getFirstSpell(entity.getFullName()).toUpperCase());
          entity.setCreatorUserId(userProvider.get().getUserId());
          this.save(entity);
      }
  
      @Override
      public boolean update(String id, DictionaryDataEntity entity) {
          entity.setId(id);
          entity.setLastModifyTime(DateUtil.getNowDate());
          entity.setLastModifyUserId(userProvider.get().getUserId());
         return this.updateById(entity);
      }
  
      @Override
      @Transactional
      public boolean first(String id) {
          boolean isOk = false;
          //获取要上移的那条数据的信息
          DictionaryDataEntity upEntity = this.getById(id);
          Long upSortCode = upEntity.getSortCode() == null ? 0 : upEntity.getSortCode();
          //查询上几条记录
          QueryWrapper<DictionaryDataEntity> queryWrapper = new QueryWrapper<>();
          queryWrapper.lambda()
                  .eq(DictionaryDataEntity::getDictionaryTypeId, upEntity.getDictionaryTypeId())
                  .eq(DictionaryDataEntity::getParentId, upEntity.getParentId())
                  .lt(DictionaryDataEntity::getSortCode, upSortCode)
                  .orderByDesc(DictionaryDataEntity::getSortCode);
          List<DictionaryDataEntity> downEntity = this.list(queryWrapper);
          if (downEntity.size() > 0) {
              //交换两条记录的sort值
              Long temp = upEntity.getSortCode();
              upEntity.setSortCode(downEntity.get(0).getSortCode());
              downEntity.get(0).setSortCode(temp);
              updateById(downEntity.get(0));
              updateById(upEntity);
              isOk = true;
          }
          return isOk;
      }
  
      @Override
      @Transactional
      public boolean next(String id) {
          boolean isOk = false;
          //获取要下移的那条数据的信息
          DictionaryDataEntity downEntity = this.getById(id);
          Long upSortCode = downEntity.getSortCode() == null ? 0 : downEntity.getSortCode();
          //查询下几条记录
          QueryWrapper<DictionaryDataEntity> queryWrapper = new QueryWrapper<>();
          queryWrapper.lambda()
                  .eq(DictionaryDataEntity::getDictionaryTypeId, downEntity.getDictionaryTypeId())
                  .eq(DictionaryDataEntity::getParentId, downEntity.getParentId())
                  .gt(DictionaryDataEntity::getSortCode, upSortCode)
                  .orderByAsc(DictionaryDataEntity::getSortCode);
          List<DictionaryDataEntity> upEntity = this.list(queryWrapper);
          if (upEntity.size() > 0) {
              //交换两条记录的sort值
              Long temp = downEntity.getSortCode();
              downEntity.setSortCode(upEntity.get(0).getSortCode());
              upEntity.get(0).setSortCode(temp);
              updateById(upEntity.get(0));
              updateById(downEntity);
              isOk = true;
          }
          return isOk;
      }
  
      @Override
      public List<DictionaryDataEntity> getDictionName(List<String> id) {
          List<DictionaryDataEntity> dictionList = new ArrayList<>();
          if (id.size() > 0) {
              QueryWrapper<DictionaryDataEntity> queryWrapper = new QueryWrapper<>();
              queryWrapper.lambda().in(DictionaryDataEntity::getEnCode, id);
              dictionList = this.list(queryWrapper);
          }
          return dictionList;
      }
  }
  