package jnpf.cultivate.service.impl;

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.UserInfo;
import jnpf.base.service.SuperServiceImpl;
import jnpf.cultivate.mapper.FtbCourseGainedLikeMapper;
import jnpf.cultivate.service.FtbCourseGainedLikeService;
import jnpf.model.cultivate.dto.gained.FtbLikePagination;
import jnpf.model.cultivate.po.gained.FtbCourseGainedLikeEntity;
import jnpf.util.RandomUtil;
import jnpf.util.UserProvider;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 *
 * 心得点赞 服务实现类
 *
 */
@Service
public class FtbCourseGainedLikeServiceImpl extends SuperServiceImpl<FtbCourseGainedLikeMapper, FtbCourseGainedLikeEntity> implements FtbCourseGainedLikeService {

    @Autowired
    private UserProvider userProvider;
    @Resource
    private FtbCourseGainedLikeMapper ftbCourseGainedLikeMapper;

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

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

    @Override
    @DSTransactional
    public void create(FtbCourseGainedLikeEntity entity){
        UserInfo userInfo = userProvider.get();
        entity.setId(RandomUtil.uuId());
        entity.setCreatorUserId(userInfo.getUserId());
        entity.setTenantId(userInfo.getTenantId());
        this.save(entity);
        //ftbGroupEventService.addInteractionScore(entity.getCourseId(),entity.getGainedId());
    }

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

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

    @Override
    public Integer getMyLike(FtbCourseGainedLikeEntity gainedLikeEntity) {
        return ftbCourseGainedLikeMapper.getMyLike(gainedLikeEntity.getGainedId(),gainedLikeEntity.getLikeUserId());

    }

    @Override
    public void deleteLikeByGainedId(String gainedId, String likeUserId) {
        ftbCourseGainedLikeMapper.deleteLikeByGainedId(gainedId,likeUserId);
    }
}