ProjectController.php 93 KB

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