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.starry.admin.modules.shop.mapper.PlayClerkGiftInfoMapper;
import com.starry.admin.modules.shop.module.entity.PlayClerkGiftInfoEntity;
import com.starry.admin.modules.shop.service.IPlayClerkGiftInfoService;
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-05-25
 */
@Service
public class PlayClerkGiftInfoServiceImpl extends ServiceImpl<PlayClerkGiftInfoMapper, PlayClerkGiftInfoEntity>
        implements
            IPlayClerkGiftInfoService {
    @Resource
    private PlayClerkGiftInfoMapper playClerkGiftInfoMapper;

    @Override
    public PlayClerkGiftInfoEntity selectByGiftIdAndClerkId(String giftId, String clerkId) {
        LambdaQueryWrapper<PlayClerkGiftInfoEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(PlayClerkGiftInfoEntity::getClerkId, clerkId);
        lambdaQueryWrapper.eq(PlayClerkGiftInfoEntity::getGiffId, giftId);
        return this.baseMapper.selectOne(lambdaQueryWrapper);
    }

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

    /**
     * 查询店员和礼物关系
     *
     * @param id
     *            店员和礼物关系主键
     * @return 店员和礼物关系
     */
    @Override
    public PlayClerkGiftInfoEntity selectPlayClerkGiftInfoById(String id) {
        return this.baseMapper.selectById(id);
    }

    /**
     * 查询店员和礼物关系列表
     *
     * @param playClerkGiftInfo
     *            店员和礼物关系
     * @return 店员和礼物关系
     */
    @Override
    public IPage<PlayClerkGiftInfoEntity> selectPlayClerkGiftInfoByPage(PlayClerkGiftInfoEntity playClerkGiftInfo) {
        Page<PlayClerkGiftInfoEntity> page = new Page<>(1, 10);
        return this.baseMapper.selectPage(page, new LambdaQueryWrapper<>());
    }

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

    /**
     * 修改店员和礼物关系
     *
     * @param playClerkGiftInfo
     *            店员和礼物关系
     * @return 结果
     */
    @Override
    public boolean update(PlayClerkGiftInfoEntity playClerkGiftInfo) {
        return updateById(playClerkGiftInfo);
    }

    /**
     * 原子递增店员礼物数量，无记录时按增量初始化
     *
     * @param clerkId 店员ID
     * @param giftId  礼物ID
     * @param tenantId 租户ID
     * @param delta   增量
     */
    @Override
    public void incrementGiftCount(String clerkId, String giftId, String tenantId, long delta) {
        if (delta <= 0) {
            throw new IllegalArgumentException("delta must be positive");
        }
        int updated = playClerkGiftInfoMapper.incrementGiftCount(clerkId, giftId, tenantId, delta);
        if (updated == 0) {
            PlayClerkGiftInfoEntity entity = new PlayClerkGiftInfoEntity();
            entity.setId(IdUtils.getUuid());
            entity.setClerkId(clerkId);
            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) {
                playClerkGiftInfoMapper.incrementGiftCount(clerkId, giftId, tenantId, delta);
            }
        }
    }

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

    /**
     * 删除店员和礼物关系信息
     *
     * @param id
     *            店员和礼物关系主键
     * @return 结果
     */
    @Override
    public int deletePlayClerkGiftInfoById(String id) {
        return playClerkGiftInfoMapper.deleteById(id);
    }
}
