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

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.github.yulichang.wrapper.MPJLambdaWrapper;
import com.starry.admin.common.exception.CustomException;
import com.starry.admin.modules.custom.module.entity.PlayCustomGiftInfoEntity;
import com.starry.admin.modules.custom.service.impl.PlayCustomGiftInfoServiceImpl;
import com.starry.admin.modules.shop.mapper.PlayGiftInfoMapper;
import com.starry.admin.modules.shop.module.entity.PlayClerkGiftInfoEntity;
import com.starry.admin.modules.shop.module.entity.PlayGiftInfoEntity;
import com.starry.admin.modules.shop.service.IPlayGiftInfoService;
import com.starry.admin.modules.weichat.entity.gift.PlayClerkGiftReturnVo;
import com.starry.common.utils.IdUtils;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;

/**
 * 礼物Service业务层处理
 *
 * @author admin
 * @since 2024-04-25
 */
@Service
public class PlayGiftInfoServiceImpl extends ServiceImpl<PlayGiftInfoMapper, PlayGiftInfoEntity>
        implements
            IPlayGiftInfoService {
    @Resource
    private PlayGiftInfoMapper playGiftInfoMapper;

    @Resource
    private PlayClerkGiftInfoServiceImpl clerkGiftInfoService;

    @Resource
    private PlayCustomGiftInfoServiceImpl customGiftInfoService;

    /**
     * 查询礼物
     *
     * @param id
     *            礼物主键
     * @return 礼物
     */
    @Override
    public PlayGiftInfoEntity selectPlayGiftInfoById(String id) {
        PlayGiftInfoEntity entity = this.baseMapper.selectById(id);
        if (entity == null) {
            throw new CustomException("礼物不存在");
        }
        return entity;
    }

    @Override
    public List<PlayClerkGiftReturnVo> customListByAll(String customId, String obtained) {
        if ("0".equals(obtained)) {
            // 查询所有礼物，然后减去已获得礼物
            MPJLambdaWrapper<PlayGiftInfoEntity> lambdaWrapper = new MPJLambdaWrapper<>();
            lambdaWrapper.selectAll(PlayGiftInfoEntity.class);
            List<PlayClerkGiftReturnVo> list = this.baseMapper.selectJoinList(PlayClerkGiftReturnVo.class,
                    lambdaWrapper);
            List<PlayCustomGiftInfoEntity> giftInfoEntities = customGiftInfoService.selectBtyCustomId(customId);
            // 使用迭代器安全地移除元素
            Iterator<PlayClerkGiftReturnVo> iterator = list.iterator();
            while (iterator.hasNext()) {
                PlayClerkGiftReturnVo item = iterator.next();
                for (PlayCustomGiftInfoEntity giftInfoEntity : giftInfoEntities) {
                    if (giftInfoEntity.getGiffId().equals(item.getId())) {
                        iterator.remove();
                        break;
                    }
                }

            }
            return list;
        }
        if ("1".equals(obtained)) {
            MPJLambdaWrapper<PlayGiftInfoEntity> lambdaWrapper = new MPJLambdaWrapper<>();
            lambdaWrapper.selectAll(PlayGiftInfoEntity.class);
            // 礼物活动表
            lambdaWrapper.selectAs(PlayCustomGiftInfoEntity::getGiffNumber, "giffNumber");
            lambdaWrapper.innerJoin(PlayCustomGiftInfoEntity.class, PlayCustomGiftInfoEntity::getGiffId,
                    PlayGiftInfoEntity::getId);
            lambdaWrapper.eq(PlayCustomGiftInfoEntity::getCustomId, customId);
            return this.baseMapper.selectJoinList(PlayClerkGiftReturnVo.class, lambdaWrapper);
        }
        return new ArrayList<>();

    }

    @Override
    public List<PlayClerkGiftReturnVo> clerkListByAll(String clerkId, String obtained) {
        if ("0".equals(obtained)) {
            // 查询所有礼物，然后减去已获得礼物
            MPJLambdaWrapper<PlayGiftInfoEntity> lambdaWrapper = new MPJLambdaWrapper<>();
            lambdaWrapper.selectAll(PlayGiftInfoEntity.class);
            List<PlayClerkGiftReturnVo> list = this.baseMapper.selectJoinList(PlayClerkGiftReturnVo.class,
                    lambdaWrapper);
            List<PlayClerkGiftInfoEntity> clerkGiftInfoEntities = clerkGiftInfoService.selectBtyClerkId(clerkId);
            // 使用迭代器安全地移除元素
            Iterator<PlayClerkGiftReturnVo> iterator = list.iterator();
            while (iterator.hasNext()) {
                PlayClerkGiftReturnVo item = iterator.next();
                for (PlayClerkGiftInfoEntity clerkGiftInfoEntity : clerkGiftInfoEntities) {
                    if (clerkGiftInfoEntity.getGiffId().equals(item.getId())) {
                        iterator.remove();
                        break;
                    }
                }

            }
            return list;
        }
        if ("1".equals(obtained)) {
            MPJLambdaWrapper<PlayGiftInfoEntity> lambdaWrapper = new MPJLambdaWrapper<>();
            lambdaWrapper.selectAll(PlayGiftInfoEntity.class);
            // 礼物活动表
            lambdaWrapper.selectAs(PlayClerkGiftInfoEntity::getGiffNumber, "giffNumber");
            lambdaWrapper.innerJoin(PlayClerkGiftInfoEntity.class, PlayClerkGiftInfoEntity::getGiffId,
                    PlayGiftInfoEntity::getId);
            lambdaWrapper.eq(PlayClerkGiftInfoEntity::getClerkId, clerkId);
            return this.baseMapper.selectJoinList(PlayClerkGiftReturnVo.class, lambdaWrapper);
        }
        return new ArrayList<>();
    }

    @Override
    public List<PlayGiftInfoEntity> listByAll() {
        return this.baseMapper.selectList(new LambdaQueryWrapper<>());
    }

    /**
     * 查询礼物列表
     *
     * @param playGiftInfo
     *            礼物
     * @return 礼物
     */
    @Override
    public IPage<PlayGiftInfoEntity> selectPlayGiftInfoByPage(PlayGiftInfoEntity playGiftInfo) {
        Page<PlayGiftInfoEntity> page = new Page<>(1, 10);
        return this.baseMapper.selectPage(page, new LambdaQueryWrapper<>());
    }

    /**
     * 新增礼物
     *
     * @param playGiftInfo
     *            礼物
     * @return 结果
     */
    @Override
    public boolean create(PlayGiftInfoEntity playGiftInfo) {
        if (StrUtil.isBlankIfStr(playGiftInfo.getId())) {
            playGiftInfo.setId(IdUtils.getUuid());
        }
        return save(playGiftInfo);
    }

    /**
     * 修改礼物
     *
     * @param playGiftInfo
     *            礼物
     * @return 结果
     */
    @Override
    public boolean update(PlayGiftInfoEntity playGiftInfo) {
        return updateById(playGiftInfo);
    }

    /**
     * 批量删除礼物
     *
     * @param ids
     *            需要删除的礼物主键
     * @return 结果
     */
    @Override
    public int deletePlayGiftInfoByIds(String[] ids) {
        return playGiftInfoMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 删除礼物信息
     *
     * @param id
     *            礼物主键
     * @return 结果
     */
    @Override
    public int deletePlayGiftInfoById(String id) {
        return playGiftInfoMapper.deleteById(id);
    }
}
