package com.ruoyi.mall.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.uuid.IdUtils;
import org.springframework.stereotype.Service;
import com.ruoyi.mall.mapper.TbVipCodeMapper;
import com.ruoyi.mall.domain.TbVipCode;
import com.ruoyi.mall.service.ITbVipCodeService;

import javax.annotation.Resource;

/**
 * 会员码Service业务层处理
 *
 * @author lsm
 * @date 2023-12-22
 */
@Service
public class TbVipCodeServiceImpl implements ITbVipCodeService
{
    @Resource
    private TbVipCodeMapper tbVipCodeMapper;

    /**
     * 查询会员码
     *
     * @param codeId 会员码主键
     * @return 会员码
     */
    @Override
    public TbVipCode selectTbVipCodeByCodeId(Long codeId)
    {
        return tbVipCodeMapper.selectTbVipCodeByCodeId(codeId);
    }

    /**
     * 查询会员码列表
     *
     * @param tbVipCode 会员码
     * @return 会员码
     */
    @Override
    public List<TbVipCode> selectTbVipCodeList(TbVipCode tbVipCode)
    {
        return tbVipCodeMapper.selectTbVipCodeList(tbVipCode);
    }

    /**
     * 新增会员码
     *
     * @param tbVipCode 会员码
     * @return 结果
     */
    @Override
    public int insertTbVipCode(TbVipCode tbVipCode)
    {
        tbVipCode.setCreateTime(DateUtils.getNowDate());
        return tbVipCodeMapper.insertTbVipCode(tbVipCode);
    }

    /**
     * 修改会员码
     *
     * @param tbVipCode 会员码
     * @return 结果
     */
    @Override
    public int updateTbVipCode(TbVipCode tbVipCode)
    {
        tbVipCode.setUpdateTime(DateUtils.getNowDate());
        return tbVipCodeMapper.updateTbVipCode(tbVipCode);
    }

    /**
     * 批量删除会员码
     *
     * @param codeIds 需要删除的会员码主键
     * @return 结果
     */
    @Override
    public int deleteTbVipCodeByCodeIds(Long[] codeIds)
    {
        return tbVipCodeMapper.deleteTbVipCodeByCodeIds(codeIds);
    }

    /**
     * 删除会员码信息
     *
     * @param codeId 会员码主键
     * @return 结果
     */
    @Override
    public int deleteTbVipCodeByCodeId(Long codeId)
    {
        return tbVipCodeMapper.deleteTbVipCodeByCodeId(codeId);
    }

    /**
     * 生成指定数量的会员码
     * @param num 数量
     * @return
     */
    @Override
    public int createVipCode(Integer num) {
        Date nowDate = DateUtils.getNowDate();
        ArrayList<TbVipCode> list = new ArrayList<>();
        for (int i = 0; i < num; i++) {
            // 获取不重复的会员码
            String code = getVipCode();
            TbVipCode vipCode = new TbVipCode();
            vipCode.setVipCode(code);
            vipCode.setStatus(0);
            vipCode.setCreateTime(nowDate);
            vipCode.setDelFlag(0);
            list.add(vipCode);
        }
        return tbVipCodeMapper.insertVipCode(list);
    }

    /**
     * 生成不重复会员码
     * @return 结果
     */
    public String getVipCode(){
        String str = IdUtils.createVipCode();
        // 查询是否有重复
        int i = tbVipCodeMapper.selectVipCodeByCode(str);
        if (i < 1){
            return str;
        }else{
            return getVipCode();
        }
    }
}
