package weaver.interfaces.workflow.action;


import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import weaver.conn.RecordSet;
import weaver.general.Util;
import weaver.soa.workflow.request.Property;
import weaver.soa.workflow.request.RequestInfo;
import weaver.workflow.request.ResourceConditionManager;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

public class SetUsreByResourceCondition implements  Action{
    private  String resourceConditionName;
    private  String resourceBrowserName;
    @Override
    public String execute(RequestInfo requestInfo) {
        RecordSet rs=new RecordSet();
        String rlfileidName="";
        List<String> rlfileidValue=new ArrayList<>();
        List<String> fieldNameList = Util.TokenizerString(resourceConditionName, ",");
        rs.writeLog("======SetUsreByResourceCondition===resourceConditionName="+resourceConditionName+"===resourceBrowserName="+resourceBrowserName+"==");
        if(StringUtils.isBlank(resourceConditionName)||StringUtils.isBlank(resourceBrowserName)) {
            return Action.SUCCESS;
        }
        if(CollectionUtils.isEmpty(fieldNameList)){
            rs.writeLog("======SetUsreByResourceCondition===fieldNameList is  empty==");
            return Action.SUCCESS;
        }
        Property[] property = requestInfo.getMainTableInfo().getProperty();
        if(property==null||property.length==0){
            rs.writeLog("======SetUsreByResourceCondition===property==is  empty==");
            return Action.SUCCESS;
        }
        fieldNameList.stream().forEach(fieldName->{
            for (Property prop:property) {
                if(fieldName.equals(prop.getName())){
                    String value = prop.getValue();
                    if(StringUtils.isNotBlank(value)){
                        rlfileidValue.add(value);
                    }
                }
            }
        });
        rs.writeLog("======SetUsreByResourceCondition===rlfileidValue=="+rlfileidValue);
        List<String> useridList=new ArrayList<>();
        ResourceConditionManager resourceConditionManager=new ResourceConditionManager();
        for (String value :rlfileidValue) {
            String userids=resourceConditionManager.getUserIdsByResourceCondition(value);
            rs.writeLog("======SetUsreByResourceCondition===userids=="+userids);
            if(StringUtils.isNotBlank(userids)) {
                useridList.addAll(Util.TokenizerString(userids, ","));
            }
        }
        rs.writeLog("======SetUsreByResourceCondition===userids=="+useridList);
        if(CollectionUtils.isNotEmpty(useridList)){
            //去重
            useridList = useridList.stream().distinct().collect(Collectors.toList());
            String billTableName = requestInfo.getRequestManager().getBillTableName();
            rs.writeLog("======SetUsreByResourceCondition===billTableName=="+billTableName);
            if(StringUtils.isNotBlank(billTableName)) {
             rs.executeUpdate("update " + billTableName + " set " + resourceBrowserName + "='" + StringUtils.join(useridList, ",") + "' where requestid=" + requestInfo.getRequestid());
            }
        }
        return Action.SUCCESS;
    }
}
