分析页统计:生产单状态统计接口实现

This commit is contained in:
gaoqr
2024-08-06 18:31:22 +08:00
parent 483e2dcb99
commit f737c17866
15 changed files with 310 additions and 43 deletions
@@ -8,7 +8,7 @@ import lombok.Getter;
@Getter
public enum OrderStatusEnum {
EMPTY(0, "默认"),
EMPTY(0, "空单"),
NEW_ORDER(1, "新单"),
NO_SORT(2, "未排单"),
IN_PRODUCTION(3, "生产中"),
@@ -1,6 +1,8 @@
package com.cf.imes.framework.security.core.util;
import cn.hutool.core.util.StrUtil;
import com.cf.imes.framework.common.exception.ServiceException;
import com.cf.imes.framework.common.exception.enums.GlobalErrorCodeConstants;
import com.cf.imes.framework.security.core.LoginUser;
import com.cf.imes.framework.web.core.util.WebFrameworkUtils;
import org.springframework.lang.Nullable;
@@ -123,9 +125,12 @@ public class SecurityFrameworkUtils {
* 获取当前用户的组织ID
*
*/
public static Long getUserOrganId(){
return SecurityFrameworkUtils.getLoginUser().getOrganId();
public static Long getUserOrganId() {
LoginUser loginUser = SecurityFrameworkUtils.getLoginUser();
if (loginUser == null) {
throw new ServiceException(GlobalErrorCodeConstants.UNAUTHORIZED);
}
return loginUser.getOrganId();
}
}
@@ -0,0 +1,34 @@
package com.cf.imes.module.executor.enums;
/**
* 生产单统计维度单位
* @author Gqr
* @since 2024/8/6 10:04
*/
public enum OrderStatisticsUnit {
//季度、月、周、日
QUARTER(0),
MONTH(1),
WEEK(2),
DAY(3);
OrderStatisticsUnit(Integer value) {
this.value = value;
}
private Integer value;
public Integer getValue() {
return value;
}
// from value
public static OrderStatisticsUnit fromValue(Integer value) {
for (OrderStatisticsUnit unit : OrderStatisticsUnit.values()) {
if (unit.getValue().equals(value)) {
return unit;
}
}
return null;
}
}
@@ -455,19 +455,6 @@ public class OrderController {
return success(orderService.getOrderWarn());
}
// 生产单状态分组统计返回
@GetMapping("/getOrderStatusCount")
@Operation(summary = "生产单状态分组统计返回")
@Parameters({
@Parameter(name = "startTime", description = "开始时间", example = "2024-07-15"),
@Parameter(name = "endTime", description = "结束时间", example = "2024-07-16")
})
@PreAuthorize("@ss.hasPermission('productManager:List')")
public CommonResult<List<OrderStatusRespVO>> getOrderStatusCount(@RequestParam(value = "startTime") String startTime,
@RequestParam(value = "endTime") String endTime) {
return success(orderService.getOrderStatusCount(startTime + " 00:00:00", endTime + " 23:59:59"));
}
// 小板数量 <集合>
@GetMapping("/getPlateCount")
@Operation(summary = "小板数量")
@@ -5,13 +5,15 @@ import com.cf.imes.module.executor.controller.admin.order.vo.order.OrderStatisti
import com.cf.imes.module.executor.service.order.OrderStatisticsService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Map;
import static com.cf.imes.framework.common.pojo.CommonResult.success;
@@ -56,4 +58,10 @@ public class OrderStatisticsController {
public CommonResult<Integer> getOrderSquareProduce(OrderStatisticsReqVO reqVO) {
return success(orderStatisticsService.orderSquareProduce(reqVO));
}
@GetMapping("/orderStatus/group")
@Operation(summary = "生产单状态分组统计")
public CommonResult<Map<String, Object>> getOrderStatusCount(@Valid OrderStatisticsReqVO reqVO) {
return success(orderStatisticsService.getOrderStatusCount(reqVO));
}
}
@@ -1,7 +1,11 @@
package com.cf.imes.module.executor.controller.admin.order.vo.order;
import com.cf.imes.module.executor.validation.order.OrderStatisticsUnitInEnum;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import org.springframework.format.annotation.DateTimeFormat;
import java.time.LocalDate;
/**
* @author Gqr
@@ -12,4 +16,15 @@ import lombok.Data;
public class OrderStatisticsReqVO {
@Schema(description = "组织id")
private Long organId;
@Schema(description = "创建时间")
@DateTimeFormat(pattern = "yyyy-MM-dd")
private LocalDate[] createTime;
/**
* 统计维度单位
*/
@Schema(description = "统计维度单位", example = "0", allowableValues = {"0", "1", "2", "3"}, type = "integer")
@OrderStatisticsUnitInEnum
private Integer unit;
}
@@ -2,8 +2,7 @@ package com.cf.imes.module.executor.controller.admin.order.vo.order;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.*;
import java.time.LocalDateTime;
import org.jetbrains.annotations.NotNull;
@Schema(description = "管理后台 - 生产单状态统计")
@Data
@@ -11,7 +10,7 @@ import java.time.LocalDateTime;
@AllArgsConstructor
@NoArgsConstructor
@ToString(callSuper = true)
public class OrderStatusRespVO {
public class OrderStatusRespVO implements Comparable<OrderStatusRespVO> {
@Schema(description = "订单状态")
private Integer orderState;
@@ -19,5 +18,37 @@ public class OrderStatusRespVO {
private Integer orderCount;
@Schema(description = "生产单时间")
private LocalDateTime orderDate;
private String orderDate;
@Override
public int compareTo(@NotNull OrderStatusRespVO other) {
// 解析 orderDate 字符串为年、月、周
String[] partsThis = orderDate.split("-");
String[] partsOther = other.orderDate.split("-");
// 比较年份
int yearComparison = Integer.compare(Integer.parseInt(partsThis[0]), Integer.parseInt(partsOther[0]));
if (yearComparison != 0) {
return yearComparison;
}
// 比较月份
if (partsThis.length > 1 && partsOther.length > 1) {
int monthComparison = Integer.compare(Integer.parseInt(partsThis[1]), Integer.parseInt(partsOther[1]));
if (monthComparison != 0) {
return monthComparison;
}
}
// 比较周数
if (partsThis.length > 2 && partsOther.length > 2) {
int weekComparison = Integer.compare(Integer.parseInt(partsThis[2]), Integer.parseInt(partsOther[2]));
if (weekComparison != 0) {
return weekComparison;
}
}
// 如果所有部分都相同,则按状态排序
return this.orderState.compareTo(other.orderState);
}
}
@@ -10,6 +10,7 @@ import com.cf.imes.framework.mybatis.core.mapper.BaseMapperX;
import com.cf.imes.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.cf.imes.module.executor.controller.admin.order.vo.order.OrderOverdueRespVO;
import com.cf.imes.module.executor.controller.admin.order.vo.order.OrderPageReqVO;
import com.cf.imes.module.executor.controller.admin.order.vo.order.OrderStatisticsReqVO;
import com.cf.imes.module.executor.controller.admin.order.vo.order.OrderStatusRespVO;
import com.cf.imes.module.executor.controller.admin.plan.bo.OrderIds;
import com.cf.imes.module.executor.controller.admin.plan.vo.OrderRespVOCopy;
@@ -138,8 +139,13 @@ public interface OrderMapper extends BaseMapperX<OrderDO> {
List<Long> selectOrderIds(@Param(Constants.WRAPPER) Wrapper<OrderDO> wrapper);
// 生产单状态统计
List<OrderStatusRespVO> selectOrderStatusCount(@Param("organId")Long organId, @Param("startTime")String startTime, @Param("endTime")String endTime);
/**
* 生产单状态按时间分组统计
*
* @param reqVO
* @return
*/
List<OrderStatusRespVO> selectOrderStatusCount(@Param("req") OrderStatisticsReqVO reqVO);
// 生产单预警统计
List<OrderOverdueRespVO> selectOrderNear(@Param("today") LocalDateTime today, @Param("orderDate") LocalDateTime orderDate, @Param("organId") Long organId);
@@ -173,10 +173,6 @@ public interface OrderService {
*/
List<ApiOrderRespVO> getApiOrderList(JSONObject orderList);
/**
* 生产单状态统计
*/
List<OrderStatusRespVO> getOrderStatusCount(String startTime, String endTime);
/**
* 生产单超期告警分析统计
@@ -620,13 +620,6 @@ public class OrderServiceImpl implements OrderService {
return apiOrderRespVOS;
}
@Override
public List<OrderStatusRespVO> getOrderStatusCount(String startTime, String endTime) {
List<OrderStatusRespVO> orderStatusRespVOS = orderMapper.selectOrderStatusCount(OrganContextHolder.getOrganId(), startTime, endTime);
orderStatusRespVOS.removeIf(obj -> obj.getOrderState() == OrderStatusEnum.EMPTY.getStatus());
return orderStatusRespVOS;
}
@Override
public Map<String,List<OrderOverdueRespVO>> getOrderWarn() {
Map<String,List<OrderOverdueRespVO>> map = new HashMap<>();
@@ -2,6 +2,8 @@ package com.cf.imes.module.executor.service.order;
import com.cf.imes.module.executor.controller.admin.order.vo.order.OrderStatisticsReqVO;
import java.util.Map;
/**
* 生产单统计 order_{N} Service 接口
* @author Gqr
@@ -32,4 +34,9 @@ public interface OrderStatisticsService {
* 今日生产单完成总数
*/
Integer orderSquareProduce(OrderStatisticsReqVO reqVO);
/**
* 生产单状态统计
*/
Map<String, Object> getOrderStatusCount(OrderStatisticsReqVO reqVO);
}
@@ -1,13 +1,28 @@
package com.cf.imes.module.executor.service.order;
import cn.hutool.core.util.ObjectUtil;
import com.cf.imes.framework.common.enums.OrderStatusEnum;
import com.cf.imes.framework.organ.core.aop.OrganIgnore;
import com.cf.imes.framework.security.core.util.SecurityFrameworkUtils;
import com.cf.imes.module.executor.controller.admin.order.vo.order.OrderStatisticsReqVO;
import com.cf.imes.module.executor.controller.admin.order.vo.order.OrderStatusRespVO;
import com.cf.imes.module.executor.dal.mysql.order.OrderMapper;
import com.cf.imes.module.executor.enums.OrderStatisticsUnit;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
/**
* @author Gqr
@@ -49,6 +64,92 @@ public class OrderStatisticsServiceImpl implements OrderStatisticsService {
return orderMapper.selectOrderSquareProduce(getOrganIdParam(reqVO));
}
@Override
@OrganIgnore
public Map<String, Object> getOrderStatusCount(OrderStatisticsReqVO reqVO) {
Map<String, Object> resultMap = new HashMap<>();
// 计算时间跨度
getTimeSpan(reqVO);
// 查询订单状态统计数据
List<OrderStatusRespVO> orderStatusRespVOS = orderMapper.selectOrderStatusCount(reqVO);
// 基于相同的orderDate分组
Map<String, List<OrderStatusRespVO>> orderDateMap = orderStatusRespVOS.stream()
.sorted(Comparator.naturalOrder())
.collect(Collectors.groupingBy(OrderStatusRespVO::getOrderDate, LinkedHashMap::new, Collectors.toList()));
// 所有的日期集合
Set<String> dateList = orderDateMap.keySet();
resultMap.put("dateList", dateList);
// 初始化结果集
List<Map<String, Object>> series = new ArrayList<>();
for (OrderStatusEnum orderStatusEnum : OrderStatusEnum.values()) {
Map<String, Object> seriesItem = new HashMap<>();
int[] sumArr = new int[orderDateMap.size()];
// 遍历日期map
int index = 0;
for (Map.Entry<String, List<OrderStatusRespVO>> entry : orderDateMap.entrySet()) {
// 查找匹配的订单状态
Optional<OrderStatusRespVO> matchingStatus = entry.getValue().stream()
.filter(respVO -> Objects.equals(orderStatusEnum.getStatus(), respVO.getOrderState()))
.findFirst();
// 设置计数值
sumArr[index++] = matchingStatus.map(OrderStatusRespVO::getOrderCount).orElse(0);
}
seriesItem.put("data", sumArr);
seriesItem.put("name", orderStatusEnum.getDescription());
series.add(seriesItem);
}
resultMap.put("series", series);
return resultMap;
}
/**
* 获取统计的时间跨度
* 计算起止时间:前端不传入时间就根据维度单位从当前日期计算
*
* @param reqVO
*/
private void getTimeSpan(OrderStatisticsReqVO reqVO) {
LocalDate[] createTime = reqVO.getCreateTime();
LocalDate startTime = null;
LocalDate endTime;
LocalDate now = LocalDate.now();
if (ObjectUtil.isNull(createTime) || ObjectUtil.isNull(createTime[0]) || ObjectUtil.isNull(createTime[1])) {
endTime = now;
switch (OrderStatisticsUnit.fromValue(reqVO.getUnit())) {
case QUARTER:
// 从now往前的2年
startTime = now.minusYears(2);
break;
case MONTH:
// 从now往前的12个月
startTime = now.minusMonths(12);
break;
case WEEK:
// 从now往前的12周
startTime = now.minusWeeks(12);
break;
case DAY:
// 从now往前的15天
startTime = now.minusDays(15);
break;
default:
break;
}
// 声明一个LocalDateTime的数组,把startTime和endTime放进去
reqVO.setCreateTime(new LocalDate[]{startTime, endTime});
}
}
/**
* 前端传组织否则查当前用户所在的组织
*
@@ -0,0 +1,36 @@
package com.cf.imes.module.executor.validation.order;
import javax.validation.Constraint;
import javax.validation.Payload;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* 生产单统计维度单位入参校验注解
*
* @author Gqr
* @since 2024/7/17 9:33
*/
@Target({
ElementType.METHOD,
ElementType.FIELD,
ElementType.ANNOTATION_TYPE,
ElementType.CONSTRUCTOR,
ElementType.PARAMETER,
ElementType.TYPE_USE
})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Constraint(
validatedBy = {OrderStatisticsUnitInEnumValidator.class}
)
public @interface OrderStatisticsUnitInEnum {
String message() default "生产单统计维度单位[unit]错误,请检查";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
}
@@ -0,0 +1,36 @@
package com.cf.imes.module.executor.validation.order;
import cn.hutool.core.util.ObjectUtil;
import com.cf.imes.module.executor.enums.OrderStatisticsUnit;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
/**
* 生产单统计维度单位入参校验器
*
* @author Gqr
* @since 2024/7/17 9:33
*/
public class OrderStatisticsUnitInEnumValidator implements ConstraintValidator<OrderStatisticsUnitInEnum, Integer> {
@Override
public void initialize(OrderStatisticsUnitInEnum constraintAnnotation) {
ConstraintValidator.super.initialize(constraintAnnotation);
}
@Override
public boolean isValid(Integer value, ConstraintValidatorContext context) {
if (ObjectUtil.isNull(value)) {
return true;
}
OrderStatisticsUnit unit = OrderStatisticsUnit.fromValue(value);
if (ObjectUtil.isNotNull(unit)) {
return true;
} else {
return false;
}
}
}
@@ -81,16 +81,28 @@
</select>
<select id="selectOrderStatusCount" resultType="com.cf.imes.module.executor.controller.admin.order.vo.order.OrderStatusRespVO">
select
o.status as orderState,
count(o.id) as orderCount,
o.order_date
<select id="selectOrderStatusCount"
resultType="com.cf.imes.module.executor.controller.admin.order.vo.order.OrderStatusRespVO">
select o.status as orderState,
count(o.id) as orderCount
<if test="req.unit != null and req.unit == @com.cf.imes.module.executor.enums.OrderStatisticsUnit@QUARTER.getValue()">
,CONCAT(YEAR(o.order_date), '-', QUARTER(o.order_date)) as orderDate
</if>
<if test="req.unit != null and req.unit == @com.cf.imes.module.executor.enums.OrderStatisticsUnit@MONTH.getValue()">
,CONCAT(YEAR(o.order_date), '-', MONTH(o.order_date)) as orderDate
</if>
<if test="req.unit != null and req.unit == @com.cf.imes.module.executor.enums.OrderStatisticsUnit@WEEK.getValue()">
,CONCAT(YEAR(o.order_date), '-', MONTH(o.order_date), '-', FLOOR((DayOfMonth(o.order_date)-1)/7)+1) AS
orderDate
</if>
<if test="req.unit != null and req.unit == @com.cf.imes.module.executor.enums.OrderStatisticsUnit@DAY.getValue()">
,CONCAT(YEAR(o.order_date), '-', MONTH(o.order_date), '-', DAY(o.order_date)) as orderDate
</if>
from orders o
where o.organ_id = #{organId}
and DATE(o.order_date) between #{startTime} and #{endTime}
and o.deleted = 0
group by o.status, o.order_date
and o.order_date between #{req.createTime[0]} and #{req.createTime[1]}
and o.deleted = 0
group by o.status, orderDate;
</select>
<select id="selectOrderNear" resultType="com.cf.imes.module.executor.controller.admin.order.vo.order.OrderOverdueRespVO">