package com.ruoyi.system.service.impl;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.DIpcType;
import com.ruoyi.system.mapper.DIpcTypeMapper;
import com.ruoyi.system.service.IDIpcTypeService;

/**
 * ipc分类Service业务层处理
 *
 * @author ruoyi
 * @date 2023-04-05
 */
@Service
public class DIpcTypeServiceImpl extends ServiceImpl<DIpcTypeMapper, DIpcType> implements IDIpcTypeService {

	@Autowired
	private RedisCache redisCache;
	
	private static final Logger logger=LoggerFactory.getLogger(DIpcTypeServiceImpl.class);
	
	public DIpcType getDIpcTypeByCode(String code) {
		if(StringUtils.isBlank(code)) {
			return null;
		}
		
		String key="DIpcType:"+code;
		DIpcType type=redisCache.getCacheObject(key);
		if(type!=null) {
//			logger.debug("ipc类型缓存命中,code:"+code);
			return type;
		}else {
			logger.debug("ipc类型缓存没有命中,code:"+code);
		}
		
		DIpcType dIpcType=new DIpcType();
		dIpcType.setCode(code);
		List<DIpcType>list=queryList(dIpcType);
		
		if(list!=null && !list.isEmpty()) {
			DIpcType res=list.get(0);
			if(res!=null) {
				redisCache.setCacheObject(key, res);
			}
			return res;
		}
		return null;
	}
	
    /**
     * 查询ipc分类列表
     *
     * @param dIpcType ipc分类
     * @return ipc分类集合
     * @date 2023-04-05
     */
    @Override
    public List<DIpcType> queryList(DIpcType dIpcType) {
        QueryWrapper<DIpcType> queryWrapper = Wrappers.query();
                if (  StringUtils.isNotNull(dIpcType.getParentId())){
                    queryWrapper.eq("parent_id", dIpcType.getParentId());
                }
                if ( StringUtils.isNotEmpty(dIpcType.getAncestors())  ){
                    queryWrapper.eq("ancestors", dIpcType.getAncestors());
                }
                if ( StringUtils.isNotEmpty(dIpcType.getDictType())  ){
                    queryWrapper.eq("dict_type", dIpcType.getDictType());
                }
                if ( StringUtils.isNotEmpty(dIpcType.getCode())  ){
                    queryWrapper.eq("code", dIpcType.getCode());
                }
                if ( StringUtils.isNotEmpty(dIpcType.getKind())  ){
                    queryWrapper.eq("kind", dIpcType.getKind());
                }
                if (  StringUtils.isNotNull(dIpcType.getOrderNum())){
                    queryWrapper.eq("order_num", dIpcType.getOrderNum());
                }
                if ( StringUtils.isNotEmpty(dIpcType.getName())  ){
                    queryWrapper.like("name", dIpcType.getName());
                }
                if ( StringUtils.isNotEmpty(dIpcType.getDes())  ){
                    queryWrapper.eq("des", dIpcType.getDes());
                }
                if ( StringUtils.isNotEmpty(dIpcType.getNameEn())  ){
                    queryWrapper.eq("name_en", dIpcType.getNameEn());
                }
                if ( StringUtils.isNotEmpty(dIpcType.getDesEn())  ){
                    queryWrapper.eq("des_en", dIpcType.getDesEn());
                }
                if ( StringUtils.isNotEmpty(dIpcType.getNameJp())  ){
                    queryWrapper.eq("name_jp", dIpcType.getNameJp());
                }
                if ( StringUtils.isNotEmpty(dIpcType.getDesJp())  ){
                    queryWrapper.eq("des_jp", dIpcType.getDesJp());
                }
                if ( StringUtils.isNotEmpty(dIpcType.getVersion())  ){
                    queryWrapper.eq("version", dIpcType.getVersion());
                }
                if ( StringUtils.isNotEmpty(dIpcType.getVersionEn())  ){
                    queryWrapper.eq("version_en", dIpcType.getVersionEn());
                }
                if ( StringUtils.isNotEmpty(dIpcType.getVersionJp())  ){
                    queryWrapper.eq("version_jp", dIpcType.getVersionJp());
                }
                if (  StringUtils.isNotNull(dIpcType.getStatus())){
                    queryWrapper.eq("status", dIpcType.getStatus());
                }
        queryWrapper.orderByDesc("id");
        return this.list(queryWrapper);
    }
}
