package com.yunfeiyun.agmp.common.web.system.domain;

import com.yunfeiyun.agmp.common.constant.ResConstants;
import com.yunfeiyun.agmp.common.framework.web.service.PolicyService;
import com.yunfeiyun.agmp.common.annotation.Excel;
import com.yunfeiyun.agmp.common.core.domain.BaseEntity;
import com.yunfeiyun.agmp.common.framework.manager.ResManager;
import com.yunfeiyun.agmp.common.utils.StringUtils;
import com.yunfeiyun.agmp.common.utils.spring.SpringUtils;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 资源对象 SysRes
 * 
 * @author 杨晓辉
 * @date 2023-05-08
 */
public class SysRes extends BaseEntity
{
    private static final long serialVersionUID = 1L;

    /** 资源Id */
    private String resId;

    /** 资源业务Id */
    @Excel(name = "资源业务Id")
    private String resBusId;

    /** 资源类型 */
    @Excel(name = "资源类型")
    private String resMediatype;

    /** 资源业务类型 */
    @Excel(name = "资源业务类型")
    private String resBiztype;

    /** 资源路径 */
    @Excel(name = "资源路径")
    private String resUrl;

    /** OSS资源路径 */
    @Excel(name = "Oss云端资源路径")
    private String resOssUrl;

    /** 资源排序 */
    @Excel(name = "资源排序")
    private Long resSeq;

    /** 资源创建人 */
    @Excel(name = "资源创建人")
    private String resCreatorname;

    /** 资源创建者 */
    @Excel(name = "资源创建者")
    private String resCreator;

    /** 资源创建时间 */
    @Excel(name = "资源创建时间")
    private String resCreateddate;

    /** 资源表单模式 */
    @Excel(name = "资源表单模式")
    private String resFormtype;

    /** 本地资源路径 */
    @Excel(name = "本地资源路径")
    private String resLocalUrl;


    public SysRes(){

    }

    public void setResId(String resId)
    {
        this.resId = resId;
    }

    public String getResId()
    {
        return resId;
    }
    public void setResBusId(String resBusId)
    {
        this.resBusId = resBusId;
    }

    public String getResBusId()
    {
        return resBusId;
    }
    public void setResMediatype(String resMediatype)
    {
        this.resMediatype = resMediatype;
    }

    public String getResMediatype()
    {
        return resMediatype;
    }
    public void setResBiztype(String resBiztype)
    {
        this.resBiztype = resBiztype;
    }

    public String getResBiztype()
    {
        return resBiztype;
    }
    public void setResUrl(String resUrl)
    {
        this.resUrl = resUrl;
    }

    public String getResUrl()
    {
        return resUrl;
    }
    public void setResSeq(Long resSeq)
    {
        this.resSeq = resSeq;
    }

    public Long getResSeq()
    {
        return resSeq;
    }

    public void setResCreatorname(String resCreatorname)
    {
        this.resCreatorname = resCreatorname;
    }

    public String getResCreatorname()
    {
        return resCreatorname;
    }

    public String getResCreator()
    {
        return resCreator;
    }

    public void setResCreator(String resCreator)
    {
        this.resCreator = resCreator;
    }

    public String getResCreateddate()
    {
        return resCreateddate;
    }

    public void setResCreateddate(String resCreateddate)
    {
        this.resCreateddate = resCreateddate;
    }


    public String getResOssUrl() {
        return resOssUrl;
    }

    public void setResOssUrl(String resOssUrl) {
        this.resOssUrl = resOssUrl;
    }


    public String getResFormtype() {
        return resFormtype;
    }

    public void setResFormtype(String resFormtype) {
        this.resFormtype = resFormtype;
    }

    public String getResLocalUrl() {
        return resLocalUrl;
    }

    public void setResLocalUrl(String resLocalUrl) {
        this.resLocalUrl = resLocalUrl;
    }


    //资源的查询模式
    public static List<SysRes> pack(List<SysRes> collRes , ResConstants.mediaType mediaType){
        if(collRes != null && collRes.size() > 0){
            return  collRes.stream().filter(v->mediaType.getValue().equals(v.getResMediatype())).peek(v -> v.setResFormtype(ResConstants.formType.QUERY.getValue())).collect(Collectors.toList());
        }
        return new ArrayList<SysRes>();
    }

    //资源的新增和查询模式
    public static List<SysRes> mode(List<SysRes> collRes , ResConstants.formType formType){
        if(collRes != null && collRes.size() > 0){
            return  collRes.stream().peek(v -> v.setResFormtype(formType.getValue())).collect(Collectors.toList());
        }
        return new ArrayList<SysRes>();
    }
    //local转化oss
    public static List<SysRes> cloud(List<SysRes> collRes){
        if(collRes != null && collRes.size() > 0){
            return   collRes.stream().map(m -> {
                if(SpringUtils.getBean(PolicyService.class).isOssCloudPolicy()){
                    m.setResFormtype(ResConstants.formType.QUERY.getValue());
                    m.setResUrl( m.getResOssUrl() );
                }
                return m;
            }).collect(Collectors.toList());
        }
        return new ArrayList<SysRes>();
    }

    //local转化oss
    public static SysRes cloud(SysRes sysRes){
        if(sysRes != null){
            if(SpringUtils.getBean(PolicyService.class).isOssCloudPolicy()){
                sysRes.setResFormtype(ResConstants.formType.QUERY.getValue());
                sysRes.setResUrl( sysRes.getResOssUrl() );
            }
        }
        return sysRes;
    }

    //add resBy formType
    public static List<SysRes> add(List<SysRes> collRes,ResConstants.formType formType){
        if (collRes != null && collRes.size() > 0) {
            if(SpringUtils.getBean(PolicyService.class).isOssCloudPolicy()){
                return collRes.stream().map(v -> { //新增资源
                    if ((ResConstants.formType.ADD == formType) && ResConstants.formType.ADD.getValue().equals(v.getResFormtype()) && !SysRes.valid(v.getResBusId()) ) {
                        SpringUtils.getBean(ResManager.class).updateRes(v.getResId(), v.getResBusId());
                    }else if ((ResConstants.formType.UPDATE == formType) && ResConstants.formType.ADD.getValue().equals(v.getResFormtype()) && SysRes.valid(v.getResBusId()) ) {
                        SpringUtils.getBean(ResManager.class).updateRes(v.getResId(), v.getResBusId());
                    }
                    SpringUtils.getBean(ResManager.class).updateRes(v.getResId(), v.getResBusId());
                    return v;
                }).collect(Collectors.toList());
            }
        }
        return new ArrayList<>();
    }

    //add resBy formType
    public static List<SysRes> add(List<SysRes> collRes,ResConstants.formType formType,String resBusId){
        if (collRes != null && collRes.size() > 0) {
            add(collRes.stream().map(v->{
                v.setResBusId(resBusId);
                return v;
            }).collect(Collectors.toList()) , formType);
        }
        return new ArrayList<>();
    }


    public static boolean  valid(String resId){
        try {
            UUID.fromString(resId).toString();
        } catch (Exception e) {
            return false;
        }
        return true;
    }

    //校验SysRes的合法
    public static List<String>  from(List<String> resIds){
        if(resIds != null && resIds.size() > 0){
            return resIds.stream().filter(v-> SysRes.valid(v)).collect(Collectors.toList());
        }
        return new ArrayList<String>();
    }

    //校验SysRes的合法
    public static List<SysRes>  from(List<SysRes> resColl,int type){
        if(resColl != null && resColl.size() > 0){
            return resColl.stream().filter(v-> SysRes.valid(v.getResId())).collect(Collectors.toList());
        }
        return new ArrayList<SysRes>();
    }


    public static boolean empty(String resId){
        return !(!StringUtils.isEmpty(resId) && valid( resId ));
    }

    public static boolean empty(Collection<String> resColl){
        if(StringUtils.isEmpty(resColl)) return true;
        List<String> resCollFilter = resColl.stream().filter(v-> !StringUtils.isEmpty(v) && valid(v) ).collect(Collectors.toList());
        return StringUtils.isEmpty(resCollFilter);
    }


    public static SysRes build(){
        return new SysRes();
    }

    public static List<SysRes> builds(){
        return new ArrayList<SysRes>(){{ add(new SysRes()); }};
    }

    @Override
    public String toString() {
        return new ToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE)
            .append("resId", getResId())
            .append("resBusId", getResBusId())
            .append("resMediatype", getResMediatype())
            .append("resBiztype", getResBiztype())
            .append("resUrl", getResUrl())
            .append("resOssUrl", getResOssUrl())
            .append("resSeq", getResSeq())
            .append("resCreator", getResCreator())
            .append("resCreateddate", getResCreateddate())
            .toString();
    }
}
