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.Pagination;
import jnpf.base.UserInfo;
import jnpf.base.service.SuperServiceImpl;
import jnpf.cultivate.mapper.FtbCourseGainedShareMapper;
import jnpf.cultivate.service.FtbCourseGainedService;
import jnpf.cultivate.service.FtbCourseGainedShareService;
import jnpf.model.cultivate.po.gained.FtbCourseGainedShareEntity;
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 FtbCourseGainedShareServiceImpl extends SuperServiceImpl<FtbCourseGainedShareMapper, FtbCourseGainedShareEntity> implements FtbCourseGainedShareService {

    @Autowired
    private UserProvider userProvider;

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

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

    @Override
    @DSTransactional
    public void create(FtbCourseGainedShareEntity entity) {
        entity.setId(RandomUtil.uuId());
        this.save(entity);
       // ftbGroupEventService.addInteractionScore(entity.getCourseId(),entity.getGainedId());
    }

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

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

//    @Override
//    public ActionResult<String> sharingSquare(String id) {
//        ShareContentEntityFormContent shareContentEntityFormContent = new ShareContentEntityFormContent();
//
//        //用户id
//        UserInfo userInfo = userProvider.get();
//        String userId = userInfo.getId();
//
//        FtbCourseGainedEntity gainedEntity = FtbCourseGainedService.getById(id);
//
//        if (ObjectUtils.isEmpty(gainedEntity)) {
//            return ActionResult.fail("分享内容不存在");
//        }
//        String content = gainedEntity.getContent();
//        String contentId = gainedEntity.getCourseId();
//
//        FtbCourseEntity FtbCourse = FtbCourseService.getById(contentId);
//
//        shareContentEntityFormContent.setUserId(userId);
//        shareContentEntityFormContent.setContent(StringUtils.isEmpty(content) ? "" : content);
//        //fixme 未提供图片地址
//        shareContentEntityFormContent.setPicture("");
//        shareContentEntityFormContent.setContentId(ObjectUtils.isEmpty(FtbCourse) ? null : contentId);
//        shareContentEntityFormContent.setCourseName(ObjectUtils.isEmpty(FtbCourse) ? "" :
//                FtbCourse.getName());
//        try {
//            shareApi.publishContent(shareContentEntityFormContent);
//            return ActionResult.success("分享成功");
//        } catch (Exception e) {
//            return ActionResult.fail("网络异常请稍后再试");
//        }
//    }
}