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.PageBuilder;
import com.starry.admin.common.exception.CustomException;
import com.starry.admin.modules.custom.module.entity.PlayCustomGiftInfoEntity;
import com.starry.admin.modules.custom.service.IPlayCustomGiftInfoService;
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.IPlayClerkGiftInfoService;
import com.starry.admin.modules.shop.service.IPlayGiftInfoService;
import com.starry.admin.modules.weichat.entity.gift.PlayClerkGiftReturnVo;
import com.starry.common.utils.IdUtils;
import com.starry.common.utils.StringUtils;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
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 IPlayClerkGiftInfoService clerkGiftInfoService;

    @Resource
    private IPlayCustomGiftInfoService 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 tenantId, String customId, String obtained) {
        if ("0".equals(obtained)) {
            LambdaQueryWrapper<PlayGiftInfoEntity> giftWrapper = new LambdaQueryWrapper<>();
            giftWrapper.eq(PlayGiftInfoEntity::getState, "0");
            giftWrapper.eq(PlayGiftInfoEntity::getHistory, "0");
            giftWrapper.eq(StrUtil.isNotBlank(tenantId), PlayGiftInfoEntity::getTenantId, tenantId);
            List<PlayGiftInfoEntity> activeGifts = this.baseMapper.selectList(giftWrapper);

            List<PlayCustomGiftInfoEntity> obtainedRecords =
                    customGiftInfoService.selectByCustomId(customId, tenantId);
            Set<String> obtainedGiftIds = obtainedRecords.stream()
                    .map(PlayCustomGiftInfoEntity::getGiffId)
                    .collect(Collectors.toSet());

            return activeGifts.stream()
                    .filter(gift -> !obtainedGiftIds.contains(gift.getId()))
                    .map(this::toGiftReturnVoWithZeroCount)
                    .collect(Collectors.toList());
        }
        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);
            lambdaWrapper.eq(PlayGiftInfoEntity::getState, "0");
            lambdaWrapper.eq(PlayGiftInfoEntity::getHistory, "0");
            lambdaWrapper.eq(StrUtil.isNotBlank(tenantId), PlayGiftInfoEntity::getTenantId, tenantId);
            lambdaWrapper.eq(StrUtil.isNotBlank(tenantId), PlayCustomGiftInfoEntity::getTenantId, tenantId);
            return this.baseMapper.selectJoinList(PlayClerkGiftReturnVo.class, lambdaWrapper);
        }
        return new ArrayList<>();

    }

    @Override
    public List<PlayClerkGiftReturnVo> clerkListByAll(String tenantId, String clerkId, String obtained) {
        if ("0".equals(obtained)) {
            LambdaQueryWrapper<PlayGiftInfoEntity> giftWrapper = new LambdaQueryWrapper<>();
            giftWrapper.eq(PlayGiftInfoEntity::getState, "0");
            giftWrapper.eq(PlayGiftInfoEntity::getHistory, "0");
            giftWrapper.eq(StrUtil.isNotBlank(tenantId), PlayGiftInfoEntity::getTenantId, tenantId);
            List<PlayGiftInfoEntity> activeGifts = this.baseMapper.selectList(giftWrapper);

            List<PlayClerkGiftInfoEntity> obtainedRecords =
                    clerkGiftInfoService.selectBtyClerkId(clerkId, tenantId);
            Set<String> obtainedGiftIds = obtainedRecords.stream()
                    .map(PlayClerkGiftInfoEntity::getGiffId)
                    .collect(Collectors.toSet());

            return activeGifts.stream()
                    .filter(gift -> !obtainedGiftIds.contains(gift.getId()))
                    .map(this::toGiftReturnVoWithZeroCount)
                    .collect(Collectors.toList());
        }
        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);
            lambdaWrapper.eq(PlayGiftInfoEntity::getState, "0");
            lambdaWrapper.eq(PlayGiftInfoEntity::getHistory, "0");
            lambdaWrapper.eq(StrUtil.isNotBlank(tenantId), PlayGiftInfoEntity::getTenantId, tenantId);
            lambdaWrapper.eq(StrUtil.isNotBlank(tenantId), PlayClerkGiftInfoEntity::getTenantId, tenantId);
            return this.baseMapper.selectJoinList(PlayClerkGiftReturnVo.class, lambdaWrapper);
        }
        return new ArrayList<>();
    }

    @Override
    public List<PlayGiftInfoEntity> listByAll() {
        LambdaQueryWrapper<PlayGiftInfoEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PlayGiftInfoEntity::getState, "0");
        wrapper.eq(PlayGiftInfoEntity::getHistory, "0");
        wrapper.orderByAsc(PlayGiftInfoEntity::getListingTime);
        return this.baseMapper.selectList(wrapper);
    }

    private PlayClerkGiftReturnVo toGiftReturnVoWithZeroCount(PlayGiftInfoEntity gift) {
        PlayClerkGiftReturnVo vo = new PlayClerkGiftReturnVo();
        vo.setId(gift.getId());
        vo.setName(gift.getName());
        vo.setType(gift.getType());
        vo.setUrl(gift.getUrl());
        vo.setPrice(gift.getPrice());
        vo.setUnit(gift.getUnit());
        vo.setState(gift.getState());
        vo.setGiffNumber(0L);
        return vo;
    }

    /**
     * 查询礼物列表
     *
     * @param playGiftInfo
     *            礼物
     * @return 礼物
     */
    @Override
    public IPage<PlayGiftInfoEntity> selectPlayGiftInfoByPage(PlayGiftInfoEntity playGiftInfo) {
        Page<PlayGiftInfoEntity> page = PageBuilder.build();
        LambdaQueryWrapper<PlayGiftInfoEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotBlank(playGiftInfo.getName()), PlayGiftInfoEntity::getName, playGiftInfo.getName());
        wrapper.eq(StringUtils.isNotBlank(playGiftInfo.getState()), PlayGiftInfoEntity::getState, playGiftInfo.getState());
        return this.baseMapper.selectPage(page, wrapper);
    }

    /**
     * 新增礼物
     *
     * @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);
    }
}
