/*
 * Copyright [2022] [https://www.xiaonuo.vip]
 *
 * Snowy采用APACHE LICENSE 2.0开源协议，您在使用过程中，需要注意以下几点：
 *
 * 1.请不要删除和修改根目录下的LICENSE文件。
 * 2.请不要删除和修改Snowy源码头部的版权声明。
 * 3.本项目代码可免费商业使用，商业使用请保留源码和相关描述文件的项目出处，作者声明等。
 * 4.分发源码时候，请注明软件出处 https://www.xiaonuo.vip
 * 5.不可二次分发开源参与同类竞品，如有想法可联系团队xiaonuobase@qq.com商议合作。
 * 6.若您的项目无法满足以上几点，需要更多功能代码，获取Snowy商业授权许可，请在官网购买授权，地址为 https://www.xiaonuo.vip
 */
package vip.xiaonuo.biz.modular.bizuservideo.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollStreamUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import vip.xiaonuo.common.enums.CommonSortOrderEnum;
import vip.xiaonuo.common.exception.CommonException;
import vip.xiaonuo.common.page.CommonPageRequest;
import vip.xiaonuo.biz.modular.bizuservideo.entity.BizUserVideo;
import vip.xiaonuo.biz.modular.bizuservideo.mapper.BizUserVideoMapper;
import vip.xiaonuo.biz.modular.bizuservideo.param.BizUserVideoAddParam;
import vip.xiaonuo.biz.modular.bizuservideo.param.BizUserVideoEditParam;
import vip.xiaonuo.biz.modular.bizuservideo.param.BizUserVideoIdParam;
import vip.xiaonuo.biz.modular.bizuservideo.param.BizUserVideoPageParam;
import vip.xiaonuo.biz.modular.bizuservideo.service.BizUserVideoService;

import java.util.List;

/**
 * 用户视频关联表Service接口实现类
 *
 * @author fanzherong
 * @date  2025/02/17 16:03
 **/
@Service
public class BizUserVideoServiceImpl extends ServiceImpl<BizUserVideoMapper, BizUserVideo> implements BizUserVideoService {

    @Override
    public Page<BizUserVideo> page(BizUserVideoPageParam bizUserVideoPageParam) {
        QueryWrapper<BizUserVideo> queryWrapper = new QueryWrapper<>();
        if(ObjectUtil.isAllNotEmpty(bizUserVideoPageParam.getSortField(), bizUserVideoPageParam.getSortOrder())) {
            CommonSortOrderEnum.validate(bizUserVideoPageParam.getSortOrder());
            queryWrapper.orderBy(true, bizUserVideoPageParam.getSortOrder().equals(CommonSortOrderEnum.ASC.getValue()),
                    StrUtil.toUnderlineCase(bizUserVideoPageParam.getSortField()));
        } else {
            queryWrapper.lambda().orderByAsc(BizUserVideo::getId);
        }
        return this.page(CommonPageRequest.defaultPage(), queryWrapper);
    }

    @Override
    public void add(BizUserVideoAddParam bizUserVideoAddParam) {
        /*BizUserVideo bizUserVideo = BeanUtil.toBean(bizUserVideoAddParam, BizUserVideo.class);
        this.save(bizUserVideo);*/
        //根据视频id查询用户信息
        List<BizUserVideo> list = this.list(new QueryWrapper<BizUserVideo>().lambda().eq(BizUserVideo::getVideoId, bizUserVideoAddParam.getVideoId()));
        //先清除之前的记录在重新添加
        this.removeBatchByIds(CollStreamUtil.toList(list, BizUserVideo::getId));
        if(ObjectUtil.isNotEmpty(bizUserVideoAddParam.getUserIdList())){
            for(String userId : bizUserVideoAddParam.getUserIdList()){
                BizUserVideo bizUserVideo = new BizUserVideo();
                bizUserVideo.setUserId(userId);
                bizUserVideo.setVideoId(bizUserVideoAddParam.getVideoId());
                this.save(bizUserVideo);
            }
        }
    }

    @Override
    public void edit(BizUserVideoEditParam bizUserVideoEditParam) {
        BizUserVideo bizUserVideo = this.queryEntity(bizUserVideoEditParam.getId());
        BeanUtil.copyProperties(bizUserVideoEditParam, bizUserVideo);
        this.updateById(bizUserVideo);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(List<BizUserVideoIdParam> bizUserVideoIdParamList) {
        // 执行删除
        this.removeBatchByIds(CollStreamUtil.toList(bizUserVideoIdParamList, BizUserVideoIdParam::getId));
    }

    @Override
    public BizUserVideo detail(BizUserVideoIdParam bizUserVideoIdParam) {
        return this.queryEntity(bizUserVideoIdParam.getId());
    }

    @Override
    public BizUserVideo queryEntity(String id) {
        BizUserVideo bizUserVideo = this.getById(id);
        if(ObjectUtil.isEmpty(bizUserVideo)) {
            throw new CommonException("用户视频关联表不存在，id值为：{}", id);
        }
        return bizUserVideo;
    }

    @Override
    public List<BizUserVideo> list(BizUserVideoPageParam bizUserVideoPageParam) {
        QueryWrapper<BizUserVideo> queryWrapper = new QueryWrapper<>();
        if(ObjectUtil.isNotEmpty(bizUserVideoPageParam.getVideoId())){
            queryWrapper.lambda().eq(BizUserVideo::getVideoId,bizUserVideoPageParam.getVideoId());
        }
        return this.list(queryWrapper);
    }
}
