package com.ruoyi.frequency.banner.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.enums.user.UserEnums;
import com.ruoyi.frequency.banner.entity.Banner;
import com.ruoyi.frequency.banner.mapper.BannerMapper;
import com.ruoyi.frequency.banner.service.BannerService;
import com.ruoyi.frequency.customer.service.UserService;
import com.ruoyi.vo.UserInfo;
import com.ruoyi.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 轮播图Service业务层处理
 *
 * @author liwenlong
 * @date 2023-07-20
 */
@Service
public class BannerServiceImpl extends ServiceImpl<BannerMapper, Banner> implements BannerService {
    @Autowired
    private BannerMapper bannerMapper;


    /**
     * 查询轮播图
     *
     * @param id 轮播图主键
     * @return 轮播图
     */
    @Override
    public Banner selectBannerById(Long id) {

        Banner banner = bannerMapper.selectBannerById(id);

        if (banner.getType() == 4 || banner.getType() == 5) {
            Set<UserVo> userVoSet = new HashSet<>();

            UserVo userVo = new UserVo(banner.getType() == 4 ? UserEnums.customer.getCode() : UserEnums.store.getCode(), Long.valueOf(banner.getContent()));

            userVoSet.add(userVo);

            //查询用户信息
            Map<UserVo, UserInfo> userMap = userService.selectUserInfoMap(userVoSet, null);

            banner.setUserInfo(userMap.get(userVo));
        } else if (banner.getType() == 6) {
            String goodsName = bannerMapper.getMallGoodsDetail(banner.getContent());
            banner.setGoodsName(goodsName);
        }


        return banner;
    }


    @Autowired
    private UserService userService;

    public static void main(String[] args) {
        System.out.println();
    }

    /**
     * 查询轮播图列表
     *
     * @param banner 轮播图
     * @return 轮播图
     */
    @Override
    public List<Banner> selectBannerList(Banner banner) {

        List<Banner> banners = bannerMapper.selectBannerList(banner);

        if (!banners.isEmpty()) {
            Set<UserVo> userVoSet = banners.stream().filter(s -> s.getType() == 4 || s.getType() == 5)
                    .map(s -> new UserVo(s.getType() == 4 ? UserEnums.customer.getCode() : UserEnums.store.getCode(), Long.valueOf(s.getContent())))
                    .collect(Collectors.toSet());
            //查询用户信息
            Map<UserVo, UserInfo> userMap = userService.selectUserInfoMap(userVoSet, null);

            banners.stream().forEach(s -> {
                if (s.getType() == 4 || s.getType() == 5) {
                    s.setUserInfo(userMap.get(new UserVo(s.getType() == 4 ? UserEnums.customer.getCode() : UserEnums.store.getCode(), Long.valueOf(s.getContent()))));
                }
            });
        }

        return banners;
    }

    /**
     * 新增轮播图
     *
     * @param banner 轮播图
     * @return 结果
     */
    @Override
    public int insertBanner(Banner banner) {
        banner.setCreateTime(DateUtils.getNowDate());
        return bannerMapper.insert(banner);
    }

    /**
     * 修改轮播图
     *
     * @param banner 轮播图
     * @return 结果
     */
    @Override
    public int updateBanner(Banner banner) {
        banner.setUpdateTime(DateUtils.getNowDate());
        return bannerMapper.updateById(banner);
    }

    /**
     * 批量删除轮播图
     *
     * @param ids 需要删除的轮播图主键
     * @return 结果
     */
    @Override
    public int deleteBannerByIds(Long[] ids, Integer delFlag) {
        this.lambdaUpdate().set(Banner::getDelFlag, delFlag).in(Banner::getId, ids).update();
        return ids.length;
    }

    /**
     * 删除轮播图信息
     *
     * @param id 轮播图主键
     * @return 结果
     */
    @Override
    public int deleteBannerById(Long id) {
        return bannerMapper.deleteBannerById(id);
    }
}
