天天看點

hyperf統計各個狀态值的總數

根據傳入的字段,和字段值,統計出數量

調用:

/**
         * 異常類型 different_type
         * '異常類型 1:正常 2:擷取售後單失敗,3:擷取售後單失敗,4:同步平台失敗,5:未綁定平台退貨位址,6:其他',
         */
        $different_type_value = [
            1,
            2,
            3,
            4,
            5,
            6,
        ];
                $different_type = CustomerRefundOrderDao::getTypeCount('different_type', $different_type_value, 0, 0, $shop_id);

           

方法:

public static function getTypeCount(string $field, array $value, int $apply_time_start, int $apply_time_end, array $shop_id): array
    {
        if (empty($value)) {
            return [];
        }
        $countSql = "";
        foreach ($value as $v) {
            $countSql .= "count($field=$v or null) as $field" . "_$v,";
        }
        /**
         * 列印出來的sql:select count(refund_type=1 or null) as refund_type_1,count(refund_type=2 or null) as refund_type_2,count(refund_type=3 or null) as refund_type_3,count(refund_type=4 or null) as refund_type_4 from `customer_refund_order`
         */
        $countSql = rtrim($countSql, ',');
        $query = CustomerRefundOrderModel::query();
        $query->select(DB::raw($countSql))
            ->whereIn('shop_id', $shop_id)
            ->when($apply_time_start, function ($query, $apply_time_start) {
                return $query->where('apply_time', '>=', $apply_time_start);
            })
            ->when($apply_time_end, function ($query, $apply_time_end) {
                return $query->where('apply_time', '<=', $apply_time_end);
            });
        $res = $query->first();
        return $res ? $res->toArray() : [];
    }

           

傳回(json):

"different_type": {
            "different_type_1": 0,
            "different_type_2": 20,
            "different_type_3": 0,
            "different_type_4": 1,
            "different_type_5": 0,
            "different_type_6": 0
        },