Commit cb83b1f7 authored by zw's avatar zw

pk

门店排行
parent e7fb4ba5
......@@ -28,7 +28,7 @@ class RankingList extends Basic{
*/
public function getRandKingList(){
$params = $this->params;
/*$params["position"] = 1;
/* $params["position"] = 1;//职称 1店长 2经理 3主任 4顾问 0新人
$this->siteId = 10001;*/
if(!isset($params["position"]) || $params["position"] < 0 || $params["position"] > 5){
return $this->response("101","请求参数错误");
......@@ -38,6 +38,13 @@ class RankingList extends Basic{
return $this->response("200","success",$result);
}
}
public function getStoreRandKingList(){
$params = $this->params;
$result = $this->service_->StoreRankingList( $this->siteId);
if(count($result) > 0){
return $this->response("200","success",$result);
}
}
/**
* 导出分区pk榜
......
......@@ -3,6 +3,7 @@
namespace app\index\service;
use app\model\AAgents;
use app\model\AStore;
use app\model\TAgentTotalModel;
/**
......@@ -110,6 +111,87 @@ class RankingListService
}
public function StoreRankingList($siteId){
$begin_time = date('Y-m-01 00:00:00', strtotime('-1 month'));
$end_time = date("Y-m-d 23:59:59", strtotime(-date('d') . 'day'));
$params["a.site_id"] = $siteId;
$params['b.total_time'] = array('between', array($begin_time, $end_time));
$resultArr = $this->getStoreSort($params, $begin_time, $end_time, "");
if (count($resultArr) <= 0) {
return false;
}
//分组
$total = count($resultArr);
$total_page = floor($total / $this->crewNum);
$residue = $total % $this->crewNum;
$full = $total_page * ($this->crewNumMax - $this->crewNum);
$x = $this->crewNum;
$y = $total_page;
if ($full < $residue) {
$y = $y + 1;
} else {
$x = ceil($residue / $total_page) + $this->crewNum;
}
$result = [];
$i = 0;
$k = 0;
foreach ($resultArr as $key => $item) {
$result[$i][$k++] = $item;
if ($y == $total_page) {
if (($k == $x - 1) && ($residue % $total_page) > 0 && ($i + 1) <= ($total_page - ($residue % $total_page))) {
$k = 0;
++$i;
} elseif ($k == $x) {
$k = 0;
++$i;
}
} else {
if ($k == $x) {
$k = 0;
++$i;
}
}
}
//循环分组后的数组,查询出各组本月业绩排行
//本月
$begin_time = date('Y-m-01', time());
$end_time = date("Y-m-01", strtotime('+1 month'));
$param_now["a.site_id"] = $siteId;
$param_now["a.position"] = $position;
$param_now["a.status"] = array("in", "0,3");
$param_now['b.total_time'] = array('between', array($begin_time, $end_time));
foreach ($result as $index => $item) {
$ids = "";
foreach ($item as $key => $value) {
$ids .= $value["id"] . ",";
}
$ids = rtrim($ids, ",");
$param_now["a.id"] = array("in", $ids);
$sort_arr = $this->getStoreSort($param_now, $begin_time, $end_time, $ids);
$result[$index] = $sort_arr;
}
return $result;
}
public function getStoreSort($params, $begin_time, $end_time, $ids)
{
$storeModel = new AStore();
$fields = "a.id,c.store_name,a.name,a.phone,SUM(b.performance) as performance";
//查询出有业绩的经纪人
$have_performance_arr = $storeModel->getStoreListByPK($fields, $params);
//查询出无业绩的经纪人
$arr = $storeModel->getStoreListByPk1($params["a.site_id"], $params["a.position"], $begin_time, $end_time,
$ids);
return array_merge($have_performance_arr, $arr);
}
/**
* 查询数据
* @param $params
......
......@@ -431,6 +431,17 @@ class AAgents extends BaseModel
//echo $this->getLastSql();
return $data;
}
/**
* @param $siteId
* @param $position
* @param $startTime
* @param $endTime
* @param $ids
* @return mixed
* @throws \think\db\exception\BindParamException
* @throws \think\exception\PDOException
*/
public function getAgentListByPk1($siteId,$position,$startTime,$endTime,$ids){
$str_ids = " ";
if($ids){
......
......@@ -275,4 +275,45 @@ class AStore extends BaseModel
//dump($this->getLastSql());
return $result;
}
/**
* @param string $field
* @param $params
* @return false|\PDOStatement|string|\think\Collection
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\exception\DbException
*/
public function getStoreListByPK( $field = "id",$params)
{
$data = Db::table($this->table)
->field($field)
->alias("a")
->join("t_agent_total b","a.id=b.store_id","left")
->where($params)
->group("b.store_id")
->order("performance desc")
->select();
//echo $this->getLastSql();
return $data;
}
public function getStoreListByPk1($siteId,$position,$startTime,$endTime,$ids){
$str_ids = " ";
if($ids){
$str_ids = " and a.id in ($ids)";
}
$sql = "select aa.id,aa.store_name,aa.performance from
(select a.id,a.store_name,0.00 as performance from a_store a
where a.site_id=$siteId and a.status =0 $str_ids)
as aa left join
(select a.id,a.store_name,0.00 as performance from a_store a
left join t_agent_total b on a.id=b.store_id
where b.total_time BETWEEN '$startTime' and '$endTime'and a.status =0 $str_ids GROUP BY b.store_id )
as bb on aa.id = bb.id where bb.id is null";
$data = Db::table($this->table)->query($sql);
//echo $this->getLastSql();
return $data;
}
}
\ 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