ProjectController.php 89 KB

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