根据传入的字段,和字段值,统计出数量
调用:
/**
* 异常类型 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
},