SalePerformanceLists.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | likeadmin快速开发前后端分离管理后台(PHP版)
  4. // +----------------------------------------------------------------------
  5. // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
  6. // | 开源版本可自由商用,可去除界面版权logo
  7. // | gitee下载:https://gitee.com/likeshop_gitee/likeadmin
  8. // | github下载:https://github.com/likeshop-github/likeadmin
  9. // | 访问官网:https://www.likeadmin.cn
  10. // | likeadmin团队 版权所有 拥有最终解释权
  11. // +----------------------------------------------------------------------
  12. // | author: likeadminTeam
  13. // +----------------------------------------------------------------------
  14. namespace app\adminapi\lists\sale;
  15. use app\adminapi\lists\BaseAdminDataLists;
  16. use app\adminapi\logic\sale\SaleRulesLogic;
  17. use app\common\model\property\PropertyHead;
  18. use app\common\model\sale\Sale;
  19. use app\common\lists\ListsSearchInterface;
  20. use app\common\model\sale\SaleRules;
  21. use think\facade\Db;
  22. /**
  23. * SalePerformanceLists列表
  24. * Class SalePerformanceLists
  25. * @package app\adminapi\lists
  26. */
  27. class SalePerformanceLists extends BaseAdminDataLists implements ListsSearchInterface
  28. {
  29. public $count = 0;
  30. public $startDateTime = 0;
  31. public $endDateTime = 0;
  32. /**
  33. * @notes 设置搜索条件
  34. * @return \string[][]
  35. * @author likeadmin
  36. * @date 2024/12/15 10:53
  37. */
  38. public function setSearch(): array
  39. {
  40. return [];
  41. }
  42. public function queryWhere()
  43. {
  44. $where = [];
  45. if(isset($this->params['time_range']) && $this->params['time_range']){
  46. $this->startDateTime = strtotime($this->params['time_range'][0]);
  47. $this->endDateTime = strtotime($this->params['time_range'][1])+86400-1;
  48. }else{
  49. $this->startDateTime = strtotime('-1 month');
  50. $this->endDateTime = time();
  51. }
  52. if(isset($this->params['sale_id']) && $this->params['sale_id']){
  53. $where[] = ['s.id','=',$this->params['sale_id']];
  54. }
  55. return $where;
  56. }
  57. /**
  58. * @notes 获取列表
  59. * @return array
  60. * @throws \think\db\exception\DataNotFoundException
  61. * @throws \think\db\exception\DbException
  62. * @throws \think\db\exception\ModelNotFoundException
  63. * @author likeadmin
  64. * @date 2024/12/15 10:53
  65. */
  66. public function lists(): array
  67. {
  68. $this->queryWhere();
  69. $directWorkerCountSub = Db::table('la_master_worker_register')
  70. ->field(['sale_id', Db::raw('COUNT(DISTINCT worker_id) AS direct_worker_count')])
  71. ->where('worker_id', '>', 0)
  72. ->where('sale_id', '>', 0)
  73. ->whereBetweenTime('create_time',$this->startDateTime, $this->endDateTime)
  74. ->group('sale_id')
  75. ->buildSql();
  76. $tenantWorkerCountSub = Db::table('la_tenant_register')
  77. ->alias('lr')
  78. ->join('la_master_worker mw', 'lr.tenant_id = mw.tenant_id')
  79. ->field(['lr.sale_id', Db::raw('COUNT(DISTINCT mw.id) AS tenant_worker_count')])
  80. ->where('lr.tenant_id', '>', 0)
  81. ->whereBetweenTime('lr.create_time',$this->startDateTime, $this->endDateTime)
  82. ->group('lr.sale_id')
  83. ->buildSql();
  84. $lists = Db::table('la_sale')
  85. ->alias('s')
  86. ->leftJoin([$directWorkerCountSub => 'dwc'], 's.id = dwc.sale_id')
  87. ->leftJoin([$tenantWorkerCountSub => 'twc'], 's.id = twc.sale_id')
  88. ->field([
  89. 's.id',
  90. 's.sale_name',
  91. 's.mobile',
  92. Db::raw('COALESCE(dwc.direct_worker_count, 0) AS direct_worker_count'),
  93. Db::raw('COALESCE(twc.tenant_worker_count, 0) AS tenant_worker_count'),
  94. Db::raw('COALESCE(dwc.direct_worker_count, 0) + COALESCE(twc.tenant_worker_count, 0)*10 AS total_worker_count')
  95. ])
  96. ->limit($this->limitOffset, $this->limitLength)
  97. ->where($this->queryWhere())
  98. ->select()->toArray();
  99. foreach ($lists as &$v) {
  100. //$v['total_worker_count']
  101. //$v['total_worker_amount']
  102. $v['total_worker_amount'] = (SaleRulesLogic::getSaleAmountByRating((float)$v['total_worker_count'],1) * $v['total_worker_count']);
  103. //$v['id']
  104. $v['total_property_count'] = 0;
  105. $v['total_property_amount'] = $this->getSaleAmountBySaleId($v['id']);
  106. }
  107. return $lists;
  108. }
  109. /**
  110. * @notes 获取数量
  111. * @return int
  112. * @author likeadmin
  113. * @date 2024/12/15 10:53
  114. */
  115. public function count(): int
  116. {
  117. $this->queryWhere();
  118. $directWorkerCountSub = Db::table('la_master_worker_register')
  119. ->field(['sale_id', Db::raw('COUNT(DISTINCT worker_id) AS direct_worker_count')])
  120. ->where('worker_id', '>', 0)
  121. ->where('sale_id', '>', 0)
  122. ->whereBetweenTime('create_time',$this->startDateTime, $this->endDateTime)
  123. ->group('sale_id')
  124. ->buildSql();
  125. $tenantWorkerCountSub = Db::table('la_tenant_register')
  126. ->alias('lr')
  127. ->leftJoin('la_master_worker mw', 'lr.tenant_id = mw.tenant_id')
  128. ->field(['lr.sale_id', Db::raw('COUNT(DISTINCT mw.id) AS tenant_worker_count')])
  129. ->where('lr.tenant_id', '>', 0)
  130. ->whereBetweenTime('lr.create_time',$this->startDateTime, $this->endDateTime)
  131. ->group('lr.sale_id')
  132. ->buildSql();
  133. return Db::table('la_sale')
  134. ->alias('s')
  135. ->leftJoin([$directWorkerCountSub => 'dwc'], 's.id = dwc.sale_id')
  136. ->leftJoin([$tenantWorkerCountSub => 'twc'], 's.id = twc.sale_id')
  137. ->field([
  138. 's.id',
  139. 's.sale_name',
  140. Db::raw('COALESCE(dwc.direct_worker_count, 0) AS direct_worker_count'),
  141. Db::raw('COALESCE(twc.tenant_worker_count, 0) AS tenant_worker_count'),
  142. Db::raw('COALESCE(dwc.direct_worker_count, 0) + COALESCE(twc.tenant_worker_count, 0) AS total_worker_count')
  143. ])
  144. ->where($this->queryWhere())
  145. ->count();
  146. }
  147. public function setExcelComplexFields(): array
  148. {
  149. $zh_cn_fields = [
  150. '姓名','手机号','入驻加盟总绩效金额','物业合作总绩效金额'
  151. ];
  152. $data_fields = ['sale_name','mobile','total_worker_amount','total_property_amount'];
  153. return [
  154. 'zh_cn_fields' => $zh_cn_fields,
  155. 'data_fields' => $data_fields
  156. ];
  157. }
  158. /**
  159. * 根据档位获取销售绩效金额
  160. *
  161. * @param int $rating 档位
  162. * @return float|null 对应的金额,若未找到匹配的档位则返回0
  163. */
  164. public function getSaleAmountBySaleId(int $sale_id): ?float
  165. {
  166. $this->queryWhere();
  167. $rating_values = PropertyHead::where('sale_id', $sale_id)
  168. ->where('is_cooperate', 1)
  169. ->whereBetweenTime('create_time',$this->startDateTime, $this->endDateTime)
  170. ->column('rating_value');
  171. $countAmount = 0;
  172. foreach ($rating_values as $rating_value) {
  173. $countAmount += SaleRulesLogic::getSaleAmountByRating((float)$rating_value, 2);
  174. }
  175. return $countAmount;
  176. }
  177. }