package com.younike.system.service.impl;

import java.util.List;
import java.time.LocalDateTime;

import com.younike.common.core.domain.AjaxResult;
import com.younike.common.core.domain.entity.SysUser;
import com.younike.common.exception.GlobalException;
import com.younike.common.exception.ServiceException;
import com.younike.common.utils.SecurityUtils;
import com.younike.system.domain.ServerUser;
import com.younike.system.param.VideoParam;
import com.younike.system.domain.vo.VideoVo;
import com.younike.system.service.IMerchantService;
import com.younike.system.service.IServerUserService;
import com.younike.system.service.ISysUserService;
import jodd.util.StringUtil;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.younike.system.mapper.VideoMapper;
import com.younike.system.domain.Video;
import com.younike.system.service.IVideoService;

/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author elev
 * @date 2022-04-13
 */
@Service
public class VideoServiceImpl implements IVideoService {
    @Autowired
    private VideoMapper videoMapper;
    @Autowired
    private IMerchantService merchantService;
    @Autowired
    private IServerUserService serverUserService;
    @Autowired
    private ISysUserService sysUserService;


    /**
     * 查询【请填写功能名称】
     *
     * @param videoId 【请填写功能名称】主键
     * @return 【请填写功能名称】
     */
    @Override
    public Video selectVideoByVideoId(Long videoId) {
        return videoMapper.selectByVideoId(videoId);
    }

    /**
     * 新增【请填写功能名称】
     *
     * @param video 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertVideo(Video video) {
        //获取当前用户数据
        if(video.getServerId() != null){
            //查询服务人员数据
            ServerUser serverUser = serverUserService.selectServerUserByServerId(video.getServerId());
            if(ObjectUtils.isEmpty(serverUser)){
                throw new GlobalException("服务人员不存在");
            }
            video.setMerchantId(serverUser.getMerchantId());
        }
        video.setCreateTime(LocalDateTime.now());
        video.setUpdateTime(LocalDateTime.now());
        return videoMapper.insertVideo(video);
    }

    /**
     * 修改【请填写功能名称】
     *
     * @param video 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateVideo(Video video) {
        video.setUpdateTime(LocalDateTime.now());
        return videoMapper.updateVideo(video);
    }

    /**
     * 批量删除【请填写功能名称】
     *
     * @param videoIds 需要删除的【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteVideoByVideoIds(Long[] videoIds) {
        return videoMapper.deleteVideoByVideoIds(videoIds);
    }

    /**
     * 删除【请填写功能名称】信息
     *
     * @param videoId 【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteVideoByVideoId(Long videoId)
    {
        return videoMapper.deleteByVideoId(videoId);
    }

    /**
     * 服务人员获得列表数据
     * @param video
     * @return
     */
    @Override
    public List<VideoVo> getVideoList(VideoParam video) {
        List<VideoVo> list =videoMapper.getVideoList(video);
        if(list.size()>0){
            for (VideoVo vo:list) {
                //判断是否是个人商家
                if(vo.getMerchantType()==1){
                    //查询个人商家的名称
                    SysUser sysUser = sysUserService.selectSysUserById(vo.getAdminId());
                    if (!ObjectUtils.isEmpty(sysUser)){
                        vo.setMerchantName(sysUser.getNickName());
                    }
                }
                //判断父分类名称是否为空
                if(!StringUtil.isEmpty(vo.getParentCategoryName())){
                    vo.setVideoCategoryName(vo.getParentCategoryName()+"—"+vo.getVideoCategoryName());
                }else {
                    vo.setParentCategoryName(vo.getVideoCategoryName());
                }

            }
        }
        return list;
    }

    /**
     * 删除视频
     * @param videoId
     * @return
     */
    @Override
    public int remove(Long videoId) {
        Video video = new Video();
        video.setVideoId(videoId);
        video.setDelFlag("1");
        return this.updateVideo(video);
    }

    /**
     * 视频详情
     * @param videoId
     * @return
     */
    @Override
    public VideoVo getInfo(Long videoId) {
    VideoVo videoVo = videoMapper.getInfo(videoId);
        if (ObjectUtils.isEmpty(videoVo)) {
            throw new GlobalException("视频不存在");
        }
        //判断是否是个人商家
        if (videoVo.getMerchantType() == 1) {
            //查询个人商家的名称
            SysUser sysUser = sysUserService.selectSysUserById(videoVo.getAdminId());
            if (!ObjectUtils.isEmpty(sysUser)) {
                videoVo.setMerchantName(sysUser.getNickName());
            }
        }
        //判断父分类名称是否为空
        if (!StringUtil.isEmpty(videoVo.getParentCategoryName())) {
            videoVo.setVideoCategoryName(videoVo.getParentCategoryName() + "——" + videoVo.getVideoCategoryName());
        }
        return videoVo;
    }

    /**
     * 会员端：获取首页运营推荐的三条视频
     * @return
     */
    @Override
    public List<VideoVo> getRecommendVideoList(VideoParam video) {
//        video.setIsOperationTop("1");
       return this.memberVideoList(video);
    }

    /**
     * 服务人员视频列表
     * @param video
     * @return
     */
    @Override
    public List<VideoVo> serverVideoList(VideoParam video) {
        List<VideoVo> list = videoMapper.serverVideoList(video);
        if(list.size()>0){
            for (VideoVo vo:list) {
                //判断是否是个人商家
                if(vo.getMerchantType()==1){
                    //查询个人商家的名称
                    SysUser sysUser = sysUserService.selectSysUserById(vo.getAdminId());
                    if (!ObjectUtils.isEmpty(sysUser)){
                        vo.setMerchantName(sysUser.getNickName());
                    }
                }
                //判断父分类名称是否为空
                if(!StringUtil.isEmpty(vo.getParentCategoryName())){
                    vo.setVideoCategoryName(vo.getParentCategoryName()+"——"+vo.getVideoCategoryName());
                }

            }
        }
        return list;
    }

    /**
     * 会员端：店铺视频列表
     * @param video
     * @return
     */
    @Override
    public List<VideoVo> merchantVideoList(VideoParam video) {
        return this.memberVideoList(video);
    }

    /**
     * 会员端：查询百科视频列表
     * @param video
     * @return
     */
    @Override
    public List<VideoVo> videoList(VideoParam video) {
        return this.memberVideoList(video);
    }

    /**
     * 服务人员端添加视频
     * @param video
     * @return
     */
    @Override
    public int serverInsertVideo(Video video) {
        //根据服务人员id获得服务人员数据
        ServerUser serverUser = serverUserService.selectServerUserByServerId(video.getServerId());
        if(ObjectUtils.isEmpty(video)){
           return 0;
        }
        video.setUpdateBy(serverUser.getNikeName());
        video.setCreateBy(serverUser.getNikeName());
        return this.insertVideo(video);
    }

    /**
     * 服务人员更新数据
     * @param video
     * @return
     */
    @Override
    public int serverUpdateVideo(Video video) {
        //根据服务人员id获得服务人员数据
        ServerUser serverUser = serverUserService.selectServerUserByServerId(video.getServerId());
        if(ObjectUtils.isEmpty(video)){
            return 0;
        }
        video.setUpdateBy(serverUser.getNikeName());
        return this.updateVideo(video);
    }


    /**
    * 会员端：获取首页运营推荐的视频、商家视频、全部视频列表
    */
    public List<VideoVo> memberVideoList(VideoParam video) {

        List<VideoVo> list = videoMapper.memberVideoList(video);
        if(list.size()>0){
            for (VideoVo vo:list) {
                //判断是否是个人商家
                if(vo.getMerchantType()==1){
                    //查询个人商家的名称
                    SysUser sysUser = sysUserService.selectSysUserById(vo.getAdminId());
                    if (!ObjectUtils.isEmpty(sysUser)){
                        vo.setMerchantName(sysUser.getNickName());
                    }
                }
                //判断父分类名称是否为空
                if(!StringUtil.isEmpty(vo.getParentCategoryName())){
                    vo.setVideoCategoryName(vo.getParentCategoryName()+"——"+vo.getVideoCategoryName());
                }

            }
        }
        return list;
    }
}
