新增接口

This commit is contained in:
彭桃 2023-02-18 11:42:53 +08:00
parent f58eb5a821
commit 496967aaa9
2 changed files with 165 additions and 0 deletions

View File

@ -382,5 +382,55 @@ class Userinfo extends BaseController
}
}
//市场销量趋势折线图
public function orderNumGroup()
{
$date = 'lately7';
$list = Db::connect('shop')->table('eb_product_order_log')->when($date, function ($query, $date) {
getModelTime($query, $date, 'create_time');
})->field(Db::raw('from_unixtime(unix_timestamp(create_time),\'%m-%d\') as time, count(id) as new'))
->group('time')->order('time ASC')->select()->toArray();
$newUserList = array_combine(array_column($list, 'time'), array_column($list, 'new'));
$time = getDatesBetweenTwoDays(getStartModelTime($date), date('Y-m-d'));
$orderList = [];
$time_arr = [];
$value_arr = [];
foreach ($time as $item) {
$new = $newUserList[$item] ?? 0;
$time_arr[] = $item;
$value_arr[] = $new;
}
$return['day'] = $time_arr;
$return['value'] = $value_arr;
$this->apiSuccess('获取成功', $return);
}
//产品销量前十
public function get_ranking($area_id=0,$street_id=0,$village_id=0){
$where = [];
if ($area_id){
$where[] = ['area_id', '=', $area_id];
}
if ($street_id){
$where[] = ['street_id', '=', $street_id];
}
if ($village_id){
$where[] = ['village_id', '=', $village_id];
}
// 商品销量前十
$return['data'] = Db::connect('shop')->table('eb_store_product')
->alias('a')
->join('eb_product_order_log b','a.product_id = b.product_id')
->join('eb_store_order c','b.order_id = c.order_id')
->field('a.store_name as name,sum(c.total_num) as value,a.image,a.price')
->where($where)
->limit(10)
->group('a.product_id')
->order('value desc')
->select();
$this->apiSuccess('获取成功',$return);
}
}

View File

@ -484,4 +484,119 @@ if (! function_exists('upload_file')) {
return $savename;
}
}
if (!function_exists('getModelTime')) {
/**
* @param BaseQuery $model
* @param string $section
* @param string $prefix
* @param string $field
* @return mixed
* @author xaboy
* @day 2020-04-29
*/
function getModelTime( $model, string $section, $prefix = 'create_time', $field = '-',$time = '')
{
if (!isset($section)) return $model;
switch ($section) {
case 'today':
$model->whereBetween($prefix, [date('Y-m-d H:i:s', strtotime('today')), date('Y-m-d H:i:s', strtotime('tomorrow -1second'))]);
break;
case 'week':
$model->whereBetween($prefix, [date('Y-m-d H:i:s', strtotime('this week 00:00:00')), date('Y-m-d H:i:s', strtotime('next week 00:00:00 -1second'))]);
break;
case 'month':
$model->whereBetween($prefix, [date('Y-m-d H:i:s', strtotime('first Day of this month 00:00:00')), date('Y-m-d H:i:s', strtotime('first Day of next month 00:00:00 -1second'))]);
break;
case 'year':
$model->whereBetween($prefix, [date('Y-m-d H:i:s', strtotime('this year 1/1')), date('Y-m-d H:i:s', strtotime('next year 1/1 -1second'))]);
break;
case 'yesterday':
$model->whereBetween($prefix, [date('Y-m-d H:i:s', strtotime('yesterday')), date('Y-m-d H:i:s', strtotime('today -1second'))]);
break;
case 'quarter':
list($startTime, $endTime) = getMonth();
$model = $model->where($prefix, '>', $startTime);
$model = $model->where($prefix, '<', $endTime);
break;
case 'lately7':
$model = $model->where($prefix, 'between', [date('Y-m-d', strtotime("-7 day")), date('Y-m-d H:i:s')]);
break;
case 'lately30':
$model = $model->where($prefix, 'between', [date('Y-m-d', strtotime("-30 day")), date('Y-m-d H:i:s')]);
break;
default:
if (strstr($section, $field) !== false) {
list($startTime, $endTime) = explode($field, $section);
if (strlen($startTime) == 4) {
$model->whereBetweenTime($prefix, date('Y-m-d H:i:s', strtotime($section)), date('Y-m-d H:i:s', strtotime($section . ' +1day -1second')));
} else {
if ($startTime == $endTime) {
$model = $model->whereBetweenTime($prefix, date('Y-m-d 0:0:0', strtotime($startTime)), date('Y-m-d 23:59:59', strtotime($endTime)));
} else if(strpos($startTime, ':')) {
$model = $model->whereBetweenTime($prefix, $startTime, $endTime);
} else {
$model = $model->whereBetweenTime($prefix, date('Y-m-d H:i:s', strtotime($startTime)), date('Y-m-d H:i:s', strtotime($endTime . ' +1day -1second')));
}
}
}
break;
}
return $model;
}
}
if (!function_exists('getDatesBetweenTwoDays')) {
function getDatesBetweenTwoDays($startDate, $endDate)
{
$dates = [];
if (strtotime($startDate) > strtotime($endDate)) {
//如果开始日期大于结束日期直接return 防止下面的循环出现死循环
return $dates;
} elseif ($startDate == $endDate) {
//开始日期与结束日期是同一天时
array_push($dates, date('m-d', strtotime($startDate)));
return $dates;
} else {
array_push($dates, date('m-d', strtotime($startDate)));
$currentDate = $startDate;
do {
$nextDate = date('Y-m-d', strtotime($currentDate . ' +1 days'));
array_push($dates, date('m-d', strtotime($currentDate . ' +1 days')));
$currentDate = $nextDate;
} while ($endDate != $currentDate);
return $dates;
}
}
}
if (!function_exists('getStartModelTime')) {
function getStartModelTime(string $section)
{
switch ($section) {
case 'today':
case 'yesterday':
return date('Y-m-d', strtotime($section));
case 'week':
return date('Y-m-d', strtotime('this week'));
case 'month':
return date('Y-m-d', strtotime('first Day of this month'));
case 'year':
return date('Y-m-d', strtotime('this year 1/1'));
case 'quarter':
list($startTime, $endTime) = getMonth();
return $startTime;
case 'lately7':
return date('Y-m-d', strtotime("-6 day"));
case 'lately30':
return date('Y-m-d', strtotime("-30 day"));
default:
if (strstr($section, '-') !== false) {
list($startTime, $endTime) = explode('-', $section);
return date('Y-m-d H:i:s', strtotime($startTime));
}
return date('Y-m-d H:i:s');
}
}
}