TrainingLogic.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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\shops\ShopOrderGoods;
  14. use app\common\model\shops\ShopOrders;
  15. use app\common\model\training\TrainingCourse;
  16. use app\common\model\training\TrainingWorkerTask;
  17. use app\common\model\works\ServiceWork;
  18. use app\common\service\FileService;
  19. use app\workerapi\lists\MasterWorkerLists;
  20. use app\workerapi\lists\ServiceWorkLists;
  21. use think\Exception;
  22. use think\facade\Config;
  23. use think\facade\Db;
  24. class TrainingLogic extends BaseLogic
  25. {
  26. public static function getDetail(int $masterWorkerId)
  27. {
  28. try {
  29. //shop_goods_id INT ( 10 ) DEFAULT 0 NOT NULL COMMENT '商品id(套餐)',
  30. //shop_orders_id int(10) NOT NULL DEFAULT '0' COMMENT '购买单号id',
  31. // 检查是否已经购买套餐
  32. $sns = ShopOrders::where('worker_id',$masterWorkerId)->where('pay_status',1)->where('refund_status',0)->column('sn');
  33. $task_info = ['id'=>0];
  34. $task = TrainingWorkerTask::where('master_worker_id',$masterWorkerId)->findOrEmpty();
  35. if (!$task->isEmpty()) {
  36. $task_info = $task->toArray();
  37. ShopOrderGoods::whereIn('sn',$sns)->where('shop_goods_id',$task_info['shop_goods_id'])->find();
  38. // 0 不展示按钮 1 去学习 2 已学完
  39. $task_info['learning_status'] = $task_info['shop_orders_id']>0?($task_info['training_status'] <2?1:2):0;
  40. $task_info['bank_status'] = BankAccount::where('worker_id',$masterWorkerId)->where('audit_state',1)->value('id')?1:0;
  41. $masterWorker = MasterWorker::where('id',$masterWorkerId)->find();
  42. $task_info['category_status'] = empty($masterWorker['category_ids'])?0:1;
  43. $task_info['service_status'] = (empty($masterWorker['lon']) || empty($masterWorker['lat']))?0:1;
  44. }
  45. $task_info['free_videos'] = TrainingCourse::where('course_lock',1)->select()->toArray();
  46. return $task_info;
  47. } catch (\Exception $e) {
  48. self::setError($e->getMessage());
  49. return [];
  50. }
  51. }
  52. public static function addTeamMember(array $params,int $masterWorkerId)
  53. {
  54. try {
  55. $team = MasterWorkerTeam::where('master_worker_id',$masterWorkerId)->findOrEmpty();
  56. if ($team->isEmpty()) {
  57. throw new \Exception('团队不存在');
  58. }
  59. $masterWorker = \app\common\model\master_worker\MasterWorker::where('mobile',$params['mobile'])->findOrEmpty();
  60. if (!$masterWorker->isEmpty()) {
  61. throw new \Exception('该手机号已占用');
  62. }
  63. // 新增工程师
  64. $mwId = MasterWorkerRegisterLogic::createMasterWorker([
  65. 'mobile' => $params['mobile'],
  66. 'lon' => isset($params['lon'])?$params['lon']:0,
  67. 'lat' => isset($params['lat'])?$params['lat']:0,
  68. ]);
  69. MasterWorker::where('id',$mwId)->update(['team_id'=>$team->id,'team_role'=>2]);
  70. return true;
  71. } catch (\Exception $e) {
  72. self::setError($e->getMessage());
  73. return false;
  74. }
  75. }
  76. public static function getMemberList(int $masterWorkerId)
  77. {
  78. try {
  79. $team = MasterWorkerTeam::where('master_worker_id',$masterWorkerId)->findOrEmpty();
  80. if ($team->isEmpty()) {
  81. throw new \Exception('团队不存在');
  82. }
  83. return MasterWorker::where('team_id',$team['id'])->where('team_role',2)->field(['id','nickname','avatar','mobile','real_name','team_role'])->select()->toArray();
  84. } catch (\Exception $e) {
  85. self::setError($e->getMessage());
  86. return [];
  87. }
  88. }
  89. /**
  90. * 分配给团队成员
  91. * @param int $teamId
  92. * @param int $masterWorkerId
  93. * @return array
  94. */
  95. public static function allocation($params,$userInfo){
  96. Db::startTrans();
  97. try {
  98. $work = ServiceWork::findOrEmpty($params['work_id']);
  99. if($work->isEmpty()){
  100. throw new Exception('工单不存在');
  101. }
  102. if($work->work_status != 1 ){
  103. throw new \Exception('工单已被领取,不可分配');
  104. }
  105. if($work->master_worker_id == $params['master_worker_id']){
  106. throw new \Exception('分配的工程师相同');
  107. }
  108. $worker = MasterWorker::where(['id'=>$params['master_worker_id'],'team_id' =>$userInfo['team_id'],'team_role' =>2,'is_disable' =>0])->findOrEmpty();
  109. if($worker->isEmpty()){
  110. throw new \Exception('成员工程师不存在或被禁用');
  111. }
  112. if($worker->master_worker_id){
  113. MasterWorker::setWorktotal('dec',$worker->master_worker_id);
  114. }
  115. $work->master_worker_id = $params['master_worker_id'];
  116. $work->work_status = 1;
  117. $work->dispatch_time = time();
  118. MasterWorker::setWorktotal('inc',$params['master_worker_id']);
  119. $work->save();
  120. $work_log = [
  121. 'work_id'=>$work->id,
  122. 'master_worker_id'=>$work->master_worker_id,
  123. 'opera_log'=>'团队负责人['.$userInfo['user_id'].']'.$userInfo['real_name'].'于'.date('Y-m-d H:i:s',time()).'分配了工程师成员'.'编号['.$worker->worker_number.']'.$worker->real_name
  124. ];
  125. ServiceWorkerAllocateWorkerLogic::add($work_log);
  126. Db::commit();
  127. return true;
  128. }catch(\Exception $e){
  129. Db::rollback();
  130. self::setError($e->getMessage());
  131. return false;
  132. }
  133. }
  134. /**
  135. * 团队成员工单统计
  136. * @param int $teamId
  137. * @param int $masterWorkerId
  138. * @return array
  139. */
  140. public static function MemberWorkStatistics($userInfo){
  141. $lists = ServiceWork::whereIn('master_worker_id',
  142. MasterWorker::where('team_id', $userInfo['team_id'])->where('team_role', 2)->column('id')
  143. )->group('work_status')
  144. ->field('work_status, COUNT(id) as count_num')
  145. ->order('work_status asc')
  146. ->select()->toArray();
  147. $lists = array_column($lists,'count_num','work_status');
  148. $work_status_txt = array_slice(ServiceWork::WORK_STATUS_TXT, 1, 8,true);
  149. $res = [];
  150. foreach ($work_status_txt as $work_status => $name) {
  151. $res[] = [
  152. 'work_status' => $work_status,
  153. 'work_status_text' => $name,
  154. 'count_num' => $lists[$work_status]??0,
  155. ];
  156. }
  157. return $res;
  158. }
  159. }