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.LifeUserLearningRecord;
import shop.alien.entity.store.LifeUserLearningVideo;
import shop.alien.entity.store.vo.LifeUserLearningVideoVo;
import shop.alien.mapper.LifeUserLearningRecordMapper;
import shop.alien.mapper.LifeUserLearningVideoMapper;
import shop.alien.store.service.LifeUserLearningRecordService;

import java.util.List;

@Service
@RequiredArgsConstructor
@Slf4j
public class LifeUserLearningRecordServiceImpl extends ServiceImpl<LifeUserLearningRecordMapper, LifeUserLearningRecord> implements LifeUserLearningRecordService {

    private final LifeUserLearningRecordMapper lifeUserLearningRecordMapper;
    private final LifeUserLearningVideoMapper lifeUserLearningVideoMapper;

    @Override
    public R<String> add(LifeUserLearningRecord lifeUserLearningRecord) {
        log.info("LifeUserLearningRecordServiceImpl.add, param={}", lifeUserLearningRecord);
        boolean result = this.save(lifeUserLearningRecord);
        if (result) {
            return R.success("新增成功");
        }
        return R.fail("新增失败");
    }

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

    @Override
    public R<String> update(LifeUserLearningRecord lifeUserLearningRecord) {
        log.info("LifeUserLearningRecordServiceImpl.update, param={}", lifeUserLearningRecord);
        boolean result = this.updateById(lifeUserLearningRecord);
        if (result) {
            return R.success("更新成功");
        }
        return R.fail("更新失败");
    }

    @Override
    public R<LifeUserLearningRecord> getInfoById(Integer id) {
        log.info("LifeUserLearningRecordServiceImpl.getInfoById, id={}", id);
        LifeUserLearningRecord result = this.getById(id);
        return R.data(result);
    }

    @Override
    public R<LifeUserLearningRecord> getByUserId(Integer userId) {
        log.info("LifeUserLearningRecordServiceImpl.getByUserId, userId={}", userId);
        LambdaQueryWrapper<LifeUserLearningRecord> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(LifeUserLearningRecord::getUserId, userId);
        LifeUserLearningRecord result = this.getOne(queryWrapper);
        return R.data(result);
    }

    @Override
    public R<IPage<LifeUserLearningRecord>> list(Integer pageNum, Integer pageSize, Integer userId) {
        log.info("LifeUserLearningRecordServiceImpl.list, pageNum={}, pageSize={}, userId={}", pageNum, pageSize, userId);
        Page<LifeUserLearningRecord> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<LifeUserLearningRecord> queryWrapper = new LambdaQueryWrapper<>();
        if (userId != null) {
            queryWrapper.eq(LifeUserLearningRecord::getUserId, userId);
        }
        queryWrapper.orderByDesc(LifeUserLearningRecord::getCreatedTime);
        IPage<LifeUserLearningRecord> result = this.page(page, queryWrapper);
        return R.data(result);
    }

    @Override
    public R<List<LifeUserLearningVideoVo>> getListByUserId(Integer userId) {
        return R.data(lifeUserLearningRecordMapper.getListByUserId(userId));
    }
}

