ProjectController.php 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021
  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. //任务
  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 {String} title 分类名称
  592. */
  593. public function label__add()
  594. {
  595. $user = Users::authE();
  596. if (Base::isError($user)) {
  597. return $user;
  598. } else {
  599. $user = $user['data'];
  600. }
  601. //
  602. $projectid = trim(Request::input('projectid'));
  603. $inRes = Project::inThe($projectid, $user['username']);
  604. if (Base::isError($inRes)) {
  605. return $inRes;
  606. }
  607. //
  608. $title = trim(Request::input('title'));
  609. if (empty($title)) {
  610. return Base::retError('列表名称不能为空!');
  611. } elseif (mb_strlen($title) > 32) {
  612. return Base::retError('列表名称最多只能设置32个字!');
  613. }
  614. //
  615. $count = DB::table('project_label')->where('projectid', $projectid)->where('title', $title)->count();
  616. if ($count > 0) {
  617. return Base::retError('列表名称已存在!');
  618. }
  619. //
  620. $id = DB::table('project_label')->insertGetId([
  621. 'projectid' => $projectid,
  622. 'title' => $title,
  623. 'inorder' => intval(DB::table('project_label')->where('projectid', $projectid)->orderByDesc('inorder')->value('inorder')) + 1,
  624. ]);
  625. if (empty($id)) {
  626. return Base::retError('系统繁忙,请稍后再试!');
  627. }
  628. DB::table('project_log')->insert([
  629. 'type' => '日志',
  630. 'projectid' => $projectid,
  631. 'username' => $user['username'],
  632. 'detail' => '添加流程列表【' . $title . '】',
  633. 'indate' => Base::time()
  634. ]);
  635. //
  636. $row = Base::DBC2A(DB::table('project_label')->where('id', $id)->first());
  637. $row['taskLists'] = [];
  638. return Base::retSuccess('添加成功', $row);
  639. }
  640. /**
  641. * 项目子分类-重命名分类
  642. *
  643. * @apiParam {Number} projectid 项目ID
  644. * @apiParam {Number} labelid 分类ID
  645. * @apiParam {String} title 新分类名称
  646. */
  647. public function label__rename()
  648. {
  649. $user = Users::authE();
  650. if (Base::isError($user)) {
  651. return $user;
  652. } else {
  653. $user = $user['data'];
  654. }
  655. //
  656. $projectid = trim(Request::input('projectid'));
  657. $inRes = Project::inThe($projectid, $user['username']);
  658. if (Base::isError($inRes)) {
  659. return $inRes;
  660. }
  661. //
  662. $title = trim(Request::input('title'));
  663. if (empty($title)) {
  664. return Base::retError('列表名称不能为空!');
  665. } elseif (mb_strlen($title) > 32) {
  666. return Base::retError('列表名称最多只能设置32个字!');
  667. }
  668. //
  669. $labelid = intval(Request::input('labelid'));
  670. $count = DB::table('project_label')->where('id', '!=', $labelid)->where('projectid', $projectid)->where('title', $title)->count();
  671. if ($count > 0) {
  672. return Base::retError('列表名称已存在!');
  673. }
  674. //
  675. $labelDetail = Base::DBC2A(DB::table('project_label')->where('id', $labelid)->where('projectid', $projectid)->first());
  676. if (empty($labelDetail)) {
  677. return Base::retError('列表不存在或已被删除!');
  678. }
  679. //
  680. if (DB::table('project_label')->where('id', $labelDetail['id'])->update([ 'title' => $title ])) {
  681. DB::table('project_log')->insert([
  682. 'type' => '日志',
  683. 'projectid' => $projectid,
  684. 'username' => $user['username'],
  685. 'detail' => '流程列表【' . $labelDetail['title'] . '】重命名【' . $title . '】',
  686. 'indate' => Base::time()
  687. ]);
  688. }
  689. //
  690. return Base::retSuccess('修改成功');
  691. }
  692. /**
  693. * 项目子分类-删除分类
  694. *
  695. * @apiParam {Number} projectid 项目ID
  696. * @apiParam {Number} labelid 分类ID
  697. *
  698. * @throws \Throwable
  699. */
  700. public function label__delete()
  701. {
  702. $user = Users::authE();
  703. if (Base::isError($user)) {
  704. return $user;
  705. } else {
  706. $user = $user['data'];
  707. }
  708. //
  709. $projectid = trim(Request::input('projectid'));
  710. $inRes = Project::inThe($projectid, $user['username']);
  711. if (Base::isError($inRes)) {
  712. return $inRes;
  713. }
  714. //
  715. $labelid = intval(Request::input('labelid'));
  716. $labelDetail = Base::DBC2A(DB::table('project_label')->where('id', $labelid)->where('projectid', $projectid)->first());
  717. if (empty($labelDetail)) {
  718. return Base::retError('列表不存在或已被删除!');
  719. }
  720. //
  721. return DB::transaction(function () use ($user, $projectid, $labelDetail) {
  722. $taskLists = Base::DBC2A(DB::table('project_task')->where('labelid', $labelDetail['id'])->get());
  723. $logArray = [];
  724. foreach ($taskLists AS $task) {
  725. $logArray[] = [
  726. 'type' => '日志',
  727. 'projectid' => $projectid,
  728. 'taskid' => $task['id'],
  729. 'username' => $user['username'],
  730. 'detail' => '删除列表任务【' . $task['title'] . '】',
  731. 'indate' => Base::time()
  732. ];
  733. }
  734. $logArray[] = [
  735. 'type' => '日志',
  736. 'projectid' => $projectid,
  737. 'taskid' => 0,
  738. 'username' => $user['username'],
  739. 'detail' => '删除流程列表【' . $labelDetail['title'] . '】',
  740. 'indate' => Base::time()
  741. ];
  742. DB::table('project_task')->where('labelid', $labelDetail['id'])->update([
  743. 'delete' => 1,
  744. 'deletedate' => Base::time()
  745. ]);
  746. DB::table('project_label')->where('id', $labelDetail['id'])->delete();
  747. DB::table('project_log')->insert($logArray);
  748. //
  749. return Base::retSuccess('删除成功');
  750. });
  751. }
  752. /**
  753. * 项目任务-列表
  754. *
  755. * @apiParam {Number} projectid 项目ID
  756. * @apiParam {Number} [labelid] 项目子分类ID
  757. * @apiParam {String} [archived] 是否归档
  758. * - 未归档 (默认)
  759. * - 已归档
  760. * - 全部
  761. * @apiParam {String} [type] 任务类型
  762. * - 全部(默认)
  763. * - 未完成
  764. * - 已超期
  765. * - 已完成
  766. * @apiParam {Number} [statistics] 是否获取统计数据(1:获取)
  767. * @apiParam {Number} [levelsort] 是否按紧急排序(1:是)
  768. * @apiParam {Number} [page] 当前页,默认:1
  769. * @apiParam {Number} [pagesize] 每页显示数量,默认:20,最大:100
  770. */
  771. public function task__lists()
  772. {
  773. $user = Users::authE();
  774. if (Base::isError($user)) {
  775. return $user;
  776. } else {
  777. $user = $user['data'];
  778. }
  779. //
  780. $projectid = intval(Request::input('projectid'));
  781. $inRes = Project::inThe($projectid, $user['username']);
  782. if (Base::isError($inRes)) {
  783. return $inRes;
  784. }
  785. //
  786. $orderBy = '`id` ASC';
  787. $whereArray = [];
  788. $whereArray[] = ['project_lists.id', '=', $projectid];
  789. $whereArray[] = ['project_lists.delete', '=', 0];
  790. $whereArray[] = ['project_task.delete', '=', 0];
  791. if (intval(Request::input('labelid')) > 0) {
  792. $whereArray[] = ['project_task.labelid', '=', intval(Request::input('labelid'))];
  793. }
  794. $archived = trim(Request::input('archived'));
  795. switch ($archived) {
  796. case '已归档':
  797. $whereArray[] = ['project_task.archived', '=', 1];
  798. $orderBy = '`archiveddate` DESC';
  799. break;
  800. case '未归档':
  801. default:
  802. $whereArray[] = ['project_task.archived', '=', 0];
  803. break;
  804. }
  805. $type = trim(Request::input('type'));
  806. switch ($type) {
  807. case '未完成':
  808. $whereArray[] = ['project_task.complete', '=', 0];
  809. break;
  810. case '已超期':
  811. $whereArray[] = ['project_task.complete', '=', 0];
  812. $whereArray[] = ['project_task.enddate', '>', 0];
  813. $whereArray[] = ['project_task.enddate', '<=', Base::time()];
  814. break;
  815. case '已完成':
  816. $whereArray[] = ['project_task.complete', '=', 1];
  817. break;
  818. }
  819. if (intval(Request::input('levelsort')) == 1) {
  820. $orderBy = '`level` ASC,' . $orderBy;
  821. }
  822. //
  823. $lists = DB::table('project_lists')
  824. ->join('project_task', 'project_lists.id', '=', 'project_task.projectid')
  825. ->select(['project_task.*'])
  826. ->where($whereArray)
  827. ->orderByRaw($orderBy)->paginate(Min(Max(Base::nullShow(Request::input('pagesize'), 10), 1), 100));
  828. $lists = Base::getPageList($lists);
  829. if (intval(Request::input('statistics')) == 1) {
  830. $lists['statistics_unfinished'] = $type === '未完成' ? $lists['total'] : DB::table('project_task')->where('projectid', $projectid)->where('delete', 0)->where('complete', 0)->count();
  831. $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();
  832. $lists['statistics_complete'] = $type === '已完成' ? $lists['total'] : DB::table('project_task')->where('projectid', $projectid)->where('delete', 0)->where('complete', 1)->count();
  833. }
  834. if ($lists['total'] == 0) {
  835. return Base::retError('未找到任何相关的任务', $lists);
  836. }
  837. foreach ($lists['lists'] AS $key => $info) {
  838. $info['overdue'] = Project::taskIsOverdue($info);
  839. $lists['lists'][$key] = array_merge($info, Users::username2basic($info['username']));
  840. }
  841. return Base::retSuccess('success', $lists);
  842. }
  843. /**
  844. * 项目任务-添加任务
  845. *
  846. * @apiParam {Number} projectid 项目ID
  847. * @apiParam {Number} labelid 项目子分类ID
  848. * @apiParam {String} title 任务标题
  849. * @apiParam {Number} [level] 任务紧急级别(1~4,默认:2)
  850. * @apiParam {String} [username] 任务负责人用户名
  851. * - 0: 未归档
  852. * - 1: 已归档
  853. *
  854. * @throws \Throwable
  855. */
  856. public function task__add()
  857. {
  858. $user = Users::authE();
  859. if (Base::isError($user)) {
  860. return $user;
  861. } else {
  862. $user = $user['data'];
  863. }
  864. //
  865. $projectid = intval(Request::input('projectid'));
  866. $projectDetail = Base::DBC2A(DB::table('project_lists')->where('id', $projectid)->where('delete', 0)->first());
  867. if (empty($projectDetail)) {
  868. return Base::retError('项目不存在或已被删除!');
  869. }
  870. //
  871. $labelid = intval(Request::input('labelid'));
  872. $labelDetail = Base::DBC2A(DB::table('project_label')->where('id', $labelid)->where('projectid', $projectid)->first());
  873. if (empty($labelDetail)) {
  874. return Base::retError('项目子分类不存在或已被删除!');
  875. }
  876. //
  877. $inRes = Project::inThe($projectid, $user['username']);
  878. if (Base::isError($inRes)) {
  879. return $inRes;
  880. }
  881. //
  882. $username = trim(Request::input('username'));
  883. if (empty($username)) {
  884. $username = $user['username'];
  885. }
  886. if ($username != $user['username']) {
  887. $inRes = Project::inThe($projectid, $username);
  888. if (Base::isError($inRes)) {
  889. return Base::retError('负责人不在项目成员内!');
  890. }
  891. }
  892. //
  893. $title = trim(Request::input('title'));
  894. if (empty($title)) {
  895. return Base::retError('任务标题不能为空!');
  896. } elseif (mb_strlen($title) > 255) {
  897. return Base::retError('任务标题最多只能设置255个字!');
  898. }
  899. //
  900. $inArray = [
  901. 'projectid' => $projectid,
  902. 'labelid' => $labelid,
  903. 'createuser' => $user['username'],
  904. 'username' => $username,
  905. 'title' => $title,
  906. 'level' => max(1, min(4, intval(Request::input('level')))),
  907. 'indate' => Base::time(),
  908. 'subtask' => Base::array2string([]),
  909. 'files' => Base::array2string([]),
  910. 'follower' => Base::array2string([]),
  911. ];
  912. return DB::transaction(function () use ($inArray) {
  913. $taskid = DB::table('project_task')->insertGetId($inArray);
  914. if (empty($taskid)) {
  915. return Base::retError('系统繁忙,请稍后再试!');
  916. }
  917. DB::table('project_log')->insert([
  918. 'type' => '日志',
  919. 'projectid' => $inArray['projectid'],
  920. 'taskid' => $taskid,
  921. 'username' => $inArray['createuser'],
  922. 'detail' => '添加任务【' . $inArray['title'] . '】',
  923. 'indate' => Base::time()
  924. ]);
  925. Project::updateNum($inArray['projectid']);
  926. return Base::retSuccess('添加成功!');
  927. });
  928. }
  929. /**
  930. * 项目任务-归档、取消归档
  931. *
  932. * @apiParam {String} act
  933. * - cancel: 取消归档
  934. * - else: 加入归档
  935. * @apiParam {Number} taskid 任务ID
  936. */
  937. public function task__archived()
  938. {
  939. $user = Users::authE();
  940. if (Base::isError($user)) {
  941. return $user;
  942. } else {
  943. $user = $user['data'];
  944. }
  945. //
  946. $taskid = intval(Request::input('taskid'));
  947. $task = Base::DBC2A(DB::table('project_lists')
  948. ->join('project_task', 'project_lists.id', '=', 'project_task.projectid')
  949. ->select(['project_task.projectid', 'project_task.title', 'project_task.archived'])
  950. ->where([
  951. ['project_lists.delete', '=', 0],
  952. ['project_task.delete', '=', 0],
  953. ['project_task.id', '=', $taskid],
  954. ])
  955. ->first());
  956. if (empty($task)) {
  957. return Base::retError('任务不存在!');
  958. }
  959. $inRes = Project::inThe($task['projectid'], $user['username']);
  960. if (Base::isError($inRes)) {
  961. return $inRes;
  962. }
  963. //
  964. switch (Request::input('act')) {
  965. case 'cancel': {
  966. if ($task['archived'] == 0) {
  967. return Base::retError('任务未归档!');
  968. }
  969. DB::table('project_task')->where('id', $taskid)->update([
  970. 'archived' => 1,
  971. 'archiveddate' => Base::time()
  972. ]);
  973. DB::table('project_log')->insert([
  974. 'type' => '日志',
  975. 'projectid' => $task['projectid'],
  976. 'taskid' => $taskid,
  977. 'username' => $user['username'],
  978. 'detail' => '取消归档【' . $task['title'] . '】',
  979. 'indate' => Base::time()
  980. ]);
  981. return Base::retSuccess('取消归档成功');
  982. }
  983. default: {
  984. if ($task['archived'] == 1) {
  985. return Base::retError('任务已归档!');
  986. }
  987. DB::table('project_task')->where('id', $taskid)->update([
  988. 'archived' => 0,
  989. ]);
  990. DB::table('project_log')->insert([
  991. 'type' => '日志',
  992. 'projectid' => $task['projectid'],
  993. 'taskid' => $taskid,
  994. 'username' => $user['username'],
  995. 'detail' => '归档【' . $task['title'] . '】',
  996. 'indate' => Base::time()
  997. ]);
  998. return Base::retSuccess('加入归档成功');
  999. }
  1000. }
  1001. }
  1002. }