ProjectController.php 75 KB

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