package doumeemes.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 doumeemes.core.constants.ResponseStatus; 
 | 
import doumeemes.core.exception.BusinessException; 
 | 
import doumeemes.core.model.LoginUserInfo; 
 | 
import doumeemes.core.model.PageData; 
 | 
import doumeemes.core.model.PageWrap; 
 | 
import doumeemes.core.utils.Constants; 
 | 
import doumeemes.core.utils.DateUtil; 
 | 
import doumeemes.core.utils.Utils; 
 | 
import doumeemes.core.utils.excel.EasyExcelUtil; 
 | 
import doumeemes.core.utils.redis.RedisUtil; 
 | 
import doumeemes.dao.business.CategoryMapper; 
 | 
import doumeemes.dao.business.model.Category; 
 | 
import doumeemes.service.business.CategoryService; 
 | 
import doumeemes.service.ext.CategoryExtService; 
 | 
import org.apache.commons.lang3.StringUtils; 
 | 
import org.apache.shiro.SecurityUtils; 
 | 
import org.springframework.beans.factory.annotation.Autowired; 
 | 
import org.springframework.context.annotation.Lazy; 
 | 
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.util.Date; 
 | 
import java.util.List; 
 | 
  
 | 
/** 
 | 
 * 物料分类信息表Service实现 
 | 
 * @author 江蹄蹄 
 | 
 * @date 2022/04/27 16:15 
 | 
 */ 
 | 
@Service 
 | 
public class CategoryServiceImpl implements CategoryService { 
 | 
  
 | 
    @Autowired 
 | 
    private CategoryMapper categoryMapper; 
 | 
    @Autowired 
 | 
    @Lazy 
 | 
    private CategoryExtService categoryExtService; 
 | 
  
 | 
    @Autowired 
 | 
    private RedisTemplate<String, Object> redisTemplate; 
 | 
  
 | 
    @Override 
 | 
    public synchronized String  getNextCode(Integer comId ){ 
 | 
        String prefix =  "FL-" + DateUtil.getDate(new Date(),"yyyyMMdd") +"-"; 
 | 
        Integer countNum  = RedisUtil.getObject(redisTemplate,Constants.RedisKeys.COM_CATEGORY_CHECK_KEY+comId,Integer.class); 
 | 
        countNum = Constants.formatIntegerNum(countNum)+1; 
 | 
        //更新缓存 
 | 
        RedisUtil.addObject(redisTemplate,Constants.RedisKeys.COM_CATEGORY_CHECK_KEY+comId,countNum); 
 | 
        String nextIndex =Integer.toString( countNum ); 
 | 
        return prefix + StringUtils.leftPad(nextIndex,4,"0"); 
 | 
    } 
 | 
  
 | 
    @Override 
 | 
    public Integer create(Category category) { 
 | 
        categoryMapper.insert(category); 
 | 
        return category.getId(); 
 | 
    } 
 | 
  
 | 
    @Override 
 | 
    public void deleteById(Integer id) { 
 | 
        categoryMapper.deleteById(id); 
 | 
    } 
 | 
  
 | 
    @Override 
 | 
    public void delete(Category category) { 
 | 
        UpdateWrapper<Category> deleteWrapper = new UpdateWrapper<>(category); 
 | 
        categoryMapper.delete(deleteWrapper); 
 | 
    } 
 | 
  
 | 
    @Override 
 | 
    public void deleteByIdInBatch(List<Integer> ids) { 
 | 
        if (CollectionUtils.isEmpty(ids)) { 
 | 
            return; 
 | 
        } 
 | 
        categoryMapper.deleteBatchIds(ids); 
 | 
    } 
 | 
  
 | 
    @Override 
 | 
    public void updateById(Category category) { 
 | 
        categoryMapper.updateById(category); 
 | 
        LoginUserInfo user = (LoginUserInfo) SecurityUtils.getSubject().getPrincipal(); 
 | 
        categoryExtService.loadCom(user.getCompany()); 
 | 
    } 
 | 
  
 | 
    @Override 
 | 
    public void updateByIdInBatch(List<Category> categorys) { 
 | 
        if (CollectionUtils.isEmpty(categorys)) { 
 | 
            return; 
 | 
        } 
 | 
        for (Category category: categorys) { 
 | 
            categoryMapper.updateById(category); 
 | 
        } 
 | 
        LoginUserInfo user = (LoginUserInfo) SecurityUtils.getSubject().getPrincipal(); 
 | 
        categoryExtService.loadCom(user.getCompany()); 
 | 
    } 
 | 
  
 | 
    @Override 
 | 
    public Category findById(Integer id) { 
 | 
        return categoryMapper.selectById(id); 
 | 
    } 
 | 
  
 | 
    @Override 
 | 
    public Category findOne(Category category) { 
 | 
        QueryWrapper<Category> wrapper = new QueryWrapper<>(category); 
 | 
        return categoryMapper.selectOne(wrapper); 
 | 
    } 
 | 
  
 | 
    @Override 
 | 
    public List<Category> findList(Category category) { 
 | 
        QueryWrapper<Category> wrapper = new QueryWrapper<>(category); 
 | 
        return categoryMapper.selectList(wrapper); 
 | 
    } 
 | 
   
 | 
    @Override 
 | 
    public PageData<Category> findPage(PageWrap<Category> pageWrap) { 
 | 
        IPage<Category> page = new Page<>(pageWrap.getPage(), pageWrap.getCapacity()); 
 | 
        QueryWrapper<Category> queryWrapper = new QueryWrapper<>(); 
 | 
        Utils.MP.blankToNull(pageWrap.getModel()); 
 | 
        if (pageWrap.getModel().getId() != null) { 
 | 
            queryWrapper.lambda().eq(Category::getId, pageWrap.getModel().getId()); 
 | 
        } 
 | 
        if (pageWrap.getModel().getDeleted() != null) { 
 | 
            queryWrapper.lambda().eq(Category::getDeleted, pageWrap.getModel().getDeleted()); 
 | 
        } 
 | 
        if (pageWrap.getModel().getCreateUser() != null) { 
 | 
            queryWrapper.lambda().eq(Category::getCreateUser, pageWrap.getModel().getCreateUser()); 
 | 
        } 
 | 
        if (pageWrap.getModel().getCreateTime() != null) { 
 | 
            queryWrapper.lambda().ge(Category::getCreateTime, Utils.Date.getStart(pageWrap.getModel().getCreateTime())); 
 | 
            queryWrapper.lambda().le(Category::getCreateTime, Utils.Date.getEnd(pageWrap.getModel().getCreateTime())); 
 | 
        } 
 | 
        if (pageWrap.getModel().getUpdateUser() != null) { 
 | 
            queryWrapper.lambda().eq(Category::getUpdateUser, pageWrap.getModel().getUpdateUser()); 
 | 
        } 
 | 
        if (pageWrap.getModel().getUpdateTime() != null) { 
 | 
            queryWrapper.lambda().ge(Category::getUpdateTime, Utils.Date.getStart(pageWrap.getModel().getUpdateTime())); 
 | 
            queryWrapper.lambda().le(Category::getUpdateTime, Utils.Date.getEnd(pageWrap.getModel().getUpdateTime())); 
 | 
        } 
 | 
        if (pageWrap.getModel().getRemark() != null) { 
 | 
            queryWrapper.lambda().eq(Category::getRemark, pageWrap.getModel().getRemark()); 
 | 
        } 
 | 
        if (pageWrap.getModel().getRootDepartId() != null) { 
 | 
            queryWrapper.lambda().eq(Category::getRootDepartId, pageWrap.getModel().getRootDepartId()); 
 | 
        } 
 | 
        if (pageWrap.getModel().getType() != null) { 
 | 
            queryWrapper.lambda().eq(Category::getType, pageWrap.getModel().getType()); 
 | 
        } 
 | 
        if (pageWrap.getModel().getCode() != null) { 
 | 
            queryWrapper.lambda().eq(Category::getCode, pageWrap.getModel().getCode()); 
 | 
        } 
 | 
        if (pageWrap.getModel().getName() != null) { 
 | 
            queryWrapper.lambda().eq(Category::getName, pageWrap.getModel().getName()); 
 | 
        } 
 | 
        if (pageWrap.getModel().getParentId() != null) { 
 | 
            queryWrapper.lambda().eq(Category::getParentId, pageWrap.getModel().getParentId()); 
 | 
        } 
 | 
        if (pageWrap.getModel().getCateType() != null) { 
 | 
            queryWrapper.lambda().eq(Category::getCateType, pageWrap.getModel().getCateType()); 
 | 
        } 
 | 
        for(PageWrap.SortData sortData: pageWrap.getSorts()) { 
 | 
            if (sortData.getDirection().equalsIgnoreCase(PageWrap.DESC)) { 
 | 
                queryWrapper.orderByDesc(sortData.getProperty()); 
 | 
            } else { 
 | 
                queryWrapper.orderByAsc(sortData.getProperty()); 
 | 
            } 
 | 
        } 
 | 
        return PageData.from(categoryMapper.selectPage(page, queryWrapper)); 
 | 
    } 
 | 
  
 | 
    @Override 
 | 
    public long count(Category category) { 
 | 
        QueryWrapper<Category> wrapper = new QueryWrapper<>(category); 
 | 
        return categoryMapper.selectCount(wrapper); 
 | 
    } 
 | 
  
 | 
    @Transactional(rollbackFor = {BusinessException.class, Exception.class}) 
 | 
    @Override 
 | 
    public void importcategory(MultipartFile file) { 
 | 
        LoginUserInfo user = (LoginUserInfo) SecurityUtils.getSubject().getPrincipal(); 
 | 
        if (!Constants.equalsInteger(user.getType(), Constants.USERTYPE.COM)) { 
 | 
            throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(), "对不起,您无权限进行该操作!"); 
 | 
        } 
 | 
        //解析excel 
 | 
  
 | 
  
 | 
      List<Category> alllist = EasyExcelUtil.importExcel(file, 1, 1, Category.class); 
 | 
        if (alllist == null || alllist.size() == 0) { 
 | 
            throw new BusinessException(ResponseStatus.SERVER_ERROR.getCode(), "导入数据内容有误!"); 
 | 
        } 
 | 
        for (int i = 0; i < alllist.size(); i++) { 
 | 
            Category category = alllist.get(i); 
 | 
            Integer counter=i+2; 
 | 
  
 | 
            if (StringUtils.isNotEmpty(category.getCateType())) { 
 | 
  
 | 
                if (StringUtils.equals(category.getCateType().trim(), "物料")) { 
 | 
                    category.setCateType("0"); 
 | 
                } else if (StringUtils.equals(category.getCateType().trim(), "客户")) { 
 | 
                    category.setCateType("1"); 
 | 
                } else if (StringUtils.equals(category.getCateType().trim(), "工装")) { 
 | 
                    category.setCateType("2"); 
 | 
                } else if (StringUtils.equals(category.getCateType().trim(), "不良原因")) { 
 | 
                    category.setCateType("3"); 
 | 
                } else { 
 | 
                    throw new BusinessException(ResponseStatus.SERVER_ERROR.getCode(), "第"+counter+"行:分类类型错误!"); 
 | 
                } 
 | 
            } else { 
 | 
                throw new BusinessException(ResponseStatus.SERVER_ERROR.getCode(), "第"+counter+"行:分类类型不能为空!"); 
 | 
            } 
 | 
  
 | 
            Category query = new Category(); 
 | 
            query.setDeleted(Constants.ZERO); 
 | 
            query.setRootDepartId(user.getRootDepartment().getId()); 
 | 
            query.setCode(category.getCode()); 
 | 
            List<Category> list = this.findList(query); 
 | 
  
 | 
            Category query1 = new Category(); 
 | 
            query1.setDeleted(Constants.ZERO); 
 | 
            query1.setRootDepartId(user.getRootDepartment().getId()); 
 | 
            query1.setName(category.getName()); 
 | 
            List<Category> list1 = this.findList(query1); 
 | 
            if (list.size() > 0 || list1.size() > 0) { 
 | 
                throw new BusinessException(ResponseStatus.SERVER_ERROR.getCode(), "第"+counter+"行:分类编码/分类名称已存在,不允许添加!"); 
 | 
            } 
 | 
  
 | 
            if (StringUtils.equals(category.getType(), "大类")) { 
 | 
                //大类直接插入 
 | 
                category.setType("0"); 
 | 
            } else if (StringUtils.equals(category.getType(), "中类")) { 
 | 
                category.setType("1"); 
 | 
                //中类 根据名称查询大类是否存在 
 | 
                Category ca1 = new Category(); 
 | 
                ca1.setType("0"); 
 | 
                ca1.setName(category.getCodename()); 
 | 
                ca1.setDeleted(Constants.ZERO); 
 | 
                Category ca2 = this.findOne(ca1); 
 | 
                if (ca2 == null || ca2.getId() == null) { 
 | 
                    throw new BusinessException(ResponseStatus.SERVER_ERROR.getCode(), "第"+counter+"行:所属分类不存在!"); 
 | 
  
 | 
                } 
 | 
                category.setParentId(ca2.getId()); 
 | 
            } else if (StringUtils.equals(category.getType(), "小类")) { 
 | 
                //小类 根据名称查询种类类是否存在 
 | 
                category.setType("2"); 
 | 
                Category ca1 = new Category(); 
 | 
                ca1.setType("1"); 
 | 
                ca1.setName(category.getCodename()); 
 | 
                ca1.setDeleted(Constants.ZERO); 
 | 
                Category ca2 = this.findOne(ca1); 
 | 
                if (ca2 == null || ca2.getId() == null) { 
 | 
                    throw new BusinessException(ResponseStatus.SERVER_ERROR.getCode(), "第"+counter+"行:所属分类不存在!"); 
 | 
  
 | 
                } 
 | 
                category.setParentId(ca2.getId()); 
 | 
            }else{ 
 | 
                throw new BusinessException(ResponseStatus.SERVER_ERROR.getCode(), "第"+counter+"行:属性类型错误!"); 
 | 
            } 
 | 
            category.setDeleted(Constants.ZERO); 
 | 
            category.setCreateTime(new Date()); 
 | 
            category.setCreateUser(user.getId()); 
 | 
            category.setRootDepartId(user.getRootDepartment().getId()); 
 | 
  
 | 
            this.create(category); 
 | 
        } 
 | 
        categoryExtService.loadCom(user.getCompany()); 
 | 
  
 | 
    } 
 | 
} 
 |