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.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.modules.clerk.mapper.PlayClerkTypeInfoMapper;
import com.starry.admin.modules.clerk.module.entity.PlayClerkTypeInfoEntity;
import com.starry.admin.modules.clerk.module.vo.PlayClerkTypeInfoQueryVo;
import com.starry.admin.modules.clerk.module.vo.PlayClerkTypeInfoReturnVo;
import com.starry.admin.modules.clerk.service.IPlayClerkTypeInfoService;
import com.starry.admin.modules.clerk.service.IPlayClerkUserInfoService;
import com.starry.common.utils.IdUtils;
import java.util.Arrays;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;

/**
 * 店员分类信息Service业务层处理
 *
 * @author admin
 * @since 2024-05-31
 */
@Service
public class PlayClerkTypeInfoServiceImpl extends ServiceImpl<PlayClerkTypeInfoMapper, PlayClerkTypeInfoEntity>
        implements
            IPlayClerkTypeInfoService {

    @Resource
    private PlayClerkTypeInfoMapper playClerkTypeInfoMapper;

    @Resource
    private IPlayClerkUserInfoService playClerkUserInfoService;

    /**
     * 查询店员分类信息
     *
     * @param id
     *            店员分类信息主键
     * @return 店员分类信息
     */
    @Override
    public PlayClerkTypeInfoEntity selectPlayClerkTypeInfoById(String id) {
        return this.baseMapper.selectById(id);
    }

    @Override
    public List<PlayClerkTypeInfoEntity> selectByAll() {
        return this.baseMapper.selectList(new LambdaQueryWrapper<>());
    }

    /**
     * 查询店员分类信息列表
     *
     * @param vo
     *            店员分类信息
     * @return 店员分类信息
     */
    @Override
    public IPage<PlayClerkTypeInfoReturnVo> selectByPage(PlayClerkTypeInfoQueryVo vo) {
        MPJLambdaWrapper<PlayClerkTypeInfoEntity> lambdaWrapper = new MPJLambdaWrapper<>();
        lambdaWrapper.orderByAsc(PlayClerkTypeInfoEntity::getSort);
        IPage<PlayClerkTypeInfoReturnVo> page = this.baseMapper.selectJoinPage(
                new Page<>(vo.getPageNum(), vo.getPageSize()), PlayClerkTypeInfoReturnVo.class, lambdaWrapper);
        for (PlayClerkTypeInfoReturnVo record : page.getRecords()) {
            record.setClerkNumber(playClerkUserInfoService.listAllByTypeId(record.getId()).size());
        }
        return page;
    }

    /**
     * 新增店员分类信息
     *
     * @param playClerkTypeInfo
     *            店员分类信息
     * @return 结果
     */
    @Override
    public boolean create(PlayClerkTypeInfoEntity playClerkTypeInfo) {
        if (StrUtil.isBlankIfStr(playClerkTypeInfo.getId())) {
            playClerkTypeInfo.setId(IdUtils.getUuid());
        }
        return save(playClerkTypeInfo);
    }

    /**
     * 修改店员分类信息
     *
     * @param playClerkTypeInfo
     *            店员分类信息
     * @return 结果
     */
    @Override
    public boolean update(PlayClerkTypeInfoEntity playClerkTypeInfo) {
        return updateById(playClerkTypeInfo);
    }

    /**
     * 批量删除店员分类信息
     *
     * @param ids
     *            需要删除的店员分类信息主键
     * @return 结果
     */
    @Override
    public int deletePlayClerkTypeInfoByIds(String[] ids) {
        return playClerkTypeInfoMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 删除店员分类信息信息
     *
     * @param id
     *            店员分类信息主键
     * @return 结果
     */
    @Override
    public int deletePlayClerkTypeInfoById(String id) {
        return playClerkTypeInfoMapper.deleteById(id);
    }
}
