ProjectController.php 87 KB

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