ProjectController.php 93 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419
  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. $isowner = false;
  1097. if ($taskid > 0) {
  1098. $taskDetail = Base::DBC2A(DB::table('project_task')->where('id', $taskid)->first());
  1099. if (empty($taskDetail)) {
  1100. return Base::retError('任务不存在!');
  1101. }
  1102. if ($taskDetail['username'] == $user['username']) {
  1103. $isowner = true;
  1104. }
  1105. $projectid = $taskDetail['projectid'];
  1106. } else {
  1107. $projectid = intval(Request::input('projectid'));
  1108. }
  1109. //
  1110. if ($projectid > 0 && !$isowner) {
  1111. $inRes = Project::inThe($projectid, $user['username']);
  1112. if (Base::isError($inRes)) {
  1113. return $inRes;
  1114. }
  1115. }
  1116. //
  1117. $orderBy = '`inorder` DESC,`id` DESC';
  1118. $sorts = Base::json2array(Request::input('sorts'));
  1119. if (in_array($sorts['order'], ['asc', 'desc'])) {
  1120. switch ($sorts['key']) {
  1121. case 'title':
  1122. case 'labelid':
  1123. case 'enddate':
  1124. case 'username':
  1125. case 'level':
  1126. case 'indate':
  1127. case 'inorder':
  1128. case 'userorder':
  1129. $orderBy = '`' . $sorts['key'] . '` ' . $sorts['order'] . ',`id` DESC';
  1130. break;
  1131. case 'type':
  1132. $orderBy = 'CASE WHEN `complete`= 0 AND `enddate` BETWEEN 1 AND ' . Base::time() . ' THEN 0 ELSE 1 END ' . $sorts['order'] . ', `complete` ' . $sorts['order'] . ',`id` DESC';
  1133. break;
  1134. }
  1135. }
  1136. //
  1137. $selectArray = ['project_task.*'];
  1138. $whereRaw = null;
  1139. $whereFunc = null;
  1140. $whereArray = [];
  1141. $whereArray[] = ['project_task.delete', '=', 0];
  1142. if (intval(Request::input('createuser')) === 1) {
  1143. $whereArray[] = ['project_task.createuser', '=', $user['username']];
  1144. if ($projectid > 0) {
  1145. $whereArray[] = ['project_lists.id', '=', $projectid];
  1146. }
  1147. if (trim(Request::input('username'))) {
  1148. $whereArray[] = ['project_task.username', '=', trim(Request::input('username'))];
  1149. }
  1150. } else if (intval(Request::input('attention')) === 1) {
  1151. if ($projectid > 0) {
  1152. $whereArray[] = ['project_lists.id', '=', $projectid];
  1153. }
  1154. if (trim(Request::input('username'))) {
  1155. $whereArray[] = ['project_task.username', '=', trim(Request::input('username'))];
  1156. }
  1157. } else {
  1158. if ($projectid > 0) {
  1159. $whereArray[] = ['project_lists.id', '=', $projectid];
  1160. if (trim(Request::input('username'))) {
  1161. $whereArray[] = ['project_task.username', '=', trim(Request::input('username'))];
  1162. }
  1163. } else {
  1164. $whereArray[] = ['project_task.username', '=', $user['username']];
  1165. }
  1166. }
  1167. if ($taskid > 0) {
  1168. $whereArray[] = ['project_task.id', '=', intval(Request::input('taskid'))];
  1169. }
  1170. if (intval(Request::input('labelid')) > 0) {
  1171. $whereArray[] = ['project_task.labelid', '=', intval(Request::input('labelid'))];
  1172. }
  1173. if (intval(Request::input('level')) > 0) {
  1174. $whereArray[] = ['project_task.level', '=', intval(Request::input('level'))];
  1175. }
  1176. $archived = trim(Request::input('archived'));
  1177. if (empty($archived)) $archived = "未归档";
  1178. switch ($archived) {
  1179. case '已归档':
  1180. $whereArray[] = ['project_task.archived', '=', 1];
  1181. $orderBy = '`archiveddate` DESC';
  1182. break;
  1183. case '未归档':
  1184. $whereArray[] = ['project_task.archived', '=', 0];
  1185. break;
  1186. }
  1187. $type = trim(Request::input('type'));
  1188. switch ($type) {
  1189. case '未完成':
  1190. $whereArray[] = ['project_task.complete', '=', 0];
  1191. break;
  1192. case '已超期':
  1193. $whereArray[] = ['project_task.complete', '=', 0];
  1194. $whereArray[] = ['project_task.enddate', '>', 0];
  1195. $whereArray[] = ['project_task.enddate', '<=', Base::time()];
  1196. break;
  1197. case '已完成':
  1198. $whereArray[] = ['project_task.complete', '=', 1];
  1199. break;
  1200. }
  1201. $startdate = trim(Request::input('startdate'));
  1202. $enddate = trim(Request::input('enddate'));
  1203. if (Base::isDate($startdate) || Base::isDate($enddate)) {
  1204. $startdate = strtotime($startdate . ' 00:00:00');
  1205. $enddate = strtotime($enddate . ' 23:59:59');
  1206. $whereRaw.= $whereRaw ? ' AND ' : '';
  1207. $whereRaw.= "((`startdate` >= " . $startdate . " OR `startdate` = 0) AND (`enddate` <= " . $enddate . " OR `enddate` = 0))";
  1208. $orderBy = '`startdate` DESC';
  1209. }
  1210. //
  1211. $builder = DB::table('project_task');
  1212. if ($projectid > 0) {
  1213. $builder->join('project_lists', 'project_lists.id', '=', 'project_task.projectid');
  1214. }
  1215. if (intval(Request::input('attention')) === 1) {
  1216. $builder->join('project_users', 'project_users.taskid', '=', 'project_task.id');
  1217. $builder->where([
  1218. ['project_users.type', '=', '关注'],
  1219. ['project_users.username', '=', $user['username']],
  1220. ]);
  1221. $selectArray[] = 'project_users.indate AS attentiondate';
  1222. }
  1223. if ($whereRaw) {
  1224. $builder->whereRaw($whereRaw);
  1225. }
  1226. $lists = $builder->select($selectArray)
  1227. ->where($whereArray)
  1228. ->orderByRaw($orderBy)
  1229. ->paginate(Min(Max(Base::nullShow(Request::input('pagesize'), 10), 1), 100));
  1230. $lists = Base::getPageList($lists, $taskid > 0 ? false : true);
  1231. if (intval(Request::input('statistics')) == 1) {
  1232. $lists['statistics_unfinished'] = $type === '未完成' ? $lists['total'] : DB::table('project_task')->where('projectid', $projectid)->where('delete', 0)->where('archived', 0)->where('complete', 0)->count();
  1233. $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();
  1234. $lists['statistics_complete'] = $type === '已完成' ? $lists['total'] : DB::table('project_task')->where('projectid', $projectid)->where('delete', 0)->where('archived', 0)->where('complete', 1)->count();
  1235. }
  1236. if ($lists['total'] == 0) {
  1237. return Base::retError('未找到任何相关的任务!', $lists);
  1238. }
  1239. foreach ($lists['lists'] AS $key => $info) {
  1240. $info['overdue'] = Project::taskIsOverdue($info);
  1241. $info['subtask'] = Base::string2array($info['subtask']);
  1242. $info['follower'] = Base::string2array($info['follower']);
  1243. $lists['lists'][$key] = array_merge($info, Users::username2basic($info['username']));
  1244. }
  1245. if ($taskid > 0) {
  1246. if (count($lists['lists']) == 0) {
  1247. return Base::retError('未能找到此任务或无法管理此任务!');
  1248. }
  1249. $data = $lists['lists'][0];
  1250. $data['projectTitle'] = $data['projectid'] > 0 ? DB::table('project_lists')->where('id', $data['projectid'])->value('title') : '';
  1251. return Base::retSuccess('success', $data);
  1252. } else {
  1253. return Base::retSuccess('success', $lists);
  1254. }
  1255. }
  1256. /**
  1257. * 项目任务-获取数量
  1258. *
  1259. * @apiParam {Number} [level] 任务等级(1~4,留空获取所有)
  1260. */
  1261. public function task__levelnum()
  1262. {
  1263. $user = Users::authE();
  1264. if (Base::isError($user)) {
  1265. return $user;
  1266. } else {
  1267. $user = $user['data'];
  1268. }
  1269. //
  1270. $whereArray = [];
  1271. $whereArray[] = ['project_task.delete', '=', 0];
  1272. $whereArray[] = ['project_task.username', '=', $user['username']];
  1273. $whereArray[] = ['project_task.archived', '=', 0];
  1274. $array = [
  1275. 'level_1' => 0,
  1276. 'level_2' => 0,
  1277. 'level_3' => 0,
  1278. 'level_4' => 0,
  1279. ];
  1280. $level = intval(Request::input('level'));
  1281. if ($level > 0) {
  1282. $array = [];
  1283. $array['level_' . $level] = 0;
  1284. }
  1285. foreach ($array AS $key => $val) {
  1286. $level = intval(Base::leftDelete($key, 'level_'));
  1287. $array[$key] = DB::table('project_task')->where($whereArray)->where('level', $level)->count();
  1288. }
  1289. return Base::retSuccess('success', $array);
  1290. }
  1291. /**
  1292. * 项目任务-添加任务
  1293. *
  1294. * @apiParam {String} title 任务标题
  1295. * @apiParam {Number} [projectid] 项目ID
  1296. * @apiParam {Number} [labelid] 项目子分类ID
  1297. * @apiParam {Number} [level] 任务紧急级别(1~4,默认:2)
  1298. * @apiParam {String} [username] 任务负责人用户名(如果项目ID为空时此参数无效,负责人为自己)
  1299. *
  1300. * @throws \Throwable
  1301. */
  1302. public function task__add()
  1303. {
  1304. $user = Users::authE();
  1305. if (Base::isError($user)) {
  1306. return $user;
  1307. } else {
  1308. $user = $user['data'];
  1309. }
  1310. //
  1311. $projectid = intval(Request::input('projectid'));
  1312. $labelid = intval(Request::input('labelid'));
  1313. if ($projectid > 0) {
  1314. $projectDetail = Base::DBC2A(DB::table('project_lists')->where('id', $projectid)->where('delete', 0)->first());
  1315. if (empty($projectDetail)) {
  1316. return Base::retError('项目不存在或已被删除!');
  1317. }
  1318. //
  1319. $labelDetail = Base::DBC2A(DB::table('project_label')->where('id', $labelid)->where('projectid', $projectid)->first());
  1320. if (empty($labelDetail)) {
  1321. return Base::retError('项目子分类不存在或已被删除!');
  1322. }
  1323. //
  1324. $inRes = Project::inThe($projectid, $user['username']);
  1325. if (Base::isError($inRes)) {
  1326. return $inRes;
  1327. }
  1328. $checkRole = Project::role('add_role', $projectid, 0);
  1329. if (Base::isError($checkRole)) {
  1330. return $checkRole;
  1331. }
  1332. //
  1333. $username = trim(Request::input('username'));
  1334. if (empty($username)) {
  1335. $username = $user['username'];
  1336. }
  1337. if ($username != $user['username']) {
  1338. $inRes = Project::inThe($projectid, $username);
  1339. if (Base::isError($inRes)) {
  1340. return Base::retError('负责人不在项目成员内!');
  1341. }
  1342. }
  1343. } else {
  1344. $username = $user['username'];
  1345. }
  1346. //
  1347. $title = trim(Request::input('title'));
  1348. if (empty($title)) {
  1349. return Base::retError('任务标题不能为空!');
  1350. } elseif (mb_strlen($title) > 255) {
  1351. return Base::retError('任务标题最多只能设置255个字!');
  1352. }
  1353. //
  1354. $level = max(1, min(4, intval(Request::input('level'))));
  1355. $inArray = [
  1356. 'projectid' => $projectid,
  1357. 'labelid' => $labelid,
  1358. 'createuser' => $user['username'],
  1359. 'username' => $username,
  1360. 'title' => $title,
  1361. 'level' => $level,
  1362. 'inorder' => empty($projectid) ? 0 : intval(DB::table('project_task')->where('projectid', $projectid)->orderByDesc('inorder')->value('inorder')) + 1,
  1363. 'userorder' => intval(DB::table('project_task')->where('username', $user['username'])->where('level', $level)->orderByDesc('userorder')->value('userorder')) + 1,
  1364. 'indate' => Base::time(),
  1365. 'startdate' => Base::time(),
  1366. 'subtask' => Base::array2string([]),
  1367. 'follower' => Base::array2string([]),
  1368. ];
  1369. return DB::transaction(function () use ($inArray) {
  1370. $taskid = DB::table('project_task')->insertGetId($inArray);
  1371. if (empty($taskid)) {
  1372. return Base::retError('系统繁忙,请稍后再试!');
  1373. }
  1374. DB::table('project_log')->insert([
  1375. 'type' => '日志',
  1376. 'projectid' => $inArray['projectid'],
  1377. 'taskid' => $taskid,
  1378. 'username' => $inArray['createuser'],
  1379. 'detail' => '添加任务',
  1380. 'indate' => Base::time(),
  1381. 'other' => Base::array2string([
  1382. 'type' => 'task',
  1383. 'id' => $taskid,
  1384. 'title' => $inArray['title'],
  1385. ])
  1386. ]);
  1387. Project::updateNum($inArray['projectid']);
  1388. //
  1389. $task = Base::DBC2A(DB::table('project_task')->where('id', $taskid)->first());
  1390. $task['overdue'] = Project::taskIsOverdue($task);
  1391. $task['subtask'] = Base::string2array($task['subtask']);
  1392. $task['follower'] = Base::string2array($task['follower']);
  1393. $task = array_merge($task, Users::username2basic($task['username']));
  1394. return Base::retSuccess('添加成功!', $task);
  1395. });
  1396. }
  1397. /**
  1398. * 项目任务-修改
  1399. *
  1400. * @apiParam {Number} taskid 任务ID
  1401. * @apiParam {String} act 修改字段|操作类型
  1402. * - title: 标题
  1403. * - desc: 描述
  1404. * - level: 优先级
  1405. * - username: 负责人
  1406. * - plannedtime: 设置计划时间
  1407. * - unplannedtime: 取消计划时间
  1408. * - complete: 标记完成
  1409. * - unfinished: 标记未完成
  1410. * - archived: 归档
  1411. * - unarchived: 取消归档
  1412. * - delete: 删除任务
  1413. * - comment: 评论
  1414. * - attention: 添加关注
  1415. * @apiParam {String} [content] 内容数据
  1416. *
  1417. * @throws \Throwable
  1418. */
  1419. public function task__edit()
  1420. {
  1421. $user = Users::authE();
  1422. if (Base::isError($user)) {
  1423. return $user;
  1424. } else {
  1425. $user = $user['data'];
  1426. }
  1427. //
  1428. $act = trim(Request::input('act'));
  1429. $taskid = intval(Request::input('taskid'));
  1430. $task = Base::DBC2A(DB::table('project_task')
  1431. ->where([
  1432. ['delete', '=', 0],
  1433. ['id', '=', $taskid],
  1434. ])
  1435. ->first());
  1436. if (empty($task)) {
  1437. return Base::retError('任务不存在!');
  1438. }
  1439. if ($task['projectid'] > 0) {
  1440. $inRes = Project::inThe($task['projectid'], $user['username']);
  1441. if (Base::isError($inRes)) {
  1442. return $inRes;
  1443. }
  1444. if (!in_array($act, ['comment', 'attention'])) {
  1445. $checkRole = Project::role('edit_role', $task['projectid'], $task['id']);
  1446. if (Base::isError($checkRole)) {
  1447. return $checkRole;
  1448. }
  1449. switch ($act) {
  1450. case 'complete':
  1451. case 'unfinished':
  1452. $checkRole = Project::role('complete_role', $task['projectid'], $task['id']);
  1453. if (Base::isError($checkRole)) {
  1454. return $checkRole;
  1455. }
  1456. break;
  1457. case 'archived':
  1458. case 'unarchived':
  1459. $checkRole = Project::role('archived_role', $task['projectid'], $task['id']);
  1460. if (Base::isError($checkRole)) {
  1461. return $checkRole;
  1462. }
  1463. break;
  1464. case 'delete':
  1465. $checkRole = Project::role('del_role', $task['projectid'], $task['id']);
  1466. if (Base::isError($checkRole)) {
  1467. return $checkRole;
  1468. }
  1469. break;
  1470. }
  1471. }
  1472. } else {
  1473. if ($task['username'] != $user['username']) {
  1474. return Base::retError('此操作只允许任务负责人!');
  1475. }
  1476. }
  1477. //
  1478. $content = trim(Request::input('content'));
  1479. $message = "";
  1480. $upArray = [];
  1481. $logArray = [];
  1482. switch ($act) {
  1483. /**
  1484. * 修改标题
  1485. */
  1486. case 'title': {
  1487. if ($content == $task['title']) {
  1488. return Base::retError('标题未做改变!');
  1489. }
  1490. $upArray['title'] = $content;
  1491. $logArray[] = [
  1492. 'type' => '日志',
  1493. 'projectid' => $task['projectid'],
  1494. 'taskid' => $task['id'],
  1495. 'username' => $user['username'],
  1496. 'detail' => '修改任务标题',
  1497. 'indate' => Base::time(),
  1498. 'other' => Base::array2string([
  1499. 'type' => 'task',
  1500. 'id' => $task['id'],
  1501. 'title' => $content,
  1502. 'old_title' => $task['title'],
  1503. ])
  1504. ];
  1505. break;
  1506. }
  1507. /**
  1508. * 修改描述
  1509. */
  1510. case 'desc': {
  1511. if ($content == $task['desc']) {
  1512. return Base::retError('描述未做改变!');
  1513. }
  1514. $upArray['desc'] = $content;
  1515. $logArray[] = [
  1516. 'type' => '日志',
  1517. 'projectid' => $task['projectid'],
  1518. 'taskid' => $task['id'],
  1519. 'username' => $user['username'],
  1520. 'detail' => '修改任务描述',
  1521. 'indate' => Base::time(),
  1522. 'other' => Base::array2string([
  1523. 'type' => 'task',
  1524. 'id' => $task['id'],
  1525. 'title' => $task['title'],
  1526. 'old_desc' => $task['desc'],
  1527. ])
  1528. ];
  1529. break;
  1530. }
  1531. /**
  1532. * 调整任务等级
  1533. */
  1534. case 'level': {
  1535. $content = intval($content);
  1536. if ($content == $task['level']) {
  1537. return Base::retError('优先级未做改变!');
  1538. }
  1539. if ($content > 4 || $content < 1) {
  1540. return Base::retError('优先级参数错误!');
  1541. }
  1542. $upArray['level'] = $content;
  1543. $logArray[] = [
  1544. 'type' => '日志',
  1545. 'projectid' => $task['projectid'],
  1546. 'taskid' => $task['id'],
  1547. 'username' => $user['username'],
  1548. 'detail' => '调整任务等级为【P' . $content . '】',
  1549. 'indate' => Base::time(),
  1550. 'other' => Base::array2string([
  1551. 'type' => 'task',
  1552. 'id' => $task['id'],
  1553. 'title' => $task['title'],
  1554. 'old_level' => $task['level'],
  1555. ])
  1556. ];
  1557. break;
  1558. }
  1559. /**
  1560. * 修改任务负责人
  1561. */
  1562. case 'username': {
  1563. if ($content == $task['username']) {
  1564. return Base::retError('负责人未做改变!');
  1565. }
  1566. if ($task['projectid'] > 0) {
  1567. $inRes = Project::inThe($task['projectid'], $content);
  1568. if (Base::isError($inRes)) {
  1569. return Base::retError(['%不在成员列表内!', $content]);
  1570. }
  1571. }
  1572. $upArray['username'] = $content;
  1573. $logArray[] = [
  1574. 'type' => '日志',
  1575. 'projectid' => $task['projectid'],
  1576. 'taskid' => $task['id'],
  1577. 'username' => $user['username'],
  1578. 'detail' => '修改负责人',
  1579. 'indate' => Base::time(),
  1580. 'other' => Base::array2string([
  1581. 'type' => 'task',
  1582. 'id' => $task['id'],
  1583. 'title' => $task['title'],
  1584. 'old_username' => $task['username'],
  1585. ])
  1586. ];
  1587. break;
  1588. }
  1589. /**
  1590. * 修改计划时间
  1591. */
  1592. case 'plannedtime': {
  1593. list($startdate, $enddate) = explode(",", $content);
  1594. if (!Base::isDate($startdate) || !Base::isDate($enddate)) {
  1595. return Base::retError('计划时间参数错误!');
  1596. }
  1597. $startdate = strtotime($startdate);
  1598. $enddate = strtotime($enddate);
  1599. if ($startdate == $task['startdate'] && $enddate == $task['enddate']) {
  1600. return Base::retError('与原计划时间一致!');
  1601. }
  1602. if ($startdate == $enddate) {
  1603. return Base::retError('开始时间与结束时间一致!');
  1604. }
  1605. $upArray['startdate'] = $startdate;
  1606. $upArray['enddate'] = $enddate;
  1607. $logArray[] = [
  1608. 'type' => '日志',
  1609. 'projectid' => $task['projectid'],
  1610. 'taskid' => $task['id'],
  1611. 'username' => $user['username'],
  1612. 'detail' => '设置计划时间',
  1613. 'indate' => Base::time(),
  1614. 'other' => Base::array2string([
  1615. 'type' => 'task',
  1616. 'id' => $task['id'],
  1617. 'title' => $task['title'],
  1618. 'old_startdate' => $task['startdate'],
  1619. 'old_enddate' => $task['enddate'],
  1620. ])
  1621. ];
  1622. break;
  1623. }
  1624. /**
  1625. * 取消计划时间
  1626. */
  1627. case 'unplannedtime': {
  1628. $startdate = 0;
  1629. $enddate = 0;
  1630. if ($startdate == $task['startdate'] && $enddate == $task['enddate']) {
  1631. return Base::retError('与原计划时间一致!');
  1632. }
  1633. $upArray['startdate'] = $startdate;
  1634. $upArray['enddate'] = $enddate;
  1635. $logArray[] = [
  1636. 'type' => '日志',
  1637. 'projectid' => $task['projectid'],
  1638. 'taskid' => $task['id'],
  1639. 'username' => $user['username'],
  1640. 'detail' => '取消计划时间',
  1641. 'indate' => Base::time(),
  1642. 'other' => Base::array2string([
  1643. 'type' => 'task',
  1644. 'id' => $task['id'],
  1645. 'title' => $task['title'],
  1646. 'old_startdate' => $task['startdate'],
  1647. 'old_enddate' => $task['enddate'],
  1648. ])
  1649. ];
  1650. break;
  1651. }
  1652. /**
  1653. * 标记完成
  1654. */
  1655. case 'complete': {
  1656. if ($task['complete'] == 1) {
  1657. return Base::retError('任务已标记完成,请勿重复操作!');
  1658. }
  1659. $upArray['complete'] = 1;
  1660. $upArray['completedate'] = Base::time();
  1661. $logArray[] = [
  1662. 'type' => '日志',
  1663. 'projectid' => $task['projectid'],
  1664. 'taskid' => $task['id'],
  1665. 'username' => $user['username'],
  1666. 'detail' => '标记已完成',
  1667. 'indate' => Base::time(),
  1668. 'other' => Base::array2string([
  1669. 'type' => 'task',
  1670. 'id' => $task['id'],
  1671. 'title' => $task['title'],
  1672. ])
  1673. ];
  1674. break;
  1675. }
  1676. /**
  1677. * 标记未完成
  1678. */
  1679. case 'unfinished': {
  1680. if ($task['complete'] == 0) {
  1681. return Base::retError('任务未完成,无法标记未完成!');
  1682. }
  1683. $upArray['complete'] = 0;
  1684. $logArray[] = [
  1685. 'type' => '日志',
  1686. 'projectid' => $task['projectid'],
  1687. 'taskid' => $task['id'],
  1688. 'username' => $user['username'],
  1689. 'detail' => '标记未完成',
  1690. 'indate' => Base::time(),
  1691. 'other' => Base::array2string([
  1692. 'type' => 'task',
  1693. 'id' => $task['id'],
  1694. 'title' => $task['title'],
  1695. ])
  1696. ];
  1697. break;
  1698. }
  1699. /**
  1700. * 归档
  1701. */
  1702. case 'archived': {
  1703. if ($task['archived'] == 1) {
  1704. return Base::retError('任务已经归档,请勿重复操作!');
  1705. }
  1706. $upArray['archived'] = 1;
  1707. $upArray['archiveddate'] = Base::time();
  1708. $logArray[] = [
  1709. 'type' => '日志',
  1710. 'projectid' => $task['projectid'],
  1711. 'taskid' => $task['id'],
  1712. 'username' => $user['username'],
  1713. 'detail' => '任务归档',
  1714. 'indate' => Base::time(),
  1715. 'other' => Base::array2string([
  1716. 'type' => 'task',
  1717. 'id' => $task['id'],
  1718. 'title' => $task['title'],
  1719. ])
  1720. ];
  1721. break;
  1722. }
  1723. /**
  1724. * 取消归档
  1725. */
  1726. case 'unarchived': {
  1727. if ($task['archived'] == 0) {
  1728. return Base::retError('任务未归档,无法取消归档操作!');
  1729. }
  1730. $upArray['archived'] = 0;
  1731. $logArray[] = [
  1732. 'type' => '日志',
  1733. 'projectid' => $task['projectid'],
  1734. 'taskid' => $task['id'],
  1735. 'username' => $user['username'],
  1736. 'detail' => '取消归档',
  1737. 'indate' => Base::time(),
  1738. 'other' => Base::array2string([
  1739. 'type' => 'task',
  1740. 'id' => $task['id'],
  1741. 'title' => $task['title'],
  1742. ])
  1743. ];
  1744. break;
  1745. }
  1746. /**
  1747. * 删除任务
  1748. */
  1749. case 'delete': {
  1750. if ($task['delete'] == 1) {
  1751. return Base::retError('任务已删除,请勿重复操作!');
  1752. }
  1753. $upArray['delete'] = 1;
  1754. $upArray['deletedate'] = Base::time();
  1755. $logArray[] = [
  1756. 'type' => '日志',
  1757. 'projectid' => $task['projectid'],
  1758. 'taskid' => $task['id'],
  1759. 'username' => $user['username'],
  1760. 'detail' => '删除任务',
  1761. 'indate' => Base::time(),
  1762. 'other' => Base::array2string([
  1763. 'type' => 'task',
  1764. 'id' => $task['id'],
  1765. 'title' => $task['title'],
  1766. ])
  1767. ];
  1768. $message = "删除成功!";
  1769. break;
  1770. }
  1771. /**
  1772. * 评论任务
  1773. */
  1774. case 'comment': {
  1775. if (mb_strlen($content) < 2) {
  1776. return Base::retError('评论内容至少2个字!');
  1777. }
  1778. $logArray[] = [
  1779. 'type' => '评论',
  1780. 'projectid' => $task['projectid'],
  1781. 'taskid' => $task['id'],
  1782. 'username' => $user['username'],
  1783. 'detail' => $content,
  1784. 'indate' => Base::time(),
  1785. 'other' => Base::array2string([
  1786. 'type' => 'task',
  1787. 'id' => $task['id'],
  1788. 'title' => $task['title'],
  1789. ])
  1790. ];
  1791. $message = "评论成功!";
  1792. break;
  1793. }
  1794. /**
  1795. * 添加关注
  1796. */
  1797. case 'attention': {
  1798. $userArray = explode(",", $content);
  1799. DB::transaction(function () use ($user, $task, $userArray) {
  1800. foreach ($userArray AS $uname) {
  1801. $uid = Users::username2id($uname);
  1802. if (empty($uid)) {
  1803. continue;
  1804. }
  1805. $row = Base::DBC2A(DB::table('project_users')->where([
  1806. 'type' => '关注',
  1807. 'taskid' => $task['id'],
  1808. 'username' => $uname,
  1809. ])->lockForUpdate()->first());
  1810. if (empty($row)) {
  1811. DB::table('project_users')->insert([
  1812. 'type' => '关注',
  1813. 'projectid' => $task['projectid'],
  1814. 'taskid' => $task['id'],
  1815. 'isowner' => $task['username'] == $uname ? 1 : 0,
  1816. 'username' => $uname,
  1817. 'indate' => Base::time()
  1818. ]);
  1819. DB::table('project_log')->insert([
  1820. 'type' => '日志',
  1821. 'projectid' => $task['projectid'],
  1822. 'taskid' => $task['id'],
  1823. 'username' => $uname,
  1824. 'detail' => $uname == $user['username'] ? '关注任务' : '加入关注',
  1825. 'indate' => Base::time(),
  1826. 'other' => Base::array2string([
  1827. 'type' => 'task',
  1828. 'id' => $task['id'],
  1829. 'title' => $task['title'],
  1830. 'operator' => $user['username'],
  1831. 'action' => 'attention',
  1832. ])
  1833. ]);
  1834. }
  1835. }
  1836. });
  1837. $tempRow = Base::DBC2A(DB::table('project_users')->select(['username'])->where([ 'type' => '关注', 'taskid' => $task['id'] ])->get()->pluck('username'));
  1838. $upArray['follower'] = Base::array2string($tempRow);
  1839. $message = "保存成功!";
  1840. break;
  1841. }
  1842. /**
  1843. * 取消关注
  1844. */
  1845. case 'unattention': {
  1846. $userArray = explode(",", $content);
  1847. DB::transaction(function () use ($user, $task, $userArray) {
  1848. foreach ($userArray AS $uname) {
  1849. if (DB::table('project_users')->where([
  1850. 'type' => '关注',
  1851. 'taskid' => $task['id'],
  1852. 'username' => $uname,
  1853. ])->delete()) {
  1854. DB::table('project_log')->insert([
  1855. 'type' => '日志',
  1856. 'projectid' => $task['projectid'],
  1857. 'taskid' => $task['id'],
  1858. 'username' => $uname,
  1859. 'detail' => $uname == $user['username'] ? '取消关注' : '移出关注',
  1860. 'indate' => Base::time(),
  1861. 'other' => Base::array2string([
  1862. 'type' => 'task',
  1863. 'id' => $task['id'],
  1864. 'title' => $task['title'],
  1865. 'operator' => $user['username'],
  1866. 'action' => 'unattention',
  1867. ])
  1868. ]);
  1869. }
  1870. }
  1871. });
  1872. $tempRow = Base::DBC2A(DB::table('project_users')->select(['username'])->where([ 'type' => '关注', 'taskid' => $task['id'] ])->get()->pluck('username'));
  1873. $upArray['follower'] = Base::array2string($tempRow);
  1874. $message = "保存成功!";
  1875. break;
  1876. }
  1877. default: {
  1878. return Base::retError('参数错误!');
  1879. break;
  1880. }
  1881. }
  1882. //
  1883. if ($upArray) {
  1884. DB::table('project_task')->where('id', $taskid)->update($upArray);
  1885. }
  1886. if ($logArray) {
  1887. DB::table('project_log')->insert($logArray);
  1888. }
  1889. //
  1890. if (in_array($act, ['complete', 'unfinished'])) {
  1891. Project::updateNum($task['projectid']);
  1892. }
  1893. //
  1894. $task = array_merge($task, $upArray);
  1895. $task['overdue'] = Project::taskIsOverdue($task);
  1896. $task['subtask'] = Base::string2array($task['subtask']);
  1897. $task['follower'] = Base::string2array($task['follower']);
  1898. $task['projectTitle'] = $task['projectid'] > 0 ? DB::table('project_lists')->where('id', $task['projectid'])->value('title') : '';
  1899. $task = array_merge($task, Users::username2basic($task['username']));
  1900. return Base::retSuccess($message ?: '修改成功!', $task);
  1901. }
  1902. /**
  1903. * 项目任务-待推送日志
  1904. *
  1905. * @apiParam {Number} taskid 任务ID
  1906. * @apiParam {Number} [page] 当前页,默认:1
  1907. * @apiParam {Number} [pagesize] 每页显示数量,默认:20,最大:100
  1908. * @throws \Throwable
  1909. */
  1910. public function task__pushlog()
  1911. {
  1912. $user = Users::authE();
  1913. if (Base::isError($user)) {
  1914. return $user;
  1915. } else {
  1916. $user = $user['data'];
  1917. }
  1918. //
  1919. return DB::transaction(function () {
  1920. $taskid = intval(Request::input('taskid'));
  1921. $task = Base::DBC2A(DB::table('project_task')
  1922. ->select(['pushlid', 'follower', 'username'])
  1923. ->where([
  1924. ['delete', '=', 0],
  1925. ['id', '=', $taskid],
  1926. ])
  1927. ->lockForUpdate()
  1928. ->first());
  1929. if (empty($task)) {
  1930. return Base::retError('任务不存在!');
  1931. }
  1932. $task['follower'] = Base::string2array($task['follower']);
  1933. $task['follower'][] = $task['username'];
  1934. //
  1935. $pushlid = $task['pushlid'];
  1936. $lists = DB::table('project_log')
  1937. ->select(['id', 'username', 'indate', 'detail', 'other'])
  1938. ->where([
  1939. ['id', '>', $pushlid],
  1940. ['taskid', '=', $taskid],
  1941. ['indate', '>', Base::time() - 60]
  1942. ])
  1943. ->orderBy('id')->paginate(Min(Max(Base::nullShow(Request::input('pagesize'), 10), 1), 100));
  1944. $lists = Base::getPageList($lists, false);
  1945. if (count($lists['lists']) == 0) {
  1946. return Base::retError('no lists');
  1947. }
  1948. $array = [];
  1949. foreach ($lists['lists'] AS $key => $item) {
  1950. $item = array_merge($item, Users::username2basic($item['username']));
  1951. $item['other'] = Base::string2array($item['other'], ['type' => '']);
  1952. if (!in_array($item['other']['action'], ['attention', 'unattention'])) {
  1953. $array[] = $item;
  1954. }
  1955. $pushlid = $item['id'];
  1956. }
  1957. $lists['lists'] = $array;
  1958. if ($pushlid != $task['pushlid']) {
  1959. DB::table('project_task')->where('id', $taskid)->update([
  1960. 'pushlid' => $pushlid
  1961. ]);
  1962. }
  1963. return Base::retSuccess('success', array_merge($lists, $task));
  1964. });
  1965. }
  1966. /**
  1967. * 项目文件-列表
  1968. *
  1969. * @apiParam {Number} [projectid] 项目ID
  1970. * @apiParam {Number} [taskid] 任务ID(如果项目ID为空时此参必须赋值且任务必须是自己负责人或在任务所在的项目里)
  1971. * @apiParam {String} [name] 文件名称
  1972. * @apiParam {String} [username] 上传者用户名
  1973. * @apiParam {Object} [sorts] 排序方式,格式:{key:'', order:''}
  1974. * - key: name|size|username|indate
  1975. * - order: asc|desc
  1976. * @apiParam {Number} [page] 当前页,默认:1
  1977. * @apiParam {Number} [pagesize] 每页显示数量,默认:20,最大:100
  1978. */
  1979. public function files__lists()
  1980. {
  1981. $user = Users::authE();
  1982. if (Base::isError($user)) {
  1983. return $user;
  1984. } else {
  1985. $user = $user['data'];
  1986. }
  1987. //
  1988. $taskid = intval(Request::input('taskid'));
  1989. if ($taskid > 0) {
  1990. $projectid = intval(DB::table('project_task')->select(['projectid'])->where([ 'id' => $taskid])->value('projectid'));
  1991. } else {
  1992. $projectid = intval(Request::input('projectid'));
  1993. }
  1994. if ($projectid > 0) {
  1995. $inRes = Project::inThe($projectid, $user['username']);
  1996. if (Base::isError($inRes)) {
  1997. return $inRes;
  1998. }
  1999. }
  2000. //
  2001. $orderBy = '`id` DESC';
  2002. $sorts = Base::json2array(Request::input('sorts'));
  2003. if (in_array($sorts['order'], ['asc', 'desc'])) {
  2004. switch ($sorts['key']) {
  2005. case 'name':
  2006. case 'size':
  2007. case 'download':
  2008. case 'username':
  2009. case 'indate':
  2010. $orderBy = '`' . $sorts['key'] . '` ' . $sorts['order'] . ',`id` DESC';
  2011. break;
  2012. }
  2013. }
  2014. //
  2015. $whereArray = [];
  2016. if ($projectid > 0) {
  2017. $whereArray[] = ['projectid', '=', $projectid];
  2018. if ($taskid > 0) {
  2019. $whereArray[] = ['taskid', '=', $taskid];
  2020. }
  2021. } else {
  2022. if ($taskid <= 0) {
  2023. return Base::retError('参数错误!');
  2024. }
  2025. $count = DB::table('project_task')->where([ 'id' => $taskid, 'username' => $user['username']])->count();
  2026. if ($count <= 0) {
  2027. return Base::retError('你不是任务负责人!');
  2028. }
  2029. $whereArray[] = ['taskid', '=', $taskid];
  2030. }
  2031. $whereArray[] = ['delete', '=', 0];
  2032. if (intval(Request::input('taskid')) > 0) {
  2033. $whereArray[] = ['taskid', '=', intval(Request::input('taskid'))];
  2034. }
  2035. if (trim(Request::input('name'))) {
  2036. $whereArray[] = ['name', 'like', '%' . trim(Request::input('name')) . '%'];
  2037. }
  2038. if (trim(Request::input('username'))) {
  2039. $whereArray[] = ['username', '=', trim(Request::input('username'))];
  2040. }
  2041. //
  2042. $lists = DB::table('project_files')
  2043. ->where($whereArray)
  2044. ->orderByRaw($orderBy)->paginate(Min(Max(Base::nullShow(Request::input('pagesize'), 10), 1), 100));
  2045. $lists = Base::getPageList($lists);
  2046. if ($lists['total'] == 0) {
  2047. return Base::retError('未找到任何相关的文件', $lists);
  2048. }
  2049. foreach ($lists['lists'] AS $key => $item) {
  2050. $lists['lists'][$key]['path'] = Base::fillUrl($item['path']);
  2051. $lists['lists'][$key]['thumb'] = Base::fillUrl($item['thumb']);
  2052. $lists['lists'][$key]['yetdown'] = intval(Session::get('filesDownload:' . $item['id']));
  2053. }
  2054. return Base::retSuccess('success', $lists);
  2055. }
  2056. /**
  2057. * 项目文件-上传
  2058. *
  2059. * @apiParam {Number} [projectid] 项目ID
  2060. * @apiParam {Number} [taskid] 任务ID(如果项目ID为空时此参必须赋值且任务必须是自己负责人)
  2061. */
  2062. public function files__upload()
  2063. {
  2064. $user = Users::authE();
  2065. if (Base::isError($user)) {
  2066. return $user;
  2067. } else {
  2068. $user = $user['data'];
  2069. }
  2070. //
  2071. $projectid = intval(Request::input('projectid'));
  2072. $taskid = intval(Request::input('taskid'));
  2073. if ($projectid > 0) {
  2074. $inRes = Project::inThe($projectid, $user['username']);
  2075. if (Base::isError($inRes)) {
  2076. return $inRes;
  2077. }
  2078. } else {
  2079. if ($taskid <= 0) {
  2080. return Base::retError('参数错误!');
  2081. }
  2082. $row = Base::DBC2A(DB::table('project_task')->select(['projectid'])->where([ 'id' => $taskid, 'username' => $user['username']])->first());
  2083. if (empty($row)) {
  2084. return Base::retError('你不是任务负责人!');
  2085. }
  2086. $projectid = $row['projectid'];
  2087. }
  2088. //
  2089. $data = Base::upload([
  2090. "file" => Request::file('files'),
  2091. "type" => 'file',
  2092. "path" => "uploads/projects/" . $projectid . "/",
  2093. ]);
  2094. if (Base::isError($data)) {
  2095. return Base::retError($data['msg']);
  2096. } else {
  2097. $fileData = $data['data'];
  2098. $thumb = 'images/files/file.png';
  2099. switch ($fileData['ext']) {
  2100. case "docx":
  2101. $thumb = 'images/files/doc.png';
  2102. break;
  2103. case "xlsx":
  2104. $thumb = 'images/files/xls.png';
  2105. break;
  2106. case "pptx":
  2107. $thumb = 'images/files/ppt.png';
  2108. break;
  2109. case "doc":
  2110. case "xls":
  2111. case "ppt":
  2112. case "txt":
  2113. case "esp":
  2114. case "gif":
  2115. $thumb = 'images/files/' . $fileData['ext'] . '.png';
  2116. break;
  2117. case "jpg":
  2118. case "jpeg":
  2119. case "png":
  2120. if (Base::imgThumb($fileData['file'], $fileData['file'] . "_thumb.jpg", 64, 0)) {
  2121. $thumb = $fileData['path'] . "_thumb.jpg";
  2122. }
  2123. break;
  2124. }
  2125. $array = [
  2126. 'projectid' => $projectid,
  2127. 'taskid' => $taskid,
  2128. 'name' => $fileData['name'],
  2129. 'size' => $fileData['size'] * 1024,
  2130. 'ext' => $fileData['ext'],
  2131. 'path' => $fileData['path'],
  2132. 'thumb' => $thumb,
  2133. 'username' => $user['username'],
  2134. 'indate' => Base::time(),
  2135. ];
  2136. $id = DB::table('project_files')->insertGetId($array);
  2137. $array['id'] = $id;
  2138. $array['path'] = Base::fillUrl($array['path']);
  2139. $array['thumb'] = Base::fillUrl($array['thumb']);
  2140. $array['download'] = 0;
  2141. $array['yetdown'] = 0;
  2142. DB::table('project_log')->insert([
  2143. 'type' => '日志',
  2144. 'projectid' => $projectid,
  2145. 'taskid' => $taskid,
  2146. 'username' => $user['username'],
  2147. 'detail' => '上传文件',
  2148. 'indate' => Base::time(),
  2149. 'other' => Base::array2string([
  2150. 'type' => 'file',
  2151. 'id' => $id,
  2152. 'name' => $fileData['name'],
  2153. ])
  2154. ]);
  2155. if ($taskid > 0) {
  2156. DB::table('project_task')->where('id', $taskid)->increment('filenum');
  2157. }
  2158. return Base::retSuccess('success', $array);
  2159. }
  2160. }
  2161. /**
  2162. * 项目文件-下载
  2163. *
  2164. * @apiParam {Number} fileid 文件ID
  2165. */
  2166. public function files__download()
  2167. {
  2168. $fileDetail = Base::DBC2A(DB::table('project_files')->where('id', intval(Request::input('fileid')))->where('delete', 0)->first());
  2169. if (empty($fileDetail)) {
  2170. return abort(404, '文件不存在或已被删除!');
  2171. }
  2172. $filePath = public_path($fileDetail['path']);
  2173. if (!file_exists($filePath)) {
  2174. return abort(404, '文件不存在或已被删除。');
  2175. }
  2176. if (intval(Session::get('filesDownload:' . $fileDetail['id'])) !== 1) {
  2177. Session::put('filesDownload:' . $fileDetail['id'], 1);
  2178. DB::table('project_files')->where('id', $fileDetail['id'])->increment('download');
  2179. }
  2180. return response()->download($filePath, $fileDetail['name']);
  2181. }
  2182. /**
  2183. * 项目文件-重命名
  2184. *
  2185. * @apiParam {Number} fileid 文件ID
  2186. * @apiParam {String} name 新文件名称
  2187. */
  2188. public function files__rename()
  2189. {
  2190. $user = Users::authE();
  2191. if (Base::isError($user)) {
  2192. return $user;
  2193. } else {
  2194. $user = $user['data'];
  2195. }
  2196. //
  2197. $fileDetail = Base::DBC2A(DB::table('project_files')->where('id', intval(Request::input('fileid')))->where('delete', 0)->first());
  2198. if (empty($fileDetail)) {
  2199. return Base::retError('文件不存在或已被删除!');
  2200. }
  2201. if ($fileDetail['username'] != $user['username']) {
  2202. $inRes = Project::inThe($fileDetail['projectid'], $user['username'], true);
  2203. if (Base::isError($inRes)) {
  2204. return Base::retError('此操作仅支持管理员或上传者!');
  2205. }
  2206. }
  2207. //
  2208. $name = Base::rightDelete(trim(Request::input('name')), '.' . $fileDetail['ext']);
  2209. if (empty($name)) {
  2210. return Base::retError('文件名称不能为空!');
  2211. } elseif (mb_strlen($name) > 32) {
  2212. return Base::retError('文件名称最多只能设置32个字!');
  2213. }
  2214. //
  2215. $name .= '.' . $fileDetail['ext'];
  2216. if (DB::table('project_files')->where('id', $fileDetail['id'])->update([ 'name' => $name ])) {
  2217. DB::table('project_log')->insert([
  2218. 'type' => '日志',
  2219. 'projectid' => $fileDetail['projectid'],
  2220. 'taskid' => $fileDetail['taskid'],
  2221. 'username' => $user['username'],
  2222. 'detail' => '文件【' . $fileDetail['name'] . '】重命名',
  2223. 'indate' => Base::time(),
  2224. 'other' => Base::array2string([
  2225. 'type' => 'file',
  2226. 'id' => $fileDetail['id'],
  2227. 'name' => $name,
  2228. ])
  2229. ]);
  2230. }
  2231. //
  2232. return Base::retSuccess('修改成功!', [
  2233. 'name' => $name,
  2234. ]);
  2235. }
  2236. /**
  2237. * 项目文件-删除
  2238. *
  2239. * @apiParam {Number} fileid 文件ID
  2240. */
  2241. public function files__delete()
  2242. {
  2243. $user = Users::authE();
  2244. if (Base::isError($user)) {
  2245. return $user;
  2246. } else {
  2247. $user = $user['data'];
  2248. }
  2249. //
  2250. $fileDetail = Base::DBC2A(DB::table('project_files')->where('id', intval(Request::input('fileid')))->where('delete', 0)->first());
  2251. if (empty($fileDetail)) {
  2252. return Base::retError('文件不存在或已被删除!');
  2253. }
  2254. if ($fileDetail['username'] != $user['username']) {
  2255. $inRes = Project::inThe($fileDetail['projectid'], $user['username'], true);
  2256. if (Base::isError($inRes)) {
  2257. return Base::retError('此操作仅支持管理员或上传者!');
  2258. }
  2259. }
  2260. //
  2261. DB::table('project_files')->where('id', $fileDetail['id'])->update([
  2262. 'delete' => 1,
  2263. 'deletedate' => Base::time()
  2264. ]);
  2265. DB::table('project_log')->insert([
  2266. 'type' => '日志',
  2267. 'projectid' => $fileDetail['projectid'],
  2268. 'taskid' => $fileDetail['taskid'],
  2269. 'username' => $user['username'],
  2270. 'detail' => '删除文件',
  2271. 'indate' => Base::time(),
  2272. 'other' => Base::array2string([
  2273. 'type' => 'file',
  2274. 'id' => $fileDetail['id'],
  2275. 'name' => $fileDetail['name'],
  2276. ])
  2277. ]);
  2278. if ($fileDetail['taskid'] > 0) {
  2279. DB::table('project_task')->where('id', $fileDetail['taskid'])->decrement('filenum');
  2280. }
  2281. //
  2282. return Base::retSuccess('删除成功!');
  2283. }
  2284. /**
  2285. * 项目动态-列表
  2286. *
  2287. * @apiParam {Number} [projectid] 项目ID
  2288. * @apiParam {Number} [taskid] 任务ID(如果项目ID为空时此参必须赋值且任务必须是自己负责人)
  2289. * @apiParam {String} [type] 类型
  2290. * - 全部: 日志+评论(默认)
  2291. * - 日志
  2292. * - 评论
  2293. * @apiParam {String} [username] 用户名
  2294. * @apiParam {Number} [page] 当前页,默认:1
  2295. * @apiParam {Number} [pagesize] 每页显示数量,默认:20,最大:100
  2296. */
  2297. public function log__lists()
  2298. {
  2299. $user = Users::authE();
  2300. if (Base::isError($user)) {
  2301. return $user;
  2302. } else {
  2303. $user = $user['data'];
  2304. }
  2305. //
  2306. $projectid = intval(Request::input('projectid'));
  2307. if ($projectid > 0) {
  2308. $inRes = Project::inThe($projectid, $user['username']);
  2309. if (Base::isError($inRes)) {
  2310. return $inRes;
  2311. }
  2312. }
  2313. //
  2314. $taskid = intval(Request::input('taskid'));
  2315. $whereArray = [];
  2316. $whereFunc = null;
  2317. if ($projectid > 0) {
  2318. $whereArray[] = ['projectid', '=', $projectid];
  2319. if ($taskid > 0) {
  2320. $whereArray[] = ['taskid', '=', $taskid];
  2321. }
  2322. } else {
  2323. if ($taskid < 0) {
  2324. return Base::retError('参数错误!');
  2325. }
  2326. $count = DB::table('project_task')->where([ 'id' => $taskid, 'username' => $user['username']])->count();
  2327. if ($count <= 0) {
  2328. return Base::retError('你不是任务负责人!');
  2329. }
  2330. $whereArray[] = ['taskid', '=', $taskid];
  2331. }
  2332. if (trim(Request::input('username'))) {
  2333. $whereArray[] = ['username', '=', trim(Request::input('username'))];
  2334. }
  2335. switch (trim(Request::input('type'))) {
  2336. case '日志': {
  2337. $whereArray[] = ['type', '=', '日志'];
  2338. break;
  2339. }
  2340. case '评论': {
  2341. $whereArray[] = ['type', '=', '评论'];
  2342. break;
  2343. }
  2344. default: {
  2345. $whereFunc = function ($query) {
  2346. $query->whereIn('type', ['日志', '评论']);
  2347. };
  2348. break;
  2349. }
  2350. }
  2351. //
  2352. $lists = DB::table('project_log')
  2353. ->where($whereArray)
  2354. ->where($whereFunc)
  2355. ->orderByDesc('indate')->paginate(Min(Max(Base::nullShow(Request::input('pagesize'), 10), 1), 100));
  2356. $lists = Base::getPageList($lists);
  2357. if ($lists['total'] == 0) {
  2358. return Base::retError('未找到任何相关的记录', $lists);
  2359. }
  2360. foreach ($lists['lists'] AS $key => $item) {
  2361. $item = array_merge($item, Users::username2basic($item['username']));
  2362. $item['timeData'] = [
  2363. 'ymd' => date(date("Y", $item['indate']) == date("Y", Base::time()) ? "m-d" : "Y-m-d", $item['indate']),
  2364. 'hi' => date("h:i", $item['indate']) ,
  2365. 'week' => "周" . Base::getTimeWeek($item['indate']),
  2366. 'segment' => Base::getTimeDayeSegment($item['indate']),
  2367. ];
  2368. $item['other'] = Base::string2array($item['other'], ['type' => '']);
  2369. $lists['lists'][$key] = $item;
  2370. }
  2371. return Base::retSuccess('success', $lists);
  2372. }
  2373. }