package shop.alien.lawyer.service.impl;

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 lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import shop.alien.entity.result.R;
import shop.alien.entity.store.LawyerConsultationReview;
import shop.alien.lawyer.service.LawyerConsultationReviewService;
import shop.alien.mapper.LawyerConsultationReviewMapper;

import java.util.List;

/**
 * 咨询评价 服务实现类
 *
 * @author system
 * @since 2025-01-XX
 */
@Slf4j
@Transactional
@Service
@RequiredArgsConstructor
public class LawyerConsultationReviewServiceImpl extends ServiceImpl<LawyerConsultationReviewMapper, LawyerConsultationReview> implements LawyerConsultationReviewService {

    private final LawyerConsultationReviewMapper consultationReviewMapper;

    @Override
    public R<IPage<LawyerConsultationReview>> getConsultationReviewList(int pageNum, int pageSize, Integer consultationOrderId,
                                                                        Integer lawyerUserId, Integer clientUserId) {
        log.info("LawyerConsultationReviewServiceImpl.getConsultationReviewList?pageNum={},pageSize={},consultationOrderId={},lawyerUserId={},clientUserId={}",
                pageNum, pageSize, consultationOrderId, lawyerUserId, clientUserId);
        Page<LawyerConsultationReview> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<LawyerConsultationReview> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(LawyerConsultationReview::getDeleteFlag, 0);
        if (consultationOrderId != null) {
            queryWrapper.eq(LawyerConsultationReview::getConsultationOrderId, consultationOrderId);
        }
        if (lawyerUserId != null) {
            queryWrapper.eq(LawyerConsultationReview::getLawyerUserId, lawyerUserId);
        }
        if (clientUserId != null) {
            queryWrapper.eq(LawyerConsultationReview::getClientUserId, clientUserId);
        }
        queryWrapper.orderByDesc(LawyerConsultationReview::getCreatedTime);
        IPage<LawyerConsultationReview> pageResult = this.page(page, queryWrapper);
        return R.data(pageResult);
    }

    @Override
    public List<LawyerConsultationReview> getListByLawyerId(Integer lawyerUserId) {
        log.info("LawyerConsultationReviewServiceImpl.getListByLawyerId?lawyerUserId={}", lawyerUserId);
        LambdaQueryWrapper<LawyerConsultationReview> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(LawyerConsultationReview::getLawyerUserId, lawyerUserId)
                .eq(LawyerConsultationReview::getDeleteFlag, 0)
                .orderByDesc(LawyerConsultationReview::getCreatedTime);
        return this.list(queryWrapper);
    }

    @Override
    public R<LawyerConsultationReview> addConsultationReview(LawyerConsultationReview consultationReview) {
        log.info("LawyerConsultationReviewServiceImpl.addConsultationReview?consultationReview={}", consultationReview);
        boolean result = this.save(consultationReview);
        if (result) {
            return R.data(consultationReview);
        }
        return R.fail("新增失败");
    }

    @Override
    public R<LawyerConsultationReview> editConsultationReview(LawyerConsultationReview consultationReview) {
        log.info("LawyerConsultationReviewServiceImpl.editConsultationReview?consultationReview={}", consultationReview);
        boolean result = this.updateById(consultationReview);
        if (result) {
            return R.data(consultationReview);
        }
        return R.fail("修改失败");
    }

    @Override
    public R<Boolean> deleteConsultationReview(Integer id) {
        log.info("LawyerConsultationReviewServiceImpl.deleteConsultationReview?id={}", id);
        boolean result = this.removeById(id);
        if (result) {
            return R.success("删除成功");
        }
        return R.fail("删除失败");
    }
}

