package com.starry.admin.modules.custom.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.module.entity.PlayClerkUserInfoEntity;
import com.starry.admin.modules.custom.entity.PlayCustomFollowInfoEntity;
import com.starry.admin.modules.custom.mapper.PlayCustomFollowInfoMapper;
import com.starry.admin.modules.custom.service.IPlayCustomFollowInfoService;
import com.starry.admin.modules.weichat.entity.PlayClerkFollowQueryVo;
import com.starry.admin.modules.weichat.entity.PlayClerkFollowReturnVo;
import com.starry.common.utils.IdUtils;
import java.util.Arrays;
import java.util.Date;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;

/**
 * 顾客关注陪聊信息Service业务层处理
 *
 * @author admin
 * @since 2024-04-30
 */
@Service
public class PlayCustomFollowInfoServiceImpl extends ServiceImpl<PlayCustomFollowInfoMapper, PlayCustomFollowInfoEntity>
        implements
            IPlayCustomFollowInfoService {
    @Resource
    private PlayCustomFollowInfoMapper playCustomFollowInfoMapper;

    /**
     * 查询顾客关注陪聊信息
     *
     * @param id
     *            顾客关注陪聊信息主键
     * @return 顾客关注陪聊信息
     */
    @Override
    public PlayCustomFollowInfoEntity selectPlayCustomFollowInfoById(String id) {
        return this.baseMapper.selectById(id);
    }

    @Override
    public String queryFollowState(String customUserId, String clerkUserId) {
        LambdaQueryWrapper<PlayCustomFollowInfoEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(PlayCustomFollowInfoEntity::getClerkId, clerkUserId);
        lambdaQueryWrapper.eq(PlayCustomFollowInfoEntity::getCustomId, customUserId);
        PlayCustomFollowInfoEntity entity = this.baseMapper.selectOne(lambdaQueryWrapper);
        return entity == null ? "0" : entity.getFollowState();
    }

    @Override
    public void updateFollowState(String customUserId, String clerkUserId, String followState) {
        LambdaQueryWrapper<PlayCustomFollowInfoEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(PlayCustomFollowInfoEntity::getClerkId, clerkUserId);
        lambdaQueryWrapper.eq(PlayCustomFollowInfoEntity::getCustomId, customUserId);
        PlayCustomFollowInfoEntity entity = this.baseMapper.selectOne(lambdaQueryWrapper);
        if (entity == null) {
            entity = new PlayCustomFollowInfoEntity();
            entity.setCustomId(customUserId);
            entity.setFollowState(followState);
            entity.setClerkId(clerkUserId);
            entity.setFollowTime(new Date());
            this.baseMapper.insert(entity);
        } else {
            entity.setFollowState(followState);
            this.baseMapper.updateById(entity);
        }
    }

    @Override
    public IPage<PlayClerkFollowReturnVo> selectByPage(PlayClerkFollowQueryVo vo) {
        MPJLambdaWrapper<PlayCustomFollowInfoEntity> lambdaQueryWrapper = new MPJLambdaWrapper<PlayCustomFollowInfoEntity>()
                // 查询主表全部字段
                .selectAll(PlayCustomFollowInfoEntity.class)
                // 陪聊用户表全部字段
                .selectAll(PlayClerkUserInfoEntity.class)
                // 陪聊用户表
                .leftJoin(PlayClerkUserInfoEntity.class, PlayClerkUserInfoEntity::getId,
                        PlayCustomFollowInfoEntity::getClerkId);
        if (StrUtil.isNotBlank(vo.getCustomId())) {
            lambdaQueryWrapper.eq(PlayCustomFollowInfoEntity::getCustomId, vo.getCustomId());
        }
        if (StrUtil.isNotBlank(vo.getFollowState())) {
            lambdaQueryWrapper.eq(PlayCustomFollowInfoEntity::getFollowState, vo.getFollowState());
        }
        IPage<PlayClerkFollowReturnVo> iPage = this.baseMapper.selectJoinPage(
                new Page<>(vo.getPageNum(), vo.getPageSize()), PlayClerkFollowReturnVo.class, lambdaQueryWrapper);
        for (PlayClerkFollowReturnVo record : iPage.getRecords()) {
            LambdaQueryWrapper<PlayCustomFollowInfoEntity> lambdaQueryWrapper1 = new LambdaQueryWrapper<>();
            lambdaQueryWrapper1.eq(PlayCustomFollowInfoEntity::getClerkId, record.getClerkId());
            record.setFollowNumber(this.baseMapper.selectList(lambdaQueryWrapper1).size());
        }
        return iPage;
    }

    /**
     * 查询顾客关注陪聊信息列表
     *
     * @param playCustomFollowInfo
     *            顾客关注陪聊信息
     * @return 顾客关注陪聊信息
     */
    @Override
    public IPage<PlayCustomFollowInfoEntity> selectPlayCustomFollowInfoByPage(
            PlayCustomFollowInfoEntity playCustomFollowInfo) {
        Page<PlayCustomFollowInfoEntity> page = new Page<>(1, 10);
        return this.baseMapper.selectPage(page, new LambdaQueryWrapper<>());
    }

    /**
     * 新增顾客关注陪聊信息
     *
     * @param playCustomFollowInfo
     *            顾客关注陪聊信息
     * @return 结果
     */
    @Override
    public boolean create(PlayCustomFollowInfoEntity playCustomFollowInfo) {
        if (StrUtil.isBlankIfStr(playCustomFollowInfo.getId())) {
            playCustomFollowInfo.setId(IdUtils.getUuid());
        }
        return save(playCustomFollowInfo);
    }

    /**
     * 修改顾客关注陪聊信息
     *
     * @param playCustomFollowInfo
     *            顾客关注陪聊信息
     * @return 结果
     */
    @Override
    public boolean update(PlayCustomFollowInfoEntity playCustomFollowInfo) {
        return updateById(playCustomFollowInfo);
    }

    /**
     * 批量删除顾客关注陪聊信息
     *
     * @param ids
     *            需要删除的顾客关注陪聊信息主键
     * @return 结果
     */
    @Override
    public int deletePlayCustomFollowInfoByIds(String[] ids) {
        return playCustomFollowInfoMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 删除顾客关注陪聊信息信息
     *
     * @param id
     *            顾客关注陪聊信息主键
     * @return 结果
     */
    @Override
    public int deletePlayCustomFollowInfoById(String id) {
        return playCustomFollowInfoMapper.deleteById(id);
    }
}
