package com.ifish.serviceImpl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ifish.dto.AdInfoDto;
import com.ifish.dto.JsonResult;
import com.ifish.entity.AdInfo;
import com.ifish.enums.AdTypeEnum;
import com.ifish.enums.SubDirectoryEnum;
import com.ifish.mapper.Tbl_Ad_Info_Mapper;
import com.ifish.service.AdInfoService;
import com.ifish.service.CacheService;
import com.ifish.util.IfishFilePath;
import com.ifish.util.ResultUtil;

/**
 * 广告位信息
 *
 * @author Administrator
 *
 */
@Service
@Transactional
public class AdInfoServiceImpl implements AdInfoService {

    @Autowired
    private CacheService cacheService;

    @Autowired
    private Tbl_Ad_Info_Mapper tbl_Ad_Info_Mapper;

    /**
     * 根据类型获取广告信息
     */
    @Override
    public JsonResult<?> getAdInfosByType(AdTypeEnum adTypeEnum) {
        //根据类型查询广告
        List<AdInfo> adInfos = cacheService.getAdInfoList(adTypeEnum.getKey());
        //封装广告信息并返回
        List<AdInfoDto> dtoList = new ArrayList<AdInfoDto>();
        for (AdInfo adInfo : adInfos) {
            AdInfoDto dto = new AdInfoDto(adInfo.getAdId(), adInfo.getAdType(), IfishFilePath.getPath(SubDirectoryEnum.adInfo, adInfo.getAdImage()), adInfo.getAdLink(), adInfo.getOrderNum());
            dtoList.add(dto);
        }
        return ResultUtil.success(dtoList);
    }

    @Override
    public JsonResult<?> getMaxInfo() {
        Integer ad_id = tbl_Ad_Info_Mapper.getMaxInfo();
        return ResultUtil.success(ad_id);
    }

}
