package com.starry.admin.modules.play.service.impl;

import com.starry.common.utils.IdUtils;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.starry.admin.common.exception.CustomException;
import com.starry.admin.modules.play.mapper.PlayResourcesInfoMapper;
import com.starry.admin.modules.play.module.entity.PlayResourcesInfoEntity;
import com.starry.admin.modules.play.service.IPlayResourcesInfoService;
import com.starry.admin.utils.SecurityUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Date;

/**
 * 陪聊资源Service业务层处理
 *
 * @author admin
 * @since 2024-03-24
 */
@Service
public class PlayResourcesInfoServiceImpl extends ServiceImpl<PlayResourcesInfoMapper, PlayResourcesInfoEntity> implements IPlayResourcesInfoService {
    @Resource
    private PlayResourcesInfoMapper playResourcesInfoMapper;

    /**
     * 查询陪聊资源
     *
     * @param id 陪聊资源主键
     * @return 陪聊资源
     */
    @Override
    public PlayResourcesInfoEntity selectPlayResourcesInfoById(String id) {
        return this.baseMapper.selectById(id);
    }

    /**
     * 查询陪聊资源列表
     *
     * @param playResourcesInfo 陪聊资源
     * @return 陪聊资源
     */
    @Override
    public IPage<PlayResourcesInfoEntity> selectPlayResourcesInfoByPage(PlayResourcesInfoEntity playResourcesInfo) {
        Page<PlayResourcesInfoEntity> page = new Page<>(1, 10);
        return this.baseMapper.selectPage(page, new LambdaQueryWrapper<PlayResourcesInfoEntity>());
    }

    /**
     * 新增陪聊资源
     *
     * @param playResourcesInfo 陪聊资源
     * @return 结果
     */
    @Override
    public boolean create(PlayResourcesInfoEntity playResourcesInfo) {
        if (StrUtil.isBlankIfStr(playResourcesInfo.getId())) {
            playResourcesInfo.setId(IdUtils.getUuid());
        }
        playResourcesInfo.setPlayUserId(SecurityUtils.getLoginUser().getUserId());
        return save(playResourcesInfo);
    }


    @Override
    public boolean examinePlayResourcesInfo(String id, boolean success, String remark) {
        PlayResourcesInfoEntity entity = this.selectPlayResourcesInfoById(id);
        if (entity == null) {
            throw new CustomException("资源不存在，请检查");
        }
        entity.setResourcesState(success ? "1" : "2");
        entity.setReviewedBy(SecurityUtils.getLoginUser().getUserId());
        entity.setReviewedTime(new Date());
        entity.setReviewedRemark(remark);
        // 审核通过后，先删除旧的资源，然后将新的资源标识审核通过状态
        if (success) {
            // 查询当前用户，所有同类型的资源，然后删除之前的资源
            LambdaQueryWrapper<PlayResourcesInfoEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(PlayResourcesInfoEntity::getPlayUserId, entity.getPlayUserId());
            lambdaQueryWrapper.eq(PlayResourcesInfoEntity::getTenantId, entity.getTenantId());
            lambdaQueryWrapper.eq(PlayResourcesInfoEntity::getResourcesType, entity.getResourcesType());
            for (PlayResourcesInfoEntity item : this.baseMapper.selectList(lambdaQueryWrapper)) {
                if (!id.equals(item.getId())) {
                    this.deletePlayResourcesInfoById(item.getId());
                }
            }
        }
        this.update(entity);
        // 查询旧资源
        return true;
    }

    /**
     * 修改陪聊资源
     *
     * @param playResourcesInfo 陪聊资源
     * @return 结果
     */
    @Override
    public boolean update(PlayResourcesInfoEntity playResourcesInfo) {
        return updateById(playResourcesInfo);
    }

    /**
     * 批量删除陪聊资源
     *
     * @param ids 需要删除的陪聊资源主键
     * @return 结果
     */
    @Override
    public int deletePlayResourcesInfoByIds(String[] ids) {
        return playResourcesInfoMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 删除陪聊资源信息
     *
     * @param id 陪聊资源主键
     * @return 结果
     */
    @Override
    public int deletePlayResourcesInfoById(String id) {
        return playResourcesInfoMapper.deleteById(id);
    }
}
