package jnpf.cultivate.service.impl;

import com.alibaba.cloud.commons.lang.StringUtils;
import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import jnpf.base.Pagination;
import jnpf.base.UserInfo;
import jnpf.base.service.SuperServiceImpl;
import jnpf.cultivate.mapper.FtbCourseGainedReaderMapper;
import jnpf.cultivate.service.FtbCourseGainedReaderService;
import jnpf.model.cultivate.po.gained.FtbCourseGainedReaderEntity;
import jnpf.util.RandomUtil;
import jnpf.util.UserProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 *
 * 心得可查看用户关联表 服务实现类
 *
 */
@Service
public class FtbCourseGainedReaderServiceImpl extends SuperServiceImpl<FtbCourseGainedReaderMapper, FtbCourseGainedReaderEntity> implements FtbCourseGainedReaderService {

    @Autowired
    private UserProvider userProvider;

    @Override
    public List<FtbCourseGainedReaderEntity> getList(Pagination pagination){
        //通过UserProvider获取用户信息
        UserInfo userInfo = userProvider.get();
        QueryWrapper<FtbCourseGainedReaderEntity> queryWrapper = new QueryWrapper<>();
        //排序
        if (StringUtils.isEmpty(pagination.getSidx())) {
        } else {
        queryWrapper = "asc".equalsIgnoreCase(pagination.getSort()) ? queryWrapper.orderByAsc(pagination.getSidx()) : queryWrapper.orderByDesc(pagination.getSidx());
        }
        Page<FtbCourseGainedReaderEntity> page = new Page<>(pagination.getCurrentPage(), pagination.getPageSize());
        IPage<FtbCourseGainedReaderEntity> userIPage = this.page(page, queryWrapper);
        return pagination.setData(userIPage.getRecords(), page.getTotal());
    }

    @Override
    public FtbCourseGainedReaderEntity getInfo(String id){
        QueryWrapper<FtbCourseGainedReaderEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(FtbCourseGainedReaderEntity::getId, id);
        return this.getOne(queryWrapper);
    }

    @Override
    @DSTransactional
    public void create(FtbCourseGainedReaderEntity entity){
        UserInfo userInfo = userProvider.get();
        entity.setId(RandomUtil.uuId());
        entity.setCreatorUserId(userInfo.getUserId());
        entity.setTenantId(userInfo.getTenantId());
        this.save(entity);
    }

    @Override
    @Transactional
    public void update(String id, FtbCourseGainedReaderEntity entity){
        entity.setId(id);
        this.updateById(entity);
    }

    @Override
    public void delete(FtbCourseGainedReaderEntity entity) {
        if (entity != null) {
        this.removeById(entity.getId());
        }
    }
}