ProjectController.php 93 KB

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