package com.kym.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.github.pagehelper.PageHelper;
import com.kym.common.utils.CommUtil;
import com.kym.entity.Feedback;
import com.kym.entity.queryParams.FeedbackQueryParam;
import com.kym.entity.common.PageBean;
import com.kym.mapper.FeedbackMapper;
import com.kym.service.FeedbackService;
import com.kym.service.mybatisplus.MyBaseServiceImpl;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * <p>
 * 纠错反馈 服务实现类
 * </p>
 *
 * @author skyline
 * @since 2024-11-12
 */
@Service
public class FeedbackServiceImpl extends MyBaseServiceImpl<FeedbackMapper, Feedback> implements FeedbackService {

    @Override
    public int add(Feedback feedback) {
        StpUtil.checkLogin();
        feedback.setSubmitUserId(StpUtil.getLoginIdAsLong());
        feedback.setSubmitTime(LocalDateTime.now());
        save(feedback);
        return 0;
    }

    @Override
    public void modify(Feedback feedback) {

    }

    @Override
    public void reply(Feedback feedback) {
        Feedback old = getById(feedback.getId());
        old.setReplyContent(feedback.getReplyContent());
        old.setReplyUserId(StpUtil.getLoginIdAsLong());
        old.setReplyTime(LocalDateTime.now());
        updateById(old);
    }

    @Override
    public Object list(FeedbackQueryParam query) {
        PageHelper.startPage(query.getPageNum(), query.getPageSize());
        var res = lambdaQuery()
                .like(CommUtil.isNotEmptyAndNull(query.getTitle()), Feedback::getTitle, query.getTitle())
                .eq(CommUtil.isNotEmptyAndNull(query.getType()), Feedback::getType, query.getType())
                .eq(CommUtil.isNotEmptyAndNull(query.getStatus()), Feedback::getStatus, query.getStatus())
                .eq(CommUtil.isNotEmptyAndNull(query.getSubmitUserId()), Feedback::getSubmitUserId, StpUtil.getLoginIdAsLong()).list();
        return new PageBean<>(res);
    }

    @Override
    public Feedback detail(long id) {
        return getById(id);
    }

    @Override
    public void status(long id, int status) {
        lambdaUpdate().set(Feedback::getStatus, status).eq(Feedback::getId, id).update();
    }

    @Override
    public void remove(long id) {
        removeById(id);
    }
}
