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.FtbCourseGainedCommentMapper;
import jnpf.cultivate.service.FtbCourseGainedCommentService;
import jnpf.cultivate.service.FtbCourseGainedService;
import jnpf.model.cultivate.dto.gained.FtbCommentPagination;
import jnpf.model.cultivate.po.gained.FtbCourseGainedCommentEntity;
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 java.util.List;

/**
 *
 * 心得评论 服务实现类
 *
 * @author yanglei
 * @since 2023-07-19
 */
@Service
public class FtbCourseGainedCommentServiceImpl extends SuperServiceImpl<FtbCourseGainedCommentMapper, FtbCourseGainedCommentEntity> implements FtbCourseGainedCommentService {

    @Autowired
    private UserProvider userProvider;


    @Override
    public List<FtbCourseGainedCommentEntity> getList(FtbCommentPagination pagination){
        //通过UserProvider获取用户信息
        UserInfo userInfo = userProvider.get();
        QueryWrapper<FtbCourseGainedCommentEntity> queryWrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(pagination.getGainedId())){
            queryWrapper.lambda().eq(FtbCourseGainedCommentEntity::getGainedId, pagination.getGainedId());
        }
        if (pagination.getIsApp() == 1){
            queryWrapper.lambda().isNull(FtbCourseGainedCommentEntity::getParentId);
        }
        if (StringUtils.isNotEmpty(pagination.getOrganizeId())) {
            queryWrapper.lambda().like(FtbCourseGainedCommentEntity::getOrganizeId, pagination.getOrganizeId());
        }
        if (StringUtils.isNotEmpty(pagination.getKeyword())) {
            queryWrapper.lambda().and(
                    t -> t.like(FtbCourseGainedCommentEntity::getAccount, pagination.getKeyword())
                            .or().like(FtbCourseGainedCommentEntity::getRealName, pagination.getKeyword()));
        }
        //排序
        if (StringUtils.isEmpty(pagination.getSidx())) {
        } else {
        queryWrapper = "asc".equalsIgnoreCase(pagination.getSort()) ? queryWrapper.orderByAsc(pagination.getSidx()) : queryWrapper.orderByDesc(pagination.getSidx());
        }
        Page<FtbCourseGainedCommentEntity> page = new Page<>(pagination.getCurrentPage(), pagination.getPageSize());
        IPage<FtbCourseGainedCommentEntity> userIPage = this.page(page, queryWrapper);
        return pagination.setData(userIPage.getRecords(), page.getTotal());
    }

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


    @Override
    @DSTransactional
    public void create(FtbCourseGainedCommentEntity entity){
        UserInfo userInfo = userProvider.get();
        entity.setId(RandomUtil.uuId());
        entity.setCreatorUserId(userInfo.getUserId());
        entity.setTenantId(userInfo.getTenantId());
        entity.setRealName(userInfo.getUserName());
        entity.setOrganizeId(userInfo.getOrganizeId());
        entity.setAccount(userInfo.getUserAccount());
        this.save(entity);
        //FtbGroupEventService.add(entity.getCourseId(), entity.getId(), entity.getGainedId(), FtbGroupEventTypeEnum.COMMENT.getCode());
    }

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

    @Override
    public void delete(FtbCourseGainedCommentEntity entity) {
        if (entity != null) {
        this.removeById(entity.getId());
         //   FtbGroupEventService.del(entity.getId());
        }
    }
}