ProjectController.php 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051
  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')->orderBy('id')->get());
  98. $simpleLabel = [];
  99. //任务
  100. $task = Base::DBC2A(DB::table('project_task')->where([ 'projectid' => $projectid, 'delete' => 0, 'complete' => 0 ])->orderByDesc('inorder')->orderByDesc('id')->get());
  101. //任务归类
  102. foreach ($label AS $index => $temp) {
  103. $taskLists = [];
  104. foreach ($task AS $info) {
  105. if ($temp['id'] == $info['labelid']) {
  106. $info['overdue'] = Project::taskIsOverdue($info);
  107. $taskLists[] = array_merge($info, Users::username2basic($info['username']));
  108. }
  109. }
  110. $label[$index]['taskLists'] = $taskLists;
  111. $simpleLabel[] = ['id' => $temp['id'], 'title' => $temp['title']];
  112. }
  113. //
  114. return Base::retSuccess('success', [
  115. 'project' => $projectDetail,
  116. 'label' => $label,
  117. 'simpleLabel' => $simpleLabel,
  118. ]);
  119. }
  120. /**
  121. * 添加项目
  122. *
  123. * @apiParam {String} title 项目名称
  124. * @apiParam {Array} labels 流程,格式[流程1, 流程2]
  125. */
  126. public function add()
  127. {
  128. $user = Users::authE();
  129. if (Base::isError($user)) {
  130. return $user;
  131. } else {
  132. $user = $user['data'];
  133. }
  134. //项目名称
  135. $title = trim(Request::input('title'));
  136. if (mb_strlen($title) < 2) {
  137. return Base::retError('项目名称不可以少于2个字!');
  138. } elseif (mb_strlen($title) > 32) {
  139. return Base::retError('项目名称最多只能设置32个字!');
  140. }
  141. //流程
  142. $labels = Request::input('labels');
  143. if (!is_array($labels)) $labels = [];
  144. $insertLabels = [];
  145. $inorder = 0;
  146. foreach ($labels AS $label) {
  147. $label = trim($label);
  148. if ($label) {
  149. $insertLabels[] = [
  150. 'title' => $label,
  151. 'inorder' => $inorder++,
  152. ];
  153. }
  154. }
  155. if (empty($insertLabels)) {
  156. $insertLabels[] = [
  157. 'title' => '默认',
  158. 'inorder' => 0,
  159. ];
  160. }
  161. //开始创建
  162. $projectid = DB::table('project_lists')->insertGetId([
  163. 'title' => $title,
  164. 'username' => $user['username'],
  165. 'createuser' => $user['username'],
  166. 'indate' => Base::time()
  167. ]);
  168. if ($projectid) {
  169. foreach ($insertLabels AS $key => $label) {
  170. $insertLabels[$key]['projectid'] = $projectid;
  171. }
  172. DB::table('project_label')->insert($insertLabels);
  173. DB::table('project_log')->insert([
  174. 'type' => '日志',
  175. 'projectid' => $projectid,
  176. 'username' => $user['username'],
  177. 'detail' => '创建项目',
  178. 'indate' => Base::time()
  179. ]);
  180. DB::table('project_users')->insert([
  181. 'type' => '成员',
  182. 'projectid' => $projectid,
  183. 'isowner' => 1,
  184. 'username' => $user['username'],
  185. 'indate' => Base::time()
  186. ]);
  187. return Base::retSuccess('添加成功!');
  188. } else {
  189. return Base::retError('添加失败!');
  190. }
  191. }
  192. /**
  193. * 收藏项目
  194. *
  195. * @apiParam {String} act 类型
  196. * - cancel: 取消收藏
  197. * - else: 添加收藏
  198. * @apiParam {Number} projectid 项目ID
  199. *
  200. * @throws \Throwable
  201. */
  202. public function favor()
  203. {
  204. $user = Users::authE();
  205. if (Base::isError($user)) {
  206. return $user;
  207. } else {
  208. $user = $user['data'];
  209. }
  210. //
  211. $projectid = trim(Request::input('projectid'));
  212. $projectDetail = Base::DBC2A(DB::table('project_lists')->where('id', $projectid)->where('delete', 0)->first());
  213. if (empty($projectDetail)) {
  214. return Base::retError('项目不存在或已被删除!');
  215. }
  216. return DB::transaction(function () use ($projectDetail, $user) {
  217. switch (Request::input('act')) {
  218. case 'cancel': {
  219. if (DB::table('project_users')->where([
  220. 'type' => '收藏',
  221. 'projectid' => $projectDetail['id'],
  222. 'username' => $user['username'],
  223. ])->delete()) {
  224. DB::table('project_log')->insert([
  225. 'type' => '日志',
  226. 'projectid' => $projectDetail['id'],
  227. 'username' => $user['username'],
  228. 'detail' => '取消收藏',
  229. 'indate' => Base::time()
  230. ]);
  231. return Base::retSuccess('取消成功');
  232. }
  233. return Base::retSuccess('已取消');
  234. }
  235. default: {
  236. $row = Base::DBC2A(DB::table('project_users')->where([
  237. 'type' => '收藏',
  238. 'projectid' => $projectDetail['id'],
  239. 'username' => $user['username'],
  240. ])->lockForUpdate()->first());
  241. if (empty($row)) {
  242. DB::table('project_users')->insert([
  243. 'type' => '收藏',
  244. 'projectid' => $projectDetail['id'],
  245. 'isowner' => $projectDetail['username'] == $user['username'] ? 1 : 0,
  246. 'username' => $user['username'],
  247. 'indate' => Base::time()
  248. ]);
  249. DB::table('project_log')->insert([
  250. 'type' => '日志',
  251. 'projectid' => $projectDetail['id'],
  252. 'username' => $user['username'],
  253. 'detail' => '收藏项目',
  254. 'indate' => Base::time()
  255. ]);
  256. return Base::retSuccess('收藏成功');
  257. }
  258. return Base::retSuccess('已收藏');
  259. }
  260. }
  261. });
  262. }
  263. /**
  264. * 重命名项目
  265. *
  266. * @apiParam {Number} projectid 项目ID
  267. * @apiParam {String} title 项目新名称
  268. */
  269. public function rename()
  270. {
  271. $user = Users::authE();
  272. if (Base::isError($user)) {
  273. return $user;
  274. } else {
  275. $user = $user['data'];
  276. }
  277. //
  278. $projectid = trim(Request::input('projectid'));
  279. $projectDetail = Base::DBC2A(DB::table('project_lists')->where('id', $projectid)->where('delete', 0)->first());
  280. if (empty($projectDetail)) {
  281. return Base::retError('项目不存在或已被删除!');
  282. }
  283. if ($projectDetail['username'] != $user['username']) {
  284. return Base::retError('你不是项目负责人!');
  285. }
  286. //
  287. $title = trim(Request::input('title'));
  288. if (mb_strlen($title) < 2) {
  289. return Base::retError('项目名称不可以少于2个字!');
  290. } elseif (mb_strlen($title) > 32) {
  291. return Base::retError('项目名称最多只能设置32个字!');
  292. }
  293. //
  294. DB::table('project_lists')->where('id', $projectDetail['id'])->update([
  295. 'title' => $title
  296. ]);
  297. DB::table('project_log')->insert([
  298. 'type' => '日志',
  299. 'projectid' => $projectDetail['id'],
  300. 'username' => $user['username'],
  301. 'detail' => '【' . $projectDetail['title'] . '】重命名【' . $title . '】',
  302. 'indate' => Base::time()
  303. ]);
  304. //
  305. return Base::retSuccess('修改成功');
  306. }
  307. /**
  308. * 移交项目
  309. *
  310. * @apiParam {Number} projectid 项目ID
  311. * @apiParam {String} username 项目新负责人用户名
  312. *
  313. * @throws \Throwable
  314. */
  315. public function transfer()
  316. {
  317. $user = Users::authE();
  318. if (Base::isError($user)) {
  319. return $user;
  320. } else {
  321. $user = $user['data'];
  322. }
  323. //
  324. $projectid = trim(Request::input('projectid'));
  325. $projectDetail = Base::DBC2A(DB::table('project_lists')->where('id', $projectid)->where('delete', 0)->first());
  326. if (empty($projectDetail)) {
  327. return Base::retError('项目不存在或已被删除!');
  328. }
  329. if ($projectDetail['username'] != $user['username']) {
  330. return Base::retError('你不是项目负责人!');
  331. }
  332. //
  333. $username = trim(Request::input('username'));
  334. if ($username == $projectDetail['username']) {
  335. return Base::retError('你已是项目负责人!');
  336. }
  337. $count = DB::table('users')->where('username', $username)->count();
  338. if ($count <= 0) {
  339. return Base::retError('成员用户名(' . $username . ')不存在!');
  340. }
  341. //判断是否已在项目成员内
  342. $inRes = Project::inThe($projectDetail['id'], $username);
  343. if (Base::isError($inRes)) {
  344. DB::table('project_users')->insert([
  345. 'type' => '成员',
  346. 'projectid' => $projectDetail['id'],
  347. 'isowner' => 0,
  348. 'username' => $username,
  349. 'indate' => Base::time()
  350. ]);
  351. DB::table('project_log')->insert([
  352. 'type' => '日志',
  353. 'projectid' => $projectDetail['id'],
  354. 'username' => $username,
  355. 'detail' => '移交项目,自动加入项目',
  356. 'indate' => Base::time()
  357. ]);
  358. }
  359. //开始移交
  360. return DB::transaction(function () use ($user, $username, $projectDetail) {
  361. DB::table('project_lists')->where('id', $projectDetail['id'])->update([
  362. 'username' => $username
  363. ]);
  364. DB::table('project_log')->insert([
  365. 'type' => '日志',
  366. 'projectid' => $projectDetail['id'],
  367. 'username' => $user['username'],
  368. 'detail' => '【' . $projectDetail['username'] . '】移交给【' . $username . '】',
  369. 'indate' => Base::time()
  370. ]);
  371. DB::table('project_users')->where([
  372. 'projectid' => $projectDetail['id'],
  373. 'username' => $projectDetail['username'],
  374. ])->update([
  375. 'isowner' => 0
  376. ]);
  377. DB::table('project_users')->where([
  378. 'projectid' => $projectDetail['id'],
  379. 'username' => $username,
  380. ])->update([
  381. 'isowner' => 1
  382. ]);
  383. return Base::retSuccess('移交成功');
  384. });
  385. }
  386. /**
  387. * 删除项目
  388. *
  389. * @apiParam {Number} projectid 项目ID
  390. */
  391. public function delete()
  392. {
  393. $user = Users::authE();
  394. if (Base::isError($user)) {
  395. return $user;
  396. } else {
  397. $user = $user['data'];
  398. }
  399. //
  400. $projectid = trim(Request::input('projectid'));
  401. $projectDetail = Base::DBC2A(DB::table('project_lists')->where('id', $projectid)->where('delete', 0)->first());
  402. if (empty($projectDetail)) {
  403. return Base::retError('项目不存在或已被删除!');
  404. }
  405. if ($projectDetail['username'] != $user['username']) {
  406. return Base::retError('你不是项目负责人!');
  407. }
  408. //
  409. DB::table('project_lists')->where('id', $projectDetail['id'])->update([
  410. 'delete' => 1,
  411. 'deletedate' => Base::time()
  412. ]);
  413. DB::table('project_log')->insert([
  414. 'type' => '日志',
  415. 'projectid' => $projectDetail['id'],
  416. 'username' => $user['username'],
  417. 'detail' => '删除项目',
  418. 'indate' => Base::time()
  419. ]);
  420. //
  421. return Base::retSuccess('删除成功');
  422. }
  423. /**
  424. * 退出项目
  425. *
  426. * @apiParam {Number} projectid 项目ID
  427. */
  428. public function out()
  429. {
  430. $user = Users::authE();
  431. if (Base::isError($user)) {
  432. return $user;
  433. } else {
  434. $user = $user['data'];
  435. }
  436. //
  437. $projectid = trim(Request::input('projectid'));
  438. $projectDetail = Base::DBC2A(DB::table('project_lists')->where('id', $projectid)->where('delete', 0)->first());
  439. if (empty($projectDetail)) {
  440. return Base::retError('项目不存在或已被删除!');
  441. }
  442. if ($projectDetail['username'] == $user['username']) {
  443. return Base::retError('你是项目负责人,不可退出项目!');
  444. }
  445. $inRes = Project::inThe($projectid, $user['username']);
  446. if (Base::isError($inRes)) {
  447. return $inRes;
  448. }
  449. //
  450. DB::table('project_users')->where([
  451. 'type' => '成员',
  452. 'projectid' => $projectDetail['id'],
  453. 'username' => $user['username'],
  454. ])->delete();
  455. DB::table('project_log')->insert([
  456. 'type' => '日志',
  457. 'projectid' => $projectDetail['id'],
  458. 'username' => $user['username'],
  459. 'detail' => '退出项目',
  460. 'indate' => Base::time()
  461. ]);
  462. //
  463. return Base::retSuccess('退出项目成功');
  464. }
  465. /**
  466. * 项目成员-列表
  467. *
  468. * @apiParam {Number} projectid 项目ID
  469. * @apiParam {Number} [page] 当前页,默认:1
  470. * @apiParam {Number} [pagesize] 每页显示数量,默认:20,最大:100
  471. */
  472. public function users__lists()
  473. {
  474. $user = Users::authE();
  475. if (Base::isError($user)) {
  476. return $user;
  477. } else {
  478. $user = $user['data'];
  479. }
  480. //
  481. $projectid = intval(Request::input('projectid'));
  482. $inRes = Project::inThe($projectid, $user['username']);
  483. if (Base::isError($inRes)) {
  484. return $inRes;
  485. }
  486. //
  487. $lists = DB::table('project_lists')
  488. ->join('project_users', 'project_lists.id', '=', 'project_users.projectid')
  489. ->select(['project_lists.title', 'project_users.*'])
  490. ->where([
  491. ['project_lists.id', $projectid],
  492. ['project_lists.delete', 0],
  493. ['project_users.type', '成员'],
  494. ])
  495. ->orderByDesc('project_lists.id')->paginate(Min(Max(Base::nullShow(Request::input('pagesize'), 10), 1), 100));
  496. $lists = Base::getPageList($lists);
  497. if ($lists['total'] == 0) {
  498. return Base::retError('未找到任何相关的成员');
  499. }
  500. foreach ($lists['lists'] AS $key => $projectDetail) {
  501. $userInfo = Users::username2basic($projectDetail['username']);
  502. $lists['lists'][$key]['userimg'] = $userInfo['userimg'];
  503. $lists['lists'][$key]['nickname'] = $userInfo['nickname'];
  504. $lists['lists'][$key]['profession'] = $userInfo['profession'];
  505. }
  506. return Base::retSuccess('success', $lists);
  507. }
  508. /**
  509. * 项目成员-添加、删除
  510. *
  511. * @apiParam {String} act
  512. * - delete: 删除成员
  513. * - else: 添加成员
  514. * @apiParam {Number} projectid 项目ID
  515. * @apiParam {Array|String} username 用户名(或用户名组)
  516. */
  517. public function users__join()
  518. {
  519. $user = Users::authE();
  520. if (Base::isError($user)) {
  521. return $user;
  522. } else {
  523. $user = $user['data'];
  524. }
  525. //
  526. $projectid = trim(Request::input('projectid'));
  527. $projectDetail = Base::DBC2A(DB::table('project_lists')->where('id', $projectid)->where('delete', 0)->first());
  528. if (empty($projectDetail)) {
  529. return Base::retError('项目不存在或已被删除!');
  530. }
  531. if ($projectDetail['username'] != $user['username']) {
  532. return Base::retError('你是不是项目负责人!');
  533. }
  534. $usernames = Request::input('username');
  535. if (empty($usernames)) {
  536. return Base::retError('参数错误!');
  537. }
  538. if (!is_array($usernames)) {
  539. if (Base::strExists($usernames, ',')) {
  540. $usernames = explode(',', $usernames);
  541. } else {
  542. $usernames = [$usernames];
  543. }
  544. }
  545. //
  546. $logArray = [];
  547. foreach ($usernames AS $username) {
  548. $inRes = Project::inThe($projectid, $username);
  549. switch (Request::input('act')) {
  550. case 'delete': {
  551. if (!Base::isError($inRes) && $projectDetail['username'] != $username) {
  552. DB::table('project_users')->where([
  553. 'type' => '成员',
  554. 'projectid' => $projectid,
  555. 'username' => $username,
  556. ])->delete();
  557. $logArray[] = [
  558. 'type' => '日志',
  559. 'projectid' => $projectDetail['id'],
  560. 'username' => $user['username'],
  561. 'detail' => '将成员【' . $username . '】移出项目',
  562. 'indate' => Base::time()
  563. ];
  564. }
  565. break;
  566. }
  567. default: {
  568. if (Base::isError($inRes)) {
  569. DB::table('project_users')->insert([
  570. 'type' => '成员',
  571. 'projectid' => $projectid,
  572. 'isowner' => 0,
  573. 'username' => $username,
  574. 'indate' => Base::time()
  575. ]);
  576. $logArray[] = [
  577. 'type' => '日志',
  578. 'projectid' => $projectDetail['id'],
  579. 'username' => $username,
  580. 'detail' => '将成员【' . $username . '】加入项目',
  581. 'indate' => Base::time()
  582. ];
  583. }
  584. break;
  585. }
  586. }
  587. }
  588. return Base::retSuccess('操作完成');
  589. }
  590. /**
  591. * 项目子分类-添加分类
  592. *
  593. * @apiParam {Number} projectid 项目ID
  594. * @apiParam {String} title 分类名称
  595. */
  596. public function label__add()
  597. {
  598. $user = Users::authE();
  599. if (Base::isError($user)) {
  600. return $user;
  601. } else {
  602. $user = $user['data'];
  603. }
  604. //
  605. $projectid = trim(Request::input('projectid'));
  606. $inRes = Project::inThe($projectid, $user['username']);
  607. if (Base::isError($inRes)) {
  608. return $inRes;
  609. }
  610. //
  611. $title = trim(Request::input('title'));
  612. if (empty($title)) {
  613. return Base::retError('列表名称不能为空!');
  614. } elseif (mb_strlen($title) > 32) {
  615. return Base::retError('列表名称最多只能设置32个字!');
  616. }
  617. //
  618. $count = DB::table('project_label')->where('projectid', $projectid)->where('title', $title)->count();
  619. if ($count > 0) {
  620. return Base::retError('列表名称已存在!');
  621. }
  622. //
  623. $id = DB::table('project_label')->insertGetId([
  624. 'projectid' => $projectid,
  625. 'title' => $title,
  626. 'inorder' => intval(DB::table('project_label')->where('projectid', $projectid)->orderByDesc('inorder')->value('inorder')) + 1,
  627. ]);
  628. if (empty($id)) {
  629. return Base::retError('系统繁忙,请稍后再试!');
  630. }
  631. DB::table('project_log')->insert([
  632. 'type' => '日志',
  633. 'projectid' => $projectid,
  634. 'username' => $user['username'],
  635. 'detail' => '添加任务列表【' . $title . '】',
  636. 'indate' => Base::time()
  637. ]);
  638. //
  639. $row = Base::DBC2A(DB::table('project_label')->where('id', $id)->first());
  640. $row['taskLists'] = [];
  641. return Base::retSuccess('添加成功', $row);
  642. }
  643. /**
  644. * 项目子分类-重命名分类
  645. *
  646. * @apiParam {Number} projectid 项目ID
  647. * @apiParam {Number} labelid 分类ID
  648. * @apiParam {String} title 新分类名称
  649. */
  650. public function label__rename()
  651. {
  652. $user = Users::authE();
  653. if (Base::isError($user)) {
  654. return $user;
  655. } else {
  656. $user = $user['data'];
  657. }
  658. //
  659. $projectid = trim(Request::input('projectid'));
  660. $inRes = Project::inThe($projectid, $user['username']);
  661. if (Base::isError($inRes)) {
  662. return $inRes;
  663. }
  664. //
  665. $title = trim(Request::input('title'));
  666. if (empty($title)) {
  667. return Base::retError('列表名称不能为空!');
  668. } elseif (mb_strlen($title) > 32) {
  669. return Base::retError('列表名称最多只能设置32个字!');
  670. }
  671. //
  672. $labelid = intval(Request::input('labelid'));
  673. $count = DB::table('project_label')->where('id', '!=', $labelid)->where('projectid', $projectid)->where('title', $title)->count();
  674. if ($count > 0) {
  675. return Base::retError('列表名称已存在!');
  676. }
  677. //
  678. $labelDetail = Base::DBC2A(DB::table('project_label')->where('id', $labelid)->where('projectid', $projectid)->first());
  679. if (empty($labelDetail)) {
  680. return Base::retError('列表不存在或已被删除!');
  681. }
  682. //
  683. if (DB::table('project_label')->where('id', $labelDetail['id'])->update([ 'title' => $title ])) {
  684. DB::table('project_log')->insert([
  685. 'type' => '日志',
  686. 'projectid' => $projectid,
  687. 'username' => $user['username'],
  688. 'detail' => '任务列表【' . $labelDetail['title'] . '】重命名【' . $title . '】',
  689. 'indate' => Base::time()
  690. ]);
  691. }
  692. //
  693. return Base::retSuccess('修改成功');
  694. }
  695. /**
  696. * 项目子分类-删除分类
  697. *
  698. * @apiParam {Number} projectid 项目ID
  699. * @apiParam {Number} labelid 分类ID
  700. *
  701. * @throws \Throwable
  702. */
  703. public function label__delete()
  704. {
  705. $user = Users::authE();
  706. if (Base::isError($user)) {
  707. return $user;
  708. } else {
  709. $user = $user['data'];
  710. }
  711. //
  712. $projectid = trim(Request::input('projectid'));
  713. $inRes = Project::inThe($projectid, $user['username']);
  714. if (Base::isError($inRes)) {
  715. return $inRes;
  716. }
  717. //
  718. $labelid = intval(Request::input('labelid'));
  719. $labelDetail = Base::DBC2A(DB::table('project_label')->where('id', $labelid)->where('projectid', $projectid)->first());
  720. if (empty($labelDetail)) {
  721. return Base::retError('列表不存在或已被删除!');
  722. }
  723. //
  724. return DB::transaction(function () use ($user, $projectid, $labelDetail) {
  725. $taskLists = Base::DBC2A(DB::table('project_task')->where('labelid', $labelDetail['id'])->get());
  726. $logArray = [];
  727. foreach ($taskLists AS $task) {
  728. $logArray[] = [
  729. 'type' => '日志',
  730. 'projectid' => $projectid,
  731. 'taskid' => $task['id'],
  732. 'username' => $user['username'],
  733. 'detail' => '删除列表任务【' . $task['title'] . '】',
  734. 'indate' => Base::time()
  735. ];
  736. }
  737. $logArray[] = [
  738. 'type' => '日志',
  739. 'projectid' => $projectid,
  740. 'taskid' => 0,
  741. 'username' => $user['username'],
  742. 'detail' => '删除任务列表【' . $labelDetail['title'] . '】',
  743. 'indate' => Base::time()
  744. ];
  745. DB::table('project_task')->where('labelid', $labelDetail['id'])->update([
  746. 'delete' => 1,
  747. 'deletedate' => Base::time()
  748. ]);
  749. DB::table('project_label')->where('id', $labelDetail['id'])->delete();
  750. DB::table('project_log')->insert($logArray);
  751. //
  752. return Base::retSuccess('删除成功');
  753. });
  754. }
  755. /**
  756. * 项目任务-列表
  757. *
  758. * @apiParam {Number} projectid 项目ID
  759. * @apiParam {Number} [labelid] 项目子分类ID
  760. * @apiParam {String} [archived] 是否归档
  761. * - 未归档 (默认)
  762. * - 已归档
  763. * - 全部
  764. * @apiParam {String} [type] 任务类型
  765. * - 全部(默认)
  766. * - 未完成
  767. * - 已超期
  768. * - 已完成
  769. * @apiParam {String} [username] 负责人用户名
  770. * @apiParam {Number} [statistics] 是否获取统计数据(1:获取)
  771. * @apiParam {Object} [sorts] 排序方式,格式:{key:'', order:''}("archived=已归档"时无效)
  772. * - key: title|labelid|enddate|username|level|indate|type
  773. * - order: asc|desc
  774. * @apiParam {Number} [page] 当前页,默认:1
  775. * @apiParam {Number} [pagesize] 每页显示数量,默认:20,最大:100
  776. */
  777. public function task__lists()
  778. {
  779. $user = Users::authE();
  780. if (Base::isError($user)) {
  781. return $user;
  782. } else {
  783. $user = $user['data'];
  784. }
  785. //
  786. $projectid = intval(Request::input('projectid'));
  787. $inRes = Project::inThe($projectid, $user['username']);
  788. if (Base::isError($inRes)) {
  789. return $inRes;
  790. }
  791. //
  792. $orderBy = '`inorder` DESC,`id` DESC';
  793. $sorts = Base::json2array(Request::input('sorts'));
  794. if (in_array($sorts['key'], ['asc', 'desc'])) {
  795. switch ($sorts['key']) {
  796. case 'title':
  797. case 'labelid':
  798. case 'enddate':
  799. case 'username':
  800. case 'level':
  801. case 'indate':
  802. $orderBy = '`' . $sorts['key'] . '` ' . $sorts['key'] . ',`id` DESC';
  803. break;
  804. case 'type':
  805. $orderBy = 'CASE WHEN `complete`= 0 AND `enddate` BETWEEN 1 AND ' . Base::time() . ' THEN 0 ELSE 1 END ' . $sorts['key'] . ', `complete` ' . $sorts['key'] . ',`id` DESC';
  806. break;
  807. }
  808. }
  809. //
  810. $whereArray = [];
  811. $whereArray[] = ['project_lists.id', '=', $projectid];
  812. $whereArray[] = ['project_lists.delete', '=', 0];
  813. $whereArray[] = ['project_task.delete', '=', 0];
  814. if (intval(Request::input('labelid')) > 0) {
  815. $whereArray[] = ['project_task.labelid', '=', intval(Request::input('labelid'))];
  816. }
  817. if (intval(Request::input('level')) > 0) {
  818. $whereArray[] = ['project_task.level', '=', intval(Request::input('level'))];
  819. }
  820. if (trim(Request::input('username'))) {
  821. $whereArray[] = ['project_task.username', '=', trim(Request::input('username'))];
  822. }
  823. $archived = trim(Request::input('archived'));
  824. if (empty($archived)) $archived = "未归档";
  825. switch ($archived) {
  826. case '已归档':
  827. $whereArray[] = ['project_task.archived', '=', 1];
  828. $orderBy = '`archiveddate` DESC';
  829. break;
  830. case '未归档':
  831. $whereArray[] = ['project_task.archived', '=', 0];
  832. break;
  833. }
  834. $type = trim(Request::input('type'));
  835. switch ($type) {
  836. case '未完成':
  837. $whereArray[] = ['project_task.complete', '=', 0];
  838. break;
  839. case '已超期':
  840. $whereArray[] = ['project_task.complete', '=', 0];
  841. $whereArray[] = ['project_task.enddate', '>', 0];
  842. $whereArray[] = ['project_task.enddate', '<=', Base::time()];
  843. break;
  844. case '已完成':
  845. $whereArray[] = ['project_task.complete', '=', 1];
  846. break;
  847. }
  848. //
  849. $lists = DB::table('project_lists')
  850. ->join('project_task', 'project_lists.id', '=', 'project_task.projectid')
  851. ->select(['project_task.*'])
  852. ->where($whereArray)
  853. ->orderByRaw($orderBy)->paginate(Min(Max(Base::nullShow(Request::input('pagesize'), 10), 1), 100));
  854. $lists = Base::getPageList($lists);
  855. if (intval(Request::input('statistics')) == 1) {
  856. $lists['statistics_unfinished'] = $type === '未完成' ? $lists['total'] : DB::table('project_task')->where('projectid', $projectid)->where('delete', 0)->where('complete', 0)->count();
  857. $lists['statistics_overdue'] = $type === '已超期' ? $lists['total'] : DB::table('project_task')->where('projectid', $projectid)->where('delete', 0)->where('complete', 0)->whereBetween('enddate', [1, Base::time()])->count();
  858. $lists['statistics_complete'] = $type === '已完成' ? $lists['total'] : DB::table('project_task')->where('projectid', $projectid)->where('delete', 0)->where('complete', 1)->count();
  859. }
  860. if ($lists['total'] == 0) {
  861. return Base::retError('未找到任何相关的任务', $lists);
  862. }
  863. foreach ($lists['lists'] AS $key => $info) {
  864. $info['overdue'] = Project::taskIsOverdue($info);
  865. $lists['lists'][$key] = array_merge($info, Users::username2basic($info['username']));
  866. }
  867. return Base::retSuccess('success', $lists);
  868. }
  869. /**
  870. * 项目任务-添加任务
  871. *
  872. * @apiParam {Number} projectid 项目ID
  873. * @apiParam {Number} labelid 项目子分类ID
  874. * @apiParam {String} title 任务标题
  875. * @apiParam {Number} [level] 任务紧急级别(1~4,默认:2)
  876. * @apiParam {String} [username] 任务负责人用户名
  877. * - 0: 未归档
  878. * - 1: 已归档
  879. *
  880. * @throws \Throwable
  881. */
  882. public function task__add()
  883. {
  884. $user = Users::authE();
  885. if (Base::isError($user)) {
  886. return $user;
  887. } else {
  888. $user = $user['data'];
  889. }
  890. //
  891. $projectid = intval(Request::input('projectid'));
  892. $projectDetail = Base::DBC2A(DB::table('project_lists')->where('id', $projectid)->where('delete', 0)->first());
  893. if (empty($projectDetail)) {
  894. return Base::retError('项目不存在或已被删除!');
  895. }
  896. //
  897. $labelid = intval(Request::input('labelid'));
  898. $labelDetail = Base::DBC2A(DB::table('project_label')->where('id', $labelid)->where('projectid', $projectid)->first());
  899. if (empty($labelDetail)) {
  900. return Base::retError('项目子分类不存在或已被删除!');
  901. }
  902. //
  903. $inRes = Project::inThe($projectid, $user['username']);
  904. if (Base::isError($inRes)) {
  905. return $inRes;
  906. }
  907. //
  908. $username = trim(Request::input('username'));
  909. if (empty($username)) {
  910. $username = $user['username'];
  911. }
  912. if ($username != $user['username']) {
  913. $inRes = Project::inThe($projectid, $username);
  914. if (Base::isError($inRes)) {
  915. return Base::retError('负责人不在项目成员内!');
  916. }
  917. }
  918. //
  919. $title = trim(Request::input('title'));
  920. if (empty($title)) {
  921. return Base::retError('任务标题不能为空!');
  922. } elseif (mb_strlen($title) > 255) {
  923. return Base::retError('任务标题最多只能设置255个字!');
  924. }
  925. //
  926. $inArray = [
  927. 'projectid' => $projectid,
  928. 'labelid' => $labelid,
  929. 'createuser' => $user['username'],
  930. 'username' => $username,
  931. 'title' => $title,
  932. 'level' => max(1, min(4, intval(Request::input('level')))),
  933. 'indate' => Base::time(),
  934. 'subtask' => Base::array2string([]),
  935. 'files' => Base::array2string([]),
  936. 'follower' => Base::array2string([]),
  937. ];
  938. return DB::transaction(function () use ($inArray) {
  939. $taskid = DB::table('project_task')->insertGetId($inArray);
  940. if (empty($taskid)) {
  941. return Base::retError('系统繁忙,请稍后再试!');
  942. }
  943. DB::table('project_log')->insert([
  944. 'type' => '日志',
  945. 'projectid' => $inArray['projectid'],
  946. 'taskid' => $taskid,
  947. 'username' => $inArray['createuser'],
  948. 'detail' => '添加任务【' . $inArray['title'] . '】',
  949. 'indate' => Base::time()
  950. ]);
  951. Project::updateNum($inArray['projectid']);
  952. //
  953. $task = Base::DBC2A(DB::table('project_task')->where('id', $taskid)->first());
  954. $task['overdue'] = Project::taskIsOverdue($task);
  955. $task = array_merge($task, Users::username2basic($task['username']));
  956. return Base::retSuccess('添加成功!', $task);
  957. });
  958. }
  959. /**
  960. * 项目任务-归档、取消归档
  961. *
  962. * @apiParam {String} act
  963. * - cancel: 取消归档
  964. * - else: 加入归档
  965. * @apiParam {Number} taskid 任务ID
  966. */
  967. public function task__archived()
  968. {
  969. $user = Users::authE();
  970. if (Base::isError($user)) {
  971. return $user;
  972. } else {
  973. $user = $user['data'];
  974. }
  975. //
  976. $taskid = intval(Request::input('taskid'));
  977. $task = Base::DBC2A(DB::table('project_lists')
  978. ->join('project_task', 'project_lists.id', '=', 'project_task.projectid')
  979. ->select(['project_task.projectid', 'project_task.title', 'project_task.archived'])
  980. ->where([
  981. ['project_lists.delete', '=', 0],
  982. ['project_task.delete', '=', 0],
  983. ['project_task.id', '=', $taskid],
  984. ])
  985. ->first());
  986. if (empty($task)) {
  987. return Base::retError('任务不存在!');
  988. }
  989. $inRes = Project::inThe($task['projectid'], $user['username']);
  990. if (Base::isError($inRes)) {
  991. return $inRes;
  992. }
  993. //
  994. switch (Request::input('act')) {
  995. case 'cancel': {
  996. if ($task['archived'] == 0) {
  997. return Base::retError('任务未归档!');
  998. }
  999. DB::table('project_task')->where('id', $taskid)->update([
  1000. 'archived' => 1,
  1001. 'archiveddate' => Base::time()
  1002. ]);
  1003. DB::table('project_log')->insert([
  1004. 'type' => '日志',
  1005. 'projectid' => $task['projectid'],
  1006. 'taskid' => $taskid,
  1007. 'username' => $user['username'],
  1008. 'detail' => '取消归档【' . $task['title'] . '】',
  1009. 'indate' => Base::time()
  1010. ]);
  1011. return Base::retSuccess('取消归档成功');
  1012. }
  1013. default: {
  1014. if ($task['archived'] == 1) {
  1015. return Base::retError('任务已归档!');
  1016. }
  1017. DB::table('project_task')->where('id', $taskid)->update([
  1018. 'archived' => 0,
  1019. ]);
  1020. DB::table('project_log')->insert([
  1021. 'type' => '日志',
  1022. 'projectid' => $task['projectid'],
  1023. 'taskid' => $taskid,
  1024. 'username' => $user['username'],
  1025. 'detail' => '归档【' . $task['title'] . '】',
  1026. 'indate' => Base::time()
  1027. ]);
  1028. return Base::retSuccess('加入归档成功');
  1029. }
  1030. }
  1031. }
  1032. }