package com.zanxiang.game.module.manage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zanxiang.erp.security.util.SecurityUtil;
import com.zanxiang.game.module.manage.pojo.dto.KfWebSocketMsgDTO;
import com.zanxiang.game.module.manage.pojo.params.KfWebSocketMsgParam;
import com.zanxiang.game.module.manage.service.IKfQuickReplyService;
import com.zanxiang.game.module.mybatis.entity.KfQuickReply;
import com.zanxiang.game.module.mybatis.mapper.KfQuickReplyMapper;
import com.zanxiang.module.util.bean.BeanUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.util.Strings;
import org.springframework.stereotype.Service;
import reactor.util.function.Tuple2;
import reactor.util.function.Tuples;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author : lingfeng
 * @time : 2024-03-05
 * @description : 客服快捷回复
 */
@Slf4j
@Service
public class KfQuickReplyServiceImpl extends ServiceImpl<KfQuickReplyMapper, KfQuickReply> implements IKfQuickReplyService {

    @Override
    public boolean quickReplyAdd(KfWebSocketMsgParam.QuickReplyBean param) {
        String content = param.getContent();
        if (Strings.isBlank(content) || Strings.isBlank(content.replaceAll("\\s", ""))) {
            return Boolean.FALSE;
        }
        return super.save(KfQuickReply.builder()
                .kfUserId(SecurityUtil.getUserId())
                .content(content)
                .status(Boolean.TRUE)
                .createTime(LocalDateTime.now())
                .updateTime(LocalDateTime.now())
                .build());
    }

    @Override
    public boolean quickReplyDelete(KfWebSocketMsgParam.QuickReplyBean param) {
        return super.update(new LambdaUpdateWrapper<KfQuickReply>()
                .set(KfQuickReply::getStatus, Boolean.FALSE)
                .set(KfQuickReply::getUpdateTime, LocalDateTime.now())
                .eq(KfQuickReply::getId, param.getId())
                .eq(KfQuickReply::getKfUserId, SecurityUtil.getUserId())
        );
    }

    @Override
    public boolean quickReplyUpdate(KfWebSocketMsgParam.QuickReplyBean param) {
        return super.update(new LambdaUpdateWrapper<KfQuickReply>()
                .set(KfQuickReply::getContent, param.getContent())
                .set(KfQuickReply::getUpdateTime, LocalDateTime.now())
                .eq(KfQuickReply::getId, param.getId())
                .eq(KfQuickReply::getKfUserId, SecurityUtil.getUserId())
        );
    }

    @Override
    public Tuple2<KfWebSocketMsgDTO.PageBean, List<KfWebSocketMsgDTO.QuickReplyBean>> quickReplyList(KfWebSocketMsgParam.PageBean pageBean) {
        Page<KfQuickReply> kfQuickReplyPage = super.page(new Page<>(pageBean.getPageNum(), pageBean.getPageSize()),
                new QueryWrapper<KfQuickReply>().lambda()
                        .eq(KfQuickReply::getKfUserId, SecurityUtil.getUserId())
                        .eq(KfQuickReply::getStatus, Boolean.TRUE)
                        .orderByDesc(KfQuickReply::getCreateTime)
        );
        List<KfWebSocketMsgDTO.QuickReplyBean> quickReplyList = kfQuickReplyPage.getRecords().stream()
                .map(this::transform).collect(Collectors.toList());
        return Tuples.of(KfWebSocketMsgDTO.transformPage(kfQuickReplyPage), quickReplyList);
    }

    private KfWebSocketMsgDTO.QuickReplyBean transform(KfQuickReply kfQuickReply) {
        if (kfQuickReply == null) {
            return null;
        }
        return BeanUtil.copy(kfQuickReply, KfWebSocketMsgDTO.QuickReplyBean.class);
    }
}
