package com.yunzhupaas.flowable.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yunzhupaas.base.UserInfo;
import com.yunzhupaas.base.service.SuperServiceImpl;
import com.yunzhupaas.flowable.entity.CommonEntity;
import com.yunzhupaas.flowable.mapper.CommonMapper;
import com.yunzhupaas.flowable.service.CommonService;
import com.yunzhupaas.util.RandomUtil;
import com.yunzhupaas.util.UserProvider;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 类的描述
 *
 * @author YUNZHUPAASYUNZHUPAAS开发组
 * @version 5.0.x
 * @since 2024/5/22 20:32
 */
@Service
public class CommonServiceImpl extends SuperServiceImpl<CommonMapper, CommonEntity> implements CommonService {

    @Override
    public List<CommonEntity> getCommonByUserId(String userId) {

        QueryWrapper<CommonEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(CommonEntity::getCreatorUserId, userId);

        return this.list(queryWrapper);
    }

    @Override
    public int setCommonFLow(String flowId) {

        UserInfo userInfo = UserProvider.getUser();
        String userId = userInfo.getUserId();

        QueryWrapper<CommonEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(CommonEntity::getFlowId, flowId)
                .eq(CommonEntity::getCreatorUserId, userId);
        List<CommonEntity> list = this.list(queryWrapper);

        if (CollectionUtil.isNotEmpty(list)) {
            this.removeByIds(list);
            return 2;
        } else {
            CommonEntity entity = new CommonEntity();
            entity.setId(RandomUtil.uuId());
            entity.setFlowId(flowId);
            entity.setCreatorUserId(userId);
            this.save(entity);
        }
        return 1;
    }
}
