ProjectController.php 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845
  1. <?php
  2. namespace App\Http\Controllers\Api;
  3. use App\Http\Controllers\Controller;
  4. use App\Module\Base;
  5. use App\Module\Project;
  6. use App\Module\Users;
  7. use DB;
  8. use Request;
  9. use Session;
  10. /**
  11. * @apiDefine project
  12. *
  13. * 项目
  14. */
  15. class ProjectController extends Controller
  16. {
  17. public function __invoke($method, $action = '')
  18. {
  19. $app = $method ? $method : 'main';
  20. if ($action) {
  21. $app .= "__" . $action;
  22. }
  23. return (method_exists($this, $app)) ? $this->$app() : Base::ajaxError("404 not found (" . str_replace("__", "/", $app) . ").");
  24. }
  25. /**
  26. * 项目列表
  27. *
  28. * @apiParam {String} act 类型
  29. * - join: 加入的项目(默认)
  30. * - favor: 收藏的项目
  31. * - manage: 管理的项目
  32. * @apiParam {Number} [page] 当前页,默认:1
  33. * @apiParam {Number} [pagesize] 每页显示数量,默认:20,最大:100
  34. */
  35. public function lists()
  36. {
  37. $user = Users::authE();
  38. if (Base::isError($user)) {
  39. return $user;
  40. } else {
  41. $user = $user['data'];
  42. }
  43. //
  44. $whereArray = [];
  45. $whereArray[] = ['project_lists.delete', '=', 0];
  46. $whereArray[] = ['project_users.username', '=', $user['username']];
  47. switch (Request::input('act')) {
  48. case "favor": {
  49. $whereArray[] = ['project_users.type', '=', '收藏'];
  50. break;
  51. }
  52. case "manage": {
  53. $whereArray[] = ['project_users.type', '=', '成员'];
  54. $whereArray[] = ['project_users.isowner', '=', 1];
  55. break;
  56. }
  57. default: {
  58. $whereArray[] = ['project_users.type', '=', '成员'];
  59. break;
  60. }
  61. }
  62. $lists = DB::table('project_lists')
  63. ->join('project_users', 'project_lists.id', '=', 'project_users.projectid')
  64. ->select(['project_lists.*', 'project_users.isowner', 'project_users.indate as uindate'])
  65. ->where($whereArray)
  66. ->orderByDesc('project_lists.id')->paginate(Min(Max(Base::nullShow(Request::input('pagesize'), 10), 1), 100));
  67. $lists = Base::getPageList($lists);
  68. if ($lists['total'] == 0) {
  69. return Base::retError('未找到任何相关的项目');
  70. }
  71. return Base::retSuccess('success', $lists);
  72. }
  73. /**
  74. * 项目详情
  75. *
  76. * @apiParam {Number} projectid 项目ID
  77. */
  78. public function detail()
  79. {
  80. $user = Users::authE();
  81. if (Base::isError($user)) {
  82. return $user;
  83. } else {
  84. $user = $user['data'];
  85. }
  86. //
  87. $projectid = trim(Request::input('projectid'));
  88. $projectDetail = Base::DBC2A(DB::table('project_lists')->where('id', $projectid)->where('delete', 0)->first());
  89. if (empty($projectDetail)) {
  90. return Base::retError('项目不存在或已被删除!');
  91. }
  92. $inRes = Project::inThe($projectid, $user['username']);
  93. if (Base::isError($inRes)) {
  94. return $inRes;
  95. }
  96. //子分类
  97. $label = Base::DBC2A(DB::table('project_label')->where('projectid', $projectid)->orderBy('inorder')->orderByDesc('id')->get());
  98. //任务
  99. $task = Base::DBC2A(DB::table('project_task')->where([ 'projectid' => $projectid, 'delete' => 0, 'complete' => 0 ])->orderBy('level')->orderBy('id')->get());
  100. //任务归类
  101. foreach ($label AS $index => $temp) {
  102. $taskLists = [];
  103. foreach ($task AS $info) {
  104. if ($temp['id'] == $info['labelid']) {
  105. $info['overdue'] = Project::taskIsOverdue($info);
  106. $taskLists[] = array_merge($info, Users::username2basic($info['username']));
  107. }
  108. }
  109. $label[$index]['taskLists'] = Project::sortTask($taskLists);
  110. }
  111. //
  112. return Base::retSuccess('success', [
  113. 'project' => $projectDetail,
  114. 'label' => $label,
  115. ]);
  116. }
  117. /**
  118. * 添加项目
  119. *
  120. * @apiParam {String} title 项目名称
  121. * @apiParam {Array} labels 流程,格式[流程1, 流程2]
  122. */
  123. public function add()
  124. {
  125. $user = Users::authE();
  126. if (Base::isError($user)) {
  127. return $user;
  128. } else {
  129. $user = $user['data'];
  130. }
  131. //项目名称
  132. $title = trim(Request::input('title'));
  133. if (mb_strlen($title) < 2) {
  134. return Base::retError('项目名称不可以少于2个字!');
  135. } elseif (mb_strlen($title) > 32) {
  136. return Base::retError('项目名称最多只能设置32个字!');
  137. }
  138. //流程
  139. $labels = Request::input('labels');
  140. if (!is_array($labels)) $labels = [];
  141. $insertLabels = [];
  142. $inorder = 0;
  143. foreach ($labels AS $label) {
  144. $label = trim($label);
  145. if ($label) {
  146. $insertLabels[] = [
  147. 'title' => $label,
  148. 'inorder' => $inorder++,
  149. ];
  150. }
  151. }
  152. if (empty($insertLabels)) {
  153. $insertLabels[] = [
  154. 'title' => '默认',
  155. 'inorder' => 0,
  156. ];
  157. }
  158. //开始创建
  159. $projectid = DB::table('project_lists')->insertGetId([
  160. 'title' => $title,
  161. 'username' => $user['username'],
  162. 'createuser' => $user['username'],
  163. 'indate' => Base::time()
  164. ]);
  165. if ($projectid) {
  166. foreach ($insertLabels AS $key => $label) {
  167. $insertLabels[$key]['projectid'] = $projectid;
  168. }
  169. DB::table('project_label')->insert($insertLabels);
  170. DB::table('project_log')->insert([
  171. 'type' => '日志',
  172. 'projectid' => $projectid,
  173. 'username' => $user['username'],
  174. 'detail' => '创建项目',
  175. 'indate' => Base::time()
  176. ]);
  177. DB::table('project_users')->insert([
  178. 'type' => '成员',
  179. 'projectid' => $projectid,
  180. 'isowner' => 1,
  181. 'username' => $user['username'],
  182. 'indate' => Base::time()
  183. ]);
  184. return Base::retSuccess('添加成功!');
  185. } else {
  186. return Base::retError('添加失败!');
  187. }
  188. }
  189. /**
  190. * 收藏项目
  191. *
  192. * @apiParam {String} act 类型
  193. * - cancel: 取消收藏
  194. * - else: 添加收藏
  195. * @apiParam {Number} projectid 项目ID
  196. *
  197. * @throws \Throwable
  198. */
  199. public function favor()
  200. {
  201. $user = Users::authE();
  202. if (Base::isError($user)) {
  203. return $user;
  204. } else {
  205. $user = $user['data'];
  206. }
  207. //
  208. $projectid = trim(Request::input('projectid'));
  209. $projectDetail = Base::DBC2A(DB::table('project_lists')->where('id', $projectid)->where('delete', 0)->first());
  210. if (empty($projectDetail)) {
  211. return Base::retError('项目不存在或已被删除!');
  212. }
  213. return DB::transaction(function () use ($projectDetail, $user) {
  214. switch (Request::input('act')) {
  215. case 'cancel': {
  216. if (DB::table('project_users')->where([
  217. 'type' => '收藏',
  218. 'projectid' => $projectDetail['id'],
  219. 'username' => $user['username'],
  220. ])->delete()) {
  221. DB::table('project_log')->insert([
  222. 'type' => '日志',
  223. 'projectid' => $projectDetail['id'],
  224. 'username' => $user['username'],
  225. 'detail' => '取消收藏',
  226. 'indate' => Base::time()
  227. ]);
  228. return Base::retSuccess('取消成功');
  229. }
  230. return Base::retSuccess('已取消');
  231. }
  232. default: {
  233. $row = Base::DBC2A(DB::table('project_users')->where([
  234. 'type' => '收藏',
  235. 'projectid' => $projectDetail['id'],
  236. 'username' => $user['username'],
  237. ])->lockForUpdate()->first());
  238. if (empty($row)) {
  239. DB::table('project_users')->insert([
  240. 'type' => '收藏',
  241. 'projectid' => $projectDetail['id'],
  242. 'isowner' => $projectDetail['username'] == $user['username'] ? 1 : 0,
  243. 'username' => $user['username'],
  244. 'indate' => Base::time()
  245. ]);
  246. DB::table('project_log')->insert([
  247. 'type' => '日志',
  248. 'projectid' => $projectDetail['id'],
  249. 'username' => $user['username'],
  250. 'detail' => '收藏项目',
  251. 'indate' => Base::time()
  252. ]);
  253. return Base::retSuccess('收藏成功');
  254. }
  255. return Base::retSuccess('已收藏');
  256. }
  257. }
  258. });
  259. }
  260. /**
  261. * 重命名项目
  262. *
  263. * @apiParam {Number} projectid 项目ID
  264. * @apiParam {String} title 项目新名称
  265. */
  266. public function rename()
  267. {
  268. $user = Users::authE();
  269. if (Base::isError($user)) {
  270. return $user;
  271. } else {
  272. $user = $user['data'];
  273. }
  274. //
  275. $projectid = trim(Request::input('projectid'));
  276. $projectDetail = Base::DBC2A(DB::table('project_lists')->where('id', $projectid)->where('delete', 0)->first());
  277. if (empty($projectDetail)) {
  278. return Base::retError('项目不存在或已被删除!');
  279. }
  280. if ($projectDetail['username'] != $user['username']) {
  281. return Base::retError('你不是项目负责人!');
  282. }
  283. //
  284. $title = trim(Request::input('title'));
  285. if (mb_strlen($title) < 2) {
  286. return Base::retError('项目名称不可以少于2个字!');
  287. } elseif (mb_strlen($title) > 32) {
  288. return Base::retError('项目名称最多只能设置32个字!');
  289. }
  290. //
  291. DB::table('project_lists')->where('id', $projectDetail['id'])->update([
  292. 'title' => $title
  293. ]);
  294. DB::table('project_log')->insert([
  295. 'type' => '日志',
  296. 'projectid' => $projectDetail['id'],
  297. 'username' => $user['username'],
  298. 'detail' => '【' . $projectDetail['title'] . '】重命名【' . $title . '】',
  299. 'indate' => Base::time()
  300. ]);
  301. //
  302. return Base::retSuccess('修改成功');
  303. }
  304. /**
  305. * 移交项目
  306. *
  307. * @apiParam {Number} projectid 项目ID
  308. * @apiParam {String} username 项目新负责人用户名
  309. *
  310. * @throws \Throwable
  311. */
  312. public function transfer()
  313. {
  314. $user = Users::authE();
  315. if (Base::isError($user)) {
  316. return $user;
  317. } else {
  318. $user = $user['data'];
  319. }
  320. //
  321. $projectid = trim(Request::input('projectid'));
  322. $projectDetail = Base::DBC2A(DB::table('project_lists')->where('id', $projectid)->where('delete', 0)->first());
  323. if (empty($projectDetail)) {
  324. return Base::retError('项目不存在或已被删除!');
  325. }
  326. if ($projectDetail['username'] != $user['username']) {
  327. return Base::retError('你不是项目负责人!');
  328. }
  329. //
  330. $username = trim(Request::input('username'));
  331. if ($username == $projectDetail['username']) {
  332. return Base::retError('你已是项目负责人!');
  333. }
  334. $count = DB::table('users')->where('username', $username)->count();
  335. if ($count <= 0) {
  336. return Base::retError('成员用户名(' . $username . ')不存在!');
  337. }
  338. //判断是否已在项目成员内
  339. $inRes = Project::inThe($projectDetail['id'], $username);
  340. if (Base::isError($inRes)) {
  341. DB::table('project_users')->insert([
  342. 'type' => '成员',
  343. 'projectid' => $projectDetail['id'],
  344. 'isowner' => 0,
  345. 'username' => $username,
  346. 'indate' => Base::time()
  347. ]);
  348. DB::table('project_log')->insert([
  349. 'type' => '日志',
  350. 'projectid' => $projectDetail['id'],
  351. 'username' => $username,
  352. 'detail' => '移交项目,自动加入项目',
  353. 'indate' => Base::time()
  354. ]);
  355. }
  356. //开始移交
  357. return DB::transaction(function () use ($user, $username, $projectDetail) {
  358. DB::table('project_lists')->where('id', $projectDetail['id'])->update([
  359. 'username' => $username
  360. ]);
  361. DB::table('project_log')->insert([
  362. 'type' => '日志',
  363. 'projectid' => $projectDetail['id'],
  364. 'username' => $user['username'],
  365. 'detail' => '【' . $projectDetail['username'] . '】移交给【' . $username . '】',
  366. 'indate' => Base::time()
  367. ]);
  368. DB::table('project_users')->where([
  369. 'projectid' => $projectDetail['id'],
  370. 'username' => $projectDetail['username'],
  371. ])->update([
  372. 'isowner' => 0
  373. ]);
  374. DB::table('project_users')->where([
  375. 'projectid' => $projectDetail['id'],
  376. 'username' => $username,
  377. ])->update([
  378. 'isowner' => 1
  379. ]);
  380. return Base::retSuccess('移交成功');
  381. });
  382. }
  383. /**
  384. * 删除项目
  385. *
  386. * @apiParam {Number} projectid 项目ID
  387. */
  388. public function delete()
  389. {
  390. $user = Users::authE();
  391. if (Base::isError($user)) {
  392. return $user;
  393. } else {
  394. $user = $user['data'];
  395. }
  396. //
  397. $projectid = trim(Request::input('projectid'));
  398. $projectDetail = Base::DBC2A(DB::table('project_lists')->where('id', $projectid)->where('delete', 0)->first());
  399. if (empty($projectDetail)) {
  400. return Base::retError('项目不存在或已被删除!');
  401. }
  402. if ($projectDetail['username'] != $user['username']) {
  403. return Base::retError('你不是项目负责人!');
  404. }
  405. //
  406. DB::table('project_lists')->where('id', $projectDetail['id'])->update([
  407. 'delete' => 1,
  408. 'deletedate' => Base::time()
  409. ]);
  410. DB::table('project_log')->insert([
  411. 'type' => '日志',
  412. 'projectid' => $projectDetail['id'],
  413. 'username' => $user['username'],
  414. 'detail' => '删除项目',
  415. 'indate' => Base::time()
  416. ]);
  417. //
  418. return Base::retSuccess('删除成功');
  419. }
  420. /**
  421. * 退出项目
  422. *
  423. * @apiParam {Number} projectid 项目ID
  424. */
  425. public function out()
  426. {
  427. $user = Users::authE();
  428. if (Base::isError($user)) {
  429. return $user;
  430. } else {
  431. $user = $user['data'];
  432. }
  433. //
  434. $projectid = trim(Request::input('projectid'));
  435. $projectDetail = Base::DBC2A(DB::table('project_lists')->where('id', $projectid)->where('delete', 0)->first());
  436. if (empty($projectDetail)) {
  437. return Base::retError('项目不存在或已被删除!');
  438. }
  439. if ($projectDetail['username'] == $user['username']) {
  440. return Base::retError('你是项目负责人,不可退出项目!');
  441. }
  442. $inRes = Project::inThe($projectid, $user['username']);
  443. if (Base::isError($inRes)) {
  444. return $inRes;
  445. }
  446. //
  447. DB::table('project_users')->where([
  448. 'type' => '成员',
  449. 'projectid' => $projectDetail['id'],
  450. 'username' => $user['username'],
  451. ])->delete();
  452. DB::table('project_log')->insert([
  453. 'type' => '日志',
  454. 'projectid' => $projectDetail['id'],
  455. 'username' => $user['username'],
  456. 'detail' => '退出项目',
  457. 'indate' => Base::time()
  458. ]);
  459. //
  460. return Base::retSuccess('退出项目成功');
  461. }
  462. /**
  463. * 项目成员-列表
  464. *
  465. * @apiParam {Number} projectid 项目ID
  466. * @apiParam {Number} [page] 当前页,默认:1
  467. * @apiParam {Number} [pagesize] 每页显示数量,默认:20,最大:100
  468. */
  469. public function users__lists()
  470. {
  471. $user = Users::authE();
  472. if (Base::isError($user)) {
  473. return $user;
  474. } else {
  475. $user = $user['data'];
  476. }
  477. //
  478. $projectid = intval(Request::input('projectid'));
  479. $inRes = Project::inThe($projectid, $user['username']);
  480. if (Base::isError($inRes)) {
  481. return $inRes;
  482. }
  483. //
  484. $lists = DB::table('project_lists')
  485. ->join('project_users', 'project_lists.id', '=', 'project_users.projectid')
  486. ->select(['project_lists.title', 'project_users.*'])
  487. ->where([
  488. ['project_lists.id', $projectid],
  489. ['project_lists.delete', 0],
  490. ['project_users.type', '成员'],
  491. ])
  492. ->orderByDesc('project_lists.id')->paginate(Min(Max(Base::nullShow(Request::input('pagesize'), 10), 1), 100));
  493. $lists = Base::getPageList($lists);
  494. if ($lists['total'] == 0) {
  495. return Base::retError('未找到任何相关的成员');
  496. }
  497. foreach ($lists['lists'] AS $key => $projectDetail) {
  498. $userInfo = Users::username2basic($projectDetail['username']);
  499. $lists['lists'][$key]['userimg'] = $userInfo['userimg'];
  500. $lists['lists'][$key]['nickname'] = $userInfo['nickname'];
  501. $lists['lists'][$key]['profession'] = $userInfo['profession'];
  502. }
  503. return Base::retSuccess('success', $lists);
  504. }
  505. /**
  506. * 项目成员-添加、删除
  507. *
  508. * @apiParam {String} act
  509. * - delete: 删除成员
  510. * - else: 添加成员
  511. * @apiParam {Number} projectid 项目ID
  512. * @apiParam {Array|String} username 用户名(或用户名组)
  513. */
  514. public function users__join()
  515. {
  516. $user = Users::authE();
  517. if (Base::isError($user)) {
  518. return $user;
  519. } else {
  520. $user = $user['data'];
  521. }
  522. //
  523. $projectid = trim(Request::input('projectid'));
  524. $projectDetail = Base::DBC2A(DB::table('project_lists')->where('id', $projectid)->where('delete', 0)->first());
  525. if (empty($projectDetail)) {
  526. return Base::retError('项目不存在或已被删除!');
  527. }
  528. if ($projectDetail['username'] != $user['username']) {
  529. return Base::retError('你是不是项目负责人!');
  530. }
  531. $usernames = Request::input('username');
  532. if (empty($usernames)) {
  533. return Base::retError('参数错误!');
  534. }
  535. if (!is_array($usernames)) {
  536. if (Base::strExists($usernames, ',')) {
  537. $usernames = explode(',', $usernames);
  538. } else {
  539. $usernames = [$usernames];
  540. }
  541. }
  542. //
  543. $logArray = [];
  544. foreach ($usernames AS $username) {
  545. $inRes = Project::inThe($projectid, $username);
  546. switch (Request::input('act')) {
  547. case 'delete': {
  548. if (!Base::isError($inRes) && $projectDetail['username'] != $username) {
  549. DB::table('project_users')->where([
  550. 'type' => '成员',
  551. 'projectid' => $projectid,
  552. 'username' => $username,
  553. ])->delete();
  554. $logArray[] = [
  555. 'type' => '日志',
  556. 'projectid' => $projectDetail['id'],
  557. 'username' => $user['username'],
  558. 'detail' => '将成员【' . $username . '】移出项目',
  559. 'indate' => Base::time()
  560. ];
  561. }
  562. break;
  563. }
  564. default: {
  565. if (Base::isError($inRes)) {
  566. DB::table('project_users')->insert([
  567. 'type' => '成员',
  568. 'projectid' => $projectid,
  569. 'isowner' => 0,
  570. 'username' => $username,
  571. 'indate' => Base::time()
  572. ]);
  573. $logArray[] = [
  574. 'type' => '日志',
  575. 'projectid' => $projectDetail['id'],
  576. 'username' => $username,
  577. 'detail' => '将成员【' . $username . '】加入项目',
  578. 'indate' => Base::time()
  579. ];
  580. }
  581. break;
  582. }
  583. }
  584. }
  585. return Base::retSuccess('操作完成');
  586. }
  587. /**
  588. * 项目任务-列表
  589. *
  590. * @apiParam {Number} projectid 项目ID
  591. * @apiParam {Number} [labelid] 项目子分类ID
  592. * @apiParam {String} [archived] 是否归档
  593. * - 未归档 (默认)
  594. * - 已归档
  595. * - 全部
  596. * @apiParam {String} [type] 任务类型
  597. * - 全部(默认)
  598. * - 未完成
  599. * - 已超期
  600. * - 已完成
  601. * @apiParam {Number} [statistics] 是否获取统计数据(1:获取)
  602. * @apiParam {Number} [page] 当前页,默认:1
  603. * @apiParam {Number} [pagesize] 每页显示数量,默认:20,最大:100
  604. */
  605. public function task__lists()
  606. {
  607. $user = Users::authE();
  608. if (Base::isError($user)) {
  609. return $user;
  610. } else {
  611. $user = $user['data'];
  612. }
  613. //
  614. $projectid = intval(Request::input('projectid'));
  615. $inRes = Project::inThe($projectid, $user['username']);
  616. if (Base::isError($inRes)) {
  617. return $inRes;
  618. }
  619. //
  620. $orderBy = '`id` ASC';
  621. $whereArray = [];
  622. $whereArray[] = ['project_lists.id', '=', $projectid];
  623. $whereArray[] = ['project_lists.delete', '=', 0];
  624. if (intval(Request::input('labelid')) > 0) {
  625. $whereArray[] = ['project_task.labelid', '=', intval(Request::input('labelid'))];
  626. }
  627. $archived = trim(Request::input('archived'));
  628. switch ($archived) {
  629. case '已归档':
  630. $whereArray[] = ['project_task.archived', '=', 1];
  631. $orderBy = '`archiveddate` DESC';
  632. break;
  633. case '未归档':
  634. default:
  635. $whereArray[] = ['project_task.archived', '=', 0];
  636. break;
  637. }
  638. $type = trim(Request::input('type'));
  639. switch ($type) {
  640. case '未完成':
  641. $whereArray[] = ['project_task.complete', '=', 0];
  642. break;
  643. case '已超期':
  644. $whereArray[] = ['project_task.complete', '=', 0];
  645. $whereArray[] = ['project_task.enddate', '>', 0];
  646. $whereArray[] = ['project_task.enddate', '<=', Base::time()];
  647. break;
  648. case '已完成':
  649. $whereArray[] = ['project_task.complete', '=', 1];
  650. break;
  651. }
  652. //
  653. $lists = DB::table('project_lists')
  654. ->join('project_task', 'project_lists.id', '=', 'project_task.projectid')
  655. ->select(['project_task.*'])
  656. ->where($whereArray)
  657. ->orderByRaw($orderBy)->paginate(Min(Max(Base::nullShow(Request::input('pagesize'), 10), 1), 100));
  658. $lists = Base::getPageList($lists);
  659. if (intval(Request::input('statistics')) == 1) {
  660. $lists['statistics_unfinished'] = $type === '未完成' ? $lists['total'] : DB::table('project_task')->where('projectid', $projectid)->where('complete', 0)->count();
  661. $lists['statistics_overdue'] = $type === '已超期' ? $lists['total'] : DB::table('project_task')->where('projectid', $projectid)->where('complete', 0)->whereBetween('enddate', [1, Base::time()])->count();
  662. $lists['statistics_complete'] = $type === '已完成' ? $lists['total'] : DB::table('project_task')->where('projectid', $projectid)->where('complete', 1)->count();
  663. }
  664. if ($lists['total'] == 0) {
  665. return Base::retError('未找到任何相关的任务', $lists);
  666. }
  667. foreach ($lists['lists'] AS $key => $info) {
  668. $info['overdue'] = Project::taskIsOverdue($info);
  669. $lists['lists'][$key] = array_merge($info, Users::username2basic($info['username']));
  670. }
  671. return Base::retSuccess('success', $lists);
  672. }
  673. /**
  674. * 项目任务-添加任务
  675. *
  676. * @apiParam {Number} projectid 项目ID
  677. * @apiParam {Number} labelid 项目子分类ID
  678. * @apiParam {String} title 任务标题
  679. * @apiParam {Number} [level] 任务紧急级别(1~4,默认:2)
  680. * @apiParam {String} [username] 任务负责人用户名
  681. * - 0: 未归档
  682. * - 1: 已归档
  683. *
  684. * @throws \Throwable
  685. */
  686. public function task__add()
  687. {
  688. $user = Users::authE();
  689. if (Base::isError($user)) {
  690. return $user;
  691. } else {
  692. $user = $user['data'];
  693. }
  694. //
  695. $projectid = intval(Request::input('projectid'));
  696. $projectDetail = Base::DBC2A(DB::table('project_lists')->where('id', $projectid)->where('delete', 0)->first());
  697. if (empty($projectDetail)) {
  698. return Base::retError('项目不存在或已被删除!');
  699. }
  700. //
  701. $labelid = intval(Request::input('labelid'));
  702. $labelDetail = Base::DBC2A(DB::table('project_label')->where('id', $labelid)->where('projectid', $projectid)->first());
  703. if (empty($labelDetail)) {
  704. return Base::retError('项目子分类不存在或已被删除!');
  705. }
  706. //
  707. $inRes = Project::inThe($projectid, $user['username']);
  708. if (Base::isError($inRes)) {
  709. return $inRes;
  710. }
  711. //
  712. $username = trim(Request::input('username'));
  713. if (empty($username)) {
  714. $username = $user['username'];
  715. }
  716. if ($username != $user['username']) {
  717. $inRes = Project::inThe($projectid, $username);
  718. if (Base::isError($inRes)) {
  719. return Base::retError('负责人不在项目成员内!');
  720. }
  721. }
  722. //
  723. $title = trim(Request::input('title'));
  724. if (empty($title)) {
  725. return Base::retError('任务标题不能为空!');
  726. }
  727. //
  728. $inArray = [
  729. 'projectid' => $projectid,
  730. 'labelid' => $labelid,
  731. 'createuser' => $user['username'],
  732. 'username' => $username,
  733. 'title' => $title,
  734. 'level' => max(1, min(4, intval(Request::input('level')))),
  735. 'indate' => Base::time(),
  736. 'subtask' => Base::array2string([]),
  737. 'files' => Base::array2string([]),
  738. 'follower' => Base::array2string([]),
  739. ];
  740. return DB::transaction(function () use ($inArray) {
  741. $taskid = DB::table('project_task')->insertGetId($inArray);
  742. if (empty($taskid)) {
  743. return Base::retError('系统繁忙,请稍后再试!');
  744. }
  745. DB::table('project_log')->insert([
  746. 'type' => '日志',
  747. 'projectid' => $inArray['projectid'],
  748. 'taskid' => $taskid,
  749. 'username' => $inArray['createuser'],
  750. 'detail' => '添加任务【' . $inArray['title'] . '】',
  751. 'indate' => Base::time()
  752. ]);
  753. Project::updateNum($inArray['projectid']);
  754. return Base::retSuccess('添加成功!');
  755. });
  756. }
  757. /**
  758. * 项目任务-归档、取消归档
  759. *
  760. * @apiParam {String} act
  761. * - cancel: 取消归档
  762. * - else: 加入归档
  763. * @apiParam {Number} taskid 任务ID
  764. */
  765. public function task__archived()
  766. {
  767. $user = Users::authE();
  768. if (Base::isError($user)) {
  769. return $user;
  770. } else {
  771. $user = $user['data'];
  772. }
  773. //
  774. $taskid = intval(Request::input('taskid'));
  775. $task = Base::DBC2A(DB::table('project_lists')
  776. ->join('project_task', 'project_lists.id', '=', 'project_task.projectid')
  777. ->select(['project_task.projectid', 'project_task.title', 'project_task.archived'])
  778. ->where([
  779. ['project_lists.delete', '=', 0],
  780. ['project_task.id', '=', $taskid],
  781. ])
  782. ->first());
  783. if (empty($task)) {
  784. return Base::retError('任务不存在!');
  785. }
  786. $inRes = Project::inThe($task['projectid'], $user['username']);
  787. if (Base::isError($inRes)) {
  788. return $inRes;
  789. }
  790. //
  791. switch (Request::input('act')) {
  792. case 'cancel': {
  793. if ($task['archived'] == 0) {
  794. return Base::retError('任务未归档!');
  795. }
  796. DB::table('project_task')->where('id', $taskid)->update([
  797. 'archived' => 1,
  798. 'archiveddate' => Base::time()
  799. ]);
  800. DB::table('project_log')->insert([
  801. 'type' => '日志',
  802. 'projectid' => $task['projectid'],
  803. 'taskid' => $taskid,
  804. 'username' => $user['username'],
  805. 'detail' => '取消归档【' . $task['title'] . '】',
  806. 'indate' => Base::time()
  807. ]);
  808. return Base::retSuccess('取消归档成功');
  809. }
  810. default: {
  811. if ($task['archived'] == 1) {
  812. return Base::retError('任务已归档!');
  813. }
  814. DB::table('project_task')->where('id', $taskid)->update([
  815. 'archived' => 0,
  816. ]);
  817. DB::table('project_log')->insert([
  818. 'type' => '日志',
  819. 'projectid' => $task['projectid'],
  820. 'taskid' => $taskid,
  821. 'username' => $user['username'],
  822. 'detail' => '归档【' . $task['title'] . '】',
  823. 'indate' => Base::time()
  824. ]);
  825. return Base::retSuccess('加入归档成功');
  826. }
  827. }
  828. }
  829. }