ProjectController.php 87 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255
  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. $info['subtask'] = Base::string2array($info['subtask']);
  108. $info['follower'] = Base::string2array($info['follower']);
  109. $taskLists[] = array_merge($info, Users::username2basic($info['username']));
  110. }
  111. }
  112. $label[$index]['taskLists'] = $taskLists;
  113. $simpleLabel[] = ['id' => $temp['id'], 'title' => $temp['title']];
  114. }
  115. //
  116. return Base::retSuccess('success', [
  117. 'project' => $projectDetail,
  118. 'label' => $label,
  119. 'simpleLabel' => $simpleLabel,
  120. ]);
  121. }
  122. /**
  123. * 获取项目负责人
  124. *
  125. * @apiParam {Number} projectid 项目ID
  126. */
  127. public function leader()
  128. {
  129. $user = Users::authE();
  130. if (Base::isError($user)) {
  131. return $user;
  132. } else {
  133. $user = $user['data'];
  134. }
  135. //
  136. $projectid = trim(Request::input('projectid'));
  137. $projectDetail = Base::DBC2A(DB::table('project_lists')->select(['username'])->where('id', $projectid)->where('delete', 0)->first());
  138. if (empty($projectDetail)) {
  139. return Base::retError('项目不存在或已被删除!');
  140. }
  141. return Base::retSuccess('success', [
  142. 'username' => $projectDetail['username'],
  143. ]);
  144. }
  145. /**
  146. * 添加项目
  147. *
  148. * @apiParam {String} title 项目名称
  149. * @apiParam {Array} labels 流程,格式[流程1, 流程2]
  150. */
  151. public function add()
  152. {
  153. $user = Users::authE();
  154. if (Base::isError($user)) {
  155. return $user;
  156. } else {
  157. $user = $user['data'];
  158. }
  159. //项目名称
  160. $title = trim(Request::input('title'));
  161. if (mb_strlen($title) < 2) {
  162. return Base::retError('项目名称不可以少于2个字!');
  163. } elseif (mb_strlen($title) > 32) {
  164. return Base::retError('项目名称最多只能设置32个字!');
  165. }
  166. //流程
  167. $labels = Request::input('labels');
  168. if (!is_array($labels)) $labels = [];
  169. $insertLabels = [];
  170. $inorder = 0;
  171. foreach ($labels AS $label) {
  172. $label = trim($label);
  173. if ($label) {
  174. $insertLabels[] = [
  175. 'title' => $label,
  176. 'inorder' => $inorder++,
  177. ];
  178. }
  179. }
  180. if (empty($insertLabels)) {
  181. $insertLabels[] = [
  182. 'title' => '默认',
  183. 'inorder' => 0,
  184. ];
  185. }
  186. //开始创建
  187. $projectid = DB::table('project_lists')->insertGetId([
  188. 'title' => $title,
  189. 'username' => $user['username'],
  190. 'createuser' => $user['username'],
  191. 'indate' => Base::time()
  192. ]);
  193. if ($projectid) {
  194. foreach ($insertLabels AS $key => $label) {
  195. $insertLabels[$key]['projectid'] = $projectid;
  196. }
  197. DB::table('project_label')->insert($insertLabels);
  198. DB::table('project_log')->insert([
  199. 'type' => '日志',
  200. 'projectid' => $projectid,
  201. 'username' => $user['username'],
  202. 'detail' => '创建项目',
  203. 'indate' => Base::time()
  204. ]);
  205. DB::table('project_users')->insert([
  206. 'type' => '成员',
  207. 'projectid' => $projectid,
  208. 'isowner' => 1,
  209. 'username' => $user['username'],
  210. 'indate' => Base::time()
  211. ]);
  212. return Base::retSuccess('添加成功!');
  213. } else {
  214. return Base::retError('添加失败!');
  215. }
  216. }
  217. /**
  218. * 收藏项目
  219. *
  220. * @apiParam {String} act 类型
  221. * - cancel: 取消收藏
  222. * - else: 添加收藏
  223. * @apiParam {Number} projectid 项目ID
  224. *
  225. * @throws \Throwable
  226. */
  227. public function favor()
  228. {
  229. $user = Users::authE();
  230. if (Base::isError($user)) {
  231. return $user;
  232. } else {
  233. $user = $user['data'];
  234. }
  235. //
  236. $projectid = trim(Request::input('projectid'));
  237. $projectDetail = Base::DBC2A(DB::table('project_lists')->where('id', $projectid)->where('delete', 0)->first());
  238. if (empty($projectDetail)) {
  239. return Base::retError('项目不存在或已被删除!');
  240. }
  241. return DB::transaction(function () use ($projectDetail, $user) {
  242. switch (Request::input('act')) {
  243. case 'cancel': {
  244. if (DB::table('project_users')->where([
  245. 'type' => '收藏',
  246. 'projectid' => $projectDetail['id'],
  247. 'username' => $user['username'],
  248. ])->delete()) {
  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. default: {
  261. $row = Base::DBC2A(DB::table('project_users')->where([
  262. 'type' => '收藏',
  263. 'projectid' => $projectDetail['id'],
  264. 'username' => $user['username'],
  265. ])->lockForUpdate()->first());
  266. if (empty($row)) {
  267. DB::table('project_users')->insert([
  268. 'type' => '收藏',
  269. 'projectid' => $projectDetail['id'],
  270. 'isowner' => $projectDetail['username'] == $user['username'] ? 1 : 0,
  271. 'username' => $user['username'],
  272. 'indate' => Base::time()
  273. ]);
  274. DB::table('project_log')->insert([
  275. 'type' => '日志',
  276. 'projectid' => $projectDetail['id'],
  277. 'username' => $user['username'],
  278. 'detail' => '收藏项目',
  279. 'indate' => Base::time()
  280. ]);
  281. return Base::retSuccess('收藏成功!');
  282. }
  283. return Base::retSuccess('已收藏!');
  284. }
  285. }
  286. });
  287. }
  288. /**
  289. * 重命名项目
  290. *
  291. * @apiParam {Number} projectid 项目ID
  292. * @apiParam {String} title 项目新名称
  293. */
  294. public function rename()
  295. {
  296. $user = Users::authE();
  297. if (Base::isError($user)) {
  298. return $user;
  299. } else {
  300. $user = $user['data'];
  301. }
  302. //
  303. $projectid = trim(Request::input('projectid'));
  304. $projectDetail = Base::DBC2A(DB::table('project_lists')->where('id', $projectid)->where('delete', 0)->first());
  305. if (empty($projectDetail)) {
  306. return Base::retError('项目不存在或已被删除!');
  307. }
  308. if ($projectDetail['username'] != $user['username']) {
  309. return Base::retError('你不是项目负责人!');
  310. }
  311. //
  312. $title = trim(Request::input('title'));
  313. if (mb_strlen($title) < 2) {
  314. return Base::retError('项目名称不可以少于2个字!');
  315. } elseif (mb_strlen($title) > 32) {
  316. return Base::retError('项目名称最多只能设置32个字!');
  317. }
  318. //
  319. DB::table('project_lists')->where('id', $projectDetail['id'])->update([
  320. 'title' => $title
  321. ]);
  322. DB::table('project_log')->insert([
  323. 'type' => '日志',
  324. 'projectid' => $projectDetail['id'],
  325. 'username' => $user['username'],
  326. 'detail' => '【' . $projectDetail['title'] . '】重命名【' . $title . '】',
  327. 'indate' => Base::time()
  328. ]);
  329. //
  330. return Base::retSuccess('修改成功!');
  331. }
  332. /**
  333. * 移交项目
  334. *
  335. * @apiParam {Number} projectid 项目ID
  336. * @apiParam {String} username 项目新负责人用户名
  337. *
  338. * @throws \Throwable
  339. */
  340. public function transfer()
  341. {
  342. $user = Users::authE();
  343. if (Base::isError($user)) {
  344. return $user;
  345. } else {
  346. $user = $user['data'];
  347. }
  348. //
  349. $projectid = trim(Request::input('projectid'));
  350. $projectDetail = Base::DBC2A(DB::table('project_lists')->where('id', $projectid)->where('delete', 0)->first());
  351. if (empty($projectDetail)) {
  352. return Base::retError('项目不存在或已被删除!');
  353. }
  354. if ($projectDetail['username'] != $user['username']) {
  355. return Base::retError('你不是项目负责人!');
  356. }
  357. //
  358. $username = trim(Request::input('username'));
  359. if ($username == $projectDetail['username']) {
  360. return Base::retError('你已是项目负责人!');
  361. }
  362. $count = DB::table('users')->where('username', $username)->count();
  363. if ($count <= 0) {
  364. return Base::retError(['成员用户名(%)不存在!', $username]);
  365. }
  366. //判断是否已在项目成员内
  367. $inRes = Project::inThe($projectDetail['id'], $username);
  368. if (Base::isError($inRes)) {
  369. DB::table('project_users')->insert([
  370. 'type' => '成员',
  371. 'projectid' => $projectDetail['id'],
  372. 'isowner' => 0,
  373. 'username' => $username,
  374. 'indate' => Base::time()
  375. ]);
  376. DB::table('project_log')->insert([
  377. 'type' => '日志',
  378. 'projectid' => $projectDetail['id'],
  379. 'username' => $username,
  380. 'detail' => '自动加入项目',
  381. 'indate' => Base::time()
  382. ]);
  383. }
  384. //开始移交
  385. return DB::transaction(function () use ($user, $username, $projectDetail) {
  386. DB::table('project_lists')->where('id', $projectDetail['id'])->update([
  387. 'username' => $username
  388. ]);
  389. DB::table('project_log')->insert([
  390. 'type' => '日志',
  391. 'projectid' => $projectDetail['id'],
  392. 'username' => $user['username'],
  393. 'detail' => '【' . $projectDetail['username'] . '】移交给【' . $username . '】',
  394. 'indate' => Base::time()
  395. ]);
  396. DB::table('project_users')->where([
  397. 'projectid' => $projectDetail['id'],
  398. 'username' => $projectDetail['username'],
  399. ])->update([
  400. 'isowner' => 0
  401. ]);
  402. DB::table('project_users')->where([
  403. 'projectid' => $projectDetail['id'],
  404. 'username' => $username,
  405. ])->update([
  406. 'isowner' => 1
  407. ]);
  408. return Base::retSuccess('移交成功!');
  409. });
  410. }
  411. /**
  412. * 删除项目
  413. *
  414. * @apiParam {Number} projectid 项目ID
  415. */
  416. public function delete()
  417. {
  418. $user = Users::authE();
  419. if (Base::isError($user)) {
  420. return $user;
  421. } else {
  422. $user = $user['data'];
  423. }
  424. //
  425. $projectid = trim(Request::input('projectid'));
  426. $projectDetail = Base::DBC2A(DB::table('project_lists')->where('id', $projectid)->where('delete', 0)->first());
  427. if (empty($projectDetail)) {
  428. return Base::retError('项目不存在或已被删除!');
  429. }
  430. if ($projectDetail['username'] != $user['username']) {
  431. return Base::retError('你不是项目负责人!');
  432. }
  433. //
  434. DB::table('project_lists')->where('id', $projectDetail['id'])->update([
  435. 'delete' => 1,
  436. 'deletedate' => Base::time()
  437. ]);
  438. DB::table('project_log')->insert([
  439. 'type' => '日志',
  440. 'projectid' => $projectDetail['id'],
  441. 'username' => $user['username'],
  442. 'detail' => '删除项目',
  443. 'indate' => Base::time()
  444. ]);
  445. //
  446. return Base::retSuccess('删除成功!');
  447. }
  448. /**
  449. * 排序任务
  450. *
  451. * @apiParam {Number} projectid 项目ID
  452. * @apiParam {String} oldsort 旧排序数据
  453. * @apiParam {String} newsort 新排序数据
  454. * @apiParam {Number} label 赋值表示排序分类,否则排序任务(调整任务所属分类)
  455. */
  456. public function sort()
  457. {
  458. $user = Users::authE();
  459. if (Base::isError($user)) {
  460. return $user;
  461. } else {
  462. $user = $user['data'];
  463. }
  464. //
  465. $projectid = trim(Request::input('projectid'));
  466. $inRes = Project::inThe($projectid, $user['username']);
  467. if (Base::isError($inRes)) {
  468. return $inRes;
  469. }
  470. //
  471. $oldSort = explode(";", Request::input('oldsort'));
  472. $newSort = explode(";", Request::input('newsort'));
  473. if (count($oldSort) != count($newSort)) {
  474. return Base::retError('参数错误!');
  475. }
  476. if (intval(Request::input('label'))) {
  477. //排序分类
  478. foreach ($newSort AS $sort => $item) {
  479. list($newLabelid, $newTask) = explode(':', $item);
  480. list($oldLabelid, $oldTask) = explode(':', $oldSort[$sort]);
  481. if ($newLabelid != $oldLabelid) {
  482. DB::table('project_label')->where([
  483. 'id' => $newLabelid,
  484. 'projectid' => $projectid
  485. ])->update([
  486. 'inorder' => intval($sort)
  487. ]);
  488. }
  489. }
  490. $detail = '调整任务列表排序';
  491. $sortType = 'label';
  492. } else {
  493. //排序任务(调整任务归类)
  494. foreach ($newSort AS $sort => $item) {
  495. list($newLabelid, $newTask) = explode(':', $item);
  496. list($oldLabelid, $oldTask) = explode(':', $oldSort[$sort]);
  497. if ($newTask != $oldTask) {
  498. $newTask = explode('-', $newTask);
  499. $inorder = count($newTask);
  500. foreach ($newTask AS $taskid) {
  501. DB::table('project_task')->where([
  502. 'id' => $taskid,
  503. 'projectid' => $projectid
  504. ])->update([
  505. 'labelid' => $newLabelid,
  506. 'inorder' => $inorder
  507. ]);
  508. $inorder--;
  509. }
  510. }
  511. }
  512. $detail = '调整任务排序';
  513. $sortType = 'task';
  514. }
  515. //
  516. $row = Base::DBC2A(DB::table('project_log')->where([ 'type' => '日志', 'projectid' => $projectid ])->orderByDesc('id')->first());
  517. $continue = 1;
  518. if ($row && $row['username'] == $user['username'] && $row['indate'] + 300 > Base::time()) {
  519. $other = Base::string2array($row['other']);
  520. if ($other['sortType'] == $sortType) {
  521. $continue = intval($other['continue']) + 1;
  522. if ($continue <= 100) {
  523. DB::table('project_log')->where('id', $row['id'])->update([
  524. 'detail' => $detail . '(' . $continue . '次)',
  525. 'other' => Base::array2string([
  526. 'sortType' => $sortType,
  527. 'continue' => $continue,
  528. 'times' => $other['times'] . '|' . Base::time(),
  529. ])
  530. ]);
  531. }
  532. }
  533. }
  534. if ($continue == 1) {
  535. DB::table('project_log')->insert([
  536. 'type' => '日志',
  537. 'projectid' => $projectid,
  538. 'username' => $user['username'],
  539. 'detail' => $detail,
  540. 'indate' => Base::time(),
  541. 'other' => Base::array2string([
  542. 'sortType' => $sortType,
  543. 'continue' => $continue,
  544. 'times' => Base::time(),
  545. ])
  546. ]);
  547. }
  548. return Base::retSuccess('保存成功!');
  549. }
  550. /**
  551. * 排序任务(todo待办)
  552. *
  553. * @apiParam {String} oldsort 旧排序数据
  554. * @apiParam {String} newsort 新排序数据
  555. */
  556. public function sort__todo()
  557. {
  558. $user = Users::authE();
  559. if (Base::isError($user)) {
  560. return $user;
  561. } else {
  562. $user = $user['data'];
  563. }
  564. //
  565. $oldSort = explode(";", Request::input('oldsort'));
  566. $newSort = explode(";", Request::input('newsort'));
  567. if (count($oldSort) != count($newSort)) {
  568. return Base::retError('参数错误!');
  569. }
  570. //
  571. $levels = [];
  572. $logArray = [];
  573. $taskLevel = [];
  574. foreach ($newSort AS $sort => $item) {
  575. list($newLevel, $newTask) = explode(':', $item);
  576. list($oldLevel, $oldTask) = explode(':', $oldSort[$sort]);
  577. if ($newTask != $oldTask) {
  578. $newTask = explode('-', $newTask);
  579. $oldTask = explode('-', $oldTask);
  580. $userorder = intval(DB::table('project_task')->select('userorder')->where([
  581. 'delete' => 0,
  582. 'archived' => 0,
  583. 'level' => $newLevel,
  584. 'username' => $user['username'],
  585. ])->orderByDesc('userorder')->value('userorder'));
  586. if (count($newTask) < count($oldTask)) {
  587. $userorder--;
  588. } else {
  589. $userorder++;
  590. }
  591. foreach ($newTask AS $taskid) {
  592. $task = Base::DBC2A(DB::table('project_task')->select(['id', 'title', 'projectid', 'level', 'userorder'])->where([
  593. 'id' => $taskid,
  594. 'username' => $user['username']
  595. ])->first());
  596. $upArray = [];
  597. if ($task) {
  598. if ($task['level'] != $newLevel) {
  599. $upArray['level'] = $newLevel;
  600. $logArray[] = [
  601. 'type' => '日志',
  602. 'projectid' => $task['projectid'],
  603. 'taskid' => $task['id'],
  604. 'username' => $user['username'],
  605. 'detail' => '调整任务等级为【P' . $newLevel . '】',
  606. 'indate' => Base::time(),
  607. 'other' => Base::array2string([
  608. 'type' => 'task',
  609. 'id' => $task['id'],
  610. 'title' => $task['title'],
  611. ])
  612. ];
  613. $taskLevel[] = [
  614. 'id' => $task['id'],
  615. 'level' => $newLevel,
  616. ];
  617. }
  618. if ($task['userorder'] != $userorder) {
  619. $upArray['userorder'] = $userorder;
  620. }
  621. }
  622. if ($upArray) {
  623. DB::table('project_task')->where('id', $taskid)->update($upArray);
  624. }
  625. $userorder--;
  626. }
  627. $levels[] = $newLevel;
  628. }
  629. }
  630. if ($logArray) {
  631. DB::table('project_log')->insert($logArray);
  632. }
  633. //
  634. return Base::retSuccess('保存成功!', [
  635. 'levels' => $levels,
  636. 'taskLevel' => $taskLevel,
  637. ]);
  638. }
  639. /**
  640. * 退出项目
  641. *
  642. * @apiParam {Number} projectid 项目ID
  643. */
  644. public function out()
  645. {
  646. $user = Users::authE();
  647. if (Base::isError($user)) {
  648. return $user;
  649. } else {
  650. $user = $user['data'];
  651. }
  652. //
  653. $projectid = trim(Request::input('projectid'));
  654. $projectDetail = Base::DBC2A(DB::table('project_lists')->where('id', $projectid)->where('delete', 0)->first());
  655. if (empty($projectDetail)) {
  656. return Base::retError('项目不存在或已被删除!');
  657. }
  658. if ($projectDetail['username'] == $user['username']) {
  659. return Base::retError('你是项目负责人,不可退出项目!');
  660. }
  661. $inRes = Project::inThe($projectid, $user['username']);
  662. if (Base::isError($inRes)) {
  663. return $inRes;
  664. }
  665. //
  666. DB::table('project_users')->where([
  667. 'type' => '成员',
  668. 'projectid' => $projectDetail['id'],
  669. 'username' => $user['username'],
  670. ])->delete();
  671. DB::table('project_log')->insert([
  672. 'type' => '日志',
  673. 'projectid' => $projectDetail['id'],
  674. 'username' => $user['username'],
  675. 'detail' => '退出项目',
  676. 'indate' => Base::time()
  677. ]);
  678. //
  679. return Base::retSuccess('退出项目成功!');
  680. }
  681. /**
  682. * 项目成员-列表
  683. *
  684. * @apiParam {Number} projectid 项目ID
  685. * @apiParam {Number} [page] 当前页,默认:1
  686. * @apiParam {Number} [pagesize] 每页显示数量,默认:20,最大:100
  687. */
  688. public function users__lists()
  689. {
  690. $user = Users::authE();
  691. if (Base::isError($user)) {
  692. return $user;
  693. } else {
  694. $user = $user['data'];
  695. }
  696. //
  697. $projectid = intval(Request::input('projectid'));
  698. $inRes = Project::inThe($projectid, $user['username']);
  699. if (Base::isError($inRes)) {
  700. return $inRes;
  701. }
  702. //
  703. $lists = DB::table('project_lists')
  704. ->join('project_users', 'project_lists.id', '=', 'project_users.projectid')
  705. ->select(['project_lists.title', 'project_users.*'])
  706. ->where([
  707. ['project_lists.id', $projectid],
  708. ['project_lists.delete', 0],
  709. ['project_users.type', '成员'],
  710. ])
  711. ->orderByDesc('project_users.isowner')->orderByDesc('project_users.id')->paginate(Min(Max(Base::nullShow(Request::input('pagesize'), 10), 1), 100));
  712. $lists = Base::getPageList($lists);
  713. if ($lists['total'] == 0) {
  714. return Base::retError('未找到任何相关的成员');
  715. }
  716. foreach ($lists['lists'] AS $key => $projectDetail) {
  717. $userInfo = Users::username2basic($projectDetail['username']);
  718. $lists['lists'][$key]['userimg'] = $userInfo['userimg'];
  719. $lists['lists'][$key]['nickname'] = $userInfo['nickname'];
  720. $lists['lists'][$key]['profession'] = $userInfo['profession'];
  721. }
  722. return Base::retSuccess('success', $lists);
  723. }
  724. /**
  725. * 项目成员-添加、删除
  726. *
  727. * @apiParam {String} act
  728. * - delete: 删除成员
  729. * - else: 添加成员
  730. * @apiParam {Number} projectid 项目ID
  731. * @apiParam {Array|String} username 用户名(或用户名组)
  732. */
  733. public function users__join()
  734. {
  735. $user = Users::authE();
  736. if (Base::isError($user)) {
  737. return $user;
  738. } else {
  739. $user = $user['data'];
  740. }
  741. //
  742. $projectid = trim(Request::input('projectid'));
  743. $projectDetail = Base::DBC2A(DB::table('project_lists')->where('id', $projectid)->where('delete', 0)->first());
  744. if (empty($projectDetail)) {
  745. return Base::retError('项目不存在或已被删除!');
  746. }
  747. $usernames = Request::input('username');
  748. if (empty($usernames)) {
  749. return Base::retError('参数错误!');
  750. }
  751. if (!is_array($usernames)) {
  752. if (Base::strExists($usernames, ',')) {
  753. $usernames = explode(',', $usernames);
  754. } else {
  755. $usernames = [$usernames];
  756. }
  757. }
  758. //
  759. $logArray = [];
  760. foreach ($usernames AS $username) {
  761. $inRes = Project::inThe($projectid, $username);
  762. switch (Request::input('act')) {
  763. case 'delete': {
  764. if (!Base::isError($inRes) && $projectDetail['username'] != $username) {
  765. DB::table('project_users')->where([
  766. 'type' => '成员',
  767. 'projectid' => $projectid,
  768. 'username' => $username,
  769. ])->delete();
  770. $logArray[] = [
  771. 'type' => '日志',
  772. 'projectid' => $projectDetail['id'],
  773. 'username' => $user['username'],
  774. 'detail' => '将成员【' . $username . '】移出项目',
  775. 'indate' => Base::time()
  776. ];
  777. }
  778. break;
  779. }
  780. default: {
  781. if (Base::isError($inRes)) {
  782. DB::table('project_users')->insert([
  783. 'type' => '成员',
  784. 'projectid' => $projectid,
  785. 'isowner' => 0,
  786. 'username' => $username,
  787. 'indate' => Base::time()
  788. ]);
  789. $logArray[] = [
  790. 'type' => '日志',
  791. 'projectid' => $projectDetail['id'],
  792. 'username' => $username,
  793. 'detail' => '将成员【' . $username . '】加入项目',
  794. 'indate' => Base::time()
  795. ];
  796. }
  797. break;
  798. }
  799. }
  800. }
  801. return Base::retSuccess('操作完成!');
  802. }
  803. /**
  804. * 项目子分类-添加分类
  805. *
  806. * @apiParam {Number} projectid 项目ID
  807. * @apiParam {String} title 分类名称
  808. */
  809. public function label__add()
  810. {
  811. $user = Users::authE();
  812. if (Base::isError($user)) {
  813. return $user;
  814. } else {
  815. $user = $user['data'];
  816. }
  817. //
  818. $projectid = trim(Request::input('projectid'));
  819. $inRes = Project::inThe($projectid, $user['username']);
  820. if (Base::isError($inRes)) {
  821. return $inRes;
  822. }
  823. //
  824. $title = trim(Request::input('title'));
  825. if (empty($title)) {
  826. return Base::retError('列表名称不能为空!');
  827. } elseif (mb_strlen($title) > 32) {
  828. return Base::retError('列表名称最多只能设置32个字!');
  829. }
  830. //
  831. $count = DB::table('project_label')->where('projectid', $projectid)->where('title', $title)->count();
  832. if ($count > 0) {
  833. return Base::retError('列表名称已存在!');
  834. }
  835. //
  836. $id = DB::table('project_label')->insertGetId([
  837. 'projectid' => $projectid,
  838. 'title' => $title,
  839. 'inorder' => intval(DB::table('project_label')->where('projectid', $projectid)->orderByDesc('inorder')->value('inorder')) + 1,
  840. ]);
  841. if (empty($id)) {
  842. return Base::retError('系统繁忙,请稍后再试!');
  843. }
  844. DB::table('project_log')->insert([
  845. 'type' => '日志',
  846. 'projectid' => $projectid,
  847. 'username' => $user['username'],
  848. 'detail' => '添加任务列表【' . $title . '】',
  849. 'indate' => Base::time()
  850. ]);
  851. //
  852. $row = Base::DBC2A(DB::table('project_label')->where('id', $id)->first());
  853. $row['taskLists'] = [];
  854. return Base::retSuccess('添加成功!', $row);
  855. }
  856. /**
  857. * 项目子分类-重命名分类
  858. *
  859. * @apiParam {Number} projectid 项目ID
  860. * @apiParam {Number} labelid 分类ID
  861. * @apiParam {String} title 新分类名称
  862. */
  863. public function label__rename()
  864. {
  865. $user = Users::authE();
  866. if (Base::isError($user)) {
  867. return $user;
  868. } else {
  869. $user = $user['data'];
  870. }
  871. //
  872. $projectid = trim(Request::input('projectid'));
  873. $inRes = Project::inThe($projectid, $user['username']);
  874. if (Base::isError($inRes)) {
  875. return $inRes;
  876. }
  877. //
  878. $title = trim(Request::input('title'));
  879. if (empty($title)) {
  880. return Base::retError('列表名称不能为空!');
  881. } elseif (mb_strlen($title) > 32) {
  882. return Base::retError('列表名称最多只能设置32个字!');
  883. }
  884. //
  885. $labelid = intval(Request::input('labelid'));
  886. $count = DB::table('project_label')->where('id', '!=', $labelid)->where('projectid', $projectid)->where('title', $title)->count();
  887. if ($count > 0) {
  888. return Base::retError('列表名称已存在!');
  889. }
  890. //
  891. $labelDetail = Base::DBC2A(DB::table('project_label')->where('id', $labelid)->where('projectid', $projectid)->first());
  892. if (empty($labelDetail)) {
  893. return Base::retError('列表不存在或已被删除!');
  894. }
  895. //
  896. if (DB::table('project_label')->where('id', $labelDetail['id'])->update([ 'title' => $title ])) {
  897. DB::table('project_log')->insert([
  898. 'type' => '日志',
  899. 'projectid' => $projectid,
  900. 'username' => $user['username'],
  901. 'detail' => '任务列表【' . $labelDetail['title'] . '】重命名【' . $title . '】',
  902. 'indate' => Base::time()
  903. ]);
  904. }
  905. //
  906. return Base::retSuccess('修改成功!');
  907. }
  908. /**
  909. * 项目子分类-删除分类
  910. *
  911. * @apiParam {Number} projectid 项目ID
  912. * @apiParam {Number} labelid 分类ID
  913. *
  914. * @throws \Throwable
  915. */
  916. public function label__delete()
  917. {
  918. $user = Users::authE();
  919. if (Base::isError($user)) {
  920. return $user;
  921. } else {
  922. $user = $user['data'];
  923. }
  924. //
  925. $projectid = trim(Request::input('projectid'));
  926. $inRes = Project::inThe($projectid, $user['username']);
  927. if (Base::isError($inRes)) {
  928. return $inRes;
  929. }
  930. //
  931. $labelid = intval(Request::input('labelid'));
  932. $labelDetail = Base::DBC2A(DB::table('project_label')->where('id', $labelid)->where('projectid', $projectid)->first());
  933. if (empty($labelDetail)) {
  934. return Base::retError('列表不存在或已被删除!');
  935. }
  936. //
  937. return DB::transaction(function () use ($user, $projectid, $labelDetail) {
  938. $taskLists = Base::DBC2A(DB::table('project_task')->where('labelid', $labelDetail['id'])->get());
  939. $logArray = [];
  940. foreach ($taskLists AS $task) {
  941. $logArray[] = [
  942. 'type' => '日志',
  943. 'projectid' => $projectid,
  944. 'taskid' => $task['id'],
  945. 'username' => $user['username'],
  946. 'detail' => '删除列表任务',
  947. 'indate' => Base::time(),
  948. 'other' => Base::array2string([
  949. 'type' => 'task',
  950. 'id' => $task['id'],
  951. 'title' => $task['title'],
  952. ])
  953. ];
  954. }
  955. $logArray[] = [
  956. 'type' => '日志',
  957. 'projectid' => $projectid,
  958. 'taskid' => 0,
  959. 'username' => $user['username'],
  960. 'detail' => '删除任务列表【' . $labelDetail['title'] . '】',
  961. 'indate' => Base::time()
  962. ];
  963. DB::table('project_task')->where('labelid', $labelDetail['id'])->update([
  964. 'delete' => 1,
  965. 'deletedate' => Base::time()
  966. ]);
  967. DB::table('project_label')->where('id', $labelDetail['id'])->delete();
  968. DB::table('project_log')->insert($logArray);
  969. //
  970. return Base::retSuccess('删除成功!');
  971. });
  972. }
  973. /**
  974. * 项目任务-列表
  975. *
  976. * @apiParam {Number} [projectid] 项目ID
  977. * @apiParam {Number} [taskid] 任务ID (1、填写此参数时projectid强制为此任务的projectid;2、赋值返回详细数据,不返回列表数据)
  978. * @apiParam {Number} [labelid] 项目子分类ID
  979. * @apiParam {String} [username] 负责人用户名(如果项目ID为空时此参数无效只获取自己的任务)
  980. * @apiParam {Number} [level] 任务等级(1~4)
  981. * @apiParam {String} [archived] 任务是否归档
  982. * - 未归档 (默认)
  983. * - 已归档
  984. * - 全部
  985. * @apiParam {String} [type] 任务类型
  986. * - 全部(默认)
  987. * - 未完成
  988. * - 已超期
  989. * - 已完成
  990. * @apiParam {Number} [createuser] 是否仅获取自己创建的项目(1:是;赋值时projectid和username不强制)
  991. * @apiParam {Number} [attention] 是否仅获取关注数据(1:是;赋值时projectid和username不强制)
  992. * @apiParam {Number} [statistics] 是否获取统计数据(1:获取)
  993. * @apiParam {String} [startdate] 任务开始时间,格式:YYYY-MM-DD
  994. * @apiParam {String} [enddate] 任务结束时间,格式:YYYY-MM-DD
  995. *
  996. * @apiParam {Object} [sorts] 排序方式,格式:{key:'', order:''}
  997. * - key: title|labelid|enddate|username|level|indate|type|inorder(默认)|userorder
  998. * - order: asc|desc
  999. * - 【archived=已归档】或【startdate和enddate赋值】时无效
  1000. *
  1001. * @apiParam {Number} [page] 当前页,默认:1
  1002. * @apiParam {Number} [pagesize] 每页显示数量,默认:20,最大:100
  1003. */
  1004. public function task__lists()
  1005. {
  1006. $user = Users::authE();
  1007. if (Base::isError($user)) {
  1008. return $user;
  1009. } else {
  1010. $user = $user['data'];
  1011. }
  1012. //
  1013. $taskid = intval(Request::input('taskid'));
  1014. if ($taskid > 0) {
  1015. $projectid = intval(DB::table('project_task')->where('id', $taskid)->value('projectid'));
  1016. } else {
  1017. $projectid = intval(Request::input('projectid'));
  1018. }
  1019. //
  1020. if ($projectid > 0) {
  1021. $inRes = Project::inThe($projectid, $user['username']);
  1022. if (Base::isError($inRes)) {
  1023. return $inRes;
  1024. }
  1025. }
  1026. //
  1027. $orderBy = '`inorder` DESC,`id` DESC';
  1028. $sorts = Base::json2array(Request::input('sorts'));
  1029. if (in_array($sorts['order'], ['asc', 'desc'])) {
  1030. switch ($sorts['key']) {
  1031. case 'title':
  1032. case 'labelid':
  1033. case 'enddate':
  1034. case 'username':
  1035. case 'level':
  1036. case 'indate':
  1037. case 'inorder':
  1038. case 'userorder':
  1039. $orderBy = '`' . $sorts['key'] . '` ' . $sorts['order'] . ',`id` DESC';
  1040. break;
  1041. case 'type':
  1042. $orderBy = 'CASE WHEN `complete`= 0 AND `enddate` BETWEEN 1 AND ' . Base::time() . ' THEN 0 ELSE 1 END ' . $sorts['order'] . ', `complete` ' . $sorts['order'] . ',`id` DESC';
  1043. break;
  1044. }
  1045. }
  1046. //
  1047. $selectArray = ['project_task.*'];
  1048. $whereRaw = null;
  1049. $whereFunc = null;
  1050. $whereArray = [];
  1051. $whereArray[] = ['project_task.delete', '=', 0];
  1052. if (intval(Request::input('createuser')) === 1) {
  1053. $whereArray[] = ['project_task.createuser', '=', $user['username']];
  1054. if ($projectid > 0) {
  1055. $whereArray[] = ['project_lists.id', '=', $projectid];
  1056. $whereArray[] = ['project_lists.delete', '=', 0];
  1057. }
  1058. if (trim(Request::input('username'))) {
  1059. $whereArray[] = ['project_task.username', '=', trim(Request::input('username'))];
  1060. }
  1061. } else if (intval(Request::input('attention')) === 1) {
  1062. if ($projectid > 0) {
  1063. $whereArray[] = ['project_lists.id', '=', $projectid];
  1064. $whereArray[] = ['project_lists.delete', '=', 0];
  1065. }
  1066. if (trim(Request::input('username'))) {
  1067. $whereArray[] = ['project_task.username', '=', trim(Request::input('username'))];
  1068. }
  1069. } else {
  1070. if ($projectid > 0) {
  1071. $whereArray[] = ['project_lists.id', '=', $projectid];
  1072. $whereArray[] = ['project_lists.delete', '=', 0];
  1073. if (trim(Request::input('username'))) {
  1074. $whereArray[] = ['project_task.username', '=', trim(Request::input('username'))];
  1075. }
  1076. } else {
  1077. $whereArray[] = ['project_task.username', '=', $user['username']];
  1078. }
  1079. }
  1080. if ($taskid > 0) {
  1081. $whereArray[] = ['project_task.id', '=', intval(Request::input('taskid'))];
  1082. }
  1083. if (intval(Request::input('labelid')) > 0) {
  1084. $whereArray[] = ['project_task.labelid', '=', intval(Request::input('labelid'))];
  1085. }
  1086. if (intval(Request::input('level')) > 0) {
  1087. $whereArray[] = ['project_task.level', '=', intval(Request::input('level'))];
  1088. }
  1089. $archived = trim(Request::input('archived'));
  1090. if (empty($archived)) $archived = "未归档";
  1091. switch ($archived) {
  1092. case '已归档':
  1093. $whereArray[] = ['project_task.archived', '=', 1];
  1094. $orderBy = '`archiveddate` DESC';
  1095. break;
  1096. case '未归档':
  1097. $whereArray[] = ['project_task.archived', '=', 0];
  1098. break;
  1099. }
  1100. $type = trim(Request::input('type'));
  1101. switch ($type) {
  1102. case '未完成':
  1103. $whereArray[] = ['project_task.complete', '=', 0];
  1104. break;
  1105. case '已超期':
  1106. $whereArray[] = ['project_task.complete', '=', 0];
  1107. $whereArray[] = ['project_task.enddate', '>', 0];
  1108. $whereArray[] = ['project_task.enddate', '<=', Base::time()];
  1109. break;
  1110. case '已完成':
  1111. $whereArray[] = ['project_task.complete', '=', 1];
  1112. break;
  1113. }
  1114. $startdate = trim(Request::input('startdate'));
  1115. $enddate = trim(Request::input('enddate'));
  1116. if (Base::isDate($startdate) || Base::isDate($enddate)) {
  1117. $startdate = strtotime($startdate . ' 00:00:00');
  1118. $enddate = strtotime($enddate . ' 23:59:59');
  1119. $whereRaw.= $whereRaw ? ' AND ' : '';
  1120. $whereRaw.= "((`startdate` >= " . $startdate . " OR `startdate` = 0) AND (`enddate` <= " . $enddate . " OR `enddate` = 0))";
  1121. $orderBy = '`startdate` DESC';
  1122. }
  1123. //
  1124. $builder = DB::table('project_task');
  1125. if ($projectid > 0) {
  1126. $builder->join('project_lists', 'project_lists.id', '=', 'project_task.projectid');
  1127. }
  1128. if (intval(Request::input('attention')) === 1) {
  1129. $builder->join('project_users', 'project_users.taskid', '=', 'project_task.id');
  1130. $builder->where([
  1131. ['project_users.type', '=', '关注'],
  1132. ['project_users.username', '=', $user['username']],
  1133. ]);
  1134. $selectArray[] = 'project_users.indate AS attentiondate';
  1135. }
  1136. if ($whereRaw) {
  1137. $builder->whereRaw($whereRaw);
  1138. }
  1139. $lists = $builder->select($selectArray)
  1140. ->where($whereArray)
  1141. ->orderByRaw($orderBy)
  1142. ->paginate(Min(Max(Base::nullShow(Request::input('pagesize'), 10), 1), 100));
  1143. $lists = Base::getPageList($lists, $taskid > 0 ? false : true);
  1144. if (intval(Request::input('statistics')) == 1) {
  1145. $lists['statistics_unfinished'] = $type === '未完成' ? $lists['total'] : DB::table('project_task')->where('projectid', $projectid)->where('delete', 0)->where('archived', 0)->where('complete', 0)->count();
  1146. $lists['statistics_overdue'] = $type === '已超期' ? $lists['total'] : DB::table('project_task')->where('projectid', $projectid)->where('delete', 0)->where('archived', 0)->where('complete', 0)->whereBetween('enddate', [1, Base::time()])->count();
  1147. $lists['statistics_complete'] = $type === '已完成' ? $lists['total'] : DB::table('project_task')->where('projectid', $projectid)->where('delete', 0)->where('archived', 0)->where('complete', 1)->count();
  1148. }
  1149. if ($lists['total'] == 0) {
  1150. return Base::retError('未找到任何相关的任务!', $lists);
  1151. }
  1152. foreach ($lists['lists'] AS $key => $info) {
  1153. $info['overdue'] = Project::taskIsOverdue($info);
  1154. $info['subtask'] = Base::string2array($info['subtask']);
  1155. $info['follower'] = Base::string2array($info['follower']);
  1156. $lists['lists'][$key] = array_merge($info, Users::username2basic($info['username']));
  1157. }
  1158. if ($taskid > 0) {
  1159. if (count($lists['lists']) == 0) {
  1160. return Base::retError('未能找到此任务或无法管理此任务!');
  1161. }
  1162. return Base::retSuccess('success', $lists['lists'][0]);
  1163. } else {
  1164. return Base::retSuccess('success', $lists);
  1165. }
  1166. }
  1167. /**
  1168. * 项目任务-添加任务
  1169. *
  1170. * @apiParam {String} title 任务标题
  1171. * @apiParam {Number} [projectid] 项目ID
  1172. * @apiParam {Number} [labelid] 项目子分类ID
  1173. * @apiParam {Number} [level] 任务紧急级别(1~4,默认:2)
  1174. * @apiParam {String} [username] 任务负责人用户名(如果项目ID为空时此参数无效,负责人为自己)
  1175. *
  1176. * @throws \Throwable
  1177. */
  1178. public function task__add()
  1179. {
  1180. $user = Users::authE();
  1181. if (Base::isError($user)) {
  1182. return $user;
  1183. } else {
  1184. $user = $user['data'];
  1185. }
  1186. //
  1187. $projectid = intval(Request::input('projectid'));
  1188. $labelid = intval(Request::input('labelid'));
  1189. if ($projectid > 0) {
  1190. $projectDetail = Base::DBC2A(DB::table('project_lists')->where('id', $projectid)->where('delete', 0)->first());
  1191. if (empty($projectDetail)) {
  1192. return Base::retError('项目不存在或已被删除!');
  1193. }
  1194. //
  1195. $labelDetail = Base::DBC2A(DB::table('project_label')->where('id', $labelid)->where('projectid', $projectid)->first());
  1196. if (empty($labelDetail)) {
  1197. return Base::retError('项目子分类不存在或已被删除!');
  1198. }
  1199. //
  1200. $inRes = Project::inThe($projectid, $user['username']);
  1201. if (Base::isError($inRes)) {
  1202. return $inRes;
  1203. }
  1204. //
  1205. $username = trim(Request::input('username'));
  1206. if (empty($username)) {
  1207. $username = $user['username'];
  1208. }
  1209. if ($username != $user['username']) {
  1210. $inRes = Project::inThe($projectid, $username);
  1211. if (Base::isError($inRes)) {
  1212. return Base::retError('负责人不在项目成员内!');
  1213. }
  1214. }
  1215. } else {
  1216. $username = $user['username'];
  1217. }
  1218. //
  1219. $title = trim(Request::input('title'));
  1220. if (empty($title)) {
  1221. return Base::retError('任务标题不能为空!');
  1222. } elseif (mb_strlen($title) > 255) {
  1223. return Base::retError('任务标题最多只能设置255个字!');
  1224. }
  1225. //
  1226. $level = max(1, min(4, intval(Request::input('level'))));
  1227. $inArray = [
  1228. 'projectid' => $projectid,
  1229. 'labelid' => $labelid,
  1230. 'createuser' => $user['username'],
  1231. 'username' => $username,
  1232. 'title' => $title,
  1233. 'level' => $level,
  1234. 'inorder' => empty($projectid) ? 0 : intval(DB::table('project_task')->where('projectid', $projectid)->orderByDesc('inorder')->value('inorder')) + 1,
  1235. 'userorder' => intval(DB::table('project_task')->where('username', $user['username'])->where('level', $level)->orderByDesc('userorder')->value('userorder')) + 1,
  1236. 'indate' => Base::time(),
  1237. 'startdate' => Base::time(),
  1238. 'subtask' => Base::array2string([]),
  1239. 'follower' => Base::array2string([]),
  1240. ];
  1241. return DB::transaction(function () use ($inArray) {
  1242. $taskid = DB::table('project_task')->insertGetId($inArray);
  1243. if (empty($taskid)) {
  1244. return Base::retError('系统繁忙,请稍后再试!');
  1245. }
  1246. DB::table('project_log')->insert([
  1247. 'type' => '日志',
  1248. 'projectid' => $inArray['projectid'],
  1249. 'taskid' => $taskid,
  1250. 'username' => $inArray['createuser'],
  1251. 'detail' => '添加任务',
  1252. 'indate' => Base::time(),
  1253. 'other' => Base::array2string([
  1254. 'type' => 'task',
  1255. 'id' => $taskid,
  1256. 'title' => $inArray['title'],
  1257. ])
  1258. ]);
  1259. Project::updateNum($inArray['projectid']);
  1260. //
  1261. $task = Base::DBC2A(DB::table('project_task')->where('id', $taskid)->first());
  1262. $task['overdue'] = Project::taskIsOverdue($task);
  1263. $task['subtask'] = Base::string2array($task['subtask']);
  1264. $task['follower'] = Base::string2array($task['follower']);
  1265. $task = array_merge($task, Users::username2basic($task['username']));
  1266. return Base::retSuccess('添加成功!', $task);
  1267. });
  1268. }
  1269. /**
  1270. * 项目任务-修改
  1271. *
  1272. * @apiParam {Number} taskid 任务ID
  1273. * @apiParam {String} act 修改字段|操作类型
  1274. * - title: 标题
  1275. * - desc: 描述
  1276. * - level: 优先级
  1277. * - username: 负责人
  1278. * - plannedtime: 设置计划时间
  1279. * - unplannedtime: 取消计划时间
  1280. * - complete: 标记完成
  1281. * - unfinished: 标记未完成
  1282. * - archived: 归档
  1283. * - unarchived: 取消归档
  1284. * - delete: 删除任务
  1285. * - comment: 评论
  1286. * - attention: 添加关注
  1287. * @apiParam {String} [content] 内容数据
  1288. *
  1289. * @throws \Throwable
  1290. */
  1291. public function task__edit()
  1292. {
  1293. $user = Users::authE();
  1294. if (Base::isError($user)) {
  1295. return $user;
  1296. } else {
  1297. $user = $user['data'];
  1298. }
  1299. //
  1300. $act = trim(Request::input('act'));
  1301. $taskid = intval(Request::input('taskid'));
  1302. $task = Base::DBC2A(DB::table('project_task')
  1303. ->where([
  1304. ['delete', '=', 0],
  1305. ['id', '=', $taskid],
  1306. ])
  1307. ->first());
  1308. if (empty($task)) {
  1309. return Base::retError('任务不存在!');
  1310. }
  1311. if ($task['projectid'] > 0) {
  1312. $inRes = Project::inThe($task['projectid'], $user['username']);
  1313. if (Base::isError($inRes)) {
  1314. return $inRes;
  1315. }
  1316. if (!$inRes['data']['isowner']
  1317. && $task['username'] != $user['username']
  1318. && !in_array($act, ['comment', 'attention'])) {
  1319. return Base::retError('此操作只允许项目管理员或者任务负责人!');
  1320. }
  1321. } else {
  1322. if ($task['username'] != $user['username']) {
  1323. return Base::retError('此操作只允许任务负责人!');
  1324. }
  1325. }
  1326. //
  1327. $content = trim(Request::input('content'));
  1328. $message = "";
  1329. $upArray = [];
  1330. $logArray = [];
  1331. switch ($act) {
  1332. /**
  1333. * 修改标题
  1334. */
  1335. case 'title': {
  1336. if ($content == $task['title']) {
  1337. return Base::retError('标题未做改变!');
  1338. }
  1339. $upArray['title'] = $content;
  1340. $logArray[] = [
  1341. 'type' => '日志',
  1342. 'projectid' => $task['projectid'],
  1343. 'taskid' => $task['id'],
  1344. 'username' => $user['username'],
  1345. 'detail' => '修改任务标题',
  1346. 'indate' => Base::time(),
  1347. 'other' => Base::array2string([
  1348. 'type' => 'task',
  1349. 'id' => $task['id'],
  1350. 'title' => $content,
  1351. 'old_title' => $task['title'],
  1352. ])
  1353. ];
  1354. break;
  1355. }
  1356. /**
  1357. * 修改描述
  1358. */
  1359. case 'desc': {
  1360. if ($content == $task['desc']) {
  1361. return Base::retError('描述未做改变!');
  1362. }
  1363. $upArray['desc'] = $content;
  1364. $logArray[] = [
  1365. 'type' => '日志',
  1366. 'projectid' => $task['projectid'],
  1367. 'taskid' => $task['id'],
  1368. 'username' => $user['username'],
  1369. 'detail' => '修改任务描述',
  1370. 'indate' => Base::time(),
  1371. 'other' => Base::array2string([
  1372. 'type' => 'task',
  1373. 'id' => $task['id'],
  1374. 'title' => $task['title'],
  1375. 'old_desc' => $task['desc'],
  1376. ])
  1377. ];
  1378. break;
  1379. }
  1380. /**
  1381. * 调整任务等级
  1382. */
  1383. case 'level': {
  1384. $content = intval($content);
  1385. if ($content == $task['level']) {
  1386. return Base::retError('优先级未做改变!');
  1387. }
  1388. if ($content > 4 || $content < 1) {
  1389. return Base::retError('优先级参数错误!');
  1390. }
  1391. $upArray['level'] = $content;
  1392. $logArray[] = [
  1393. 'type' => '日志',
  1394. 'projectid' => $task['projectid'],
  1395. 'taskid' => $task['id'],
  1396. 'username' => $user['username'],
  1397. 'detail' => '调整任务等级为【P' . $content . '】',
  1398. 'indate' => Base::time(),
  1399. 'other' => Base::array2string([
  1400. 'type' => 'task',
  1401. 'id' => $task['id'],
  1402. 'title' => $task['title'],
  1403. 'old_level' => $task['level'],
  1404. ])
  1405. ];
  1406. break;
  1407. }
  1408. /**
  1409. * 修改任务负责人
  1410. */
  1411. case 'username': {
  1412. if ($content == $task['username']) {
  1413. return Base::retError('负责人未做改变!');
  1414. }
  1415. if ($task['projectid'] > 0) {
  1416. $inRes = Project::inThe($task['projectid'], $content);
  1417. if (Base::isError($inRes)) {
  1418. return Base::retError(['%不在成员列表内!', $content]);
  1419. }
  1420. }
  1421. $upArray['username'] = $content;
  1422. $logArray[] = [
  1423. 'type' => '日志',
  1424. 'projectid' => $task['projectid'],
  1425. 'taskid' => $task['id'],
  1426. 'username' => $user['username'],
  1427. 'detail' => '修改负责人',
  1428. 'indate' => Base::time(),
  1429. 'other' => Base::array2string([
  1430. 'type' => 'task',
  1431. 'id' => $task['id'],
  1432. 'title' => $task['title'],
  1433. 'old_username' => $task['username'],
  1434. ])
  1435. ];
  1436. break;
  1437. }
  1438. /**
  1439. * 修改计划时间
  1440. */
  1441. case 'plannedtime': {
  1442. list($startdate, $enddate) = explode(",", $content);
  1443. if (!Base::isDate($startdate) || !Base::isDate($enddate)) {
  1444. return Base::retError('计划时间参数错误!');
  1445. }
  1446. $startdate = strtotime($startdate);
  1447. $enddate = strtotime($enddate);
  1448. if ($startdate == $task['startdate'] && $enddate == $task['enddate']) {
  1449. return Base::retError('与原计划时间一致!');
  1450. }
  1451. $upArray['startdate'] = $startdate;
  1452. $upArray['enddate'] = $enddate;
  1453. $logArray[] = [
  1454. 'type' => '日志',
  1455. 'projectid' => $task['projectid'],
  1456. 'taskid' => $task['id'],
  1457. 'username' => $user['username'],
  1458. 'detail' => '设置计划时间',
  1459. 'indate' => Base::time(),
  1460. 'other' => Base::array2string([
  1461. 'type' => 'task',
  1462. 'id' => $task['id'],
  1463. 'title' => $task['title'],
  1464. 'old_startdate' => $task['startdate'],
  1465. 'old_enddate' => $task['enddate'],
  1466. ])
  1467. ];
  1468. break;
  1469. }
  1470. /**
  1471. * 取消计划时间
  1472. */
  1473. case 'unplannedtime': {
  1474. $startdate = 0;
  1475. $enddate = 0;
  1476. if ($startdate == $task['startdate'] && $enddate == $task['enddate']) {
  1477. return Base::retError('与原计划时间一致!');
  1478. }
  1479. $upArray['startdate'] = $startdate;
  1480. $upArray['enddate'] = $enddate;
  1481. $logArray[] = [
  1482. 'type' => '日志',
  1483. 'projectid' => $task['projectid'],
  1484. 'taskid' => $task['id'],
  1485. 'username' => $user['username'],
  1486. 'detail' => '取消计划时间',
  1487. 'indate' => Base::time(),
  1488. 'other' => Base::array2string([
  1489. 'type' => 'task',
  1490. 'id' => $task['id'],
  1491. 'title' => $task['title'],
  1492. 'old_startdate' => $task['startdate'],
  1493. 'old_enddate' => $task['enddate'],
  1494. ])
  1495. ];
  1496. break;
  1497. }
  1498. /**
  1499. * 标记完成
  1500. */
  1501. case 'complete': {
  1502. if ($task['complete'] == 1) {
  1503. return Base::retError('任务已标记完成,请勿重复操作!');
  1504. }
  1505. $upArray['complete'] = 1;
  1506. $upArray['completedate'] = Base::time();
  1507. $logArray[] = [
  1508. 'type' => '日志',
  1509. 'projectid' => $task['projectid'],
  1510. 'taskid' => $task['id'],
  1511. 'username' => $user['username'],
  1512. 'detail' => '标记已完成',
  1513. 'indate' => Base::time(),
  1514. 'other' => Base::array2string([
  1515. 'type' => 'task',
  1516. 'id' => $task['id'],
  1517. 'title' => $task['title'],
  1518. ])
  1519. ];
  1520. break;
  1521. }
  1522. /**
  1523. * 标记未完成
  1524. */
  1525. case 'unfinished': {
  1526. if ($task['complete'] == 0) {
  1527. return Base::retError('任务未完成,无法标记未完成!');
  1528. }
  1529. $upArray['complete'] = 0;
  1530. $logArray[] = [
  1531. 'type' => '日志',
  1532. 'projectid' => $task['projectid'],
  1533. 'taskid' => $task['id'],
  1534. 'username' => $user['username'],
  1535. 'detail' => '标记未完成',
  1536. 'indate' => Base::time(),
  1537. 'other' => Base::array2string([
  1538. 'type' => 'task',
  1539. 'id' => $task['id'],
  1540. 'title' => $task['title'],
  1541. ])
  1542. ];
  1543. break;
  1544. }
  1545. /**
  1546. * 归档
  1547. */
  1548. case 'archived': {
  1549. if ($task['archived'] == 1) {
  1550. return Base::retError('任务已经归档,请勿重复操作!');
  1551. }
  1552. $upArray['archived'] = 1;
  1553. $upArray['archiveddate'] = Base::time();
  1554. $logArray[] = [
  1555. 'type' => '日志',
  1556. 'projectid' => $task['projectid'],
  1557. 'taskid' => $task['id'],
  1558. 'username' => $user['username'],
  1559. 'detail' => '任务归档',
  1560. 'indate' => Base::time(),
  1561. 'other' => Base::array2string([
  1562. 'type' => 'task',
  1563. 'id' => $task['id'],
  1564. 'title' => $task['title'],
  1565. ])
  1566. ];
  1567. break;
  1568. }
  1569. /**
  1570. * 取消归档
  1571. */
  1572. case 'unarchived': {
  1573. if ($task['archived'] == 0) {
  1574. return Base::retError('任务未归档,无法取消归档操作!');
  1575. }
  1576. $upArray['archived'] = 0;
  1577. $logArray[] = [
  1578. 'type' => '日志',
  1579. 'projectid' => $task['projectid'],
  1580. 'taskid' => $task['id'],
  1581. 'username' => $user['username'],
  1582. 'detail' => '取消归档',
  1583. 'indate' => Base::time(),
  1584. 'other' => Base::array2string([
  1585. 'type' => 'task',
  1586. 'id' => $task['id'],
  1587. 'title' => $task['title'],
  1588. ])
  1589. ];
  1590. break;
  1591. }
  1592. /**
  1593. * 删除任务
  1594. */
  1595. case 'delete': {
  1596. if ($task['delete'] == 1) {
  1597. return Base::retError('任务已删除,请勿重复操作!');
  1598. }
  1599. $upArray['delete'] = 1;
  1600. $upArray['deletedate'] = Base::time();
  1601. $logArray[] = [
  1602. 'type' => '日志',
  1603. 'projectid' => $task['projectid'],
  1604. 'taskid' => $task['id'],
  1605. 'username' => $user['username'],
  1606. 'detail' => '删除任务',
  1607. 'indate' => Base::time(),
  1608. 'other' => Base::array2string([
  1609. 'type' => 'task',
  1610. 'id' => $task['id'],
  1611. 'title' => $task['title'],
  1612. ])
  1613. ];
  1614. $message = "删除成功!";
  1615. break;
  1616. }
  1617. /**
  1618. * 评论任务
  1619. */
  1620. case 'comment': {
  1621. if (mb_strlen($content) < 2) {
  1622. return Base::retError('评论内容至少2个字!');
  1623. }
  1624. $logArray[] = [
  1625. 'type' => '评论',
  1626. 'projectid' => $task['projectid'],
  1627. 'taskid' => $task['id'],
  1628. 'username' => $user['username'],
  1629. 'detail' => $content,
  1630. 'indate' => Base::time(),
  1631. 'other' => Base::array2string([
  1632. 'type' => 'task',
  1633. 'id' => $task['id'],
  1634. 'title' => $task['title'],
  1635. ])
  1636. ];
  1637. $message = "评论成功!";
  1638. break;
  1639. }
  1640. /**
  1641. * 添加关注
  1642. */
  1643. case 'attention': {
  1644. $userArray = explode(",", $content);
  1645. DB::transaction(function () use ($user, $task, $userArray) {
  1646. foreach ($userArray AS $uname) {
  1647. $uid = Users::username2id($uname);
  1648. if (empty($uid)) {
  1649. continue;
  1650. }
  1651. $row = Base::DBC2A(DB::table('project_users')->where([
  1652. 'type' => '关注',
  1653. 'taskid' => $task['id'],
  1654. 'username' => $uname,
  1655. ])->lockForUpdate()->first());
  1656. if (empty($row)) {
  1657. DB::table('project_users')->insert([
  1658. 'type' => '关注',
  1659. 'projectid' => $task['projectid'],
  1660. 'taskid' => $task['id'],
  1661. 'isowner' => $task['username'] == $uname ? 1 : 0,
  1662. 'username' => $uname,
  1663. 'indate' => Base::time()
  1664. ]);
  1665. DB::table('project_log')->insert([
  1666. 'type' => '日志',
  1667. 'projectid' => $task['projectid'],
  1668. 'taskid' => $task['id'],
  1669. 'username' => $uname,
  1670. 'detail' => $uname == $user['username'] ? '关注任务' : '加入关注',
  1671. 'indate' => Base::time(),
  1672. 'other' => Base::array2string([
  1673. 'type' => 'task',
  1674. 'id' => $task['id'],
  1675. 'title' => $task['title'],
  1676. 'operator' => $user['username'],
  1677. ])
  1678. ]);
  1679. }
  1680. }
  1681. });
  1682. $tempRow = Base::DBC2A(DB::table('project_users')->select(['username'])->where([ 'type' => '关注', 'taskid' => $task['id'] ])->get()->pluck('username'));
  1683. $upArray['follower'] = Base::array2string($tempRow);
  1684. $message = "保存成功!";
  1685. break;
  1686. }
  1687. /**
  1688. * 取消关注
  1689. */
  1690. case 'unattention': {
  1691. $userArray = explode(",", $content);
  1692. DB::transaction(function () use ($user, $task, $userArray) {
  1693. foreach ($userArray AS $uname) {
  1694. if (DB::table('project_users')->where([
  1695. 'type' => '关注',
  1696. 'taskid' => $task['id'],
  1697. 'username' => $uname,
  1698. ])->delete()) {
  1699. DB::table('project_log')->insert([
  1700. 'type' => '日志',
  1701. 'projectid' => $task['projectid'],
  1702. 'taskid' => $task['id'],
  1703. 'username' => $uname,
  1704. 'detail' => $uname == $user['username'] ? '取消关注' : '移出关注',
  1705. 'indate' => Base::time(),
  1706. 'other' => Base::array2string([
  1707. 'type' => 'task',
  1708. 'id' => $task['id'],
  1709. 'title' => $task['title'],
  1710. 'operator' => $user['username'],
  1711. ])
  1712. ]);
  1713. }
  1714. }
  1715. });
  1716. $tempRow = Base::DBC2A(DB::table('project_users')->select(['username'])->where([ 'type' => '关注', 'taskid' => $task['id'] ])->get()->pluck('username'));
  1717. $upArray['follower'] = Base::array2string($tempRow);
  1718. $message = "保存成功!";
  1719. break;
  1720. }
  1721. default: {
  1722. return Base::retError('参数错误!');
  1723. break;
  1724. }
  1725. }
  1726. //
  1727. if ($upArray) {
  1728. DB::table('project_task')->where('id', $taskid)->update($upArray);
  1729. }
  1730. if ($logArray) {
  1731. DB::table('project_log')->insert($logArray);
  1732. }
  1733. //
  1734. if (in_array($act, ['complete', 'unfinished'])) {
  1735. Project::updateNum($task['projectid']);
  1736. }
  1737. //
  1738. $task = array_merge($task, $upArray);
  1739. $task['overdue'] = Project::taskIsOverdue($task);
  1740. $task['subtask'] = Base::string2array($task['subtask']);
  1741. $task['follower'] = Base::string2array($task['follower']);
  1742. $task = array_merge($task, Users::username2basic($task['username']));
  1743. return Base::retSuccess($message ?: '修改成功!', $task);
  1744. }
  1745. /**
  1746. * 项目任务-待推送日志
  1747. *
  1748. * @apiParam {Number} taskid 任务ID
  1749. * @apiParam {Number} [page] 当前页,默认:1
  1750. * @apiParam {Number} [pagesize] 每页显示数量,默认:20,最大:100
  1751. * @throws \Throwable
  1752. */
  1753. public function task__pushlog()
  1754. {
  1755. $user = Users::authE();
  1756. if (Base::isError($user)) {
  1757. return $user;
  1758. } else {
  1759. $user = $user['data'];
  1760. }
  1761. //
  1762. return DB::transaction(function () {
  1763. $taskid = intval(Request::input('taskid'));
  1764. $task = Base::DBC2A(DB::table('project_task')
  1765. ->select(['pushlid', 'follower', 'username'])
  1766. ->where([
  1767. ['delete', '=', 0],
  1768. ['id', '=', $taskid],
  1769. ])
  1770. ->lockForUpdate()
  1771. ->first());
  1772. if (empty($task)) {
  1773. return Base::retError('任务不存在!');
  1774. }
  1775. $task['follower'] = Base::string2array($task['follower']);
  1776. $task['follower'][] = $task['username'];
  1777. //
  1778. $pushlid = $task['pushlid'];
  1779. $lists = DB::table('project_log')
  1780. ->select(['id', 'username', 'indate', 'detail', 'other'])
  1781. ->where([
  1782. ['id', '>', $pushlid],
  1783. ['taskid', '=', $taskid],
  1784. ['indate', '>', Base::time() - 60]
  1785. ])
  1786. ->orderBy('id')->paginate(Min(Max(Base::nullShow(Request::input('pagesize'), 10), 1), 100));
  1787. $lists = Base::getPageList($lists, false);
  1788. if (count($lists['lists']) == 0) {
  1789. return Base::retError('no lists');
  1790. }
  1791. foreach ($lists['lists'] AS $key => $item) {
  1792. $item = array_merge($item, Users::username2basic($item['username']));
  1793. $item['other'] = Base::string2array($item['other'], ['type' => '']);
  1794. $lists['lists'][$key] = $item;
  1795. $pushlid = $item['id'];
  1796. }
  1797. if ($pushlid != $task['pushlid']) {
  1798. DB::table('project_task')->where('id', $taskid)->update([
  1799. 'pushlid' => $pushlid
  1800. ]);
  1801. }
  1802. return Base::retSuccess('success', array_merge($lists, $task));
  1803. });
  1804. }
  1805. /**
  1806. * 项目文件-列表
  1807. *
  1808. * @apiParam {Number} [projectid] 项目ID
  1809. * @apiParam {Number} [taskid] 任务ID(如果项目ID为空时此参必须赋值且任务必须是自己负责人或在任务所在的项目里)
  1810. * @apiParam {String} [name] 文件名称
  1811. * @apiParam {String} [username] 上传者用户名
  1812. * @apiParam {Object} [sorts] 排序方式,格式:{key:'', order:''}
  1813. * - key: name|size|username|indate
  1814. * - order: asc|desc
  1815. * @apiParam {Number} [page] 当前页,默认:1
  1816. * @apiParam {Number} [pagesize] 每页显示数量,默认:20,最大:100
  1817. */
  1818. public function files__lists()
  1819. {
  1820. $user = Users::authE();
  1821. if (Base::isError($user)) {
  1822. return $user;
  1823. } else {
  1824. $user = $user['data'];
  1825. }
  1826. //
  1827. $taskid = intval(Request::input('taskid'));
  1828. if ($taskid > 0) {
  1829. $projectid = intval(DB::table('project_task')->select(['projectid'])->where([ 'id' => $taskid])->value('projectid'));
  1830. } else {
  1831. $projectid = intval(Request::input('projectid'));
  1832. }
  1833. if ($projectid > 0) {
  1834. $inRes = Project::inThe($projectid, $user['username']);
  1835. if (Base::isError($inRes)) {
  1836. return $inRes;
  1837. }
  1838. }
  1839. //
  1840. $orderBy = '`id` DESC';
  1841. $sorts = Base::json2array(Request::input('sorts'));
  1842. if (in_array($sorts['order'], ['asc', 'desc'])) {
  1843. switch ($sorts['key']) {
  1844. case 'name':
  1845. case 'size':
  1846. case 'download':
  1847. case 'username':
  1848. case 'indate':
  1849. $orderBy = '`' . $sorts['key'] . '` ' . $sorts['order'] . ',`id` DESC';
  1850. break;
  1851. }
  1852. }
  1853. //
  1854. $whereArray = [];
  1855. if ($projectid > 0) {
  1856. $whereArray[] = ['projectid', '=', $projectid];
  1857. if ($taskid > 0) {
  1858. $whereArray[] = ['taskid', '=', $taskid];
  1859. }
  1860. } else {
  1861. if ($taskid <= 0) {
  1862. return Base::retError('参数错误!');
  1863. }
  1864. $count = DB::table('project_task')->where([ 'id' => $taskid, 'username' => $user['username']])->count();
  1865. if ($count <= 0) {
  1866. return Base::retError('你不是任务负责人!');
  1867. }
  1868. $whereArray[] = ['taskid', '=', $taskid];
  1869. }
  1870. $whereArray[] = ['delete', '=', 0];
  1871. if (intval(Request::input('taskid')) > 0) {
  1872. $whereArray[] = ['taskid', '=', intval(Request::input('taskid'))];
  1873. }
  1874. if (trim(Request::input('name'))) {
  1875. $whereArray[] = ['name', 'like', '%' . trim(Request::input('name')) . '%'];
  1876. }
  1877. if (trim(Request::input('username'))) {
  1878. $whereArray[] = ['username', '=', trim(Request::input('username'))];
  1879. }
  1880. //
  1881. $lists = DB::table('project_files')
  1882. ->where($whereArray)
  1883. ->orderByRaw($orderBy)->paginate(Min(Max(Base::nullShow(Request::input('pagesize'), 10), 1), 100));
  1884. $lists = Base::getPageList($lists);
  1885. if ($lists['total'] == 0) {
  1886. return Base::retError('未找到任何相关的文件', $lists);
  1887. }
  1888. foreach ($lists['lists'] AS $key => $item) {
  1889. $lists['lists'][$key]['path'] = Base::fillUrl($item['path']);
  1890. $lists['lists'][$key]['thumb'] = Base::fillUrl($item['thumb']);
  1891. $lists['lists'][$key]['yetdown'] = intval(Session::get('filesDownload:' . $item['id']));
  1892. }
  1893. return Base::retSuccess('success', $lists);
  1894. }
  1895. /**
  1896. * 项目文件-上传
  1897. *
  1898. * @apiParam {Number} [projectid] 项目ID
  1899. * @apiParam {Number} [taskid] 任务ID(如果项目ID为空时此参必须赋值且任务必须是自己负责人)
  1900. */
  1901. public function files__upload()
  1902. {
  1903. $user = Users::authE();
  1904. if (Base::isError($user)) {
  1905. return $user;
  1906. } else {
  1907. $user = $user['data'];
  1908. }
  1909. //
  1910. $projectid = intval(Request::input('projectid'));
  1911. $taskid = intval(Request::input('taskid'));
  1912. if ($projectid > 0) {
  1913. $inRes = Project::inThe($projectid, $user['username']);
  1914. if (Base::isError($inRes)) {
  1915. return $inRes;
  1916. }
  1917. } else {
  1918. if ($taskid <= 0) {
  1919. return Base::retError('参数错误!');
  1920. }
  1921. $row = Base::DBC2A(DB::table('project_task')->select(['projectid'])->where([ 'id' => $taskid, 'username' => $user['username']])->first());
  1922. if (empty($row)) {
  1923. return Base::retError('你不是任务负责人!');
  1924. }
  1925. $projectid = $row['projectid'];
  1926. }
  1927. //
  1928. $data = Base::upload([
  1929. "file" => Request::file('files'),
  1930. "type" => 'file',
  1931. "path" => "uploads/projects/" . $projectid . "/",
  1932. ]);
  1933. if (Base::isError($data)) {
  1934. return Base::retError($data['msg']);
  1935. } else {
  1936. $fileData = $data['data'];
  1937. $thumb = 'images/files/file.png';
  1938. switch ($fileData['ext']) {
  1939. case "docx":
  1940. $thumb = 'images/files/doc.png';
  1941. break;
  1942. case "xlsx":
  1943. $thumb = 'images/files/xls.png';
  1944. break;
  1945. case "pptx":
  1946. $thumb = 'images/files/ppt.png';
  1947. break;
  1948. case "doc":
  1949. case "xls":
  1950. case "ppt":
  1951. case "txt":
  1952. case "esp":
  1953. case "gif":
  1954. $thumb = 'images/files/' . $fileData['ext'] . '.png';
  1955. break;
  1956. case "jpg":
  1957. case "jpeg":
  1958. case "png":
  1959. if (Base::imgThumb($fileData['file'], $fileData['file'] . "_thumb.jpg", 64, 0)) {
  1960. $thumb = $fileData['path'] . "_thumb.jpg";
  1961. }
  1962. break;
  1963. }
  1964. $array = [
  1965. 'projectid' => $projectid,
  1966. 'taskid' => $taskid,
  1967. 'name' => $fileData['name'],
  1968. 'size' => $fileData['size'] * 1024,
  1969. 'ext' => $fileData['ext'],
  1970. 'path' => $fileData['path'],
  1971. 'thumb' => $thumb,
  1972. 'username' => $user['username'],
  1973. 'indate' => Base::time(),
  1974. ];
  1975. $id = DB::table('project_files')->insertGetId($array);
  1976. $array['id'] = $id;
  1977. $array['path'] = Base::fillUrl($array['path']);
  1978. $array['thumb'] = Base::fillUrl($array['thumb']);
  1979. $array['download'] = 0;
  1980. $array['yetdown'] = 0;
  1981. DB::table('project_log')->insert([
  1982. 'type' => '日志',
  1983. 'projectid' => $projectid,
  1984. 'taskid' => $taskid,
  1985. 'username' => $user['username'],
  1986. 'detail' => '上传文件',
  1987. 'indate' => Base::time(),
  1988. 'other' => Base::array2string([
  1989. 'type' => 'file',
  1990. 'id' => $id,
  1991. 'name' => $fileData['name'],
  1992. ])
  1993. ]);
  1994. if ($taskid > 0) {
  1995. DB::table('project_task')->where('id', $taskid)->increment('filenum');
  1996. }
  1997. return Base::retSuccess('success', $array);
  1998. }
  1999. }
  2000. /**
  2001. * 项目文件-下载
  2002. *
  2003. * @apiParam {Number} fileid 文件ID
  2004. */
  2005. public function files__download()
  2006. {
  2007. $fileDetail = Base::DBC2A(DB::table('project_files')->where('id', intval(Request::input('fileid')))->where('delete', 0)->first());
  2008. if (empty($fileDetail)) {
  2009. return abort(404, '文件不存在或已被删除!');
  2010. }
  2011. $filePath = public_path($fileDetail['path']);
  2012. if (!file_exists($filePath)) {
  2013. return abort(404, '文件不存在或已被删除。');
  2014. }
  2015. if (intval(Session::get('filesDownload:' . $fileDetail['id'])) !== 1) {
  2016. Session::put('filesDownload:' . $fileDetail['id'], 1);
  2017. DB::table('project_files')->where('id', $fileDetail['id'])->increment('download');
  2018. }
  2019. return response()->download($filePath, $fileDetail['name']);
  2020. }
  2021. /**
  2022. * 项目文件-重命名
  2023. *
  2024. * @apiParam {Number} fileid 文件ID
  2025. * @apiParam {String} name 新文件名称
  2026. */
  2027. public function files__rename()
  2028. {
  2029. $user = Users::authE();
  2030. if (Base::isError($user)) {
  2031. return $user;
  2032. } else {
  2033. $user = $user['data'];
  2034. }
  2035. //
  2036. $fileDetail = Base::DBC2A(DB::table('project_files')->where('id', intval(Request::input('fileid')))->where('delete', 0)->first());
  2037. if (empty($fileDetail)) {
  2038. return Base::retError('文件不存在或已被删除!');
  2039. }
  2040. if ($fileDetail['username'] != $user['username']) {
  2041. $inRes = Project::inThe($fileDetail['projectid'], $user['username'], true);
  2042. if (Base::isError($inRes)) {
  2043. return Base::retError('此操作仅支持管理员或上传者!');
  2044. }
  2045. }
  2046. //
  2047. $name = Base::rightDelete(trim(Request::input('name')), '.' . $fileDetail['ext']);
  2048. if (empty($name)) {
  2049. return Base::retError('文件名称不能为空!');
  2050. } elseif (mb_strlen($name) > 32) {
  2051. return Base::retError('文件名称最多只能设置32个字!');
  2052. }
  2053. //
  2054. $name .= '.' . $fileDetail['ext'];
  2055. if (DB::table('project_files')->where('id', $fileDetail['id'])->update([ 'name' => $name ])) {
  2056. DB::table('project_log')->insert([
  2057. 'type' => '日志',
  2058. 'projectid' => $fileDetail['projectid'],
  2059. 'taskid' => $fileDetail['taskid'],
  2060. 'username' => $user['username'],
  2061. 'detail' => '文件【' . $fileDetail['name'] . '】重命名',
  2062. 'indate' => Base::time(),
  2063. 'other' => Base::array2string([
  2064. 'type' => 'file',
  2065. 'id' => $fileDetail['id'],
  2066. 'name' => $name,
  2067. ])
  2068. ]);
  2069. }
  2070. //
  2071. return Base::retSuccess('修改成功!', [
  2072. 'name' => $name,
  2073. ]);
  2074. }
  2075. /**
  2076. * 项目文件-删除
  2077. *
  2078. * @apiParam {Number} fileid 文件ID
  2079. */
  2080. public function files__delete()
  2081. {
  2082. $user = Users::authE();
  2083. if (Base::isError($user)) {
  2084. return $user;
  2085. } else {
  2086. $user = $user['data'];
  2087. }
  2088. //
  2089. $fileDetail = Base::DBC2A(DB::table('project_files')->where('id', intval(Request::input('fileid')))->where('delete', 0)->first());
  2090. if (empty($fileDetail)) {
  2091. return Base::retError('文件不存在或已被删除!');
  2092. }
  2093. if ($fileDetail['username'] != $user['username']) {
  2094. $inRes = Project::inThe($fileDetail['projectid'], $user['username'], true);
  2095. if (Base::isError($inRes)) {
  2096. return Base::retError('此操作仅支持管理员或上传者!');
  2097. }
  2098. }
  2099. //
  2100. DB::table('project_files')->where('id', $fileDetail['id'])->update([
  2101. 'delete' => 1,
  2102. 'deletedate' => Base::time()
  2103. ]);
  2104. DB::table('project_log')->insert([
  2105. 'type' => '日志',
  2106. 'projectid' => $fileDetail['projectid'],
  2107. 'taskid' => $fileDetail['taskid'],
  2108. 'username' => $user['username'],
  2109. 'detail' => '删除文件',
  2110. 'indate' => Base::time(),
  2111. 'other' => Base::array2string([
  2112. 'type' => 'file',
  2113. 'id' => $fileDetail['id'],
  2114. 'name' => $fileDetail['name'],
  2115. ])
  2116. ]);
  2117. if ($fileDetail['taskid'] > 0) {
  2118. DB::table('project_task')->where('id', $fileDetail['taskid'])->decrement('filenum');
  2119. }
  2120. //
  2121. return Base::retSuccess('删除成功!');
  2122. }
  2123. /**
  2124. * 项目动态-列表
  2125. *
  2126. * @apiParam {Number} [projectid] 项目ID
  2127. * @apiParam {Number} [taskid] 任务ID(如果项目ID为空时此参必须赋值且任务必须是自己负责人)
  2128. * @apiParam {String} [type] 类型
  2129. * - 全部: 日志+评论(默认)
  2130. * - 日志
  2131. * - 评论
  2132. * @apiParam {String} [username] 用户名
  2133. * @apiParam {Number} [page] 当前页,默认:1
  2134. * @apiParam {Number} [pagesize] 每页显示数量,默认:20,最大:100
  2135. */
  2136. public function log__lists()
  2137. {
  2138. $user = Users::authE();
  2139. if (Base::isError($user)) {
  2140. return $user;
  2141. } else {
  2142. $user = $user['data'];
  2143. }
  2144. //
  2145. $projectid = intval(Request::input('projectid'));
  2146. if ($projectid > 0) {
  2147. $inRes = Project::inThe($projectid, $user['username']);
  2148. if (Base::isError($inRes)) {
  2149. return $inRes;
  2150. }
  2151. }
  2152. //
  2153. $taskid = intval(Request::input('taskid'));
  2154. $whereArray = [];
  2155. $whereFunc = null;
  2156. if ($projectid > 0) {
  2157. $whereArray[] = ['projectid', '=', $projectid];
  2158. if ($taskid > 0) {
  2159. $whereArray[] = ['taskid', '=', $taskid];
  2160. }
  2161. } else {
  2162. if ($taskid < 0) {
  2163. return Base::retError('参数错误!');
  2164. }
  2165. $count = DB::table('project_task')->where([ 'id' => $taskid, 'username' => $user['username']])->count();
  2166. if ($count <= 0) {
  2167. return Base::retError('你不是任务负责人!');
  2168. }
  2169. $whereArray[] = ['taskid', '=', $taskid];
  2170. }
  2171. if (trim(Request::input('username'))) {
  2172. $whereArray[] = ['username', '=', trim(Request::input('username'))];
  2173. }
  2174. switch (trim(Request::input('type'))) {
  2175. case '日志': {
  2176. $whereArray[] = ['type', '=', '日志'];
  2177. break;
  2178. }
  2179. case '评论': {
  2180. $whereArray[] = ['type', '=', '评论'];
  2181. break;
  2182. }
  2183. default: {
  2184. $whereFunc = function ($query) {
  2185. $query->whereIn('type', ['日志', '评论']);
  2186. };
  2187. break;
  2188. }
  2189. }
  2190. //
  2191. $lists = DB::table('project_log')
  2192. ->where($whereArray)
  2193. ->where($whereFunc)
  2194. ->orderByDesc('indate')->paginate(Min(Max(Base::nullShow(Request::input('pagesize'), 10), 1), 100));
  2195. $lists = Base::getPageList($lists);
  2196. if ($lists['total'] == 0) {
  2197. return Base::retError('未找到任何相关的记录', $lists);
  2198. }
  2199. foreach ($lists['lists'] AS $key => $item) {
  2200. $item = array_merge($item, Users::username2basic($item['username']));
  2201. $item['timeData'] = [
  2202. 'ymd' => date(date("Y", $item['indate']) == date("Y", Base::time()) ? "m-d" : "Y-m-d", $item['indate']),
  2203. 'hi' => date("h:i", $item['indate']) ,
  2204. 'week' => "周" . Base::getTimeWeek($item['indate']),
  2205. 'segment' => Base::getTimeDayeSegment($item['indate']),
  2206. ];
  2207. $item['other'] = Base::string2array($item['other'], ['type' => '']);
  2208. $lists['lists'][$key] = $item;
  2209. }
  2210. return Base::retSuccess('success', $lists);
  2211. }
  2212. }