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

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSONObject;
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.common.component.JwtToken;
import com.starry.admin.common.domain.LoginUser;
import com.starry.admin.common.exception.CustomException;
import com.starry.admin.modules.clerk.mapper.PlayClerkUserInfoMapper;
import com.starry.admin.modules.clerk.module.entity.PlayClerkCommodityEntity;
import com.starry.admin.modules.clerk.module.entity.PlayClerkLevelInfoEntity;
import com.starry.admin.modules.clerk.module.entity.PlayClerkUserInfoEntity;
import com.starry.admin.modules.clerk.module.entity.PlayClerkUserQueryVo;
import com.starry.admin.modules.clerk.module.entity.PlayClerkUserReturnVo;
import com.starry.admin.modules.clerk.module.entity.PlayClerkUserReviewInfoEntity;
import com.starry.admin.modules.clerk.module.vo.PlayClerkCommodityQueryVo;
import com.starry.admin.modules.clerk.module.vo.PlayClerkUnsettledWagesInfoQueryVo;
import com.starry.admin.modules.clerk.module.vo.PlayClerkUnsettledWagesInfoReturnVo;
import com.starry.admin.modules.clerk.service.IPlayClerkCommodityService;
import com.starry.admin.modules.clerk.service.IPlayClerkLevelInfoService;
import com.starry.admin.modules.clerk.service.IPlayClerkUserInfoService;
import com.starry.admin.modules.custom.entity.PlayCustomFollowInfoEntity;
import com.starry.admin.modules.custom.service.IPlayCustomFollowInfoService;
import com.starry.admin.modules.order.module.entity.PlayOrderInfoEntity;
import com.starry.admin.modules.order.service.IPlayOrderInfoService;
import com.starry.admin.modules.personnel.module.entity.PlayPersonnelAdminInfoEntity;
import com.starry.admin.modules.personnel.module.entity.PlayPersonnelGroupInfoEntity;
import com.starry.admin.modules.personnel.module.entity.PlayPersonnelWaiterInfoEntity;
import com.starry.admin.modules.personnel.service.IPlayBalanceDetailsInfoService;
import com.starry.admin.modules.personnel.service.IPlayPersonnelAdminInfoService;
import com.starry.admin.modules.personnel.service.IPlayPersonnelGroupInfoService;
import com.starry.admin.modules.personnel.service.IPlayPersonnelWaiterInfoService;
import com.starry.admin.modules.statistics.module.vo.PlayClerkPerformanceInfoQueryVo;
import com.starry.admin.modules.system.service.LoginService;
import com.starry.admin.modules.weichat.entity.PlayClerkUserLoginResponseVo;
import com.starry.admin.modules.weichat.entity.clerk.PlayClerkUserInfoQueryVo;
import com.starry.admin.modules.weichat.entity.clerk.PlayClerkUserInfoResultVo;
import com.starry.admin.utils.SecurityUtils;
import com.starry.common.utils.ConvertUtil;
import com.starry.common.utils.IdUtils;
import com.starry.common.utils.StringUtils;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;

/**
 * 店员Service业务层处理
 *
 * @author admin
 * @since 2024-03-30
 */
@Service
public class PlayClerkUserInfoServiceImpl extends ServiceImpl<PlayClerkUserInfoMapper, PlayClerkUserInfoEntity>
        implements
            IPlayClerkUserInfoService {
    @Resource
    private PlayClerkUserInfoMapper playClerkUserInfoMapper;
    @Resource
    private PlayClerkUserReviewInfoServiceImpl playClerkUserReviewInfoService;
    @Resource
    private IPlayClerkCommodityService playClerkCommodityService;
    @Resource
    private IPlayCustomFollowInfoService customFollowInfoService;
    @Resource
    private IPlayBalanceDetailsInfoService playBalanceDetailsInfoService;
    @Resource
    private IPlayOrderInfoService playOrderInfoService;
    @Resource
    private IPlayPersonnelGroupInfoService playClerkGroupInfoService;
    @Resource
    private IPlayPersonnelAdminInfoService playPersonnelAdminInfoService;
    @Resource
    private IPlayPersonnelWaiterInfoService playClerkWaiterInfoService;
    @Resource
    private LoginService loginService;
    @Resource
    private JwtToken jwtToken;
    @Resource
    private IPlayClerkLevelInfoService playClerkLevelInfoService;

    @Override
    public List<PlayClerkUserInfoEntity> listAllByTypeId(String typeId) {
        MPJLambdaWrapper<PlayClerkUserInfoEntity> lambdaWrapper = new MPJLambdaWrapper<>();
        lambdaWrapper.select(PlayClerkUserInfoEntity::getId);
        lambdaWrapper.eq(PlayClerkUserInfoEntity::getTypeId, typeId);
        return this.baseMapper.selectList(lambdaWrapper);
    }

    @Override
    public List<PlayClerkUserInfoEntity> listAllByGroupId(String groupId) {
        MPJLambdaWrapper<PlayClerkUserInfoEntity> lambdaWrapper = new MPJLambdaWrapper<>();
        lambdaWrapper.select(PlayClerkUserInfoEntity::getId, PlayClerkUserInfoEntity::getListingState);
        lambdaWrapper.eq(PlayClerkUserInfoEntity::getGroupId, groupId);
        return this.baseMapper.selectList(lambdaWrapper);
    }

    @Override
    public Long getTotalClerkUser(String tenantId) {
        MPJLambdaWrapper<PlayClerkUserInfoEntity> lambdaWrapper = new MPJLambdaWrapper<>();
        lambdaWrapper.eq(PlayClerkUserInfoEntity::getTenantId, tenantId);
        return this.baseMapper.selectCount(lambdaWrapper);
    }

    @Override
    public PlayClerkLevelInfoEntity queryLevelCommission(String clerkId) {
        MPJLambdaWrapper<PlayClerkUserInfoEntity> lambdaWrapper = new MPJLambdaWrapper<>();
        lambdaWrapper.selectAll(PlayClerkLevelInfoEntity.class);
        lambdaWrapper.selectAs(PlayClerkUserInfoEntity::getLevelId, "levelId");
        lambdaWrapper.leftJoin(PlayClerkLevelInfoEntity.class, PlayClerkLevelInfoEntity::getId,
                PlayClerkUserInfoEntity::getLevelId);
        lambdaWrapper.eq(PlayClerkUserInfoEntity::getId, clerkId);
        return this.baseMapper.selectJoinOne(PlayClerkLevelInfoEntity.class, lambdaWrapper);

    }

    @Override
    public PlayClerkUserInfoEntity selectByOpenid(String openId) {
        LambdaQueryWrapper<PlayClerkUserInfoEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(PlayClerkUserInfoEntity::getOpenid, openId);
        return this.baseMapper.selectOne(lambdaQueryWrapper);
    }

    /**
     * 查询店员
     *
     * @param id
     *            店员主键
     * @return 店员
     */
    @Override
    public PlayClerkUserInfoEntity selectById(String id) {
        PlayClerkUserInfoEntity entity = this.baseMapper.selectById(id);
        if (entity == null) {
            throw new CustomException("店员不存在");
        }
        return entity;
    }

    @Override
    public PlayClerkUserLoginResponseVo getVo(PlayClerkUserInfoEntity userInfo) {
        PlayClerkUserLoginResponseVo result = ConvertUtil.entityToVo(userInfo, PlayClerkUserLoginResponseVo.class);
        // List<PlayClerkDataReviewInfoEntity> list =
        // playClerkDataReviewInfoService.queryByClerkId(userInfo.getId(),"0");
        // // 判断头像、音频、相册是否可以编辑，如果存在未审核的数据，则不允许编辑
        // Map<String, PlayClerkDataReviewInfoEntity> map =
        // list.stream().collect(Collectors.toMap(PlayClerkDataReviewInfoEntity::getDataType,
        // account -> account, (entity1, entity2) -> entity1));
        // if (map.containsKey("1")) {
        // result.setAvatarAllowEdit(false);
        // }
        // if (map.containsKey("2")) {
        // result.setAlbumAllowEdit(false);
        // }
        // if (map.containsKey("3")) {
        // result.setAudioAllowEdit(false);
        // }
        // 是店员之后，判断是否可以登录
        if ("1".equals(result.getClerkState())) {
            // 设置店员是否运行登录
            if ("0".equals(userInfo.getOnboardingState())) {
                result.setAllowLogin("1");
                result.setDisableLoginReason("你已离职，需要复职请联系店铺管理员");
            }
            if ("0".equals(userInfo.getListingState())) {
                result.setAllowLogin("1");
                result.setDisableLoginReason("你已被下架，没有权限访问");
            }

        }

        // 如果存在未审批的申请，或者当前已经是店员-可以申请陪聊
        PlayClerkUserReviewInfoEntity entity = playClerkUserReviewInfoService.queryByClerkId(userInfo.getId(), "0");
        if (entity != null || "1".equals(result.getClerkState())) {
            result.setClerkAllowEdit(false);
        }

        // 查询店员服务项目
        List<PlayClerkCommodityEntity> clerkCommodityEntities = playClerkCommodityService
                .selectCommodityTypeByUser(userInfo.getId(), "");
        List<PlayClerkCommodityQueryVo> playClerkCommodityQueryVos = new ArrayList<>();
        for (PlayClerkCommodityEntity clerkCommodityEntity : clerkCommodityEntities) {
            playClerkCommodityQueryVos.add(new PlayClerkCommodityQueryVo(clerkCommodityEntity.getCommodityName(),
                    clerkCommodityEntity.getEnablingState()));
        }
        result.setCommodity(playClerkCommodityQueryVos);
        result.setArea(userInfo.getProvince() + "-" + userInfo.getCity());

        result.setPcData(this.getPcData(userInfo));
        result.setLevelInfo(playClerkLevelInfoService.selectPlayClerkLevelInfoById(userInfo.getLevelId()));
        return result;
    }

    @Override
    public void updateTokenById(String id, String token) {
        PlayClerkUserInfoEntity entity = new PlayClerkUserInfoEntity();
        entity.setToken(token);
        entity.setId(id);
        entity.setOnlineState("1");
        this.baseMapper.updateById(entity);
    }

    @Override
    public void updateAccountBalanceById(String id, BigDecimal accountBalance) {

    }

    @Override
    public void updateAccountBalanceById(String userId, BigDecimal balanceBeforeOperation,
            BigDecimal balanceAfterOperation, String operationType, String operationAction, BigDecimal balanceMoney,
            String orderId) {
        // 修改用户余额
        this.baseMapper.updateById(new PlayClerkUserInfoEntity(userId, balanceAfterOperation));
        // 记录余额变更记录
        playBalanceDetailsInfoService.insertBalanceDetailsInfo("0", userId, balanceBeforeOperation,
                balanceAfterOperation, operationType, operationAction, balanceMoney, BigDecimal.ZERO, orderId);
    }

    /**
     * 查询店员列表
     *
     * @param vo
     *            店员查询对象
     * @return 店员
     */
    @Override
    public IPage<PlayClerkUserInfoResultVo> selectPlayClerkUserInfoByPage(PlayClerkUserInfoQueryVo vo) {
        Page<PlayClerkUserInfoResultVo> page = new Page<>(vo.getPageNum(), vo.getPageSize());

        MPJLambdaWrapper<PlayClerkUserInfoEntity> lambdaQueryWrapper = new MPJLambdaWrapper<>();
        // 查询不隐藏的
        lambdaQueryWrapper.eq(PlayClerkUserInfoEntity::getDisplayState, "1");
        // 查询主表全部字段
        lambdaQueryWrapper.selectAll(PlayClerkUserInfoEntity.class).selectAs(PlayClerkUserInfoEntity::getCity,
                "address");
        // 等级表
        lambdaQueryWrapper.selectAs(PlayClerkLevelInfoEntity::getName, "levelName");
        lambdaQueryWrapper.leftJoin(PlayClerkLevelInfoEntity.class, PlayClerkLevelInfoEntity::getId,
                PlayClerkUserInfoEntity::getLevelId);

        // 服务项目表
        if (StrUtil.isNotBlank(vo.getNickname())) {
            lambdaQueryWrapper.like(PlayClerkUserInfoEntity::getNickname, vo.getNickname());
        }
        if (StrUtil.isNotBlank(vo.getTypeId())) {
            lambdaQueryWrapper.eq(PlayClerkUserInfoEntity::getTypeId, vo.getTypeId());
        }
        if (StrUtil.isNotBlank(vo.getLevelId())) {
            lambdaQueryWrapper.eq(PlayClerkUserInfoEntity::getLevelId, vo.getLevelId());
        }
        if (StrUtil.isNotBlank(vo.getSex())) {
            lambdaQueryWrapper.eq(PlayClerkUserInfoEntity::getSex, vo.getSex());
        }
        if (StrUtil.isNotBlank(vo.getProvince())) {
            lambdaQueryWrapper.eq(PlayClerkUserInfoEntity::getProvince, vo.getProvince());
        }
        if (StrUtil.isNotBlank(vo.getClerkState())) {
            lambdaQueryWrapper.eq(PlayClerkUserInfoEntity::getClerkState, vo.getClerkState());
        }
        if (StrUtil.isNotBlank(vo.getRecommendationState())) {
            lambdaQueryWrapper.eq(PlayClerkUserInfoEntity::getRecommendationState, vo.getRecommendationState());
        }
        if (StrUtil.isNotBlank(vo.getOnboardingState())) {
            lambdaQueryWrapper.eq(PlayClerkUserInfoEntity::getOnboardingState, vo.getOnboardingState());
        }

        // 排序：置顶状态优先，在线用户其次，最后按创建时间倒序
        lambdaQueryWrapper.orderByDesc(PlayClerkUserInfoEntity::getPinToTopState)
                          .orderByDesc(PlayClerkUserInfoEntity::getOnlineState)
                          .orderByDesc(PlayClerkUserInfoEntity::getCreatedTime);

        return this.baseMapper.selectJoinPage(page, PlayClerkUserInfoResultVo.class, lambdaQueryWrapper);
    }

    @Override
    public List<PlayClerkUserInfoEntity> listAll() {
        LambdaQueryWrapper<PlayClerkUserInfoEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(PlayClerkUserInfoEntity::getClerkState, "1");
        return this.baseMapper.selectList(lambdaQueryWrapper);

    }

    @Override
    public IPage<PlayClerkUnsettledWagesInfoReturnVo> listUnsettledWagesByPage(PlayClerkUnsettledWagesInfoQueryVo vo) {
        MPJLambdaWrapper<PlayClerkUserInfoEntity> lambdaQueryWrapper = new MPJLambdaWrapper<>();
        // 查询所有店员
        lambdaQueryWrapper.selectAs(PlayClerkUserInfoEntity::getNickname, "clerkNickname")
                .selectAs(PlayClerkUserInfoEntity::getId, "clerkId");
        lambdaQueryWrapper.eq(PlayClerkUserInfoEntity::getClerkState, "1");
        // 加入组员的筛选
        List<String> clerkIdList = playClerkGroupInfoService.getValidClerkIdList(SecurityUtils.getLoginUser(), null);
        lambdaQueryWrapper.in(PlayClerkUserInfoEntity::getId, clerkIdList);
        if (StrUtil.isNotBlank(vo.getClerkId())) {
            lambdaQueryWrapper.eq(PlayClerkUserInfoEntity::getId, vo.getClerkId());
        }
        if (StrUtil.isNotBlank(vo.getListingState())) {
            lambdaQueryWrapper.eq(PlayClerkUserInfoEntity::getListingState, vo.getListingState());
        }
        // 查询店员订单信息
        lambdaQueryWrapper.selectCollection(PlayOrderInfoEntity.class,
                PlayClerkUnsettledWagesInfoReturnVo::getOrderInfoEntities);
        lambdaQueryWrapper.leftJoin(PlayOrderInfoEntity.class, PlayOrderInfoEntity::getAcceptBy,
                PlayClerkUserInfoEntity::getId);
        lambdaQueryWrapper.eq(PlayOrderInfoEntity::getOrderSettlementState, "0");

        return this.baseMapper.selectJoinPage(new Page<>(vo.getPageNum(), vo.getPageSize()),
                PlayClerkUnsettledWagesInfoReturnVo.class, lambdaQueryWrapper);
    }

    @Override
    public IPage<PlayClerkUserInfoEntity> selectByPage(PlayClerkPerformanceInfoQueryVo vo) {
        LambdaQueryWrapper<PlayClerkUserInfoEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (StrUtil.isNotBlank(vo.getClerkId())) {
            lambdaQueryWrapper.eq(PlayClerkUserInfoEntity::getId, vo.getClerkId());
        }
        // if (StrUtil.isNotBlank(vo.getGroupId())) {
        // lambdaQueryWrapper.eq(PlayClerkUserInfoEntity::getGroupId, vo.getGroupId());
        // }
        if (StrUtil.isNotBlank(vo.getSex())) {
            lambdaQueryWrapper.eq(PlayClerkUserInfoEntity::getSex, vo.getSex());
        }
        if (StrUtil.isNotBlank(vo.getListingState())) {
            lambdaQueryWrapper.eq(PlayClerkUserInfoEntity::getListingState, vo.getListingState());
        }
        lambdaQueryWrapper.eq(PlayClerkUserInfoEntity::getClerkState, "1");
        // 加入组员的筛选
        List<String> clerkIdList = playClerkGroupInfoService.getValidClerkIdList(SecurityUtils.getLoginUser(), null);
        lambdaQueryWrapper.in(PlayClerkUserInfoEntity::getId, clerkIdList);
        return this.baseMapper.selectPage(new Page<>(vo.getPageNum(), vo.getPageSize()), lambdaQueryWrapper);
    }

    @Override
    public IPage<PlayClerkUserReturnVo> selectByPage(PlayClerkUserQueryVo vo) {
        MPJLambdaWrapper<PlayClerkUserInfoEntity> lambdaQueryWrapper = new MPJLambdaWrapper<>();

        LoginUser loginUser = SecurityUtils.getLoginUser();
        // 主表（店员表全部字段）
        lambdaQueryWrapper.selectAll(PlayClerkUserInfoEntity.class);

        if (StrUtil.isNotBlank(vo.getId())) {
            lambdaQueryWrapper.eq(PlayClerkUserInfoEntity::getId, vo.getId());
        }
        if (StrUtil.isNotBlank(vo.getNickname())) {
            lambdaQueryWrapper.eq(PlayClerkUserInfoEntity::getNickname, vo.getNickname());
        }
        if (StrUtil.isNotBlank(vo.getPhone())) {
            lambdaQueryWrapper.eq(PlayClerkUserInfoEntity::getPhone, vo.getPhone());
        }
        if (StrUtil.isNotBlank(vo.getGroupId())) {
            lambdaQueryWrapper.eq(PlayClerkUserInfoEntity::getGroupId, vo.getGroupId());
        }
        if (StrUtil.isNotBlank(vo.getSex())) {
            lambdaQueryWrapper.eq(PlayClerkUserInfoEntity::getSex, vo.getSex());
        }
        if (StrUtil.isNotBlank(vo.getFixingLevel())) {
            lambdaQueryWrapper.eq(PlayClerkUserInfoEntity::getFixingLevel, vo.getFixingLevel());
        }
        if (StrUtil.isNotBlank(vo.getLevelId())) {
            lambdaQueryWrapper.eq(PlayClerkUserInfoEntity::getLevelId, vo.getLevelId());
        }
        if (StrUtil.isNotBlank(vo.getProvince())) {
            lambdaQueryWrapper.eq(PlayClerkUserInfoEntity::getProvince, vo.getProvince());
        }
        if (StrUtil.isNotBlank(vo.getCity())) {
            lambdaQueryWrapper.eq(PlayClerkUserInfoEntity::getCity, vo.getCity());
        }
        if (StrUtil.isNotBlank(vo.getOnboardingState())) {
            lambdaQueryWrapper.eq(PlayClerkUserInfoEntity::getOnboardingState, vo.getOnboardingState());
        }
        if (StrUtil.isNotBlank(vo.getRecommendationState())) {
            lambdaQueryWrapper.eq(PlayClerkUserInfoEntity::getRecommendationState, vo.getRecommendationState());
        }
        if (StrUtil.isNotBlank(vo.getPinToTopState())) {
            lambdaQueryWrapper.eq(PlayClerkUserInfoEntity::getPinToTopState, vo.getPinToTopState());
        }
        if (StrUtil.isNotBlank(vo.getOnlineState())) {
            lambdaQueryWrapper.eq(PlayClerkUserInfoEntity::getOnlineState, vo.getOnlineState());
        }
        if (StrUtil.isNotBlank(vo.getDisplayState())) {
            lambdaQueryWrapper.eq(PlayClerkUserInfoEntity::getDisplayState, vo.getDisplayState());
        }
        if (StrUtil.isNotBlank(vo.getRealState())) {
            lambdaQueryWrapper.eq(PlayClerkUserInfoEntity::getRealState, vo.getRealState());
        }
        if (StrUtil.isNotBlank(vo.getMandatoryRealState())) {
            lambdaQueryWrapper.eq(PlayClerkUserInfoEntity::getMandatoryRealState, vo.getMandatoryRealState());
        }
        if (StrUtil.isNotBlank(vo.getClerkState())) {
            lambdaQueryWrapper.eq(PlayClerkUserInfoEntity::getClerkState, vo.getClerkState());
        }
        if (StrUtil.isNotBlank(vo.getCode())) {
            lambdaQueryWrapper.eq(PlayClerkUserInfoEntity::getCode, vo.getCode());
        }

        // 加入组员的筛选
        List<String> clerkIdList = playClerkGroupInfoService.getValidClerkIdList(SecurityUtils.getLoginUser(), null);
        lambdaQueryWrapper.in(PlayClerkUserInfoEntity::getId, clerkIdList);

        // 排序：置顶状态优先，在线用户其次，最后按创建时间倒序
        lambdaQueryWrapper.orderByDesc(PlayClerkUserInfoEntity::getPinToTopState)
                          .orderByDesc(PlayClerkUserInfoEntity::getOnlineState)
                          .orderByDesc(PlayClerkUserInfoEntity::getCreatedTime);

        IPage<PlayClerkUserReturnVo> page = this.baseMapper.selectJoinPage(
                new Page<>(vo.getPageNum(), vo.getPageSize()), PlayClerkUserReturnVo.class, lambdaQueryWrapper);

        for (PlayClerkUserReturnVo record : page.getRecords()) {
            BigDecimal orderTotalAmount = new BigDecimal("0");
            int orderContinueNumber = 0;
            int orderNumber = 0;
            for (PlayOrderInfoEntity orderInfo : playOrderInfoService.queryBySettlementOrder(record.getId(), "")) {
                if ("0".equals(orderInfo.getFirstOrder())) {
                    orderContinueNumber++;
                }
                orderNumber++;
                if (!"4".equals(orderInfo.getOrderStatus())) {
                    orderTotalAmount = orderTotalAmount.add(orderInfo.getFinalAmount());
                }
            }
            record.setOrderTotalAmount(orderTotalAmount);
            record.setOrderNumber(String.valueOf(orderNumber));
            record.setOrderContinueNumber(String.valueOf(orderContinueNumber));
        }

        return page;
    }

    @Override
    public IPage<PlayClerkUserInfoResultVo> selectByPage(PlayClerkUserInfoQueryVo vo, String customUserId) {
        IPage<PlayClerkUserInfoResultVo> voPage = this.selectPlayClerkUserInfoByPage(vo);
        Map<String, String> customFollows = new HashMap<>(16);
        // 如果当前顾客已登录，查询用户关注状态
        if (StrUtil.isNotBlank(customUserId)) {
            LambdaQueryWrapper<PlayCustomFollowInfoEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(PlayCustomFollowInfoEntity::getCustomId, customUserId);
            List<PlayCustomFollowInfoEntity> customFollowInfoEntities = customFollowInfoService
                    .list(lambdaQueryWrapper);
            customFollows = customFollowInfoEntities.stream().collect(Collectors
                    .toMap(PlayCustomFollowInfoEntity::getClerkId, PlayCustomFollowInfoEntity::getFollowState));
        }
        for (PlayClerkUserInfoResultVo record : voPage.getRecords()) {
            record.setFollowState(customFollows.containsKey(record.getId()) ? "1" : "0");
            record.setCommodity(playClerkCommodityService.getClerkCommodityList(record.getId(), "1"));
        }
        voPage.getRecords().parallelStream().forEach(ca -> {
            ca.setLevelInfo(playClerkLevelInfoService.selectPlayClerkLevelInfoById(ca.getLevelId()));
        });
        return voPage;
    }

    /**
     * 新增店员
     *
     * @param playClerkUserInfo
     *            店员
     * @return 结果
     */
    @Override
    public boolean create(PlayClerkUserInfoEntity playClerkUserInfo) {
        if (StrUtil.isBlankIfStr(playClerkUserInfo.getId())) {
            playClerkUserInfo.setId(IdUtils.getUuid());
        }
        return save(playClerkUserInfo);
    }

    /**
     * 修改店员
     *
     * @param playClerkUserInfo
     *            店员
     * @return 结果
     */
    @Override
    public boolean update(PlayClerkUserInfoEntity playClerkUserInfo) {
        return updateById(playClerkUserInfo);
    }

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

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

    @Override
    public List<PlayClerkUserInfoEntity> simpleList() {
        List<String> clerkIdList = playClerkGroupInfoService.getValidClerkIdList(SecurityUtils.getLoginUser(), null);
        LambdaQueryWrapper<PlayClerkUserInfoEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(PlayClerkUserInfoEntity::getDeleted, 0);
        lambdaQueryWrapper.in(PlayClerkUserInfoEntity::getId, clerkIdList);
        lambdaQueryWrapper.select(PlayClerkUserInfoEntity::getId, PlayClerkUserInfoEntity::getNickname,
                PlayClerkUserInfoEntity::getAvatar, PlayClerkUserInfoEntity::getTypeId,
                PlayClerkUserInfoEntity::getGroupId, PlayClerkUserInfoEntity::getPhone);
        lambdaQueryWrapper.orderByDesc(PlayClerkUserInfoEntity::getId);
        return this.baseMapper.selectList(lambdaQueryWrapper);
    }

    @Override
    public JSONObject getPcData(PlayClerkUserInfoEntity entity) {
        JSONObject data = new JSONObject();
        data.fluentPut("token", "");
        data.fluentPut("role", "");
        if (StringUtils.isEmpty(entity.getSysUserId())) {
            return data;
        }
        LoginUser loginUserInfo = loginService.getLoginUserInfo(entity.getSysUserId());
        Map<String, Object> tokenMap = jwtToken.createToken(loginUserInfo);
        data.fluentPut("token", tokenMap.get("token"));
        PlayPersonnelAdminInfoEntity adminInfoEntity = playPersonnelAdminInfoService
                .selectByUserId(entity.getSysUserId());
        if (Objects.nonNull(adminInfoEntity)) {
            data.fluentPut("role", "operator");
            return data;
        }
        PlayPersonnelGroupInfoEntity groupInfoEntity = playClerkGroupInfoService.selectByUserId(entity.getSysUserId());
        if (Objects.nonNull(groupInfoEntity)) {
            data.fluentPut("role", "leader");
            return data;
        }
        PlayPersonnelWaiterInfoEntity waiterInfoEntity = playClerkWaiterInfoService
                .selectByUserId(entity.getSysUserId());
        if (Objects.nonNull(waiterInfoEntity)) {
            data.fluentPut("role", "waiter");
            return data;
        }
        return data;
    }
}
