Commit b66ab852 authored by hujun's avatar hujun

经纪人通话统计

parent 4a37c916
......@@ -107,14 +107,46 @@ class AliYunSecretReport extends BaseModel
}
/**
* 返回user_id
*
* @return array
* @param $pageNo
* @param $pageSize
* @param String $field
* @param array $where
* @param String $group
* @return false|int|\PDOStatement|string|\think\Collection
*/
public function getCallUserId() {
return $this->where('create_time', 'between', ['2018-08-28', '2018-08-31'])
->where('users_id','NOT NULL')
->group('users_id')
->column('users_id');
public function getListGroupAll($pageNo, $pageSize, String $field, Array $where, String $group) {
try {
$data = $this->field($field)
->where($where)
->group($group)
->limit($pageSize)
->page($pageNo)
->select();
} catch (\Exception $e) {
$data = 0;
}
return $data;
}
/**
* @param array $where
* @param String $group
* @return int|string
*/
public function getListGroupCount(Array $where, String $group) {
try {
$data = $this->where($where)
->group($group)
->count();
} catch (\Exception $e) {
$data = 0;
}
return $data;
}
public function getListAll($field, $where) {
return $this->field($field)
->where($where)
->select();
}
}
\ No newline at end of file
<?php
namespace app\model;
use think\Db;
use think\Model;
/**
* Created by PhpStorm.
* User : zw
* Date : 2018/3/15
* Time : 下午2:00
* Intro:
*/
class TAgentTotalCallModel extends Model
{
protected $table = "t_agent_call";
private $db_;
function __construct()
{
$this->db_ = Db::name($this->table);
}
/**
* @param $params
* @return int
*/
public function addTotal($params)
{
Db::startTrans();
try {
$this->db_->insertAll($params);
Db::commit();
return 1;
} catch (\Exception $e) {
print_r($e);
Db::rollback();
return 0;
}
}
/**
* @param $params
* @return int
*/
public function saveTotal($params)
{
Db::startTrans();
try {
foreach ($params as $k=>$v) {
$this->db_->table($this->table)->update($v);
}
Db::commit();
return 1;
} catch (\Exception $e) {
print_r($e);
Db::rollback();
return 0;
}
}
/**
* 获取最后统计的时间
*
* @param $field
* @param $param
* @return array|false|\PDOStatement|string|Model
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\exception\DbException
*/
public function getTotalEndTimeByAgentId($field, $param)
{
return $this->db_
->field($field)
->where($param)
->order("total_time desc")
->find();
}
/**
* @param String $field
* @param array $where
* @return array|false|int|\PDOStatement|string|Model
*/
public function getInfo(String $field,array $where) {
try {
$data = $this->db_
->field($field)
->where($where)
->find();
} catch (\Exception $e) {
$data = 0;
}
return $data;
}
}
\ No newline at end of file
......@@ -470,6 +470,8 @@ Route::group('task', [
'queryRecordFile' => ['task/PrivacyNumber/queryRecordFile', ['method' => 'get']], //下载录音
'releaseNumber' => ['task/PrivacyNumber/releaseNumber', ['method' => 'get']], //释放号码
'checkBindPhone' => ['task/PrivacyNumber/checkBindPhone', ['method' => 'get']], //检查绑定关系,去除表中不存在的绑定关系
'totalAgentCall' => ['task/PrivacyNumber/totalAgentCall', ['method' => 'get']], //统计每天经纪人拨打电话
'oldTotalAgentCall' => ['task/PrivacyNumber/oldTotalAgentCall', ['method' => 'get']], //历史数据拨打数据
'updateStatusByTime' => ['task/updateShopStatusTask/updateStatusByTime', ['method' => 'get']], //修改上下架
......
......@@ -17,6 +17,7 @@ use app\model\AAgents;
use app\model\AAgentsPhone;
use app\model\AliYunSecretReport;
use app\model\BindingPhone;
use app\model\TAgentTotalCallModel;
use think\Db;
use think\Log;
use think\Request;
......@@ -26,11 +27,13 @@ class PrivacyNumber
private $redis;
private $m_bind;
private $m_secret_report;
private $m_agent;
public function __construct()
{
$this->redis = RedisExt::getRedis();
$this->m_secret_report = new AliYunSecretReport();
$this->m_bind = new BindingPhone();
$this->m_agent = new AAgents();
}
/**
......@@ -375,4 +378,130 @@ class PrivacyNumber
$call_service = new CallPhoneService();
$call_service->defaultFollowUp();
}
/**
* 统计每天经纪人拨打电话
*
* @param $month
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\exception\DbException
*/
public function totalAgentCall($month)
{
set_time_limit(0); // 取消脚本运行时间的超时上限
$call = new TAgentTotalCallModel();
if (empty($month)) {
$yesterday = date('Y-m-d', strtotime('-1 day'));
} else {
$yesterday = $month;
}
$where['create_time'] = ['between', [$yesterday .' 00:00:00', $yesterday . ' 23:59:59']];
$where['time'] = ['>', 0];
$total = $this->m_secret_report->getListGroupCount($where, 'phone_no');
$pageSize = 50;
$pageTotal = ceil($total / $pageSize);
for ($pageNo = 1; $pageNo <= $pageTotal; $pageNo++) {
$secret_data = $this->m_secret_report->getListGroupAll($pageNo, $pageSize, 'sum(time) as time,phone_no,agents_id', $where, 'phone_no');
if (empty($secret_data[0])) {
continue;
}
foreach ($secret_data as $k=>$v) {
$tmp_data = [];
$call_where['agent_id'] = $v['agents_id'];
$call_where['total_time'] = $yesterday;
$call_where['phone'] = $v['phone_no'];
$call_data = $call->getInfo('id,time,money', $call_where);
$where_agent['agents_id'] = $v['agents_id'];
$where_agent['phone_no'] = $v['phone_no'];
$where_agent['create_time'] = $where['create_time'];
$where_agent['time'] = $where['time'];
$time_arr = $this->m_secret_report->getListAll('time', $where_agent);
$minute = 0;
foreach ($time_arr as $t_val) {
if ($t_val['time']%60 == 0){
$minute += $t_val['time']/60;
} else {
$minute += $t_val['time']/60 + 1;
}
}
$tmp_data['money'] = $minute * 0.055 + $minute * 0.04;
$tmp_data['time'] = $v['time'];
$tmp_data['phone'] = $v['phone_no'];
$tmp_data['agent_id'] = $v['agents_id'];
if (empty($call_data)) {
$tmp_data['total_time'] = $yesterday;
$insert_data[] = $this->binCallData($tmp_data);
} else {
$tmp_data['id'] = $call_data['id'];
$tmp_data['total_time'] = $yesterday;
$update_data[] = $this->binCallData($tmp_data);
}
unset($tmp_data);
}
if (!empty($insert_data)) {
$call->addTotal($insert_data);
unset($insert_data);
}
if (!empty($update_data)) {
$call->saveTotal($update_data);
unset($update_data);
}
}
return;
}
/**
* 组装数据
*
* @param $data
* @return mixed
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\exception\DbException
*/
private function binCallData($data) {
if (!empty($data['agent_id'])) {
$agent_data = $this->m_agent->getAgentInfo('name,district_id,store_id,phone', $data['agent_id']);
$insert_data['district_id'] = $agent_data['district_id'];
$insert_data['store_id'] = $agent_data['store_id'];
$insert_data['agent_id'] = $data['agent_id'];
$insert_data['name'] = $agent_data['name'];
}
$insert_data['phone'] = $data['phone'];
$insert_data['total_time'] = $data['total_time'];
$insert_data['time'] = $data['time'];
$insert_data['money'] = $data['money'];
if (!empty($data['id'])) {
$insert_data['id'] = $data['id'];
}
return $insert_data;
}
/**
* 历史数据
*/
public function oldTotalAgentCall() {
// $month = ['2018-03','2018-04','2018-05','2018-06','2018-07','2018-08','2018-09','2018-10','2018-11'];
$month = ['2018-11'];
foreach ($month as $v) {
$firstday = date('Y-m-01', strtotime($v));
$lastday = date('d', strtotime("$firstday +1 month -1 day"));
for ($i=1; $i<=$lastday; $i++) {
$this->totalAgentCall($v.'-'.$i);
}
}
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment