TaskRepository = $TaskRepository; $this->TaskLogRepository = $TaskLogRepository; $this->MemberPointRepository = $MemberPointRepository; $this->MemberHandselRepository = $MemberHandselRepository; } public function doTask($id, $uid = 0, $utype = 0) { $task = $this->TaskRepository->getTaskById($id); if (!$task) { return ['code'=>0,'info'=>'没有找到对应的任务','data'=>null]; } if (!$uid) { if (auth('web-member')->user()) { $uid = auth('web-member')->user()->id; $utype = auth('web-member')->user()->utype; } if (auth('web-company')->user()) { $uid = auth('web-company')->user()->id; $utype= auth('web-company')->user()->utype; } } if ($task->once==1) { //查询是否完成过该任务--单次任务 $log = $this->TaskLogRepository->getTaskLog($uid, $task, $utype); if (!$log) { return $this->addTask($task, $uid, $utype); } else { return ['code'=>0,'info'=>'已经完成过这个任务了','data'=>null]; } } else { //查询今天完成过该任务的次数--日常任务。 $taskLog = $this->TaskLogRepository->getTaskLogCount($uid, $task->id, $utype); if ($taskLog<$task->times || $task->times==-1) { return $this->addTask($task, $uid, $utype); } else { return ['code'=>0,'info'=>'完成任务超出限额','data'=>null]; } } } private function addTask($task, $uid, $utype) { DB::beginTransaction(); $p2 = intval($task->points); try { $taskLog = $this->TaskLogRepository->addNew(['uid'=>$uid,'task_id'=>$task->id,'utype'=>$utype,'points'=>$task->points,'once'=>$task->once]); if (!$taskLog) { throw new \Exception('操作失败--task_log'); } $MembersPoint = $this->MemberPointRepository->getPointsOne($uid, $utype); if ($MembersPoint) { //更新数据。 $p1 = intval($MembersPoint->points); $points = $p1+$p2; $MembersP = $this->MemberPointRepository->updateNew($uid, $utype, ['points'=>$points]); } else { //新增数据。 $MembersP = $this->MemberPointRepository->addNew(['uid'=>$uid,'utype'=>$utype,'points'=>$task->points]); } if (!$MembersP) { throw new \Exception('操作失败--members_points'); } $MembersH = $this->MemberHandselRepository->addNew(['uid'=>$uid,'utype'=>$utype,'htype'=>$task->t_alias,'htype_cn'=>$task->title,'operate'=>1,'points'=>$task->points]); if (!$MembersH) { throw new \Exception('操作失败--members_handsel'); } DB::commit(); return ['code'=>1,'info'=>'操作成功','data'=>['points'=>$p2]]; } catch (\Exception $e) { DB::rollback(); return ['code'=>0,'info'=>$e->getMessage(),'data'=>null]; } } public function checkTask($id) { $task = $this->TaskRepository->getTaskById($id); if ($task) { return true; } return false; } public function doUserTask($user, $id) { $task = $this->TaskRepository->getTaskById($id); if (!$task) { return ['code'=>0,'info'=>'没有找到对应的任务','data'=>null]; } if ($task->once) { //一次性任务 $log = $this->TaskLogRepository->getTaskLog($user->uid, $task, $user->utype); if (!$log) { return $this->addTask($task, $user->uid, $user->utype); } else { return ['code'=>0,'info'=>'已经完成过这个任务了','data'=>null]; } } else { //日常任务 $taskLog = $this->TaskLogRepository->getTaskLogCount($user->uid, $task->id, $user->utype); if ($taskLog<$task->times || $task->times==-1) { return $this->addTask($task, $user->uid, $user->utype); } else { return ['code'=>0,'info'=>'完成任务超出限额','data'=>null]; } } } }