package com.yanzhu.base.service.impl;

import java.util.Collection;
import java.util.List;

import com.yanzhu.common.core.domain.entity.SysDictData;
import com.yanzhu.common.core.domain.entity.SysUser;
import com.yanzhu.common.core.redis.RedisCache;
import com.yanzhu.common.utils.DateUtils;
import com.yanzhu.common.utils.SecurityUtils;
import com.yanzhu.system.mapper.SysDictDataMapper;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.yanzhu.base.mapper.BaseAssetsTypeMapper;
import com.yanzhu.base.domain.BaseAssetsType;
import com.yanzhu.base.service.IBaseAssetsTypeService;

import javax.annotation.PostConstruct;

/**
 * 物资类型Service业务层处理
 * 
 * @author yanZhu
 * @date 2024-02-24
 */
@Service
public class BaseAssetsTypeServiceImpl implements IBaseAssetsTypeService 
{
    @Autowired
    private BaseAssetsTypeMapper baseAssetsTypeMapper;

    @Autowired
    private RedisCache redisCache;

    /**
     * 加载字典缓存数据
     */
    private void loadingCache()
    {
        Collection<String> collection = redisCache.keys("YANZHU.BASE.ASSETSTYPE.findAllByCategory.*");
        redisCache.deleteObject(collection);
    }

    /**
     * 查询物资类型
     * 
     * @param id 物资类型主键
     * @return 物资类型
     */
    @Override
    public BaseAssetsType selectBaseAssetsTypeById(Long id)
    {
        return baseAssetsTypeMapper.selectBaseAssetsTypeById(id);
    }

    /**
     * 查询物资类型列表
     * 
     * @param baseAssetsType 物资类型
     * @return 物资类型
     */
    @Override
    public List<BaseAssetsType> selectBaseAssetsTypeList(BaseAssetsType baseAssetsType)
    {
        return baseAssetsTypeMapper.selectBaseAssetsTypeList(baseAssetsType);
    }

    /**
     * 新增物资类型
     * 
     * @param baseAssetsType 物资类型
     * @return 结果
     */
    @Override
    public int insertBaseAssetsType(BaseAssetsType baseAssetsType)
    {
        baseAssetsType.setCreateTime(DateUtils.getNowDate());
        int res = baseAssetsTypeMapper.insertBaseAssetsType(baseAssetsType);
        if(res>0){
            this.loadingCache();
        }
        return res;
    }

    /**
     * 修改物资类型
     * 
     * @param baseAssetsType 物资类型
     * @return 结果
     */
    @Override
    public int updateBaseAssetsType(BaseAssetsType baseAssetsType)
    {
        baseAssetsType.setUpdateTime(DateUtils.getNowDate());
        int res = baseAssetsTypeMapper.updateBaseAssetsType(baseAssetsType);
        if(res>0){
            this.loadingCache();
        }
        return res;
    }

    /**
     * 批量删除物资类型
     * 
     * @param ids 需要删除的物资类型主键
     * @return 结果
     */
    @Override
    public int deleteBaseAssetsTypeByIds(Long[] ids)
    {
        int res = baseAssetsTypeMapper.deleteBaseAssetsTypeByIds(ids);
        if(res>0){
            this.loadingCache();
        }
        return res;
    }

    /**
     * 删除物资类型信息
     * 
     * @param id 物资类型主键
     * @return 结果
     */
    @Override
    public int deleteBaseAssetsTypeById(Long id)
    {
        int res = baseAssetsTypeMapper.deleteBaseAssetsTypeById(id);
        if(res>0){
            this.loadingCache();
        }
        return res;
    }

    /**
     * 查询物资类型
     *
     * @param category 物资类型
     * @return 结果
     */
    public List<BaseAssetsType> findAllByCategory(String category){
        //boolean isAdmin = SysUser.isAdmin(SecurityUtils.getUserId());
        BaseAssetsType baseAssetsType = new BaseAssetsType();
        baseAssetsType.setParentId(0L);
        baseAssetsType.setType(category);
        List<BaseAssetsType> list = baseAssetsTypeMapper.selectBaseAssetsTypeList(baseAssetsType);
        if(CollectionUtils.isNotEmpty(list)){
            for(BaseAssetsType entity:list){
                baseAssetsType.setParentId(entity.getId());
                baseAssetsType.setDefaultDeptId(SecurityUtils.getLoginUser().getUser().getParDeptId());
                List<BaseAssetsType> dataList = baseAssetsTypeMapper.selectBaseAssetsTypeList(baseAssetsType);
                if(CollectionUtils.isNotEmpty(list)){
                    entity.setChildrenAssetsTypeList(dataList);
                }
            }
        }
        return list;
    }
}
