ProjectController.php 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031
  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 {Number} [page] 当前页,默认:1
  772. * @apiParam {Number} [pagesize] 每页显示数量,默认:20,最大:100
  773. */
  774. public function task__lists()
  775. {
  776. $user = Users::authE();
  777. if (Base::isError($user)) {
  778. return $user;
  779. } else {
  780. $user = $user['data'];
  781. }
  782. //
  783. $projectid = intval(Request::input('projectid'));
  784. $inRes = Project::inThe($projectid, $user['username']);
  785. if (Base::isError($inRes)) {
  786. return $inRes;
  787. }
  788. //
  789. $orderBy = '`inorder` DESC,`id` DESC';
  790. $whereArray = [];
  791. $whereArray[] = ['project_lists.id', '=', $projectid];
  792. $whereArray[] = ['project_lists.delete', '=', 0];
  793. $whereArray[] = ['project_task.delete', '=', 0];
  794. if (intval(Request::input('labelid')) > 0) {
  795. $whereArray[] = ['project_task.labelid', '=', intval(Request::input('labelid'))];
  796. }
  797. if (intval(Request::input('level')) > 0) {
  798. $whereArray[] = ['project_task.level', '=', intval(Request::input('level'))];
  799. }
  800. if (trim(Request::input('username'))) {
  801. $whereArray[] = ['project_task.username', '=', trim(Request::input('username'))];
  802. }
  803. $archived = trim(Request::input('archived'));
  804. if (empty($archived)) $archived = "未归档";
  805. switch ($archived) {
  806. case '已归档':
  807. $whereArray[] = ['project_task.archived', '=', 1];
  808. $orderBy = '`archiveddate` DESC';
  809. break;
  810. case '未归档':
  811. $whereArray[] = ['project_task.archived', '=', 0];
  812. break;
  813. }
  814. $type = trim(Request::input('type'));
  815. switch ($type) {
  816. case '未完成':
  817. $whereArray[] = ['project_task.complete', '=', 0];
  818. break;
  819. case '已超期':
  820. $whereArray[] = ['project_task.complete', '=', 0];
  821. $whereArray[] = ['project_task.enddate', '>', 0];
  822. $whereArray[] = ['project_task.enddate', '<=', Base::time()];
  823. break;
  824. case '已完成':
  825. $whereArray[] = ['project_task.complete', '=', 1];
  826. break;
  827. }
  828. //
  829. $lists = DB::table('project_lists')
  830. ->join('project_task', 'project_lists.id', '=', 'project_task.projectid')
  831. ->select(['project_task.*'])
  832. ->where($whereArray)
  833. ->orderByRaw($orderBy)->paginate(Min(Max(Base::nullShow(Request::input('pagesize'), 10), 1), 100));
  834. $lists = Base::getPageList($lists);
  835. if (intval(Request::input('statistics')) == 1) {
  836. $lists['statistics_unfinished'] = $type === '未完成' ? $lists['total'] : DB::table('project_task')->where('projectid', $projectid)->where('delete', 0)->where('complete', 0)->count();
  837. $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();
  838. $lists['statistics_complete'] = $type === '已完成' ? $lists['total'] : DB::table('project_task')->where('projectid', $projectid)->where('delete', 0)->where('complete', 1)->count();
  839. }
  840. if ($lists['total'] == 0) {
  841. return Base::retError('未找到任何相关的任务', $lists);
  842. }
  843. foreach ($lists['lists'] AS $key => $info) {
  844. $info['overdue'] = Project::taskIsOverdue($info);
  845. $lists['lists'][$key] = array_merge($info, Users::username2basic($info['username']));
  846. }
  847. return Base::retSuccess('success', $lists);
  848. }
  849. /**
  850. * 项目任务-添加任务
  851. *
  852. * @apiParam {Number} projectid 项目ID
  853. * @apiParam {Number} labelid 项目子分类ID
  854. * @apiParam {String} title 任务标题
  855. * @apiParam {Number} [level] 任务紧急级别(1~4,默认:2)
  856. * @apiParam {String} [username] 任务负责人用户名
  857. * - 0: 未归档
  858. * - 1: 已归档
  859. *
  860. * @throws \Throwable
  861. */
  862. public function task__add()
  863. {
  864. $user = Users::authE();
  865. if (Base::isError($user)) {
  866. return $user;
  867. } else {
  868. $user = $user['data'];
  869. }
  870. //
  871. $projectid = intval(Request::input('projectid'));
  872. $projectDetail = Base::DBC2A(DB::table('project_lists')->where('id', $projectid)->where('delete', 0)->first());
  873. if (empty($projectDetail)) {
  874. return Base::retError('项目不存在或已被删除!');
  875. }
  876. //
  877. $labelid = intval(Request::input('labelid'));
  878. $labelDetail = Base::DBC2A(DB::table('project_label')->where('id', $labelid)->where('projectid', $projectid)->first());
  879. if (empty($labelDetail)) {
  880. return Base::retError('项目子分类不存在或已被删除!');
  881. }
  882. //
  883. $inRes = Project::inThe($projectid, $user['username']);
  884. if (Base::isError($inRes)) {
  885. return $inRes;
  886. }
  887. //
  888. $username = trim(Request::input('username'));
  889. if (empty($username)) {
  890. $username = $user['username'];
  891. }
  892. if ($username != $user['username']) {
  893. $inRes = Project::inThe($projectid, $username);
  894. if (Base::isError($inRes)) {
  895. return Base::retError('负责人不在项目成员内!');
  896. }
  897. }
  898. //
  899. $title = trim(Request::input('title'));
  900. if (empty($title)) {
  901. return Base::retError('任务标题不能为空!');
  902. } elseif (mb_strlen($title) > 255) {
  903. return Base::retError('任务标题最多只能设置255个字!');
  904. }
  905. //
  906. $inArray = [
  907. 'projectid' => $projectid,
  908. 'labelid' => $labelid,
  909. 'createuser' => $user['username'],
  910. 'username' => $username,
  911. 'title' => $title,
  912. 'level' => max(1, min(4, intval(Request::input('level')))),
  913. 'indate' => Base::time(),
  914. 'subtask' => Base::array2string([]),
  915. 'files' => Base::array2string([]),
  916. 'follower' => Base::array2string([]),
  917. ];
  918. return DB::transaction(function () use ($inArray) {
  919. $taskid = DB::table('project_task')->insertGetId($inArray);
  920. if (empty($taskid)) {
  921. return Base::retError('系统繁忙,请稍后再试!');
  922. }
  923. DB::table('project_log')->insert([
  924. 'type' => '日志',
  925. 'projectid' => $inArray['projectid'],
  926. 'taskid' => $taskid,
  927. 'username' => $inArray['createuser'],
  928. 'detail' => '添加任务【' . $inArray['title'] . '】',
  929. 'indate' => Base::time()
  930. ]);
  931. Project::updateNum($inArray['projectid']);
  932. //
  933. $task = Base::DBC2A(DB::table('project_task')->where('id', $taskid)->first());
  934. $task['overdue'] = Project::taskIsOverdue($task);
  935. $task = array_merge($task, Users::username2basic($task['username']));
  936. return Base::retSuccess('添加成功!', $task);
  937. });
  938. }
  939. /**
  940. * 项目任务-归档、取消归档
  941. *
  942. * @apiParam {String} act
  943. * - cancel: 取消归档
  944. * - else: 加入归档
  945. * @apiParam {Number} taskid 任务ID
  946. */
  947. public function task__archived()
  948. {
  949. $user = Users::authE();
  950. if (Base::isError($user)) {
  951. return $user;
  952. } else {
  953. $user = $user['data'];
  954. }
  955. //
  956. $taskid = intval(Request::input('taskid'));
  957. $task = Base::DBC2A(DB::table('project_lists')
  958. ->join('project_task', 'project_lists.id', '=', 'project_task.projectid')
  959. ->select(['project_task.projectid', 'project_task.title', 'project_task.archived'])
  960. ->where([
  961. ['project_lists.delete', '=', 0],
  962. ['project_task.delete', '=', 0],
  963. ['project_task.id', '=', $taskid],
  964. ])
  965. ->first());
  966. if (empty($task)) {
  967. return Base::retError('任务不存在!');
  968. }
  969. $inRes = Project::inThe($task['projectid'], $user['username']);
  970. if (Base::isError($inRes)) {
  971. return $inRes;
  972. }
  973. //
  974. switch (Request::input('act')) {
  975. case 'cancel': {
  976. if ($task['archived'] == 0) {
  977. return Base::retError('任务未归档!');
  978. }
  979. DB::table('project_task')->where('id', $taskid)->update([
  980. 'archived' => 1,
  981. 'archiveddate' => Base::time()
  982. ]);
  983. DB::table('project_log')->insert([
  984. 'type' => '日志',
  985. 'projectid' => $task['projectid'],
  986. 'taskid' => $taskid,
  987. 'username' => $user['username'],
  988. 'detail' => '取消归档【' . $task['title'] . '】',
  989. 'indate' => Base::time()
  990. ]);
  991. return Base::retSuccess('取消归档成功');
  992. }
  993. default: {
  994. if ($task['archived'] == 1) {
  995. return Base::retError('任务已归档!');
  996. }
  997. DB::table('project_task')->where('id', $taskid)->update([
  998. 'archived' => 0,
  999. ]);
  1000. DB::table('project_log')->insert([
  1001. 'type' => '日志',
  1002. 'projectid' => $task['projectid'],
  1003. 'taskid' => $taskid,
  1004. 'username' => $user['username'],
  1005. 'detail' => '归档【' . $task['title'] . '】',
  1006. 'indate' => Base::time()
  1007. ]);
  1008. return Base::retSuccess('加入归档成功');
  1009. }
  1010. }
  1011. }
  1012. }