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

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.code.PublicCommon;
import com.ruoyi.enums.user.UserEnums;
import com.ruoyi.frequency.activity.entity.Activity;
import com.ruoyi.frequency.activity.service.ActivityService;
import com.ruoyi.frequency.advertisingimages.entity.AdvertisingImages;
import com.ruoyi.frequency.advertisingimages.mapper.AdvertisingImagesMapper;
import com.ruoyi.frequency.advertisingimages.service.AdvertisingImagesService;
import com.ruoyi.frequency.customer.service.UserService;
import com.ruoyi.frequency.news.entity.News;
import com.ruoyi.frequency.news.service.NewsService;
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;

/**
 * 广告图Service业务层处理
 *
 * @author liwenlong
 * @date 2024-02-05
 */
@Service
public class AdvertisingImagesServiceImpl extends ServiceImpl<AdvertisingImagesMapper, AdvertisingImages> implements AdvertisingImagesService {
    @Autowired
    private AdvertisingImagesMapper advertisingImagesMapper;

    @Autowired
    private UserService userService;

    @Autowired
    private ActivityService activityService;

    @Autowired
    private NewsService newsService;

    /**
     * 查询广告图
     *
     * @param id 广告图主键
     * @return 广告图
     */
    @Override
    public AdvertisingImages selectAdvertisingImagesById(Long id) {
        AdvertisingImages advertisingImages = advertisingImagesMapper.selectAdvertisingImagesById(id);
        //类型 1富文本 2 设计师主页 3 表现师主页   4 首页   5活动页面   6新闻页面
        if (advertisingImages.getType() != null) {
            if (advertisingImages.getType() == 2 || advertisingImages.getType() == 3) {
                Set<UserVo> userVoSet = new HashSet<>();

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

                userVoSet.add(userVo);

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

                advertisingImages.setUserInfo(userMap.get(userVo));
            } else if (advertisingImages.getType() == 5) {
                Activity activity = activityService.getById(advertisingImages.getContent());
                if (activity != null) {
                    advertisingImages.setActivityTitle(activity.getTitle());
                }
            } else if (advertisingImages.getType() == 6) {
                News news = newsService.newsInfo(Long.valueOf(advertisingImages.getContent()));
                if (news != null) {
                    advertisingImages.setNewsTitle(news.getTitle());
                }
            }
        }

        return advertisingImages;
    }

    /**
     * 查询广告图列表
     *
     * @param advertisingImages 广告图
     * @return 广告图
     */
    @Override
    public List<AdvertisingImages> selectAdvertisingImagesList(AdvertisingImages advertisingImages) {
        return advertisingImagesMapper.selectAdvertisingImagesList(advertisingImages);
    }

    /**
     * 新增广告图
     *
     * @param advertisingImages 广告图
     * @return 结果
     */
    @Override
    public int insertAdvertisingImages(AdvertisingImages advertisingImages) {
        advertisingImages.setCreateData();

        int insert = advertisingImagesMapper.insert(advertisingImages);

        if (insert > 0) {
            this.lambdaUpdate().set(AdvertisingImages::getDelFlag, PublicCommon.禁用)
                    .eq(AdvertisingImages::getDelFlag, PublicCommon.启用)
                    .ne(AdvertisingImages::getId, advertisingImages.getId()).update();
        }
        return insert;
    }

    /**
     * 修改广告图
     *
     * @param advertisingImages 广告图
     * @return 结果
     */
    @Override
    public int updateAdvertisingImages(AdvertisingImages advertisingImages) {
        advertisingImages.setUpdateData();
        return advertisingImagesMapper.updateAdvertisingImages(advertisingImages);
    }

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

        return ids.length;
    }

}
