package shop.alien.store.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 shop.alien.entity.result.R;
import shop.alien.entity.store.CommonPushReview;
import shop.alien.mapper.CommonPushReviewMapper;
import shop.alien.store.service.CommonPushReviewService;

@Slf4j
@Service
@RequiredArgsConstructor
public class CommonPushReviewServiceImpl extends ServiceImpl<CommonPushReviewMapper, CommonPushReview> implements CommonPushReviewService {

    @Override
    public R<String> add(CommonPushReview review) {
        log.info("CommonPushReviewServiceImpl.add, param={}", review);
        boolean result = this.save(review);
        return result ? R.success("新增成功") : R.fail("新增失败");
    }

    @Override
    public R<String> deleteById(Long id) {
        log.info("CommonPushReviewServiceImpl.deleteById, id={}", id);
        boolean result = this.removeById(id);
        return result ? R.success("删除成功") : R.fail("删除失败");
    }

    @Override
    public R<String> update(CommonPushReview review) {
        log.info("CommonPushReviewServiceImpl.update, param={}", review);
        if (review.getId() == null) {
            return R.fail("id不能为空");
        }
        boolean result = this.updateById(review);
        return result ? R.success("更新成功") : R.fail("更新失败");
    }

    @Override
    public R<CommonPushReview> getInfoById(Long id) {
        log.info("CommonPushReviewServiceImpl.getInfoById, id={}", id);
        return R.data(this.getById(id));
    }

    @Override
    public R<IPage<CommonPushReview>> list(Integer pageNum, Integer pageSize, Long pushTaskId, Integer reviewStatus) {
        log.info("CommonPushReviewServiceImpl.list, pageNum={}, pageSize={}, pushTaskId={}, reviewStatus={}",
                pageNum, pageSize, pushTaskId, reviewStatus);
        Page<CommonPushReview> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<CommonPushReview> wrapper = new LambdaQueryWrapper<>();
        if (pushTaskId != null) {
            wrapper.eq(CommonPushReview::getPushTaskId, pushTaskId);
        }
        if (reviewStatus != null) {
            wrapper.eq(CommonPushReview::getReviewStatus, reviewStatus);
        }
        wrapper.orderByDesc(CommonPushReview::getUpdatedTime);
        return R.data(this.page(page, wrapper));
    }
}
