ProjectController.php 89 KB

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