Medal.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <?php
  2. namespace app\api\controller;
  3. use app\model\Employee;
  4. use think\facade\Request;
  5. use app\model\Medal as MedalModel;
  6. use app\model\EmployeeMedal;
  7. use app\model\CustomerClue;
  8. use app\model\TalkskillComment;
  9. use app\model\TrainDoneLog;
  10. use app\model\CustomerVisitLog;
  11. use app\model\Talkskill;
  12. use app\model\UserSignLog;
  13. class Medal extends Base
  14. {
  15. /**
  16. * 勋章体系
  17. */
  18. public function index()
  19. {
  20. $token = $this->request->token;
  21. //我的勋章
  22. $w[] = ['employee_id','=',$token['employee_id']];
  23. $w[] = ['root_id','=',$token['root_org']];
  24. $res['my'] = EmployeeMedal::with(['medal'=>function($query){
  25. $query->visible(['title','remark','path'])->bind(['title','remark','path']);
  26. }])->where($w)->order('id desc')->select();
  27. $ids = $res['my']->isEmpty() ? [] : array_column($res['my']->toArray(),'medal_id');
  28. $w[] = ['main','=',1];
  29. $res['main'] = EmployeeMedal::with(['medal'=>function($query){
  30. $query->visible(['title','remark','path'])->bind(['title','remark','path']);
  31. }])->where($w)->order('id desc')->findOrEmpty();
  32. //勋章体系
  33. $root_id = $token['root_org'];
  34. $employee_id = $token['employee_id'];
  35. $all = MedalModel::with(['employeeMedal'=>function($query) use ($root_id,$employee_id){
  36. $query->where([['root_id','=',$root_id],['employee_id','=',$employee_id]]);
  37. }])->where('1=1')->order('condition asc')->select();
  38. $next = [];
  39. foreach ($all as $v) {
  40. $v->next = false;
  41. if (!in_array($v->type,$next) && $v->employeeMedal==null) {
  42. $next[] = $v->type;
  43. //获得此勋章条件
  44. $v->next = $this->medal($v->type,$v->id);
  45. }
  46. $v->is_obtain = $v->employeeMedal==null ? false : true;//是否获得
  47. $v->is_wear = ($v->is_obtain && $v->employeeMedal->main==1) ? true : false;
  48. $res['all'][$v->type][] = $v;
  49. }
  50. return json(['code' => 0, 'data' => $res, 'msg' => '获取成功']);
  51. }
  52. public function medal($type,$medal_id){
  53. $token = $this->request->token;
  54. $w1[] = ['employee_id','=',$token['employee_id']];
  55. $w1[] = ['medal_id','=',$medal_id];
  56. $w1[] = ['root_id','=',$token['root_org']];
  57. $my = EmployeeMedal::where($w1)->findOrEmpty();
  58. if(!$my->isEmpty()) return '已获得此勋章';
  59. $w[] = ['id','=',$medal_id];
  60. $info = MedalModel::where($w)->findOrEmpty();
  61. if ($type=='sign') {
  62. //累计签到次数
  63. $w2[] = ['user_id','=',$token['uid']];
  64. $w2[] = ['root_id','=',$token['root_org']];
  65. $count = UserSignLog::where($w2)->count();
  66. $day = $count==0 ? $info->condition : $info->condition-$count;
  67. $day = $day<=0 ? 1 : $day;
  68. return '再使用系统'.$day.'天可获得';
  69. }elseif($type=='talkskill'){
  70. $w2[] = ['employee_id','=',$token['employee_id']];
  71. $w2[] = ['type','=','share'];
  72. $w2[] = ['approve','=',1];
  73. $w2[] = ['root_id','=',$token['root_org']];
  74. $count = Talkskill::where($w2)->count();
  75. $day = $count==0 ? $info->condition : $info->condition-$count;
  76. $day = $day<=0 ? 1 : $day;
  77. return '在共享'.$day.'条话术可获得';
  78. }elseif ($type=='clue') {
  79. //分享获客
  80. $w2[] = ['employee_id','=',$token['employee_id']];
  81. $w2[] = ['state','<>',2];
  82. $count = CustomerClue::where($w2)->count();
  83. $day = $count==0 ? $info->condition : $info->condition-$count;
  84. $day = $day<=0 ? 1 : $day;
  85. return '再有'.$day.'个客户浏览公司内容可获得';
  86. }elseif ($type=='comment') {
  87. //答疑解惑
  88. $w2[] = ['root_id','=',$token['root_org']];
  89. $w2[] = ['uid','=',$token['uid']];
  90. $w2[] = ['approve','=',1];
  91. $count = TalkskillComment::where($w2)->count();
  92. $day = $count==0 ? $info->condition : $info->condition-$count;
  93. $day = $day<=0 ? 1 : $day;
  94. return '再回答'.$day.'条话术可获得';
  95. }elseif ($type=='class') {
  96. //课程学习
  97. $w2[] = ['from','in',[0,1]];
  98. $w2[] = ['root_id','=',$token['root_org']];
  99. $w2[] = ['done_percent','=',100];
  100. $w2[] = ['employee_id','=',$token['employee_id']];
  101. $count = TrainDoneLog::where($w2)->count();
  102. $day = $count==0 ? $info->condition : $info->condition-$count;
  103. $day = $day<=0 ? 1 : $day;
  104. return '再学习'.$day.'个课程可获得';
  105. }elseif ($type=='visit_log') {
  106. //客户跟踪
  107. $w2[] = ['employee_id','=',$token['employee_id']];
  108. $w2[] = ['state','not in',[11, '无效']];
  109. $count = CustomerVisitLog::where($w2)->count();
  110. $day = $count==0 ? $info->condition : $info->condition-$count;
  111. $day = $day<=0 ? 1 : $day;
  112. return '再跟进客户'.$day.'次可获得';
  113. }
  114. return false;
  115. }
  116. /**
  117. * 勋章详情
  118. */
  119. public function read()
  120. {
  121. $token = $this->request->token;
  122. $param = Request::only(['id'=>0]);
  123. $w[] = ['id','=',$param['id']];
  124. $info = MedalModel::where($w)->findOrEmpty();
  125. if (!$info->isEmpty()) {
  126. $w1[] = ['employee_id','=',$token['employee_id']];
  127. $w1[] = ['medal_id','=',$info->id];
  128. $w1[] = ['root_id','=',$token['root_org']];
  129. $my = EmployeeMedal::where($w1)->findOrEmpty();
  130. $info->is_obtain = !$my->isEmpty();//是否获得该勋章
  131. //已获取
  132. if ($info->is_obtain) {
  133. $info->addtime = date('y/m',strtotime($info->addtime));
  134. $info->is_wear = $my->main==1 ? true : false;//是否佩戴该勋章
  135. //第多少个获得该勋章
  136. $w2[] = ['medal_id','=',$param['id']];
  137. $w2[] = ['id','<=',$my->id];
  138. $w2[] = ['root_id','=',$token['root_org']];
  139. $info->count = EmployeeMedal::where($w2)->count();
  140. }else{
  141. $info->addtime = '';
  142. $info->is_wear = false;
  143. $info->count = 0;
  144. }
  145. }
  146. return json(['code' => 0, 'data' => $info, 'msg' => '获取成功']);
  147. }
  148. /**
  149. * 佩戴勋章
  150. */
  151. public function wear()
  152. {
  153. $token = $this->request->token;
  154. $param = Request::only(['id'=>0]);
  155. $w[] = ['id','=',$param['id']];
  156. $info = MedalModel::where($w)->findOrEmpty();
  157. if ($info->isEmpty()) {
  158. return json(['code' => 1, 'data' => '勋章不存在', 'msg' => '勋章不存在']);
  159. }
  160. $w1[] = ['employee_id','=',$token['employee_id']];
  161. $w1[] = ['medal_id','=',$info->id];
  162. $w1[] = ['root_id','=',$token['root_org']];
  163. $my = EmployeeMedal::where($w1)->findOrEmpty();
  164. if ($my->isEmpty()) {
  165. return json(['code' => 1, 'data' => '未获得此勋章', 'msg' => '未获得此勋章']);
  166. }
  167. if ($my->main==1) {
  168. return json(['code' => 1, 'data' => '已佩戴此勋章', 'msg' => '已佩戴此勋章']);
  169. }
  170. $w2[] = ['employee_id','=',$token['employee_id']];
  171. EmployeeMedal::where($w2)->update(['main'=>0]);
  172. $my->main = 1;
  173. $my->save();
  174. return json(['code' => 0, 'data' => '佩戴成功', 'msg' => '佩戴成功']);
  175. }
  176. }