import java.util.ArrayList;
import java.util.List;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.citic.model.AverageDict;
import com.jfinal.kit.JsonKit;
import com.jfinal.plugin.activerecord.Record;
/**
* 简单实现:100条数据划分给10员工,自定义比例分配,自定义笔数分配,按平均比例进行分配,分配不完的数据直接按照顺序进行追加
* ----List<Record> dList 数据集合List[{Record}]
* ----allotcale 分配参数 [{"u":"1089","s":10},{"u":"1088","s":10}]
*
*/
public class ScaleUtil {
/**
* 自定义比例分配数据
*
* @param dList
* 待分配数据
* @param allotcale
* 分配对象
*/
public static List<AverageDict> scaleAllot(List<Record> dList,
String allotcale) {
// 分配后的数据
List<AverageDict> allotList = new ArrayList<>();
// 数据可分配条数
int total = dList.size();
// 员工合计需要分配数量
int scale = 0;
// 查询需要分配的员工数量总比例
JSONArray jArray = JSONArray.parseArray(allotcale);
for (int i = 0; i < jArray.size(); i++) {
JSONObject jObject = jArray.getJSONObject(i);
scale += jObject.getIntValue("s");
}
// 根据算法划分数据
int rows = 0;
int ret = 0;
for (int i = 0; i < jArray.size(); i++) {
JSONObject jObject = jArray.getJSONObject(i);
AverageDict ad = new AverageDict();
List<String> uList = new ArrayList<>();
ad.setUserid(jObject.getIntValue("u") + "");
int s = jObject.getIntValue("s");
rows = rows + (int) (total * (s / 100f));
while (ret < rows) {
// 判断是否超过总数,超过总数停止划分
if (ret >= total) {
break;
}
uList.add(dList.get(ret).get("bdid", "").toString());
ret = ret + 1;
}
ad.setIdsLis(uList);
allotList.add(ad);
}
System.out.println("员工:" + JsonKit.toJson(allotList));
// 剩余可分配条数
int surplus = total - ret;
// 将剩余数据进行足以
if (scale == 100 && surplus != 0) {
System.out.println("待分配:" + surplus);
for (int i = 0; i < surplus; i++) {
AverageDict ad = allotList.get(i);
@SuppressWarnings("unchecked")
List<String> uList = ad.getIdsLis();
uList.add(dList.get(ret + i).get("bdid", "").toString());
ad.setIdsLis(uList);
allotList.set(i, ad);
}
}
return allotList;
}
/**
* 自定义条数分配数据
*
* @param dList
* 待分配数据
* @param allotcale
* 分配对象
*/
public static List<AverageDict> penAllot(List<Record> dList,
String allotcale) {
// 分配后的数据
List<AverageDict> allotList = new ArrayList<>();
int total = dList.size();
// 定义参与分配的员工
JSONArray jArray = JSONArray.parseArray(allotcale);
int rows = 0;
int ret = 0;
for (int i = 0; i < jArray.size(); i++) {
JSONObject jObject = jArray.getJSONObject(i);
AverageDict ad = new AverageDict();
List<String> uList = new ArrayList<>();
ad.setUserid(jObject.getIntValue("u") + "");
int s = jObject.getIntValue("s");
rows = rows + s;
while (ret < rows) {
if (ret >= total) {
break;
}
uList.add(dList.get(ret).get("bdid", "").toString());
ret = ret + 1;
}
ad.setIdsLis(uList);
allotList.add(ad);
}
return allotList;
}
/**
* 平均分配数据
*
* @param dList
* 待分配数据
* @param allotcale
* 分配对象
*/
public static List<AverageDict> averageAllot(List<Record> dList,
String allotcale) {
// 分配后的数据
List<AverageDict> allotList = new ArrayList<>();
// 数据可分配条数
int total = dList.size();
// 根据算法划分数据
int rows = 0;
int ret = 0;
// 查询需要分配的员工数量总比例
JSONArray jArray = JSONArray.parseArray(allotcale);
// 获取每个员工可以分的的比例
float uEach = total * (1f / jArray.size());
for (int i = 0; i < jArray.size(); i++) {
JSONObject jObject = jArray.getJSONObject(i);
AverageDict ad = new AverageDict();
List<String> uList = new ArrayList<>();
ad.setUserid(jObject.getIntValue("u") + "");
rows = rows + (int) (uEach);
while (ret < rows) {
// 判断是否超过总数,超过总数停止划分
if (ret >= total) {
break;
}
uList.add(dList.get(ret).get("bdid", "").toString());
ret = ret + 1;
}
ad.setIdsLis(uList);
allotList.add(ad);
}
// 剩余可分配条数
int surplus = total - ret;
// 将剩余数据进行足以
if (surplus != 0) {
System.out.println("待分配:" + surplus);
for (int i = 0; i < surplus; i++) {
AverageDict ad = allotList.get(i);
@SuppressWarnings("unchecked")
List<String> uList = ad.getIdsLis();
uList.add(dList.get(ret + i).get("bdid", "").toString());
ad.setIdsLis(uList);
allotList.set(i, ad);
}
}
return allotList;
}
/**
* 生成测试数据
*
* @param len
* 生成数据数量
* @return 测试数据
*/
public List<Record> getList(int len) {
List<Record> aList = new ArrayList<>();
for (int i = 0; i < len; i++) {
Record rd = new Record();
rd.set("bdid", "20160901000" + i);
aList.add(rd);
}
return aList;
}
}