ProjectController.php 87 KB

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