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.update.LambdaUpdateWrapper;
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.PlayClerkRankingInfoMapper;
import com.starry.admin.modules.clerk.module.entity.*;
import com.starry.admin.modules.clerk.service.IPlayClerkRankingInfoService;
import com.starry.admin.modules.personnel.service.IPlayPersonnelGroupInfoService;
import com.starry.admin.modules.weichat.entity.order.PlayOrderHistoryRankingReturnVo;
import com.starry.admin.modules.weichat.entity.order.PlayOrderRankingListVo;
import com.starry.admin.modules.weichat.entity.order.PlayOrderRankingReturnVo;
import com.starry.admin.utils.SecurityUtils;
import com.starry.common.utils.IdUtils;
import java.time.LocalDate;
import java.util.Arrays;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;

/**
 * 店员排行Service业务层处理
 *
 * @author admin
 * @since 2024-05-25
 */
@Service
public class PlayClerkRankingInfoServiceImpl extends ServiceImpl<PlayClerkRankingInfoMapper, PlayClerkRankingInfoEntity>
        implements
            IPlayClerkRankingInfoService {
    @Resource
    private PlayClerkRankingInfoMapper playClerkRankingInfoMapper;

    @Resource
    private IPlayPersonnelGroupInfoService playClerkGroupInfoService;

    @Override
    public IPage<IPlayClerkRankingInfoReturnVo> selectByPage(IPlayClerkRankingInfoQueryVo vo) {
        MPJLambdaWrapper<PlayClerkRankingInfoEntity> lambdaQueryWrapper = new MPJLambdaWrapper<>();
        lambdaQueryWrapper.selectAll(PlayClerkRankingInfoEntity.class);
        // 拼接店员表
        lambdaQueryWrapper.selectAs(PlayClerkUserInfoEntity::getNickname, "clerkNickname")
                .selectAs(PlayClerkUserInfoEntity::getSex, "clerkSex");
        lambdaQueryWrapper.leftJoin(PlayClerkUserInfoEntity.class, PlayClerkUserInfoEntity::getId,
                PlayClerkRankingInfoEntity::getClerkId);
        if (StrUtil.isNotBlank(vo.getClerkId())) {
            lambdaQueryWrapper.eq(PlayClerkRankingInfoEntity::getClerkId, vo.getClerkId());
        }
        if (StrUtil.isNotBlank(vo.getWeeklyRanking())) {
            lambdaQueryWrapper.eq(PlayClerkRankingInfoEntity::getWeeklyRanking, vo.getWeeklyRanking());
        }
        if (StrUtil.isNotBlank(vo.getHistoricalStatistics())) {
            lambdaQueryWrapper.eq(PlayClerkRankingInfoEntity::getHistoricalStatistics, vo.getHistoricalStatistics());
        }
        if (StrUtil.isNotBlank(vo.getSex())) {
            lambdaQueryWrapper.eq(PlayClerkUserInfoEntity::getSex, vo.getSex());
        }
        if (vo.getSettlementDate() != null) {
            lambdaQueryWrapper.eq(PlayClerkRankingInfoEntity::getSettlementDate, vo.getSettlementDate());
        }
        // 加入组员的筛选
        List<String> clerkIdList = playClerkGroupInfoService.getValidClerkIdList(SecurityUtils.getLoginUser(), null);
        lambdaQueryWrapper.in(PlayClerkUserInfoEntity::getId, clerkIdList);

        lambdaQueryWrapper.orderByAsc(PlayClerkRankingInfoEntity::getRankingIndex);
        return this.baseMapper.selectJoinPage(new Page<>(vo.getPageNum(), vo.getPageSize()),
                IPlayClerkRankingInfoReturnVo.class, lambdaQueryWrapper);
    }

    /**
     * 查询店员排行
     *
     * @param id
     *            店员排行主键
     * @return 店员排行
     */
    @Override
    public PlayClerkRankingInfoEntity selectPlayClerkRankingInfoById(String id) {
        return this.baseMapper.selectById(id);
    }

    @Override
    public List<PlayOrderHistoryRankingReturnVo> selectHistoryRanking(String clerkId) {
        long serialNumber = selectSerialNumber("1");
        MPJLambdaWrapper<PlayClerkRankingInfoEntity> lambdaQueryWrapper = new MPJLambdaWrapper<>();
        // 根据排行序号和用户ID查询
        lambdaQueryWrapper.ne(PlayClerkRankingInfoEntity::getSerialNumber, serialNumber);
        lambdaQueryWrapper.eq(PlayClerkRankingInfoEntity::getClerkId, clerkId);
        return this.baseMapper.selectJoinList(PlayOrderHistoryRankingReturnVo.class, lambdaQueryWrapper);
    }

    @Override
    public PlayOrderRankingReturnVo selectCurrentRanking(String clerkId) {
        long serialNumber = selectSerialNumber("1");
        MPJLambdaWrapper<PlayClerkRankingInfoEntity> lambdaQueryWrapper = new MPJLambdaWrapper<>();
        lambdaQueryWrapper.selectAll(PlayClerkRankingInfoEntity.class);
        // 店员信息
        lambdaQueryWrapper.selectAs(PlayClerkUserInfoEntity::getId, "clerkId")
                .selectAs(PlayClerkUserInfoEntity::getNickname, "clerkNickname")
                .selectAs(PlayClerkUserInfoEntity::getAvatar, "clerkAvatar")
                .selectAs(PlayClerkUserInfoEntity::getSex, "clerkSex");
        lambdaQueryWrapper.leftJoin(PlayClerkUserInfoEntity.class, PlayClerkUserInfoEntity::getId,
                PlayClerkRankingInfoEntity::getClerkId);
        // 店员等级
        lambdaQueryWrapper.selectAs(PlayClerkLevelInfoEntity::getId, "levelId")
                .selectAs(PlayClerkLevelInfoEntity::getName, "levelName");
        lambdaQueryWrapper.leftJoin(PlayClerkLevelInfoEntity.class, PlayClerkLevelInfoEntity::getId,
                PlayClerkUserInfoEntity::getLevelId);
        // 根据排行序号和用户ID查询
        lambdaQueryWrapper.eq(PlayClerkRankingInfoEntity::getSerialNumber, serialNumber);
        lambdaQueryWrapper.eq(PlayClerkRankingInfoEntity::getClerkId, clerkId);
        lambdaQueryWrapper.eq(PlayClerkRankingInfoEntity::getHistoricalStatistics, "0");
        PlayOrderRankingReturnVo returnVo = this.baseMapper.selectJoinOne(PlayOrderRankingReturnVo.class,
                lambdaQueryWrapper);
        if (returnVo == null) {
            returnVo = new PlayOrderRankingReturnVo();
        }
        returnVo.setRankings(this.selectBySerialNumber(serialNumber));
        return returnVo;
    }

    @Override
    public List<PlayClerkRankingInfoEntity> selectMaxSerialNumber(String weeklyRanking) {
        MPJLambdaWrapper<PlayClerkRankingInfoEntity> lambdaQueryWrapper = new MPJLambdaWrapper<>();
        lambdaQueryWrapper.eq(PlayClerkRankingInfoEntity::getSerialNumber, this.selectSerialNumber(weeklyRanking));
        return this.baseMapper.selectList(lambdaQueryWrapper);
    }

    @Override
    public void updateClerkRankingInfo(String weeklyRanking, Integer serialNumber) {
        LambdaUpdateWrapper<PlayClerkRankingInfoEntity> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.eq(PlayClerkRankingInfoEntity::getWeeklyRanking, weeklyRanking);
        lambdaUpdateWrapper.eq(PlayClerkRankingInfoEntity::getSerialNumber, serialNumber);
        lambdaUpdateWrapper.set(PlayClerkRankingInfoEntity::getHistoricalStatistics, "1");
        this.baseMapper.update(null, lambdaUpdateWrapper);
    }

    /**
     * 获取当前期排名序号
     *
     * @return PlayClerkRankingInfoEntity
     */
    @Override
    public Integer selectSerialNumber(String weeklyRanking) {
        LambdaQueryWrapper<PlayClerkRankingInfoEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(PlayClerkRankingInfoEntity::getWeeklyRanking, weeklyRanking);
        lambdaQueryWrapper.orderByDesc(PlayClerkRankingInfoEntity::getSerialNumber).last("limit 1");
        PlayClerkRankingInfoEntity entity = this.baseMapper.selectOne(lambdaQueryWrapper);
        if (entity == null || entity.getSerialNumber() == null) {
            return 0;
        }
        return entity.getSerialNumber();
    }

    List<PlayOrderRankingListVo> selectBySerialNumber(long serialNumber) {
        MPJLambdaWrapper<PlayClerkRankingInfoEntity> lambdaQueryWrapper = new MPJLambdaWrapper<>();
        lambdaQueryWrapper.eq(PlayClerkRankingInfoEntity::getSerialNumber, serialNumber);
        lambdaQueryWrapper.orderByDesc(PlayClerkRankingInfoEntity::getRankingIndex);
        lambdaQueryWrapper.selectAll(PlayClerkRankingInfoEntity.class);
        // 店员信息
        lambdaQueryWrapper.selectAs(PlayClerkUserInfoEntity::getId, "clerkId")
                .selectAs(PlayClerkUserInfoEntity::getNickname, "clerkNickname")
                .selectAs(PlayClerkUserInfoEntity::getAvatar, "clerkAvatar")
                .selectAs(PlayClerkUserInfoEntity::getSex, "clerkSex");
        lambdaQueryWrapper.leftJoin(PlayClerkUserInfoEntity.class, PlayClerkUserInfoEntity::getId,
                PlayClerkRankingInfoEntity::getClerkId);
        // 店员等级
        lambdaQueryWrapper.selectAs(PlayClerkLevelInfoEntity::getId, "levelId")
                .selectAs(PlayClerkLevelInfoEntity::getName, "levelName");
        lambdaQueryWrapper.leftJoin(PlayClerkLevelInfoEntity.class, PlayClerkLevelInfoEntity::getId,
                PlayClerkUserInfoEntity::getLevelId);
        return this.baseMapper.selectJoinList(PlayOrderRankingListVo.class, lambdaQueryWrapper);
    }

    @Override
    public PlayClerkRankingInfoEntity selectByTime(String clerkId, LocalDate startTime, LocalDate endTime) {
        MPJLambdaWrapper<PlayClerkRankingInfoEntity> lambdaQueryWrapper = new MPJLambdaWrapper<>();
        lambdaQueryWrapper.eq(PlayClerkRankingInfoEntity::getStartCountDate, startTime);
        lambdaQueryWrapper.eq(PlayClerkRankingInfoEntity::getEndCountDate, endTime);
        lambdaQueryWrapper.eq(PlayClerkRankingInfoEntity::getClerkId, clerkId);
        return this.baseMapper.selectOne(lambdaQueryWrapper);
    }

    /**
     * 查询店员排行列表
     *
     * @param playClerkRankingInfo
     *            店员排行
     * @return 店员排行
     */
    @Override
    public IPage<PlayClerkRankingInfoEntity> selectPlayClerkRankingInfoByPage(
            PlayClerkRankingInfoEntity playClerkRankingInfo) {
        Page<PlayClerkRankingInfoEntity> page = new Page<>(1, 10);
        return this.baseMapper.selectPage(page, new LambdaQueryWrapper<>());
    }

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

    /**
     * 修改店员排行
     *
     * @param playClerkRankingInfo
     *            店员排行
     * @return 结果
     */
    @Override
    public boolean update(PlayClerkRankingInfoEntity playClerkRankingInfo) {
        return updateById(playClerkRankingInfo);
    }

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

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