MasterWorkerServiceOrderLists.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. <?php
  2. namespace app\adminapi\lists\master_worker;
  3. use app\adminapi\lists\BaseAdminDataLists;
  4. use app\common\lists\ListsSearchInterface;
  5. use app\common\model\goods_category\GoodsCategory;
  6. use app\common\model\master_worker\MasterWorker;
  7. use app\common\model\master_worker_register\MasterWorkerRegister;
  8. use app\common\model\works\ServiceWorkAllocateWorkerLog;
  9. use think\facade\Db;
  10. use think\facade\Log;
  11. /**
  12. * MasterWorkerServiceOrder列表
  13. * Class MasterWorkerServiceOrderLists
  14. * @package app\adminapi\lists
  15. */
  16. class MasterWorkerServiceOrderLists extends BaseAdminDataLists implements ListsSearchInterface
  17. {
  18. public $sqlJoin = '';
  19. /**
  20. * @notes 设置搜索条件
  21. * @return \string[][]
  22. * @author likeadmin
  23. * @date 2024/11/15 17:21
  24. */
  25. public function setSearch(): array
  26. {
  27. return [];
  28. }
  29. public function queryWhere()
  30. {
  31. $where = [];
  32. if (isset($this->params['real_name']) && !empty($this->params['real_name'])) {
  33. $where[] = ['a.real_name','like' ,"%".$this->params['real_name']."%"];
  34. }
  35. if (isset($this->params['worker_number']) && !empty($this->params['worker_number'])) {
  36. $where[] = ['a.worker_number','like' ,"%".$this->params['worker_number']."%"];
  37. }
  38. if (isset($this->params['recruiting_behalf']) && !empty($this->params['recruiting_behalf'])) {
  39. $where[] = ['a.recruiting_behalf','like' ,"%".$this->params['recruiting_behalf']."%"];
  40. }
  41. if (isset($this->params['labels']) && !empty($this->params['labels'])) {
  42. $sqls = [];
  43. foreach ($this->params['labels'] as $item) {
  44. $sqls[] = "FIND_IN_SET({$item}, labels) > 0";
  45. }
  46. $query_sql = implode(' OR ', $sqls);
  47. $period_ids = MasterWorker::where('labels','<>', '')->whereRaw($query_sql)->column('id');
  48. $where[] = [ 'a.id','IN',$period_ids?:[0]];
  49. }
  50. if (isset($this->params['is_recruiting_behalf']) && !empty($this->params['is_recruiting_behalf'])) {
  51. if ($this->params['is_recruiting_behalf'] == 1)
  52. $where[] = ['a.recruiting_behalf','<>' ,""];
  53. else
  54. $where[] = ['a.recruiting_behalf','=' ,""];
  55. }
  56. if(isset($this->params['category_ids']) && !empty($this->params['category_ids'])){
  57. $sqls = [];
  58. $category_ids =[];
  59. foreach ($this->params['category_ids'] as $val){
  60. ($val = json_decode($val,true))?($category_ids[] = end($val)):($category_ids[] = $val);
  61. }
  62. foreach ($category_ids as $item) {
  63. $sqls[] = "FIND_IN_SET({$item}, category_ids) > 0";
  64. }
  65. $query_sql = implode(' OR ', $sqls);
  66. Log::write('query_sql:'.$query_sql);
  67. $period_ids = MasterWorker::where('category_ids','<>', '')->whereRaw($query_sql)->column('id');
  68. $where[] = [ 'a.id','IN',$period_ids?:[0]];
  69. }
  70. if (isset($this->params['mobile']) && !empty($this->params['mobile'])) {
  71. $where[] = ['a.mobile','like' ,"%".$this->params['mobile']."%"];
  72. }
  73. if (isset($this->params['cooperation']) && !empty($this->params['cooperation'])) {
  74. $where[] = ['a.cooperation','=' ,$this->params['cooperation']];
  75. }
  76. if (!empty($this->params['start_time'])) {
  77. $this->sqlJoin .= ' AND b.update_time >= '.strtotime($this->params['start_time']);
  78. }
  79. if (!empty($this->params['end_time'])) {
  80. $this->sqlJoin .= ' AND b.update_time < '.strtotime($this->params['end_time'])+86400;
  81. }
  82. $where[] = ['a.audit_state','=' ,1];
  83. return $where;
  84. }
  85. /**
  86. * 获取数据权限
  87. * $this->adminInfo['data_rules']
  88. * province city admin_id sale_group_id sale_id property_head_id
  89. */
  90. public function queryDataWhere(){
  91. $where = [];
  92. $data_rules = $this->adminInfo['data_rules'];
  93. if (isset($data_rules['province']) && !empty($data_rules['province'])) {
  94. $where[] = ['a.province','in' ,$data_rules['province']];
  95. $where[] = ['b.province','in' ,$data_rules['province']];
  96. }
  97. if (isset($data_rules['city']) && !empty($data_rules['city'])) {
  98. $where[] = ['a.city','in' ,$data_rules['city']];
  99. $where[] = ['b.city','in' ,$data_rules['city']];
  100. }
  101. return $where;
  102. }
  103. /**
  104. * @notes 获取列表
  105. * @return array
  106. * @throws \think\db\exception\DataNotFoundException
  107. * @throws \think\db\exception\DbException
  108. * @throws \think\db\exception\ModelNotFoundException
  109. * @author likeadmin
  110. * @date 2024/11/15 17:21
  111. */
  112. public function lists(): array
  113. {
  114. $queryWhere = $this->queryWhere();
  115. $lists = Db::name('master_worker')->alias('a')->field([
  116. 'a.id','a.real_name','a.nickname','a.worker_number','a.recruiting_behalf','a.mobile','a.cooperation','a.category_ids','a.labels','a.remark',
  117. Db::raw("COUNT(b.id) AS all_count"),
  118. Db::raw("SUM(CASE WHEN b.service_status = 3 THEN 1 ELSE 0 END) AS success_count"),
  119. Db::raw("SUM(CASE WHEN b.service_status = 4 OR b.service_status = 5 THEN 1 ELSE 0 END) AS fail_count"),
  120. Db::raw("SUM(CASE WHEN b.service_status = 3 THEN b.work_total ELSE 0 END) work_total"),
  121. Db::raw("SUM(CASE WHEN b.service_status = 3 THEN b.worker_price ELSE 0 END) worker_price"),
  122. ])
  123. ->leftJoin('service_work b', 'a.id = b.master_worker_id'.$this->sqlJoin)
  124. ->where($queryWhere)
  125. ->where($this->queryDataWhere())
  126. ->group('a.id')
  127. ->order('a.id desc')
  128. ->limit($this->limitOffset, $this->limitLength)
  129. ->select()->toArray();
  130. $categoryData = GoodsCategory::select()->toArray();
  131. $ids = array_column($lists,'id');
  132. $masterWorkerRegisters = array_column(MasterWorkerRegister::with(['sale'])->whereIn('worker_id',$ids)->select()->toArray(), null,'worker_id');
  133. foreach ($lists as &$item) {
  134. $item['labels'] = $item['labels']?array_map(function ($item) { return intval($item); },explode(',',$item['labels'])):'';
  135. $item['category_name'] = $item['category_ids']?implode('、',array_column(get_parent_info($categoryData,explode(',',$item['category_ids'])),'name')):'';
  136. $item['allocate_num'] = ServiceWorkAllocateWorkerLog::where(['master_worker_id'=>$item['id']])->count();
  137. $item['register_info'] = $masterWorkerRegisters[$item['id']]??[];
  138. }
  139. return $lists;
  140. }
  141. /**
  142. * @notes 获取数量
  143. * @return int
  144. * @author likeadmin
  145. * @date 2024/11/15 17:21
  146. */
  147. public function count(): int
  148. {
  149. $queryWhere = $this->queryWhere();
  150. return Db::name('master_worker')->alias('a')->field([
  151. 'a.id','a.real_name','a.nickname','a.worker_number','a.recruiting_behalf',
  152. Db::raw("SUM(CASE WHEN b.service_status = 3 THEN 1 ELSE 0 END) AS success_count"),
  153. Db::raw("SUM(CASE WHEN b.service_status = 4 OR b.service_status = 5 THEN 1 ELSE 0 END) AS fail_count"),
  154. Db::raw("SUM(b.work_total) work_total"),
  155. Db::raw("SUM(b.worker_price) worker_price"),
  156. ])
  157. ->leftJoin('service_work b', 'a.id = b.master_worker_id'.$this->sqlJoin)
  158. ->where($queryWhere)
  159. ->where($this->queryDataWhere())
  160. ->group('a.id')
  161. ->count();
  162. }
  163. public function setExcelComplexFields(): array
  164. {
  165. $zh_cn_fields = [
  166. '工程师ID', '服务类别', '工程师编号', '工程师姓名', '工程师手机',
  167. '工程师备注', '工程师合作形式','代招人姓名', '派单数', '工程师接单数', '标签',
  168. '工程师成功单','工程师失败单','工程师成交金额','工程师提成金额'
  169. ];
  170. $data_fields = [
  171. 'id', 'category_name', 'worker_number', 'real_name', 'mobile',
  172. 'remark', function ($row) {
  173. return $row['cooperation'] == 0 ? '无' : ($row['cooperation'] == 1 ? '全日接单' : '自由接单');
  174. }, 'recruiting_behalf', 'allocate_num', 'all_count', function ($row) {
  175. if(!empty($row['labels'])){
  176. $data_table_allLabels = array_column(\app\common\logic\TableDataLogic::allLabels(),'name','id');
  177. foreach ($row['labels'] as &$val){
  178. $val = $data_table_allLabels[$val];
  179. }
  180. $row['labels'] = implode('、', $row['labels']);
  181. }
  182. return $row['labels']?:'';
  183. },
  184. 'success_count', 'fail_count', 'work_total', 'worker_price'
  185. ];
  186. return [
  187. 'zh_cn_fields' => $zh_cn_fields,
  188. 'data_fields' => $data_fields
  189. ];
  190. }
  191. }