Project.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. <?php
  2. namespace App\Module;
  3. use DB;
  4. /**
  5. * Class Project
  6. * @package App\Module
  7. */
  8. class Project
  9. {
  10. /**
  11. * 是否在项目里
  12. * @param int $projectid
  13. * @param string $username
  14. * @param bool $isowner
  15. * @return array
  16. */
  17. public static function inThe($projectid, $username, $isowner = false)
  18. {
  19. $whereArray = [
  20. 'type' => '成员',
  21. 'projectid' => $projectid,
  22. 'username' => $username,
  23. ];
  24. if ($isowner) {
  25. $whereArray['isowner'] = 1;
  26. }
  27. $row = Base::DBC2A(DB::table('project_users')->select(['isowner', 'indate'])->where($whereArray)->first());
  28. if (empty($row)) {
  29. return Base::retError('你不在项目成员内!');
  30. } else {
  31. return Base::retSuccess('你在项目内', $row);
  32. }
  33. }
  34. /**
  35. * 更新项目(complete、unfinished)
  36. * @param int $projectid
  37. */
  38. public static function updateNum($projectid)
  39. {
  40. if ($projectid > 0) {
  41. DB::table('project_lists')->where('id', $projectid)->update([
  42. 'unfinished' => DB::table('project_task')->where('projectid', $projectid)->where('complete', 0)->where('delete', 0)->count(),
  43. 'complete' => DB::table('project_task')->where('projectid', $projectid)->where('complete', 1)->where('delete', 0)->count(),
  44. ]);
  45. }
  46. }
  47. /**
  48. * 任务负责人组
  49. * @param $task
  50. * @return array
  51. */
  52. public static function taskPersons($task)
  53. {
  54. $array = [];
  55. $array[] = Users::username2basic($task['username']);
  56. $persons = [$task['username']];
  57. $subtask = Base::string2array($task['subtask']);
  58. foreach ($subtask AS $item) {
  59. if ($item['uname'] && !in_array($item['uname'], $persons)) {
  60. $persons[] = $item['uname'];
  61. $basic = Users::username2basic($item['uname']);
  62. if ($basic) {
  63. $array[] = $basic;
  64. }
  65. }
  66. }
  67. return $array;
  68. }
  69. /**
  70. * 是否负责人(任务负责人、子任务负责人)
  71. * @param $task
  72. * @param $username
  73. * @return bool
  74. */
  75. public static function isPersons($task, $username)
  76. {
  77. $persons = [$task['username']];
  78. $subtask = Base::string2array($task['subtask']);
  79. foreach ($subtask AS $item) {
  80. if ($item['uname'] && !in_array($item['uname'], $persons)) {
  81. $persons[] = $item['uname'];
  82. }
  83. }
  84. return in_array($username, $persons) ? true : false;
  85. }
  86. /**
  87. * 任务是否过期
  88. * @param array $task
  89. * @return int
  90. */
  91. public static function taskIsOverdue($task)
  92. {
  93. return $task['complete'] == 0 && $task['enddate'] > 0 && $task['enddate'] <= Base::time() ? 1 : 0;
  94. }
  95. /**
  96. * 过期的排在前
  97. * @param array $taskLists
  98. * @return mixed
  99. */
  100. public static function sortTask($taskLists)
  101. {
  102. $inOrder = [];
  103. foreach ($taskLists as $key => $oitem) {
  104. $inOrder[$key] = $oitem['overdue'] ? -1 : $key;
  105. }
  106. array_multisort($inOrder, SORT_ASC, $taskLists);
  107. return $taskLists;
  108. }
  109. /**
  110. * 获取与任务有关系的用户(关注的、在项目里的、负责人、创建者)
  111. * @param $taskId
  112. * @return array
  113. */
  114. public static function taskSomeUsers($taskId)
  115. {
  116. $taskDeatil = Base::DBC2A(DB::table('project_task')->select(['follower', 'subtask', 'createuser', 'username', 'projectid'])->where('id', $taskId)->first());
  117. if (empty($taskDeatil)) {
  118. return [];
  119. }
  120. //关注的用户
  121. $userArray = Base::string2array($taskDeatil['follower']);
  122. //子任务负责人
  123. $subtask = Base::string2array($taskDeatil['subtask']);
  124. foreach ($subtask AS $item) {
  125. $userArray[] = $item['uname'];
  126. }
  127. //创建者
  128. $userArray[] = $taskDeatil['createuser'];
  129. //负责人
  130. $userArray[] = $taskDeatil['username'];
  131. //在项目里的用户
  132. if ($taskDeatil['projectid'] > 0) {
  133. $tempLists = Base::DBC2A(DB::table('project_users')->select(['username'])->where(['projectid' => $taskDeatil['projectid'], 'type' => '成员' ])->get());
  134. foreach ($tempLists AS $item) {
  135. $userArray[] = $item['username'];
  136. }
  137. }
  138. //
  139. return array_values(array_filter(array_unique($userArray)));
  140. }
  141. /**
  142. * 项目(任务)权限
  143. * @param $type
  144. * @param $projectid
  145. * @param int $taskid
  146. * @param string $username
  147. * @return array|mixed
  148. */
  149. public static function role($type, $projectid, $taskid = 0, $username = '')
  150. {
  151. if (empty($username)) {
  152. $user = Users::authE();
  153. if (Base::isError($user)) {
  154. return $user;
  155. } else {
  156. $user = $user['data'];
  157. }
  158. $username = $user['username'];
  159. }
  160. //
  161. $project = Base::DBC2A(DB::table('project_lists')->select(['username', 'title', 'setting'])->where('id', $projectid)->where('delete', 0)->first());
  162. if (empty($project)) {
  163. return Base::retError('项目不存在或已被删除!');
  164. }
  165. // 项目负责人最高权限
  166. if ($project['username'] == $username) {
  167. unset($project['setting']);
  168. return Base::retSuccess('success', $project);
  169. }
  170. //
  171. $setting = Base::string2array($project['setting']);
  172. foreach (['edit_role', 'complete_role', 'archived_role', 'del_role'] AS $key) {
  173. $setting[$key] = is_array($setting[$key]) ? $setting[$key] : ['__', 'owner'];
  174. }
  175. $setting['add_role'] = is_array($setting['add_role']) ? $setting['add_role'] : ['__', 'member'];
  176. //
  177. $role = $setting[$type];
  178. if (empty($role) || !is_array($role)) {
  179. return Base::retError('操作权限不足!');
  180. }
  181. if (in_array('member', $role)) {
  182. $inRes = Project::inThe($projectid, $username);
  183. if (Base::isError($inRes)) {
  184. return $inRes;
  185. }
  186. } elseif (in_array('owner', $role)) {
  187. if (empty($taskid)) {
  188. return Base::retError('任务不存在!');
  189. }
  190. $task = Base::DBC2A(DB::table('project_task')
  191. ->select(['username'])
  192. ->where([
  193. ['delete', '=', 0],
  194. ['id', '=', $taskid],
  195. ])
  196. ->first());
  197. if (empty($task)) {
  198. return Base::retError('任务不存在!');
  199. }
  200. if ($task['username'] != $username) {
  201. return Base::retError('此操作只允许项目管理员或者任务负责人!');
  202. }
  203. } else {
  204. return Base::retError('此操作仅限项目负责人!');
  205. }
  206. //
  207. unset($project['setting']);
  208. return Base::retSuccess('success', $project);
  209. }
  210. }