ProjectController.php 87 KB

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