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.common.exception.CustomException;
import com.starry.admin.modules.clerk.mapper.PlayClerkArticleInfoMapper;
import com.starry.admin.modules.clerk.module.entity.PlayClerkArticleInfoEntity;
import com.starry.admin.modules.clerk.module.entity.PlayClerkUserInfoEntity;
import com.starry.admin.modules.clerk.module.entity.PlayCustomArticleInfoEntity;
import com.starry.admin.modules.clerk.module.vo.PlayClerkArticleQueryVo;
import com.starry.admin.modules.clerk.module.vo.PlayClerkArticleReturnVo;
import com.starry.admin.modules.clerk.service.IPlayClerkArticleInfoService;
import com.starry.admin.modules.personnel.service.IPlayPersonnelGroupInfoService;
import com.starry.admin.modules.weichat.entity.article.PlayClerkArticleCustomQueryVo;
import com.starry.admin.modules.weichat.entity.article.PlayClerkArticleCustomReturnVo;
import com.starry.admin.utils.SecurityUtils;
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-04
 */
@Service
public class PlayClerkArticleInfoServiceImpl extends ServiceImpl<PlayClerkArticleInfoMapper, PlayClerkArticleInfoEntity>
        implements
            IPlayClerkArticleInfoService {
    @Resource
    private PlayClerkArticleInfoMapper playClerkArticleInfoMapper;

    @Resource
    private IPlayPersonnelGroupInfoService playClerkGroupInfoService;

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

    /**
     * 查询店员动态信息列表
     *
     * @param vo
     *            店员动态信息查询对象
     * @return 店员动态信息
     */
    @Override
    public IPage<PlayClerkArticleReturnVo> selectByPage(PlayClerkArticleQueryVo vo, boolean searchByYourself) {
        MPJLambdaWrapper<PlayClerkArticleInfoEntity> lambdaQueryWrapper = new MPJLambdaWrapper<>();
        // 查询主表全部字段
        lambdaQueryWrapper.selectAll(PlayClerkArticleInfoEntity.class);
        // 陪聊用户表全部字段
        lambdaQueryWrapper.selectAs(PlayClerkUserInfoEntity::getId, "clerkId")
                .selectAs(PlayClerkUserInfoEntity::getNickname, "clerkNickname")
                .selectAs(PlayClerkUserInfoEntity::getAvatar, "clerkAvatar");
        // 陪聊动态表
        lambdaQueryWrapper.leftJoin(PlayClerkUserInfoEntity.class, PlayClerkUserInfoEntity::getId,
                PlayClerkArticleInfoEntity::getClerkId);
        // 动态点赞表
        lambdaQueryWrapper
                .selectCollection(PlayCustomArticleInfoEntity.class, PlayClerkArticleReturnVo::getArticleInfoEntities)
                .leftJoin(PlayCustomArticleInfoEntity.class, PlayCustomArticleInfoEntity::getArticleId,
                        PlayCustomArticleInfoEntity::getId);

        if (StrUtil.isNotBlank(vo.getClerkId())) {
            lambdaQueryWrapper.eq(PlayClerkArticleInfoEntity::getClerkId, vo.getClerkId());
        }
        if (StrUtil.isNotBlank(vo.getReviewState())) {
            lambdaQueryWrapper.eq(PlayClerkArticleInfoEntity::getReviewState, vo.getReviewState());
        }
        if (vo.getReleaseTime() != null && vo.getReleaseTime().size() == 2) {
            lambdaQueryWrapper.between(PlayClerkArticleInfoEntity::getReleaseTime, vo.getReleaseTime().get(0),
                    vo.getReleaseTime().get(1));
        }
        if (!searchByYourself) {
            // 后台查询时，加入组员的筛选
            List<String> clerkIdList = playClerkGroupInfoService.getValidClerkIdList(SecurityUtils.getLoginUser(),
                    null);
            lambdaQueryWrapper.in(PlayClerkArticleInfoEntity::getClerkId, clerkIdList);
        }
        IPage<PlayClerkArticleReturnVo> page = this.baseMapper.selectJoinPage(
                new Page<>(vo.getPageNum(), vo.getPageSize()), PlayClerkArticleReturnVo.class, lambdaQueryWrapper);
        for (PlayClerkArticleReturnVo record : page.getRecords()) {
            int index = 0;
            for (PlayCustomArticleInfoEntity articleInfoEntity : record.getArticleInfoEntities()) {
                if (articleInfoEntity.getId() == null) {
                    continue;
                }
                index++;
            }
            record.setAgreedQuantity(index);
        }
        return this.baseMapper.selectJoinPage(new Page<>(vo.getPageNum(), vo.getPageSize()),
                PlayClerkArticleReturnVo.class, lambdaQueryWrapper);
    }

    @Override
    public IPage<PlayClerkArticleCustomReturnVo> customSelectByPage(PlayClerkArticleCustomQueryVo vo, String customId) {
        MPJLambdaWrapper<PlayClerkArticleInfoEntity> lambdaQueryWrapper = new MPJLambdaWrapper<>();
        // 查询主表全部字段
        lambdaQueryWrapper.selectAll(PlayClerkArticleInfoEntity.class);
        // 陪聊用户表全部字段
        lambdaQueryWrapper.selectAs(PlayClerkUserInfoEntity::getId, "clerkId")
                .selectAs(PlayClerkUserInfoEntity::getNickname, "clerkNickname")
                .selectAs(PlayClerkUserInfoEntity::getAvatar, "clerkAvatar")
                .selectAs(PlayClerkUserInfoEntity::getSex, "clerkSex");
        // 陪聊用户表全部字段
        lambdaQueryWrapper.selectAs(PlayCustomArticleInfoEntity::getId, "id");
        // 陪聊动态表
        lambdaQueryWrapper.leftJoin(PlayClerkUserInfoEntity.class, PlayClerkUserInfoEntity::getId,
                PlayClerkArticleInfoEntity::getClerkId);
        if (StrUtil.isNotBlank(vo.getFollowState()) && "1".equals(vo.getFollowState())) {
            // 查询本人收藏的动态列表
            lambdaQueryWrapper.innerJoin(PlayCustomArticleInfoEntity.class, PlayCustomArticleInfoEntity::getArticleId,
                    PlayClerkArticleInfoEntity::getId);
            lambdaQueryWrapper.eq(PlayCustomArticleInfoEntity::getCustomId, customId);
            lambdaQueryWrapper.eq(PlayCustomArticleInfoEntity::getEndorseType, "0");
            lambdaQueryWrapper.eq(PlayCustomArticleInfoEntity::getEndorseState, "1");
        }
        // 查询指定店员动态
        if (StrUtil.isNotBlank(vo.getClerkId())) {
            lambdaQueryWrapper.eq(PlayClerkArticleInfoEntity::getClerkId, vo.getClerkId());
        }
        // 动态点赞表
        lambdaQueryWrapper
                .selectCollection(PlayCustomArticleInfoEntity.class, PlayClerkArticleReturnVo::getArticleInfoEntities)
                .leftJoin(PlayCustomArticleInfoEntity.class, PlayCustomArticleInfoEntity::getArticleId,
                        PlayCustomArticleInfoEntity::getId);
        lambdaQueryWrapper.eq(PlayClerkArticleInfoEntity::getReviewState, "1");

        IPage<PlayClerkArticleCustomReturnVo> page = this.baseMapper.selectJoinPage(
                new Page<>(vo.getPageNum(), vo.getPageSize()), PlayClerkArticleCustomReturnVo.class,
                lambdaQueryWrapper);
        for (PlayClerkArticleCustomReturnVo record : page.getRecords()) {
            int index = 0;
            for (PlayCustomArticleInfoEntity articleInfoEntity : record.getArticleInfoEntities()) {
                // 动态操作信息为空
                if (articleInfoEntity.getId() == null) {
                    continue;
                }
                // 点赞或者动态处于激活状态
                if ("1".equals(articleInfoEntity.getEndorseState())) {
                    if ("1".equals(articleInfoEntity.getEndorseType())) {
                        index++;
                        record.setGreedState("1");
                    } else {
                        record.setFollowState("1");
                    }
                }
            }
            record.setAgreedQuantity(index);
        }
        return page;
    }
    /**
     * 新增店员动态信息
     *
     * @param entity
     *            店员动态信息
     * @return 结果
     */
    @Override
    public boolean create(PlayClerkArticleInfoEntity entity) {
        if (!queryClerkUnauditedArticleInfo(entity.getClerkId()).isEmpty()) {
            throw new CustomException("存在未审核动态,无法新增");
        }

        if (StrUtil.isBlankIfStr(entity.getId())) {
            entity.setId(IdUtils.getUuid());
        }
        return save(entity);
    }

    /**
     * 查询店员未审核的动态
     *
     * @param clerkId
     *            店员ID
     * @return 店员未审核的动态列表
     */
    public List<PlayClerkArticleInfoEntity> queryClerkUnauditedArticleInfo(String clerkId) {
        LambdaQueryWrapper<PlayClerkArticleInfoEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(PlayClerkArticleInfoEntity::getClerkId, clerkId);
        lambdaQueryWrapper.eq(PlayClerkArticleInfoEntity::getReviewState, "0");
        return this.baseMapper.selectList(lambdaQueryWrapper);
    }

    /**
     * 修改店员动态信息
     *
     * @param playClerkArticleInfo
     *            店员动态信息
     * @return 结果
     */
    @Override
    public boolean update(PlayClerkArticleInfoEntity playClerkArticleInfo) {
        return updateById(playClerkArticleInfo);
    }

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

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