package kd.hwsc.hwsup.formplugin;
import java.sql.ResultSet;
import java.sql.Types;
import java.util.ArrayList;
import java.util.EventObject;
import java.util.List;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.entity.datamodel.events.PropertyChangedArgs;
import kd.bos.form.field.BasedataEdit;
import kd.bos.form.plugin.AbstractFormPlugin;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.QueryServiceHelper;
import kd.hwsc.hwsup.service.DispatchRuleService;
/**
* 群组规则分发
*
* @author jiaping_wu
*/
public class GroupEditplugIn extends AbstractFormPlugin {
private static final String TYPE = "type";
private static final String CUSTOMER = "bdcustomer";
private static final String PRODUCT = "product_name";
private static final String GROUP = "group";
private static final String STATE = "sr_state";
@Override
public void propertyChanged(PropertyChangedArgs e) {
String key = e.getProperty().getName();
if(TYPE.equals(key)||CUSTOMER.equals(key)||PRODUCT.equals(key)||STATE.equals(key)||GROUP.equals(key)){
onPropertyChanged();
}
}
@Override
public void beforeBindData(EventObject e) {
BasedataEdit group = this.getControl(GROUP);
QFilter qFilter = new QFilter("id", QCP.is_null, null);
group.setQFilter(qFilter);
}
private void onPropertyChanged() {
DynamicObject types = (DynamicObject)this.getModel().getValue(TYPE);
DynamicObject customers = (DynamicObject)this.getModel().getValue(CUSTOMER);
DynamicObject products = (DynamicObject)this.getModel().getValue(PRODUCT);
DynamicObject states = (DynamicObject)this.getModel().getValue(STATE);
long typesParams = 0;
long customersParams = 0;
long productsParams = 0;
long statesParams = 0;
if(types!=null){
typesParams= types.getLong("id");
}
if(customers!=null){
customersParams= customers.getLong("country.id");
}
if(products!=null){
productsParams= products.getLong("id");
}
if(states!=null){
statesParams= states.getLong("id");
}
//取出群组中拥有的职责
// List<String> rangeResult = DispatchRuleService.getStatesResult(statesParams);
List<String> rangeResult = DispatchRuleService.getStatesResult(statesParams);
//this.getView().showMessage("当前群组规则为:"+result.toString());
List<String> userRangeResult;
if(rangeResult!=null&&rangeResult.size()>0){
//userRangeResult = DispatchRuleService.getUserRangeResult(typesParams,customersParams,productsParams);
userRangeResult = DispatchRuleService.getUserRangeResult(typesParams,customersParams,productsParams);
userRangeResult.retainAll(rangeResult);
}else{
//userRangeResult = DispatchRuleService.getUserRangeResult(typesParams,customersParams,productsParams);
userRangeResult = DispatchRuleService.getUserRangeResult(typesParams,customersParams,productsParams);
}
// DynamicObjectCollection groupObj = QueryServiceHelper.query("hwsup_bd_group", "", new QFilter[]{new QFilter("range", QCP.in, userRangeResult)});
DynamicObject[] groupObj = BusinessDataServiceHelper.load("hwsup_bd_group", "entryentity.engineer", new QFilter[]{new QFilter("range", QCP.in, userRangeResult)});
long tmp = 0;
DynamicObject groupObject;
DynamicObjectCollection engineer = null;
if(groupObj!=null&&groupObj.length!=0){
int index = (int)(Math.random()*groupObj.length);
groupObject = groupObj[index];
tmp = (long) groupObject.getPkValue();
engineer = (DynamicObjectCollection) groupObject.get("entryentity");
// this.getView().showMessage(engineer.toString());
//(String[]) ((DynamicObject)((DynamicObjectCollection) groupObject.get("entryentity")).get(0).get("engineer")).getPkValue();
} else {
// getView().showTipNotification("问题单属性群组不能为空:请确认产品|问题类型|客户是否正确");
}
//过滤分组
// List<String> result = DispatchRuleService.getDispatchRuleResult(typesParams,customersParams,productsParams);
BasedataEdit group = this.getControl(GROUP);
BasedataEdit owner = this.getControl("owner");
QFilter qFilter;
if(userRangeResult.size()==0){
this.getModel().setValue(GROUP, null);
qFilter = new QFilter("id", QCP.is_null, null);
group.setQFilter(qFilter);
this.getModel().setValue("owner", null);
QFilter ownerQFilter = new QFilter("id", QCP.is_null, null);
owner.setQFilter(ownerQFilter);
}else if(userRangeResult.size()>0){
List<Object> list = new ArrayList<>();
for(int i=0;i<engineer.size();i++){
DynamicObject engineerObj = (DynamicObject)engineer.get(i).get("engineer");
list.add(engineerObj.getPkValue());
}
this.getModel().setValue("owner", list.get((int)(Math.random()*list.size())));
//过滤字段???
QFilter ownerQFilter = new QFilter("id", QCP.in, list.toArray());
owner.setQFilter(ownerQFilter);
this.getModel().setValue(GROUP, tmp);
//this.getView().showTipNotification("所有群组规则:"+result+",当前群组分发规则:"+result.toArray()[index]+"");
qFilter = new QFilter("range", QCP.in, userRangeResult.toArray());
group.setQFilter(qFilter);
}
}
// private List<String> getResult(long typesParams,long customersParams,long productsParams,List<String> userRange){
// List list= new ArrayList<>();
// for(int i=0; i<userRange.size();i++){
// SqlParameter[] params = new SqlParameter[4];
//
// params[0] = new SqlParameter(":ftype", Types.VARCHAR, typesParams);
// params[1] = new SqlParameter(":fcountry", Types.VARCHAR, customersParams);
// params[2] = new SqlParameter(":fproduct", Types.VARCHAR, productsParams);
// params[3] = new SqlParameter(":fuserrange", Types.VARCHAR, userRange.get(i));
// String sql = "select fid from t_hwsup_group_rule where ftype= ? and fcountry= ? and fproduct = ? and fuserrange = ?";
// DBRoute dbRoute = new DBRoute("secd");
//
// ResultSetHandler<String> action = new ResultSetHandler<String>() {
// @Override
// public String handle(ResultSet rs) throws Exception {
// String fid = "";
// while (rs.next()) {
// fid = rs.getString("fid");
// }
// return fid;
// }
// };
//
// String result = DB.query(dbRoute, sql, params, action);
// list.add(result);
// }
//
// return list;
// }
}