ProjectController.php 93 KB

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