/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ifish.helper;

import com.ifish.bean.VenderListBean;
import com.ifish.entity.HardwareType;
import com.ifish.entity.VenderList;
import com.ifish.mapper.Tbl_Hardware_Type_Mapper;
import com.ifish.mapper.Tbl_Vender_list_Mapper;
import com.ifish.util.RedisKey;
import com.ifish.util.StringUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 *
 * @author Administrator
 */
@Component
public class HardWareVenderHelper implements HardWareVenderHelperI {

    /**
     * 所有tbl_Hardware_Type表的操作方法接口
     */
    @Autowired
    private Tbl_Hardware_Type_Mapper tbl_Hardware_Type_Mapper;

    /**
     * redis缓存服务器方法接口
     */
    @Autowired
    private RedisHelperI redisHelperI;

    @Autowired
    private Tbl_Vender_list_Mapper tbl_Vender_list_Mapper;

    @Override
    public HardwareType getHardwareTypeByTypeCode(String code) throws Exception {
        HardwareType hardwareType = null;
        String key = RedisKey.HardWareType_key + code;
        String redisString = redisHelperI.getRedis(key);
        if (StringUtils.isNotBlank(redisString)) {
            hardwareType = (HardwareType) StringUtil.JsonToBean(redisString, HardwareType.class);
        } else {
            hardwareType = tbl_Hardware_Type_Mapper.getHardwareTypeByTypeCode(code);
            if (hardwareType != null && StringUtils.isNotBlank(hardwareType.getHardwareType())) {
                redisHelperI.setRedis(key, StringUtil.ObjectToJson(hardwareType));
            }
        }
        return hardwareType;
    }

    @Override
    public VenderList getVenderListByBrandCode(String code) throws Exception {
        VenderList venderList = new VenderList();
        VenderListBean venderListBean = null;
        String key = RedisKey.VenderList_key + code;
        String redisString = redisHelperI.getRedis(key);
        if (StringUtils.isNotBlank(redisString)) {
            venderList = (VenderList) StringUtil.JsonToBean(redisString, VenderList.class);
        } else {
            venderListBean = tbl_Vender_list_Mapper.getVenderListByBrandCode(code);
            if (venderListBean != null && StringUtils.isNotBlank(venderListBean.getBrandCode())) {
                venderList.setBrandCode(venderListBean.getBrandCode());
                venderList.setBrandIntroduce(venderListBean.getBrandIntroduce());
                venderList.setBrandName(venderListBean.getBrandName());
                venderList.setLogo(venderListBean.getBrandLogo());
                redisHelperI.setRedis(key, StringUtil.ObjectToJson(venderList));
            }
        }
        return venderList;
    }

}
