123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192 |
- <?php
- namespace app\api\controller;
- use app\model\Employee;
- use think\facade\Request;
- use app\model\Medal as MedalModel;
- use app\model\EmployeeMedal;
- use app\model\CustomerClue;
- use app\model\TalkskillComment;
- use app\model\TrainDoneLog;
- use app\model\CustomerVisitLog;
- use app\model\Talkskill;
- use app\model\UserSignLog;
- class Medal extends Base
- {
- /**
- * 勋章体系
- */
- public function index()
- {
- $token = $this->request->token;
- //我的勋章
- $w[] = ['employee_id','=',$token['employee_id']];
- $w[] = ['root_id','=',$token['root_org']];
- $res['my'] = EmployeeMedal::with(['medal'=>function($query){
- $query->visible(['title','remark','path'])->bind(['title','remark','path']);
- }])->where($w)->order('id desc')->select();
- $ids = $res['my']->isEmpty() ? [] : array_column($res['my']->toArray(),'medal_id');
- $w[] = ['main','=',1];
- $res['main'] = EmployeeMedal::with(['medal'=>function($query){
- $query->visible(['title','remark','path'])->bind(['title','remark','path']);
- }])->where($w)->order('id desc')->findOrEmpty();
- //勋章体系
- $root_id = $token['root_org'];
- $employee_id = $token['employee_id'];
- $all = MedalModel::with(['employeeMedal'=>function($query) use ($root_id,$employee_id){
- $query->where([['root_id','=',$root_id],['employee_id','=',$employee_id]]);
- }])->where('1=1')->order('condition asc')->select();
- $next = [];
- foreach ($all as $v) {
- $v->next = false;
- if (!in_array($v->type,$next) && $v->employeeMedal==null) {
- $next[] = $v->type;
- //获得此勋章条件
- $v->next = $this->medal($v->type,$v->id);
- }
- $v->is_obtain = $v->employeeMedal==null ? false : true;//是否获得
- $v->is_wear = ($v->is_obtain && $v->employeeMedal->main==1) ? true : false;
- $res['all'][$v->type][] = $v;
- }
- return json(['code' => 0, 'data' => $res, 'msg' => '获取成功']);
- }
- public function medal($type,$medal_id){
- $token = $this->request->token;
- $w1[] = ['employee_id','=',$token['employee_id']];
- $w1[] = ['medal_id','=',$medal_id];
- $w1[] = ['root_id','=',$token['root_org']];
- $my = EmployeeMedal::where($w1)->findOrEmpty();
- if(!$my->isEmpty()) return '已获得此勋章';
- $w[] = ['id','=',$medal_id];
- $info = MedalModel::where($w)->findOrEmpty();
- if ($type=='sign') {
- //累计签到次数
- $w2[] = ['user_id','=',$token['uid']];
- $w2[] = ['root_id','=',$token['root_org']];
- $count = UserSignLog::where($w2)->count();
- $day = $count==0 ? $info->condition : $info->condition-$count;
- $day = $day<=0 ? 1 : $day;
- return '再使用系统'.$day.'天可获得';
- }elseif($type=='talkskill'){
- $w2[] = ['employee_id','=',$token['employee_id']];
- $w2[] = ['type','=','share'];
- $w2[] = ['approve','=',1];
- $w2[] = ['root_id','=',$token['root_org']];
- $count = Talkskill::where($w2)->count();
- $day = $count==0 ? $info->condition : $info->condition-$count;
- $day = $day<=0 ? 1 : $day;
- return '在共享'.$day.'条话术可获得';
- }elseif ($type=='clue') {
- //分享获客
- $w2[] = ['employee_id','=',$token['employee_id']];
- $w2[] = ['state','<>',2];
- $count = CustomerClue::where($w2)->count();
- $day = $count==0 ? $info->condition : $info->condition-$count;
- $day = $day<=0 ? 1 : $day;
- return '再有'.$day.'个客户浏览公司内容可获得';
- }elseif ($type=='comment') {
- //答疑解惑
- $w2[] = ['root_id','=',$token['root_org']];
- $w2[] = ['uid','=',$token['uid']];
- $w2[] = ['approve','=',1];
- $count = TalkskillComment::where($w2)->count();
- $day = $count==0 ? $info->condition : $info->condition-$count;
- $day = $day<=0 ? 1 : $day;
- return '再回答'.$day.'条话术可获得';
- }elseif ($type=='class') {
- //课程学习
- $w2[] = ['from','in',[0,1]];
- $w2[] = ['root_id','=',$token['root_org']];
- $w2[] = ['done_percent','=',100];
- $w2[] = ['employee_id','=',$token['employee_id']];
- $count = TrainDoneLog::where($w2)->count();
- $day = $count==0 ? $info->condition : $info->condition-$count;
- $day = $day<=0 ? 1 : $day;
- return '再学习'.$day.'个课程可获得';
- }elseif ($type=='visit_log') {
- //客户跟踪
- $w2[] = ['employee_id','=',$token['employee_id']];
- $w2[] = ['state','not in',[11, '无效']];
- $count = CustomerVisitLog::where($w2)->count();
- $day = $count==0 ? $info->condition : $info->condition-$count;
- $day = $day<=0 ? 1 : $day;
- return '再跟进客户'.$day.'次可获得';
- }
- return false;
- }
- /**
- * 勋章详情
- */
- public function read()
- {
- $token = $this->request->token;
- $param = Request::only(['id'=>0]);
- $w[] = ['id','=',$param['id']];
- $info = MedalModel::where($w)->findOrEmpty();
- if (!$info->isEmpty()) {
- $w1[] = ['employee_id','=',$token['employee_id']];
- $w1[] = ['medal_id','=',$info->id];
- $w1[] = ['root_id','=',$token['root_org']];
- $my = EmployeeMedal::where($w1)->findOrEmpty();
- $info->is_obtain = !$my->isEmpty();//是否获得该勋章
- //已获取
- if ($info->is_obtain) {
- $info->addtime = date('y/m',strtotime($info->addtime));
- $info->is_wear = $my->main==1 ? true : false;//是否佩戴该勋章
- //第多少个获得该勋章
- $w2[] = ['medal_id','=',$param['id']];
- $w2[] = ['id','<=',$my->id];
- $w2[] = ['root_id','=',$token['root_org']];
- $info->count = EmployeeMedal::where($w2)->count();
- }else{
- $info->addtime = '';
- $info->is_wear = false;
- $info->count = 0;
- }
- }
- return json(['code' => 0, 'data' => $info, 'msg' => '获取成功']);
- }
- /**
- * 佩戴勋章
- */
- public function wear()
- {
- $token = $this->request->token;
- $param = Request::only(['id'=>0]);
- $w[] = ['id','=',$param['id']];
- $info = MedalModel::where($w)->findOrEmpty();
- if ($info->isEmpty()) {
- return json(['code' => 1, 'data' => '勋章不存在', 'msg' => '勋章不存在']);
- }
- $w1[] = ['employee_id','=',$token['employee_id']];
- $w1[] = ['medal_id','=',$info->id];
- $w1[] = ['root_id','=',$token['root_org']];
- $my = EmployeeMedal::where($w1)->findOrEmpty();
- if ($my->isEmpty()) {
- return json(['code' => 1, 'data' => '未获得此勋章', 'msg' => '未获得此勋章']);
- }
- if ($my->main==1) {
- return json(['code' => 1, 'data' => '已佩戴此勋章', 'msg' => '已佩戴此勋章']);
- }
- $w2[] = ['employee_id','=',$token['employee_id']];
- EmployeeMedal::where($w2)->update(['main'=>0]);
- $my->main = 1;
- $my->save();
- return json(['code' => 0, 'data' => '佩戴成功', 'msg' => '佩戴成功']);
- }
- }
|