写的比较复杂,记录下来,看看后面能不能简化下写法
list抽公用的2级结构逻辑
public Map<String, Object> commonHandle(List<String> dateList, RequestDto requestDto) {
Map<String, Object> resultMap = new LinkedHashMap<>();
Map<String, List<RMACostData>> oneMap = new LinkedHashMap<>();
List<RMACostData> list = rmaCostMapper.commonInfo(dateList, requestDto);
for (RMACostData obj : list) {
String one = this.getObjChange(obj, requestDto.getGroupList().get(0));
if (null == oneMap.get(one)) {
oneMap.put(one, new ArrayList<>());
}
oneMap.get(one).add(obj);
}
for (Map.Entry<String, List<RMACostData>> oneEntry : oneMap.entrySet()) {
List<Map<String, Object>> resultList = new ArrayList<>();
Map<String, List<RMACostData>> twoMap = new LinkedHashMap<>();
List<RMACostData> twoList = oneEntry.getValue();
//这个地方补0
//把月份去重抽出来
List<String> tempList = twoList.stream().map(pc -> pc.getMonthDate()).distinct().collect(Collectors.toList());
if (dateList.size() > tempList.size()) {
//把日期依次遍历出来
for (String str : dateList) {
int flag = 0;
for (String tempStr : tempList) {
if (str.equals(tempStr)) {
flag = 1;
}
}
if (flag == 0) {
RMACostData obj = new RMACostData();
obj.setMonthDate(str);
obj.setRmaCost(0F);
obj = this.setObjChange(obj, requestDto.getGroupList().get(0), oneEntry.getKey());
twoList.add(obj);
}
}
//twoList.stream().sorted(Comparator.comparing(RMACostData::getMonthDate)).collect(Collectors.toList());
//数组排个序
Collections.sort(twoList, new Comparator<RMACostData>() {
@Override
public int compare(RMACostData o1, RMACostData o2) {
//System.out.println("o1:" + o1 + ",o2:" + o2);
return o1.getMonthDate().compareTo(o2.getMonthDate());
}
});
}
//把缺的月份补上
for (RMACostData obj : twoList) {
//获取第二层
String two = obj.getMonthDate();
if (null == twoMap.get(two)) {
twoMap.put(two, new ArrayList<>());
}
twoMap.get(two).add(obj);
}
//以日期为map的list
for (Map.Entry<String, List<RMACostData>> twoEntry : twoMap.entrySet()) {
Map<String, Object> resultTwoMap = new LinkedHashMap<>();
//product是 时间 + TFOS
String monthStr = twoEntry.getKey();
//String product = monthStr.substring(monthStr.length() - 2) + "-" + oneEntry.getKey();
String product = monthStr.replace("/", "") + "-" + oneEntry.getKey();
resultTwoMap.put("product", product);
/处理折线
List<RMACostData> threeList = twoEntry.getValue();
Float rmaCostTotal = 0F;
//查询的二级值
String keyStr = requestDto.getGroupList().get(1);
for (RMACostData obj : threeList) {
//动态的字段取值逻辑
String key = getObjChange(obj, keyStr);
if (null != key) {
if ("是".equals(key)) {
resultTwoMap.put("rmaNG", DateUtil.fixTwo(obj.getRmaCost() / 10000F));
}
if ("否".equals(key)) {
resultTwoMap.put("rmaReduce", DateUtil.fixTwo(obj.getRmaCost() / 10000F));
}
rmaCostTotal = rmaCostTotal + obj.getRmaCost();
}
}
Float sales = 0F;
//当选择的是产品类型,并且是客户的时候 查产品&客户别销售额
if ("customer".equals(requestDto.getGroupList().get(0))) {
sales = salesProductCustomerService.value(monthStr, requestDto);
}else{
sales = salesOrgsiteProductService.value(monthStr, requestDto);
}
String saleRatio = "0%";
if (sales != 0F) {
//销售额占比
saleRatio = DateUtil.fixTwo(100 * rmaCostTotal / sales) + "%";
}
resultTwoMap.put("Total", DateUtil.fixTwo(rmaCostTotal / 10000F));
resultTwoMap.put("saleRatio", saleRatio);
resultList.add(resultTwoMap);
}
//分层,加入X轴和数组数组
Map<String, Object> map = new HashMap<>();
List<String> sortList = resultList.stream().map(pc -> pc.get("product").toString()).distinct().collect(Collectors.toList());
Collections.sort(sortList, new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
//System.out.println("o1:" + o1 + ",o2:" + o2);
return o1.compareTo(o2);
}
});
map.put("x", sortList);
map.put("list", resultList);
resultMap.put(oneEntry.getKey(), map);
}
return resultMap;
}
map的处理逻辑,兼容多种维度查询
<select id="commonInfo" resultMap="baseMap">
select sum(RMACOST) as RMACOST,
<trim prefix="" suffixOverrides=",">
<choose>
<!-- 年 -->
<when test="obj.type == 'year'">
to_char(to_date(MONTHDATE, 'yyyy-MM'), 'YYYY') as MONTHDATE,
</when>
<!-- 半年 -->
<when test="obj.type == 'halfYear'">
to_char(to_date(MONTHDATE, 'yyyy-MM'), 'YYYY') ||
case WHEN to_char(to_date(MONTHDATE, 'yyyy-MM'), 'mm') >= '06' then '2H' else '1H' END as MONTHDATE,
</when>
<!-- 季度 -->
<when test="obj.type == 'quarter'">
to_char(to_date(MONTHDATE, 'yyyy-MM'), 'YYYY') || '/Q' ||
to_char(to_date(MONTHDATE, 'yyyy-MM'), 'Q') as MONTHDATE,
</when>
<!-- 月 -->
<otherwise>
to_char(to_date(MONTHDATE, 'yyyy-MM'), 'yyyyMM') as MONTHDATE,
</otherwise>
</choose>
<!-- 查询的条件 -->
<if test="obj.groupList.size() != 0">
<foreach collection="obj.groupList" index="index" item="item" open="" close="">
<if test="item == 'orgsiteName'">
ORGSITENAME,
</if>
<if test="item == 'productType'">
PRODUCTTYPE,
</if>
<if test="item == 'customer'">
NVL(CUSTOMER, 'NULL') as CUSTOMER,
</if>
<if test="item == 'ifReduce'">
IFREDUCE
</if>
</foreach>
</if>
</trim>
from RMACOST
<where>
<!-- 时间的查询条件 -->
<choose>
<!-- 年 -->
<when test="obj.type == 'year'">
and to_char(to_date(MONTHDATE, 'yyyy-MM'), 'YYYY') in
<foreach collection="dateList" index="index" item="item" open="(" separator="," close=")">
#{item}
</foreach>
</when>
<!-- 半年 -->
<when test="obj.type == 'halfYear'">
and to_char(to_date(MONTHDATE, 'yyyy-MM'), 'YYYY') ||
case WHEN to_char(to_date(MONTHDATE, 'yyyy-MM'), 'mm') >= '06' then '2H' else '1H' END in
<foreach collection="dateList" index="index" item="item" open="(" separator="," close=")">
#{item}
</foreach>
</when>
<!-- 季度 -->
<when test="obj.type == 'quarter'">
and to_char(to_date(MONTHDATE, 'yyyy-MM'), 'YYYY') || '/Q' || to_char(to_date(MONTHDATE, 'yyyy-MM'), 'Q') in
<foreach collection="dateList" index="index" item="item" open="(" separator="," close=")">
#{item}
</foreach>
</when>
<!-- 月 -->
<otherwise>
and to_char(to_date(MONTHDATE, 'yyyy-MM'), 'yyyyMM') in
<foreach collection="dateList" index="index" item="item" open="(" separator="," close=")">
#{item}
</foreach>
</otherwise>
</choose>
<!-- 维度的查询条件 -->
<if test="obj.orgsiteName != null and obj.orgsiteName != ''">
and ORGSITENAME = #{obj.orgsiteName}
</if>
<if test="obj.productType != null and obj.productType != ''">
and PRODUCTTYPE = #{obj.productType}
</if>
<if test="obj.customer != null and obj.customer != ''">
and CUSTOMER = #{obj.customer}
</if>
<if test="obj.ifReduce != null and obj.ifReduce != ''">
and IFREDUCE = #{obj.ifReduce}
</if>
<if test="obj.fgCode != null and obj.fgCode != ''">
and FGCODE = #{obj.fgCode}
</if>
</where>
group by
<choose>
<!-- 年 -->
<when test="obj.type == 'year'">
to_char(to_date(MONTHDATE, 'yyyy-MM'), 'YYYY'),
</when>
<!-- 半年 -->
<when test="obj.type == 'halfYear'">
to_char(to_date(MONTHDATE, 'yyyy-MM'), 'YYYY') ||
case WHEN to_char(to_date(MONTHDATE, 'yyyy-MM'), 'mm') >= '06' then '2H' else '1H' END,
</when>
<!-- 季度 -->
<when test="obj.type == 'quarter'">
to_char(to_date(MONTHDATE, 'yyyy-MM'), 'YYYY') || '/Q' || to_char(to_date(MONTHDATE, 'yyyy-MM'), 'Q'),
</when>
<!-- 月 -->
<otherwise>
to_char(to_date(MONTHDATE, 'yyyy-MM'), 'yyyyMM'),
</otherwise>
</choose>
<if test="obj.groupList.size() != 0">
<foreach collection="obj.groupList" index="index" item="item" open="" close="">
<if test="item == 'orgsiteName'">
ORGSITENAME,
</if>
<if test="item == 'productType'">
PRODUCTTYPE,
</if>
<if test="item == 'customer'">
CUSTOMER,
</if>
<if test="item == 'ifReduce'">
IFREDUCE
</if>
</foreach>
</if>
order by MONTHDATE
<trim prefix="" suffixOverrides=",">
<if test="obj.groupList.size() != 0">
<foreach collection="obj.groupList" index="index" item="item" open="" separator="," close="">
<if test="item == 'orgsiteName'">
, DECODE(ORGSITENAME, 'A', 1, 'B', 2, 'C')
</if>
<if test="item == 'productType'">
, DECODE(PRODUCTTYPE, 'A', 1)
</if>
</foreach>
</if>
</trim>
</select>
取字段的方案,觉得不太好,先用着了
public String getObjChange(RMACostData obj, String str) {
switch (str) {
case "productType":
return obj.getProductType();
case "customer":
return obj.getCustomer();
case "orgsiteName":
return obj.getOrgsiteName();
case "ifReduce":
return obj.getIfReduce();
default:
return null;
}
}
public RMACostData setObjChange(RMACostData obj, String str, String value) {
switch (str) {
case "productType":
obj.setProductType(value);
return obj;
case "customer":
obj.setCustomer(value);
return obj;
case "orgsiteName":
obj.setOrgsiteName(value);
return obj;
case "ifReduce":
obj.setIfReduce(value);
return obj;
default:
return null;
}
}