Files
spacestation/api/controllers/wxapp/licheb/Score.php
T
2024-10-21 10:57:14 +08:00

157 lines
6.2 KiB
PHP

<?php
defined('WXAPP_APP') or exit('No direct script access allowed');
require_once APPPATH . 'controllers/wxapp/Wxapp.php';
class Score extends Wxapp
{
function __construct($inputs, $app_key)
{
parent::__construct($inputs, $app_key);
$this->load->model('receiver/receiver_score_config_model');
$this->load->model('receiver/receiver_score_day_model');
$this->load->model('receiver/receiver_score_month_model');
$this->load->model('receiver/receiver_score_log_model');
$this->load->model('app/licheb/app_licheb_users_model');
$this->biz_id = $this->get_biz_id();
$this->group_id = $this->session['group_id'];
}
/**
* @return array
*/
protected function get()
{
$max_score = 100; //最高得分
$uid = $this->session['uid'];
$show_biz = false;
$type = $this->input_param('type'); // 0个人 1门店
$day_type = $this->input_param('day_type'); // 默认0昨天 1上月
if ($this->group_id == App_licheb_users_model::GROUP_BIZ && $type) { //店长可查看个人或门店
$show_biz = true;
} elseif ($this->group_id == App_licheb_users_model::GROUP_INVESTOR || $this->group_id == App_licheb_users_model::GROUP_CUSTOMER_MANAGER) { //投资人和客户成功经理 只能查看门店
$show_biz = true;
}
$where = [
'biz_id' => $this->biz_id
];
if (!$show_biz) {//查看个人
$where['uid'] = $uid;
}
if ($day_type) { //上月
$up_month = strtotime("-1 month"); //上个月时间
$where['year'] = date('Y', $up_month);
$where['month'] = date('m', $up_month);
$where['type'] = $type ? Receiver_score_month_model::TYPE_BIZ : Receiver_score_month_model::TYPE_USER;
$row = $this->receiver_score_month_model->get($where);
$up_time = date('Y-m-01', time());
$title_hd = '较上月';
} else {
$up_month = strtotime("-1 day");
$where['day'] = date('Y-m-d', $up_month);
$where['type'] = $type ? Receiver_score_day_model::TYPE_BIZ : Receiver_score_day_model::TYPE_USER;
$row = $this->receiver_score_day_model->get($where);
$up_time = date('Y-m-d', $up_month);
$title_hd = '较昨日';
}
$score = ceil($row['score']) ?: 0;
$abs_score = ceil($row['change_score']) ?: 0;
if ($abs_score >= 0) {
$title = $title_hd . "上升{$abs_score}";
$score_trend = 1;
} else {
$abs_score = abs($abs_score);
$title = $title_hd . "下降{$abs_score}";
$score_trend = 2;
}
$percentage = $score / $max_score * 100;
$score_list = [];
if (!$day_type && !$show_biz) { //个人且非查看门店
$score_list = [
['name' => '基础分', 'value' => 0, 'operator' => ''],
['name' => '线索分', 'value' => 0, 'operator' => '+'],
['name' => '订单分', 'value' => 0, 'operator' => '+'],
['name' => '扣分项', 'value' => 0, 'operator' => '-'],
];
}
return [
'score' => $score,
'percentage' => $percentage,
'title' => $title,
'sub_title' => '当前显示为昨日运营情况评分',
'u_time_text' => "*更新于" . $up_time,
'score_trend' => $score_trend, //控制样式:1 上升 、2 下降
'desc' => '配置说明',
'score_list' => $score_list
];
}
/**
* 获取分数排行
* @return array
*/
public function get_lists()
{
$type = $this->input_param('type'); // 默认0店内排行 1门店排行
$day_type = $this->input_param('day_type'); // 默认0昨天 1上月
if ($day_type) {
$result = Receiver_score_month_model::getTopLists($type, $this->biz_id);
} else {
$result = Receiver_score_day_model::getTopLists($type, $this->biz_id);
}
return [
'lists' => $result['lists'],
'day' => $result['day']
];
}
/**
*
* @return array
*/
public function get_detail()
{
$uid = $this->session['uid'];
$base_list = Receiver_score_config_model::TYPE_LIST;
$day = date('Y-m-d', strtotime("-1 day"));
$where = [
'day' => $day,
'type' => Receiver_score_day_model::TYPE_USER,
'uid' => $uid,
'biz_id' => $this->biz_id
];
$dayRow = $this->receiver_score_day_model->get($where);
$user_score = $dayRow['score'] ? ceil($dayRow['score']) : 0;
$change_score = $dayRow['change_score'] ? ceil($dayRow['change_score']) : 0;
$up_data_score = $user_score + $change_score;
$score_trend = 1;
if ($up_data_score >= 0) {
$change_text = '较上一日上升' . $up_data_score . '分';
} else {
$change_text = '较上一日下降' . abs($up_data_score) . '分';
$score_trend = 2;
}
$data = [];
if ($base_list) {
$scoreLogModel = new receiver_score_log_model();
foreach ($base_list as $key => $item) {
$where = [
'biz_id' => $this->biz_id,
'uid' => $uid,
'day' => $day,
];
$scoreSum = $scoreLogModel->sum('score', $where);
$score = $scoreSum['score'] ? ceil($scoreSum['score']) : 0;
$trend = $score >= 0 ? 1 : 2;
$data_list = Receiver_score_log_model::getUserLogLists($this->biz_id, $uid, $day, $key);
$data[] = [
'name' => $item['name'],
'score' => $score,
'trend' => $trend,
'data' => $data_list
];
}
}
return ['data' => $data, 'score' => $user_score, 'up_data_score' => $up_data_score, 'change_text' => $change_text, 'score_trend' => $score_trend];
}
}