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

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
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.clerk.mapper.PlayClerkLevelInfoMapper;
import com.starry.admin.modules.clerk.module.entity.PlayClerkLevelInfoEntity;
import com.starry.admin.modules.clerk.service.IPlayClerkLevelInfoService;
import com.starry.admin.modules.system.module.entity.SysTenantEntity;
import com.starry.common.utils.IdUtils;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;

/**
 * 店员等级Service业务层处理
 *
 * @author admin
 * @since 2024-03-30
 */
@Service
public class PlayClerkLevelInfoServiceImpl extends ServiceImpl<PlayClerkLevelInfoMapper, PlayClerkLevelInfoEntity>
        implements
            IPlayClerkLevelInfoService {
    @Resource
    private PlayClerkLevelInfoMapper playClerkLevelInfoMapper;

    @Override
    public void initDefaultLevel(SysTenantEntity sysTenantEntity) {
        List<PlayClerkLevelInfoEntity> list = this.selectAll();
        if (list == null || list.isEmpty()) {
            PlayClerkLevelInfoEntity entity = new PlayClerkLevelInfoEntity();
            entity.setName("普通");
            entity.setFirstRandomRadio(45);
            entity.setNotFirstRandomRadio(50);
            entity.setFirstRewardRatio(45);
            entity.setNotFirstRewardRatio(50);
            entity.setFirstRegularRatio(45);
            entity.setNotFirstRegularRatio(50);
            entity.setLevel(1);
            entity.setOrderNumber(1L);
            entity.setStyleType(entity.getLevel());
            entity.setTenantId(sysTenantEntity.getTenantId());
            this.baseMapper.insert(entity);
        }
    }

    @Override
    public PlayClerkLevelInfoEntity getDefaultLevel() {
        List<PlayClerkLevelInfoEntity> list = this.selectAll();
        if (list != null && !list.isEmpty()) {
            return list.get(0);
        } else {
            PlayClerkLevelInfoEntity entity = new PlayClerkLevelInfoEntity();
            entity.setName("普通");
            entity.setFirstRandomRadio(45);
            entity.setNotFirstRandomRadio(50);
            entity.setFirstRewardRatio(45);
            entity.setNotFirstRewardRatio(50);
            entity.setFirstRegularRatio(45);
            entity.setNotFirstRegularRatio(50);
            entity.setLevel(1);
            entity.setOrderNumber(1L);
            entity.setStyleType(1);
            this.baseMapper.insert(entity);
            return entity;
        }
    }

    /**
     * 查询店员等级
     *
     * @param id
     *            店员等级主键
     * @return 店员等级
     */
    @Override
    public PlayClerkLevelInfoEntity selectPlayClerkLevelInfoById(String id) {
        return this.baseMapper.selectById(id);
    }

    @Override
    public List<PlayClerkLevelInfoEntity> selectAll() {
        LambdaQueryWrapper<PlayClerkLevelInfoEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.orderByAsc(PlayClerkLevelInfoEntity::getLevel);
        return this.baseMapper.selectList(lambdaQueryWrapper);
    }

    /**
     * 查询店员等级列表
     *
     * @param playClerkLevelInfo
     *            店员等级
     * @return 店员等级
     */
    @Override
    public IPage<PlayClerkLevelInfoEntity> selectPlayClerkLevelInfoByPage(PlayClerkLevelInfoEntity playClerkLevelInfo) {
        Page<PlayClerkLevelInfoEntity> page = new Page<>(1, 9999);
        return this.baseMapper.selectPage(page, new LambdaQueryWrapper<>());
    }

    /**
     * 新增店员等级
     *
     * @param playClerkLevelInfo
     *            店员等级
     * @return 结果
     */
    @Override
    public boolean create(PlayClerkLevelInfoEntity playClerkLevelInfo) {
        if (StrUtil.isBlankIfStr(playClerkLevelInfo.getId())) {
            playClerkLevelInfo.setId(IdUtils.getUuid());
        }
        playClerkLevelInfo.setCreatedTime(new Date());
        playClerkLevelInfo.setStyleType(playClerkLevelInfo.getLevel());
        if (playClerkLevelInfo.getOrderNumber() == null) {
            playClerkLevelInfo.setOrderNumber(playClerkLevelInfo.getLevel().longValue());
        }
        return save(playClerkLevelInfo);
    }

    /**
     * 修改店员等级
     *
     * @param playClerkLevelInfo
     *            店员等级
     * @return 结果
     */
    @Override
    public boolean update(PlayClerkLevelInfoEntity playClerkLevelInfo) {
        return updateById(playClerkLevelInfo);
    }

    /**
     * 批量删除店员等级
     *
     * @param ids
     *            需要删除的店员等级主键
     * @return 结果
     */
    @Override
    public int deletePlayClerkLevelInfoByIds(String[] ids) {
        return playClerkLevelInfoMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 删除店员等级信息
     *
     * @param id
     *            店员等级主键
     * @return 结果
     */
    @Override
    public int deletePlayClerkLevelInfoById(String id) {
        return playClerkLevelInfoMapper.deleteById(id);
    }

    @Override
    public int selectMaxLevel() {
        QueryWrapper<PlayClerkLevelInfoEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("max(level) as level ");
        PlayClerkLevelInfoEntity entity = this.baseMapper.selectOne(queryWrapper);
        return entity == null ? 0 : entity.getLevel();
    }

    @Override
    public void delMaxLevelByLevel(Integer level) {
        LambdaQueryWrapper<PlayClerkLevelInfoEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(PlayClerkLevelInfoEntity::getLevel, level);
        playClerkLevelInfoMapper.delete(queryWrapper);
    }
}
