Merge branch 'feature/kpi'
This commit is contained in:
@@ -0,0 +1,45 @@
|
||||
<?php
|
||||
namespace app\admin\targets\controller;
|
||||
|
||||
use app\common\controller\BaseController;
|
||||
use app\common\model\Corp;
|
||||
use app\common\model\User;
|
||||
use app\common\traits\crud\CrudControllerTrait;
|
||||
use app\admin\targets\service\SalesAnnualTargetsService;
|
||||
|
||||
class SalesAnnualTargetsController extends BaseController
|
||||
{
|
||||
use CrudControllerTrait;
|
||||
|
||||
protected $service;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->service = new SalesAnnualTargetsService();
|
||||
}
|
||||
|
||||
/**
|
||||
* 列表页数据格式化
|
||||
*
|
||||
* @param array $data
|
||||
* @return array
|
||||
*/
|
||||
protected function indexFormat($data)
|
||||
{
|
||||
if (isset($data['created_at']))
|
||||
$data['created_at'] = date('Y-m-d H:i:s', $data['created_at']);
|
||||
if (isset($data['updated_at']))
|
||||
$data['updated_at'] = date('Y-m-d H:i:s', $data['updated_at']);
|
||||
if (isset($data['corp_id'])){
|
||||
$data['corp_name'] = Corp::where('id', $data['corp_id'])->value('name');
|
||||
// $data['corp_name'] = $data['corp_name']??'合塑集团';
|
||||
}
|
||||
if (isset($data['user_id']))
|
||||
$data['user_name'] = User::where('id', $data['user_id'])->value('name');
|
||||
|
||||
if (empty($data['corp_id']) || empty($data['user_id'])){
|
||||
$data['user_name'] = '合塑集团';
|
||||
}
|
||||
return $data;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
namespace app\admin\targets\controller;
|
||||
|
||||
use app\common\controller\BaseController;
|
||||
use app\common\model\Corp;
|
||||
use app\common\model\User;
|
||||
use app\common\traits\crud\CrudControllerTrait;
|
||||
use app\admin\targets\service\SalesMonthlyTargetsService;
|
||||
|
||||
class SalesMonthlyTargetsController extends BaseController
|
||||
{
|
||||
use CrudControllerTrait;
|
||||
|
||||
protected $service;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->service = new SalesMonthlyTargetsService();
|
||||
}
|
||||
|
||||
/**
|
||||
* 列表页数据格式化
|
||||
*
|
||||
* @param array $data
|
||||
* @return array
|
||||
*/
|
||||
protected function indexFormat($data)
|
||||
{
|
||||
if (isset($data['created_at']))
|
||||
$data['created_at'] = date('Y-m-d H:i:s', $data['created_at']);
|
||||
if (isset($data['updated_at']))
|
||||
$data['updated_at'] = date('Y-m-d H:i:s', $data['updated_at']);
|
||||
if (isset($data['corp_id']))
|
||||
$data['corp_name'] = Corp::where('id', $data['corp_id'])->value('name');
|
||||
if (isset($data['user_id']))
|
||||
$data['user_name'] = User::where('id', $data['user_id'])->value('name');
|
||||
|
||||
return $data;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,156 @@
|
||||
<?php
|
||||
namespace app\admin\targets\controller;
|
||||
|
||||
use app\admin\targets\service\SalesAnnualTargetsService;
|
||||
use app\common\controller\BaseController;
|
||||
use app\common\model\Corp;
|
||||
use app\common\model\SalesAnnualTargets;
|
||||
use app\common\model\SalesMonthlyTargets;
|
||||
use app\common\model\User;
|
||||
use app\common\traits\crud\CrudControllerTrait;
|
||||
use support\Request;
|
||||
use support\Db;
|
||||
|
||||
class StatisticsController extends BaseController
|
||||
{
|
||||
use CrudControllerTrait;
|
||||
|
||||
protected $service;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->service = new SalesAnnualTargetsService();
|
||||
}
|
||||
|
||||
public function total(Request $request)
|
||||
{
|
||||
$param = $request->all();
|
||||
if (empty($param['year'])) {
|
||||
return $this->error('year不能为空');
|
||||
}
|
||||
|
||||
$data = SalesAnnualTargets::select('annual_target','corp_id')
|
||||
->whereNull('user_id')
|
||||
->where('year',$param['year'])
|
||||
->orderBy('corp_id')
|
||||
->get()
|
||||
->toArray();
|
||||
|
||||
foreach ($data as &$value) {
|
||||
$where = [];
|
||||
$where[] = ['year', '=', $param['year']];
|
||||
if (!empty($value['corp_id'])) {
|
||||
$where[] = ['corp_id', '=', $value['corp_id']];
|
||||
}
|
||||
$value['achieved'] = SalesMonthlyTargets::where($where)->sum('achieved');
|
||||
$value['progress'] = empty($value['annual_target'])?'100%':number_format(($value['achieved']/$value['annual_target']*100),2) . '%';
|
||||
$value['corp_name'] = empty($value['corp_id'])?'集团':Corp::where(['id' => $value['corp_id']])->value('name');
|
||||
}
|
||||
return $this->success($data);
|
||||
}
|
||||
|
||||
public function ranking(Request $request){
|
||||
$param = $request->all();
|
||||
$year = empty($param['year']) ? date('Y') : $param['year'];
|
||||
$where = [];
|
||||
$where[] = ['year', '=', $year];
|
||||
$ranking = SalesMonthlyTargets::select('corp_id', 'user_id', Db::raw('SUM(achieved) AS achieved'))
|
||||
->where($where)
|
||||
->groupBy('corp_id', 'user_id')
|
||||
->orderBy('achieved', 'desc')
|
||||
->get();
|
||||
foreach ($ranking as $key => $value) {
|
||||
$ranking[$key]['corp_name'] = Corp::where(['id' => $value['corp_id']])->value('name');
|
||||
$ranking[$key]['user_name'] = User::where(['id' => $value['user_id']])->value('name');
|
||||
}
|
||||
return $this->success($ranking);
|
||||
}
|
||||
|
||||
public function achievedUserByMonth(Request $request){
|
||||
$param = $request->all();
|
||||
$year = empty($param['year']) ? date('Y') : $param['year'];
|
||||
$where = [];
|
||||
$where[] = ['year', '=', $year];
|
||||
$ranking = SalesMonthlyTargets::select('corp_id', 'user_id', 'month', Db::raw('SUM(achieved) AS achieved'))
|
||||
->where($where)
|
||||
->groupBy('corp_id', 'user_id', 'month')
|
||||
->orderBy('user_id', 'asc')
|
||||
->orderBy('month', 'asc')
|
||||
->get();
|
||||
$result = [];
|
||||
foreach ($ranking as $key => $value) {
|
||||
$result[$value['user_id']]['month'][$value['month']] = $value['achieved'];
|
||||
$result[$value['user_id']]['user_id'] = $value['user_id'];
|
||||
$result[$value['user_id']]['corp_id'] = $value['corp_id'];
|
||||
}
|
||||
foreach ($result as $key => $value) {
|
||||
for($i = 1; $i <= 12; $i++){
|
||||
$result[$key]['month'][$i] = $result[$key]['month'][$i]??0;
|
||||
}
|
||||
$result[$key]['month'] = array_values($result[$key]['month']);
|
||||
$result[$key]['corp_name'] = Corp::where(['id' => $value['corp_id']])->value('name');
|
||||
$result[$key]['user_name'] = User::where(['id' => $value['user_id']])->value('name');
|
||||
}
|
||||
$result = array_values($result);
|
||||
return $this->success($result);
|
||||
}
|
||||
|
||||
/**
|
||||
* 个人年度目标完成度(百分比)
|
||||
* @param Request $request
|
||||
* @return \support\Response
|
||||
*/
|
||||
public function userProgressh(Request $request){
|
||||
$param = $request->all();
|
||||
$year = empty($param['year']) ? date('Y') : $param['year'];
|
||||
$where = [];
|
||||
$where[] = ['year', '=', $year];
|
||||
$achieved = SalesMonthlyTargets::select('corp_id', 'user_id', Db::raw('SUM(achieved) AS achieved'))
|
||||
->where($where)
|
||||
->groupBy('corp_id', 'user_id')
|
||||
->orderBy('achieved', 'desc')
|
||||
->get()
|
||||
->toArray();
|
||||
$sort = [];
|
||||
foreach ($achieved as $key => $value) {
|
||||
$achieved[$key]['corp_name'] = Corp::where(['id' => $value['corp_id']])->value('name');
|
||||
$achieved[$key]['user_name'] = User::where(['id' => $value['user_id']])->value('name');
|
||||
$target = SalesAnnualTargets::where(['user_id' => $value['user_id']])->value('annual_target');
|
||||
$achieved[$key]['progressh'] = empty($target) ? 0 : round(($value['achieved'] / $target) * 100, 2);
|
||||
$sort[] = $achieved[$key]['progressh'];
|
||||
}
|
||||
array_multisort($sort, SORT_DESC, SORT_NUMERIC, $achieved);
|
||||
|
||||
foreach ($achieved as $key => $value) {
|
||||
$achieved[$key]['progressh'] = $value['progressh'];
|
||||
}
|
||||
return $this->success($achieved);
|
||||
}
|
||||
|
||||
/**
|
||||
* 个人累积完成数量(辆)
|
||||
* @param Request $request
|
||||
* @return \support\Response
|
||||
*/
|
||||
public function userAchieved(Request $request){
|
||||
$param = $request->all();
|
||||
$year = empty($param['year']) ? date('Y') : $param['year'];
|
||||
$where = [];
|
||||
$where[] = ['year', '=', $year];
|
||||
$achieved = SalesMonthlyTargets::select('corp_id', 'user_id', Db::raw('SUM(achieved) AS achieved'))
|
||||
->where($where)
|
||||
->groupBy('corp_id', 'user_id')
|
||||
->orderBy('achieved', 'desc')
|
||||
->get()
|
||||
->toArray();
|
||||
foreach ($achieved as $key => $value) {
|
||||
$achieved[$key]['corp_name'] = Corp::where(['id' => $value['corp_id']])->value('name');
|
||||
$achieved[$key]['user_name'] = User::where(['id' => $value['user_id']])->value('name');
|
||||
$target = SalesAnnualTargets::where(['user_id' => $value['user_id']])->value('annual_target');
|
||||
$achieved[$key]['annual_target'] = empty($target) ? 0 : $target;
|
||||
$achieved[$key]['total'] = [intval($value['achieved']), $achieved[$key]['annual_target']];
|
||||
}
|
||||
return $this->success($achieved);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
namespace app\admin\targets\logic;
|
||||
|
||||
use app\common\traits\crud\CrudLogicTrait;
|
||||
use app\common\model\SalesAnnualTargets;
|
||||
|
||||
class SalesAnnualTargetsLogic
|
||||
{
|
||||
use CrudLogicTrait;
|
||||
|
||||
/**
|
||||
* 指定当前要操作的模型
|
||||
*/
|
||||
protected string $modelClass = SalesAnnualTargets::class;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
namespace app\admin\targets\logic;
|
||||
|
||||
use app\common\traits\crud\CrudLogicTrait;
|
||||
use app\common\model\SalesMonthlyTargets;
|
||||
|
||||
class SalesMonthlyTargetsLogic
|
||||
{
|
||||
use CrudLogicTrait;
|
||||
|
||||
/**
|
||||
* 指定当前要操作的模型
|
||||
*/
|
||||
protected string $modelClass = SalesMonthlyTargets::class;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,89 @@
|
||||
<?php
|
||||
|
||||
namespace app\admin\targets\service;
|
||||
|
||||
use app\admin\targets\validate\SalesAnnualTargetsValidate;
|
||||
use app\common\model\User;
|
||||
use app\common\traits\crud\CrudServiceTrait;
|
||||
use app\common\model\SalesAnnualTargets;
|
||||
use support\exception\BusinessException;
|
||||
use support\Request;
|
||||
|
||||
class SalesAnnualTargetsService
|
||||
{
|
||||
|
||||
use CrudServiceTrait;
|
||||
|
||||
/**
|
||||
* 指定当前要操作的模型
|
||||
*/
|
||||
protected string $modelClass = SalesAnnualTargets::class;
|
||||
|
||||
/**
|
||||
* 验证器
|
||||
*/
|
||||
protected $validator = SalesAnnualTargetsValidate::class;
|
||||
|
||||
/**
|
||||
* 获取验证器
|
||||
*/
|
||||
public function getValidator()
|
||||
{
|
||||
return new $this->validator;
|
||||
}
|
||||
|
||||
/**
|
||||
* 构建查询条件
|
||||
*/
|
||||
protected function buildQueryConfig(Request $request, string $scene = 'index'): array
|
||||
{
|
||||
$p = $request->all();
|
||||
|
||||
$config = [];
|
||||
$config['model'] = $this->modelClass;
|
||||
switch ($scene) {
|
||||
case 'index':
|
||||
if (isset($p['year']) && $p['year']) {
|
||||
$config['where']['year'] = $p['year'];
|
||||
}
|
||||
$config['order']['id'] = 'desc';
|
||||
break;
|
||||
case 'show':
|
||||
case 'update':
|
||||
case 'destroy':
|
||||
$config['where']['id'] = $p['id'];
|
||||
break;
|
||||
case 'store':
|
||||
break;
|
||||
}
|
||||
|
||||
return $config;
|
||||
}
|
||||
// ====== 前置处理 ======
|
||||
/**
|
||||
* 添加前置处理
|
||||
*/
|
||||
public function beforeServiceStore($data, Request $request)
|
||||
{
|
||||
if(isset($data['user_id']) && $data['user_id'] && empty($data['corp_id'])){
|
||||
$data['corp_id'] = User::where('id', $data['user_id'])->value('corp_id');
|
||||
}
|
||||
if(isset($data['corp_id']) && empty($data['corp_id'])){
|
||||
unset($data['corp_id']);
|
||||
}
|
||||
if(isset($data['user_id']) && empty($data['user_id'])){
|
||||
unset($data['user_id']);
|
||||
}
|
||||
if(isset($data['user_id']) && isset($data['user_id'])){
|
||||
$annualTargets = SalesAnnualTargets::where('year',$data['year'])->where('corp_id',$data['corp_id'])->where('user_id', $data['user_id'])->first();
|
||||
}else{
|
||||
$annualTargets = SalesAnnualTargets::where('year',$data['year'])->whereNull('corp_id')->whereNull('user_id')->first();
|
||||
}
|
||||
|
||||
if($annualTargets){
|
||||
throw new BusinessException('目标重复添加');
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
<?php
|
||||
|
||||
namespace app\admin\targets\service;
|
||||
|
||||
use app\admin\targets\validate\SalesMonthlyTargetsValidate;
|
||||
use app\common\model\User;
|
||||
use app\common\traits\crud\CrudServiceTrait;
|
||||
use app\common\model\SalesMonthlyTargets;
|
||||
use support\Request;
|
||||
|
||||
class SalesMonthlyTargetsService
|
||||
{
|
||||
|
||||
use CrudServiceTrait;
|
||||
|
||||
/**
|
||||
* 指定当前要操作的模型
|
||||
*/
|
||||
protected string $modelClass = SalesMonthlyTargets::class;
|
||||
|
||||
/**
|
||||
* 验证器
|
||||
*/
|
||||
protected $validator = SalesMonthlyTargetsValidate::class;
|
||||
|
||||
/**
|
||||
* 获取验证器
|
||||
*/
|
||||
public function getValidator()
|
||||
{
|
||||
return new $this->validator;
|
||||
}
|
||||
|
||||
/**
|
||||
* 构建查询条件
|
||||
*/
|
||||
protected function buildQueryConfig(Request $request, string $scene = 'index'): array
|
||||
{
|
||||
$p = $request->all();
|
||||
|
||||
$config = [];
|
||||
$config['model'] = $this->modelClass;
|
||||
switch ($scene) {
|
||||
case 'index':
|
||||
if (isset($p['year']) && $p['year']) {
|
||||
$config['where']['year'] = $p['year'];
|
||||
}
|
||||
$config['order']['year'] = 'desc';
|
||||
$config['order']['user_id'] = 'desc';
|
||||
$config['order']['month'] = 'asc';
|
||||
$config['order']['id'] = 'desc';
|
||||
break;
|
||||
case 'show':
|
||||
case 'update':
|
||||
case 'destroy':
|
||||
$config['where']['id'] = $p['id'];
|
||||
break;
|
||||
case 'store':
|
||||
break;
|
||||
}
|
||||
|
||||
return $config;
|
||||
}
|
||||
|
||||
// ====== 前置处理 ======
|
||||
/**
|
||||
* 添加前置处理
|
||||
*/
|
||||
public function beforeServiceStore($data, Request $request)
|
||||
{
|
||||
$data['corp_id'] = User::where('id', $data['user_id'])->value('corp_id');
|
||||
|
||||
return $data;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
namespace app\admin\targets\validate;
|
||||
|
||||
use think\Validate;
|
||||
|
||||
class SalesAnnualTargetsValidate extends Validate
|
||||
{
|
||||
// 规则
|
||||
protected $rule = [
|
||||
|
||||
];
|
||||
|
||||
// 提示
|
||||
protected $message = [
|
||||
|
||||
];
|
||||
|
||||
// 场景
|
||||
protected $scene = [
|
||||
'store' => [
|
||||
|
||||
],
|
||||
|
||||
'update' => [
|
||||
|
||||
]
|
||||
];
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
namespace app\admin\targets\validate;
|
||||
|
||||
use think\Validate;
|
||||
|
||||
class SalesMonthlyTargetsValidate extends Validate
|
||||
{
|
||||
// 规则
|
||||
protected $rule = [
|
||||
|
||||
];
|
||||
|
||||
// 提示
|
||||
protected $message = [
|
||||
|
||||
];
|
||||
|
||||
// 场景
|
||||
protected $scene = [
|
||||
'store' => [
|
||||
|
||||
],
|
||||
|
||||
'update' => [
|
||||
|
||||
]
|
||||
];
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
<?php
|
||||
|
||||
namespace app\common\model;
|
||||
|
||||
use support\Model;
|
||||
|
||||
class SalesAnnualTargets extends Model
|
||||
{
|
||||
/**
|
||||
* The connection name for the model.
|
||||
*
|
||||
* @var string|null
|
||||
*/
|
||||
protected $connection = 'mysql';
|
||||
|
||||
/**
|
||||
* The table associated with the model.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $table = 'sales_annual_targets';
|
||||
|
||||
/**
|
||||
* The primary key associated with the table.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $primaryKey = 'id';
|
||||
|
||||
/**
|
||||
* Indicates if the model should be timestamped.
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
public $timestamps = true;
|
||||
|
||||
|
||||
protected $dateFormat = 'U';
|
||||
|
||||
/**
|
||||
* 允许自动填充模型字段
|
||||
*/
|
||||
protected $fillable = ['id', 'corp_id', 'user_id', 'year', 'annual_target', 'created_at', 'updated_at'];
|
||||
|
||||
/**
|
||||
* 不允许自动填充模型字段
|
||||
*/
|
||||
protected $guarded = [];
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
<?php
|
||||
|
||||
namespace app\common\model;
|
||||
|
||||
use support\Model;
|
||||
|
||||
class SalesMonthlyTargets extends Model
|
||||
{
|
||||
/**
|
||||
* The connection name for the model.
|
||||
*
|
||||
* @var string|null
|
||||
*/
|
||||
protected $connection = 'mysql';
|
||||
|
||||
/**
|
||||
* The table associated with the model.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $table = 'sales_monthly_targets';
|
||||
|
||||
/**
|
||||
* The primary key associated with the table.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $primaryKey = 'id';
|
||||
|
||||
/**
|
||||
* Indicates if the model should be timestamped.
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
public $timestamps = true;
|
||||
|
||||
|
||||
protected $dateFormat = 'U';
|
||||
|
||||
/**
|
||||
* 允许自动填充模型字段
|
||||
*/
|
||||
protected $fillable = ['id', 'corp_id', 'user_id', 'year', 'month', 'target', 'achieved', 'amount', 'currency_id', 'created_at', 'updated_at'];
|
||||
|
||||
/**
|
||||
* 不允许自动填充模型字段
|
||||
*/
|
||||
protected $guarded = [];
|
||||
}
|
||||
@@ -324,6 +324,31 @@ Route::group('/admin', function () {
|
||||
Route::any('/print', [app\admin\controller\StockOrdersController::class, 'print']);
|
||||
Route::any('/detail', [app\admin\controller\StockOrdersController::class, 'detail']);
|
||||
});
|
||||
|
||||
// crm
|
||||
Route::group('/targets', function () {
|
||||
Route::group('/monthly', function () {
|
||||
Route::any('/list', [app\admin\targets\controller\SalesMonthlyTargetsController::class, 'index']);
|
||||
Route::any('/store', [app\admin\targets\controller\SalesMonthlyTargetsController::class, 'store']);
|
||||
Route::any('/update', [app\admin\targets\controller\SalesMonthlyTargetsController::class, 'update']);
|
||||
Route::any('/destroy', [app\admin\targets\controller\SalesMonthlyTargetsController::class, 'destroy']);
|
||||
Route::any('/show', [app\admin\targets\controller\SalesMonthlyTargetsController::class, 'show']);
|
||||
});
|
||||
Route::group('/annual', function () {
|
||||
Route::any('/list', [app\admin\targets\controller\SalesAnnualTargetsController::class, 'index']);
|
||||
Route::any('/store', [app\admin\targets\controller\SalesAnnualTargetsController::class, 'store']);
|
||||
Route::any('/update', [app\admin\targets\controller\SalesAnnualTargetsController::class, 'update']);
|
||||
Route::any('/destroy', [app\admin\targets\controller\SalesAnnualTargetsController::class, 'destroy']);
|
||||
Route::any('/show', [app\admin\targets\controller\SalesAnnualTargetsController::class, 'show']);
|
||||
});
|
||||
Route::group('/statistics', function () {
|
||||
Route::any('/total', [app\admin\targets\controller\StatisticsController::class, 'total']);
|
||||
Route::any('/ranking', [app\admin\targets\controller\StatisticsController::class, 'ranking']);
|
||||
Route::any('/user/month', [app\admin\targets\controller\StatisticsController::class, 'achievedUserByMonth']);
|
||||
Route::any('/user/progressh', [app\admin\targets\controller\StatisticsController::class, 'userProgressh']);
|
||||
Route::any('/user/achieved', [app\admin\targets\controller\StatisticsController::class, 'userAchieved']);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user