package com.xxfc.platform.app.biz;

import com.github.wxiaoqi.security.admin.feign.dto.UserDTO;
import com.github.wxiaoqi.security.common.biz.BaseBiz;
import com.github.wxiaoqi.security.common.vo.PageDataVO;
import com.xxfc.platform.app.entity.Banner;
import com.xxfc.platform.app.entity.vo.BannerVo;
import com.xxfc.platform.app.mapper.BannerMapper;
import org.springframework.stereotype.Service;

import java.time.Instant;
import java.util.ArrayList;
import java.util.List;

/**
 * @author libin
 * @version 1.0
 * @description TODO
 * @data 2019/6/26 9:24
 */
@Service
public class BannerBiz extends BaseBiz<BannerMapper,Banner> {

/*    @CacheClear(key = RedisKey.CONSTANT_CODE_PREFIX_BANNER)
    public void update(Banner banner) {
        this.updateSelectiveById(banner);
    }


    @CacheClear(key = RedisKey.CONSTANT_CODE_PREFIX_BANNER)
    public void remove(int id) {
        this.deleteById(id);
    }*/

    public List<BannerVo> findBannerList(Integer type,Integer location) {
        List<BannerVo> bannerVos = new ArrayList<>();
        List<Banner> banners = mapper.findBannerListByType(type,location);
        banners.forEach(banner -> {
            BannerVo bannerVo = new BannerVo();
            bannerVo.setCover(banner.getCover());
            bannerVo.setUrl(banner.getUrl());
            bannerVos.add(bannerVo);
        });
        return bannerVos;
    }

/*
    */
/**
     * 更改banner信息
     * @param banner
     * @param userInfo
     * @return
     *//*

    public int update(Banner banner, UserDTO userInfo) {
        banner.setUpdName(userInfo.getUsername());
        banner.setUpdTime(Instant.now().toEpochMilli());
        return  mapper.updateByPrimaryKeySelective(banner);
    }
*/

    /**
     * 分页查询banner
     * @param pageNo
     * @param pageSize
     * @return
     */
    public PageDataVO<Banner> findBannePage(Integer pageNo, Integer pageSize) {
        return  PageDataVO.pageInfo(pageNo, pageSize, () -> mapper.findBannerListByisDelOrderByRank(Banner.builder().isDel(0).build()));
    }

    /**
     * 保存或更新banner
     * @param banner
     * @param userInfo
     * @return
     */
    public int save(Banner banner, UserDTO userInfo) {
        if (banner.getId()!=null){
            banner.setUpdName(userInfo.getUsername());
            banner.setUpdTime(Instant.now().toEpochMilli());
            return mapper.updateByPrimaryKeySelective(banner);
        }else {
            banner.setCrtName(userInfo.getUsername());
            banner.setCrtTime(Instant.now().toEpochMilli());
            banner.setIsDel(0);
            banner.setRank(banner.getRank()==null?0:banner.getRank());
            return mapper.insertSelective(banner);
        }
    }

    /**
     * 逻辑删除
     * @param id
     * @param status
     * @param userInfo
     * @return
     */
    public int updateBannerStatus(Integer id, int status, UserDTO userInfo) {
        return  mapper.updateByPrimaryKeySelective(Banner.builder().id(Long.valueOf(id)).isDel(status).updName(userInfo.getUsername()).updTime(Instant.now().toEpochMilli()).build());
    }
}
