MasterWorkerTeamLogic.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php
  2. namespace app\workerapi\logic;
  3. use app\adminapi\logic\master_worker_register\MasterWorkerRegisterLogic;
  4. use app\common\enum\worker\WorkerAccountLogEnum;
  5. use app\common\enum\YesNoEnum;
  6. use app\common\logic\BaseLogic;
  7. use app\common\model\bank_account\BankAccount;
  8. use app\common\model\master_worker\MasterWorker;
  9. use app\common\model\master_worker\MasterWorkerAccountLog;
  10. use app\common\model\master_worker\MasterWorkerAgree;
  11. use app\common\model\master_worker\MasterWorkerInfo;
  12. use app\common\model\master_worker\MasterWorkerTeam;
  13. use app\common\model\works\ServiceWork;
  14. use app\common\service\FileService;
  15. use app\workerapi\lists\MasterWorkerLists;
  16. use app\workerapi\lists\ServiceWorkLists;
  17. use think\Exception;
  18. use think\facade\Config;
  19. use think\facade\Db;
  20. /**
  21. * @author 林海涛
  22. * @date 2024/7/10 下午1:45
  23. */
  24. class MasterWorkerTeamLogic extends BaseLogic
  25. {
  26. public static function getDetail(int $masterWorkerId)
  27. {
  28. try {
  29. $team = MasterWorkerTeam::where('master_worker_id',$masterWorkerId)->findOrEmpty();
  30. if ($team->isEmpty()) {
  31. throw new \Exception('团队不存在');
  32. }
  33. $team = $team->toArray();
  34. $team['count_num'] = MasterWorker::where('team_id',$team['id'])->where('team_role',2)->count();
  35. return $team;
  36. } catch (\Exception $e) {
  37. self::setError($e->getMessage());
  38. return [];
  39. }
  40. }
  41. public static function addTeamMember(array $params,int $masterWorkerId)
  42. {
  43. try {
  44. $team = MasterWorkerTeam::where('master_worker_id',$masterWorkerId)->findOrEmpty();
  45. if ($team->isEmpty()) {
  46. throw new \Exception('团队不存在');
  47. }
  48. $masterWorker = \app\common\model\master_worker\MasterWorker::where('mobile',$params['mobile'])->findOrEmpty();
  49. if (!$masterWorker->isEmpty()) {
  50. throw new \Exception('该手机号已占用');
  51. }
  52. // 新增工程师 - 所属团队、租户
  53. $mwId = MasterWorkerRegisterLogic::createMasterWorker([
  54. 'mobile' => $params['mobile'],
  55. 'lon' => isset($params['lon'])?$params['lon']:0,
  56. 'lat' => isset($params['lat'])?$params['lat']:0,
  57. 'tenant_id'=>$team->tenant_id
  58. ]);
  59. MasterWorker::where('id',$mwId)->update(['team_id'=>$team->id,'team_role'=>2]);
  60. return true;
  61. } catch (\Exception $e) {
  62. self::setError($e->getMessage());
  63. return false;
  64. }
  65. }
  66. public static function getMemberList(int $masterWorkerId)
  67. {
  68. try {
  69. $team = MasterWorkerTeam::where('master_worker_id',$masterWorkerId)->findOrEmpty();
  70. if ($team->isEmpty()) {
  71. throw new \Exception('团队不存在');
  72. }
  73. return MasterWorker::where('team_id',$team['id'])->where('team_role',2)->field(['id','nickname','avatar','mobile','real_name','team_role'])->select()->toArray();
  74. } catch (\Exception $e) {
  75. self::setError($e->getMessage());
  76. return [];
  77. }
  78. }
  79. /**
  80. * 分配给团队成员
  81. * @param int $teamId
  82. * @param int $masterWorkerId
  83. * @return array
  84. */
  85. public static function allocation($params,$userInfo){
  86. Db::startTrans();
  87. try {
  88. $work = ServiceWork::findOrEmpty($params['work_id']);
  89. if($work->isEmpty()){
  90. throw new Exception('工单不存在');
  91. }
  92. if($work->work_status != 1 ){
  93. throw new \Exception('工单已被领取,不可分配');
  94. }
  95. if($work->master_worker_id == $params['master_worker_id']){
  96. throw new \Exception('分配的工程师相同');
  97. }
  98. $worker = MasterWorker::where(['id'=>$params['master_worker_id'],'team_id' =>$userInfo['team_id'],'team_role' =>2,'is_disable' =>0])->findOrEmpty();
  99. if($worker->isEmpty()){
  100. throw new \Exception('成员工程师不存在或被禁用');
  101. }
  102. if($worker->master_worker_id){
  103. MasterWorker::setWorktotal('dec',$worker->master_worker_id);
  104. }
  105. $work->master_worker_id = $params['master_worker_id'];
  106. $work->work_status = 1;
  107. $work->dispatch_time = time();
  108. MasterWorker::setWorktotal('inc',$params['master_worker_id']);
  109. $work->save();
  110. $work_log = [
  111. 'work_id'=>$work->id,
  112. 'master_worker_id'=>$work->master_worker_id,
  113. 'opera_log'=>'团队负责人['.$userInfo['user_id'].']'.$userInfo['real_name'].'于'.date('Y-m-d H:i:s',time()).'分配了工程师成员'.'编号['.$worker->worker_number.']'.$worker->real_name
  114. ];
  115. ServiceWorkerAllocateWorkerLogic::add($work_log);
  116. Db::commit();
  117. return true;
  118. }catch(\Exception $e){
  119. Db::rollback();
  120. self::setError($e->getMessage());
  121. return false;
  122. }
  123. }
  124. /**
  125. * 团队成员工单统计
  126. * @param int $teamId
  127. * @param int $masterWorkerId
  128. * @return array
  129. */
  130. public static function MemberWorkStatistics($userInfo){
  131. $lists = ServiceWork::whereIn('master_worker_id',
  132. MasterWorker::where('team_id', $userInfo['team_id'])->where('team_role', 2)->column('id')
  133. )->group('work_status')
  134. ->field('work_status, COUNT(id) as count_num')
  135. ->order('work_status asc')
  136. ->select()->toArray();
  137. $lists = array_column($lists,'count_num','work_status');
  138. $work_status_txt = array_slice(ServiceWork::WORK_STATUS_TXT, 1, 8,true);
  139. $res = [];
  140. foreach ($work_status_txt as $work_status => $name) {
  141. $res[] = [
  142. 'work_status' => $work_status,
  143. 'work_status_text' => $name,
  144. 'count_num' => $lists[$work_status]??0,
  145. ];
  146. }
  147. return $res;
  148. }
  149. /**
  150. * 团队工单统计- 当天日期的前x天范围
  151. * @param int $teamId
  152. * @param int $masterWorkerId
  153. * @return array
  154. */
  155. public static function TeamOrderStatistics($userInfo,$day_num = 7) {
  156. $start_time = strtotime(date('Y-m-d', time() - $day_num * 86400));
  157. $end_time = time();
  158. $lists = ServiceWork::whereIn('master_worker_id',
  159. MasterWorker::where('team_id', $userInfo['team_id'])->where('team_role', 2)->column('id')
  160. )->group('DATE(FROM_UNIXTIME(create_time))')
  161. ->field('DATE(FROM_UNIXTIME(create_time)) as days, COUNT(id) as count_num')
  162. ->where('create_time','between',[$start_time,$end_time])
  163. ->order('create_time desc')
  164. ->select()->toArray();
  165. return array_column($lists,'count_num','days');
  166. }
  167. }