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.common.conf.ThreadLocalRequestDetail;
import com.starry.admin.common.exception.CustomException;
import com.starry.admin.modules.custom.mapper.PlayCustomLeaveMsgMapper;
import com.starry.admin.modules.custom.module.entity.PlayCustomLeaveMsgEntity;
import com.starry.admin.modules.custom.module.entity.PlayCustomUserInfoEntity;
import com.starry.admin.modules.custom.module.vo.PlayCustomLeaveMsgQueryVo;
import com.starry.admin.modules.custom.module.vo.PlayCustomLeaveMsgReturnVo;
import com.starry.admin.modules.custom.service.IPlayCustomLeaveMsgService;
import com.starry.common.utils.IdUtils;
import com.starry.common.utils.StringUtils;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.Arrays;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;

/**
 * 顾客留言Service业务层处理
 *
 * @author admin
 * @since 2024-05-07
 */
@Service
public class PlayCustomLeaveMsgServiceImpl extends ServiceImpl<PlayCustomLeaveMsgMapper, PlayCustomLeaveMsgEntity>
        implements
            IPlayCustomLeaveMsgService {
    @Resource
    private PlayCustomLeaveMsgMapper playCustomLeaveMsgMapper;

    @Override
    public IPage<PlayCustomLeaveMsgReturnVo> selectByPage(PlayCustomLeaveMsgQueryVo vo) {
        MPJLambdaWrapper<PlayCustomLeaveMsgEntity> lambdaQueryWrapper = new MPJLambdaWrapper<PlayCustomLeaveMsgEntity>()
                // 查询主表全部字段
                .selectAll(PlayCustomLeaveMsgEntity.class)
                // 查询顾客表
                .selectAs(PlayCustomUserInfoEntity::getId, "customId")
                .selectAs(PlayCustomUserInfoEntity::getAvatar, "customAvatar")
                .selectAs(PlayCustomUserInfoEntity::getNickname, "customNickname")
                // 子表
                .leftJoin(PlayCustomUserInfoEntity.class, PlayCustomUserInfoEntity::getId,
                        PlayCustomLeaveMsgEntity::getCustomId);
        return this.baseMapper.selectJoinPage(new Page<>(vo.getPageNum(), vo.getPageSize()),
                PlayCustomLeaveMsgReturnVo.class, lambdaQueryWrapper);
    }

    /**
     * 查询顾客留言
     *
     * @param id
     *            顾客留言主键
     * @return 顾客留言
     */
    @Override
    public PlayCustomLeaveMsgEntity selectPlayCustomLeaveMsgById(String id) {
        return this.baseMapper.selectById(id);
    }

    /**
     * 查询顾客留言列表
     *
     * @param playCustomLeaveMsg
     *            顾客留言
     * @return 顾客留言
     */
    @Override
    public IPage<PlayCustomLeaveMsgEntity> selectPlayCustomLeaveMsgByPage(PlayCustomLeaveMsgEntity playCustomLeaveMsg) {
        Page<PlayCustomLeaveMsgEntity> page = new Page<>(1, 10);
        return this.baseMapper.selectPage(page, new LambdaQueryWrapper<>());
    }

    /**
     * 查询当前用户不允许留言的原因 1、一个人在同一天只能留言一次
     *
     * @param customId
     *            顾客ID
     * @return 当前用户不允许留言的原因
     */
    @Override
    public String queryNoMessagesAllowedReason(String customId) {
        // 1、一个人在同一天只能留言一次
        LocalDateTime now = LocalDateTime.now();
        LocalDateTime start = LocalDateTime.of(now.toLocalDate(), LocalTime.MIN);
        LocalDateTime end = LocalDateTime.of(now.toLocalDate(), LocalTime.MAX);
        LambdaQueryWrapper<PlayCustomLeaveMsgEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(PlayCustomLeaveMsgEntity::getCustomId, customId);
        lambdaQueryWrapper.between(PlayCustomLeaveMsgEntity::getMsgTime, start, end);
        if (this.baseMapper.selectOne(lambdaQueryWrapper) != null) {
            return "一天只能留言一次";
        }
        // 2、
        // 3、
        return "";
    }

    /**
     * 新增顾客留言
     *
     * @param playCustomLeaveMsg
     *            顾客留言
     * @return 结果
     */
    @Override
    public boolean create(PlayCustomLeaveMsgEntity playCustomLeaveMsg) {
        String noMessagesAllowedReason = this
                .queryNoMessagesAllowedReason(ThreadLocalRequestDetail.getCustomUserInfo().getId());
        if (StringUtils.isNotEmpty(noMessagesAllowedReason)) {
            throw new CustomException(noMessagesAllowedReason);
        }
        if (StrUtil.isBlankIfStr(playCustomLeaveMsg.getId())) {
            playCustomLeaveMsg.setId(IdUtils.getUuid());
        }
        return save(playCustomLeaveMsg);
    }

    /**
     * 修改顾客留言
     *
     * @param playCustomLeaveMsg
     *            顾客留言
     * @return 结果
     */
    @Override
    public boolean update(PlayCustomLeaveMsgEntity playCustomLeaveMsg) {
        return updateById(playCustomLeaveMsg);
    }

    /**
     * 批量删除顾客留言
     *
     * @param ids
     *            需要删除的顾客留言主键
     * @return 结果
     */
    @Override
    public int deletePlayCustomLeaveMsgByIds(String[] ids) {
        return playCustomLeaveMsgMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 删除顾客留言信息
     *
     * @param id
     *            顾客留言主键
     * @return 结果
     */
    @Override
    public int deletePlayCustomLeaveMsgById(String id) {
        return playCustomLeaveMsgMapper.deleteById(id);
    }
}
