package com.starry.admin.modules.custom.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.starry.admin.modules.custom.mapper.PlayCustomGiftInfoMapper;
import com.starry.admin.modules.custom.module.entity.PlayCustomGiftInfoEntity;
import com.starry.admin.modules.custom.service.IPlayCustomGiftInfoService;
import com.starry.common.utils.IdUtils;
import java.util.Arrays;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;

/**
 * 顾客和礼物关系Service业务层处理
 *
 * @author admin
 * @since 2024-06-05
 */
@Service
public class PlayCustomGiftInfoServiceImpl extends ServiceImpl<PlayCustomGiftInfoMapper, PlayCustomGiftInfoEntity>
        implements
            IPlayCustomGiftInfoService {
    @Resource
    private PlayCustomGiftInfoMapper playCustomGiftInfoMapper;

    @Override
    public PlayCustomGiftInfoEntity selectByGiftIdAndCustomId(String giftId, String customId) {
        LambdaQueryWrapper<PlayCustomGiftInfoEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(PlayCustomGiftInfoEntity::getCustomId, customId);
        lambdaQueryWrapper.eq(PlayCustomGiftInfoEntity::getGiffId, giftId);
        return this.baseMapper.selectOne(lambdaQueryWrapper);
    }

    /**
     * 根据店员ID查询店员活动礼物列表
     *
     * @param customId
     *            店员ID
     * @return 店员活动礼物列表
     */
    @Override
    public List<PlayCustomGiftInfoEntity> selectByCustomId(String customId, String tenantId) {
        LambdaQueryWrapper<PlayCustomGiftInfoEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(PlayCustomGiftInfoEntity::getCustomId, customId);
        if (StrUtil.isNotBlank(tenantId)) {
            lambdaQueryWrapper.eq(PlayCustomGiftInfoEntity::getTenantId, tenantId);
        }
        return this.baseMapper.selectList(lambdaQueryWrapper);
    }

    /**
     * 查询顾客和礼物关系
     *
     * @param id
     *            顾客和礼物关系主键
     * @return 顾客和礼物关系
     */
    @Override
    public PlayCustomGiftInfoEntity selectPlayCustomGiftInfoById(String id) {
        return this.baseMapper.selectById(id);
    }

    /**
     * 查询顾客和礼物关系列表
     *
     * @param playCustomGiftInfo
     *            顾客和礼物关系
     * @return 顾客和礼物关系
     */
    @Override
    public IPage<PlayCustomGiftInfoEntity> selectPlayCustomGiftInfoByPage(PlayCustomGiftInfoEntity playCustomGiftInfo) {
        Page<PlayCustomGiftInfoEntity> page = new Page<>(1, 10);
        return this.baseMapper.selectPage(page, new LambdaQueryWrapper<PlayCustomGiftInfoEntity>());
    }

    /**
     * 新增顾客和礼物关系
     *
     * @param playCustomGiftInfo
     *            顾客和礼物关系
     * @return 结果
     */
    @Override
    public boolean create(PlayCustomGiftInfoEntity playCustomGiftInfo) {
        if (StrUtil.isBlankIfStr(playCustomGiftInfo.getId())) {
            playCustomGiftInfo.setId(IdUtils.getUuid());
        }
        return save(playCustomGiftInfo);
    }

    /**
     * 修改顾客和礼物关系
     *
     * @param playCustomGiftInfo
     *            顾客和礼物关系
     * @return 结果
     */
    @Override
    public boolean update(PlayCustomGiftInfoEntity playCustomGiftInfo) {
        return updateById(playCustomGiftInfo);
    }

    /**
     * 原子递增顾客礼物数量，无记录时按增量初始化
     *
     * @param customId 顾客ID
     * @param giftId   礼物ID
     * @param tenantId 租户ID
     * @param delta    增量
     */
    @Override
    public void incrementGiftCount(String customId, String giftId, String tenantId, long delta) {
        if (delta <= 0) {
            throw new IllegalArgumentException("delta must be positive");
        }
        int updated = playCustomGiftInfoMapper.incrementGiftCount(customId, giftId, tenantId, delta);
        if (updated == 0) {
            PlayCustomGiftInfoEntity entity = new PlayCustomGiftInfoEntity();
            entity.setId(IdUtils.getUuid());
            entity.setCustomId(customId);
            entity.setTenantId(tenantId);
            entity.setGiffId(giftId);
            entity.setGiffNumber(delta);
            boolean inserted = false;
            try {
                inserted = this.save(entity);
            } catch (DuplicateKeyException ex) {
                // ignore and retry update below
            }
            if (!inserted) {
                playCustomGiftInfoMapper.incrementGiftCount(customId, giftId, tenantId, delta);
            }
        }
    }

    /**
     * 批量删除顾客和礼物关系
     *
     * @param ids
     *            需要删除的顾客和礼物关系主键
     * @return 结果
     */
    @Override
    public int deletePlayCustomGiftInfoByIds(String[] ids) {
        return playCustomGiftInfoMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 删除顾客和礼物关系信息
     *
     * @param id
     *            顾客和礼物关系主键
     * @return 结果
     */
    @Override
    public int deletePlayCustomGiftInfoById(String id) {
        return playCustomGiftInfoMapper.deleteById(id);
    }
}
