package com.yunzhupaas.portal.model;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yunzhupaas.base.MyBatisPrimaryBase;
import com.yunzhupaas.base.UserInfo;
import com.yunzhupaas.portal.constant.PortalConst;
import com.yunzhupaas.portal.entity.PortalDataEntity;
import com.yunzhupaas.util.UserProvider;
import com.yunzhupaas.util.context.SpringContext;
import lombok.Data;

import java.util.Objects;

/**
 * 类功能
 *
 * @author 云筑产品开发平台组
 * @version v3.4.8
 * @copyrignt 深圳市乐程软件有限公司
 * @date 2023-04-21
 */
@Data
public class PortalCustomPrimary extends MyBatisPrimaryBase<PortalDataEntity> {

    /** 平台 */
    private String platform = PortalConst.WEB;
    /** 门户ID */
    private String portalId;
    /** 系统ID */
    private String systemId;
    /** 用户ID */
    private String creatorId;
    /** 类型（mod：模型、custom：自定义） */
    private String type = PortalConst.CUSTOM;

    public PortalCustomPrimary(String platform, String portalId, String systemId, String userId) {
        if(platform != null) this.platform = platform;
        this.portalId = portalId;
        this.systemId = systemId;
        this.creatorId = userId;
    }

    public PortalCustomPrimary(String platform, String portalId){
        if(platform != null) this.platform = platform;
        this.portalId = portalId;
        UserInfo userInfo = SpringContext.getBean(UserProvider.class).get();
        this.systemId = Objects.equals(platform, PortalConst.WEB) ? userInfo.getSystemId() : userInfo.getAppSystemId();
        this.creatorId = userInfo.getUserId();
    }

    public QueryWrapper<PortalDataEntity> getQuery(){
        queryWrapper.lambda().eq(PortalDataEntity::getType, type);
        if(this.platform != null) queryWrapper.lambda().eq(PortalDataEntity::getPlatform, platform);
        if(this.portalId != null) queryWrapper.lambda().eq(PortalDataEntity::getPortalId, portalId);
        if(this.systemId != null) queryWrapper.lambda().eq(PortalDataEntity::getSystemId, systemId);
//        if(this.creatorId != null) queryWrapper.lambda().eq(PortalDataEntity::getCreatorUserId, creatorId);
        return queryWrapper;
    }

}

