ReportController.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. <?php
  2. namespace App\Http\Controllers\Api;
  3. use App\Http\Controllers\Controller;
  4. use App\Module\Base;
  5. use App\Module\Users;
  6. use DB;
  7. use Request;
  8. /**
  9. * @apiDefine report
  10. *
  11. * 汇报
  12. */
  13. class ReportController extends Controller
  14. {
  15. public function __invoke($method, $action = '')
  16. {
  17. $app = $method ? $method : 'main';
  18. if ($action) {
  19. $app .= "__" . $action;
  20. }
  21. return (method_exists($this, $app)) ? $this->$app() : Base::ajaxError("404 not found (" . str_replace("__", "/", $app) . ").");
  22. }
  23. /**
  24. * 获取内容
  25. *
  26. * @apiParam {Number} id 数据ID
  27. */
  28. public function content()
  29. {
  30. $row = Base::DBC2A(DB::table('report_content')->select(['rid', 'content'])->where('rid', intval(Request::input('id')))->first());
  31. if (empty($row)) {
  32. return Base::retError('内容不存在或已被删除!');
  33. }
  34. return Base::retSuccess('success', $row);
  35. }
  36. /**
  37. * {post} 获取模板、保存、发送、删除
  38. *
  39. * @apiParam {String} type 类型
  40. * - 日报
  41. * - 周报
  42. * @apiParam {Number} [id] 数据ID
  43. * @apiParam {String} [act] 请求方式
  44. * - submit: 保存
  45. * - send: 仅发送
  46. * - delete: 删除汇报
  47. * - else: 获取
  48. * @apiParam {String} [send] 是否发送(1:是),仅act=submit且未发送过的有效
  49. * @apiParam {Object} [D] Request Payload 提交
  50. * - title: 标题
  51. * - ccuser: 抄送人
  52. * - content: 内容
  53. */
  54. public function template()
  55. {
  56. $user = Users::authE();
  57. if (Base::isError($user)) {
  58. return $user;
  59. } else {
  60. $user = $user['data'];
  61. }
  62. //
  63. $id = intval(Base::getPostValue('id'));
  64. $act = trim(Base::getPostValue('act'));
  65. $type = trim(Base::getPostValue('type'));
  66. if (!in_array($type, ['日报', '周报'])) {
  67. return Base::retError('参数错误!');
  68. }
  69. $dateTitle = "";
  70. //
  71. $whereArray = [];
  72. $whereArray[] = ['username', '=', $user['username']];
  73. if ($id > 0) {
  74. $whereArray[] = ['id', '=', $id];
  75. } else {
  76. switch ($type) {
  77. case "日报":
  78. $whereArray[] = ['type', '=', '日报'];
  79. $whereArray[] = ['date', '=', date("Ymd")];
  80. $dateTitle = date("Y-m-d");
  81. break;
  82. case "周报":
  83. $whereArray[] = ['type', '=', '周报'];
  84. $whereArray[] = ['date', '=', date("W")];
  85. $dateTitle = date("Y年m月") . "第" . Base::getMonthWeek() . "周";
  86. break;
  87. }
  88. }
  89. //
  90. $reportDetail = Base::DBC2A(DB::table('report_lists')->where($whereArray)->first());
  91. if ($id > 0 && empty($reportDetail)) {
  92. return Base::retError('没有相关的数据!');
  93. }
  94. if ($act == 'send') {
  95. if (empty($reportDetail)) {
  96. return Base::retError('没有相关的数据或已被删除!');
  97. }
  98. $ccuser = Base::string2array($reportDetail['ccuser']);
  99. DB::table('report_ccuser')->where(['rid' => $reportDetail['id']])->update(['cc' => 0]);
  100. foreach ($ccuser AS $ck => $cuser) {
  101. if (!$cuser) {
  102. unset($ccuser[$ck]);
  103. continue;
  104. }
  105. DB::table('report_ccuser')->updateOrInsert([
  106. 'rid' => $reportDetail['id'],
  107. 'username' => $cuser,
  108. 'indate' => Base::time()
  109. ], [
  110. 'cc' => 1
  111. ]);
  112. }
  113. DB::table('report_lists')->where('id', $reportDetail['id'])->update([
  114. 'status' => '已发送',
  115. 'ccuser' => Base::array2string($ccuser)
  116. ]);
  117. $reportDetail['ccuser'] = implode(',', $ccuser);
  118. $reportDetail['ccuserArray'] = explode(',', $reportDetail['ccuser']);
  119. return Base::retSuccess('发送成功!', array_merge($reportDetail, [
  120. 'ccuserAgain' => $reportDetail['status'] == '已发送'
  121. ]));
  122. } elseif ($act == 'delete') {
  123. if (empty($reportDetail)) {
  124. return Base::retError('没有相关的数据或已被删除!');
  125. }
  126. if ($reportDetail['status'] == '已发送') {
  127. return Base::retError('汇报已发送,无法删除!');
  128. }
  129. DB::table('report_lists')->where('id', $reportDetail['id'])->delete();
  130. DB::table('report_ccuser')->where('rid', $reportDetail['id'])->delete();
  131. DB::table('report_content')->where('rid', $reportDetail['id'])->delete();
  132. return Base::retSuccess('删除成功!');
  133. } elseif ($act == 'submit') {
  134. if (mb_strlen(Base::getPostValue('title')) < 2 || mb_strlen(Base::getPostValue('title')) > 100) {
  135. return Base::retError('标题限制2-100个字!');
  136. }
  137. if (empty($reportDetail)) {
  138. DB::table('report_lists')->insert([
  139. 'username' => $user['username'],
  140. 'title' => Base::getPostValue('title'),
  141. 'type' => $type,
  142. 'status' => '未发送',
  143. 'date' => $type=='日报'?date("Ymd"):date("W"),
  144. 'indate' => Base::time(),
  145. ]);
  146. $reportDetail = Base::DBC2A(DB::table('report_lists')->where($whereArray)->first());
  147. }
  148. if (empty($reportDetail)) {
  149. return Base::retError('系统繁忙,请稍后再试!');
  150. }
  151. //
  152. $ccuserArr = explode(",", Base::getPostValue('ccuser'));
  153. $send = $reportDetail['status'] == '已发送' ? 1 : intval(Base::getPostValue('send'));
  154. $ccuserAgain = $reportDetail['status'] == '已发送';
  155. if ($send) {
  156. DB::table('report_ccuser')->where(['rid' => $reportDetail['id']])->update(['cc' => 0]);
  157. foreach ($ccuserArr AS $ck => $cuser) {
  158. if (!$cuser) {
  159. unset($ccuserArr[$ck]);
  160. continue;
  161. }
  162. DB::table('report_ccuser')->updateOrInsert([
  163. 'rid' => $reportDetail['id'],
  164. 'username' => $cuser,
  165. 'indate' => Base::time()
  166. ], [
  167. 'cc' => 1
  168. ]);
  169. }
  170. $reportDetail['status'] = '已发送';
  171. }
  172. //
  173. DB::table('report_lists')->where('id', $reportDetail['id'])->update([
  174. 'title' => Base::getPostValue('title'),
  175. 'status' => $send ? '已发送' : '未发送',
  176. 'ccuser' => Base::array2string($ccuserArr)
  177. ]);
  178. DB::table('report_content')->updateOrInsert(['rid' => $reportDetail['id']], ['content' => Base::getPostValue('content')]);
  179. //
  180. $reportDetail = array_merge($reportDetail, [
  181. 'ccuserAgain' => $ccuserAgain,
  182. 'ccuser' => $ccuserArr,
  183. 'title' => Base::getPostValue('title'),
  184. 'content' => Base::getPostValue('content'),
  185. ]);
  186. }
  187. if (empty($reportDetail)) {
  188. //已完成
  189. $completeContent = '';
  190. $startTime = $type == '日报' ? strtotime(date('Y-m-d 00:00:00')) : strtotime(date('Y-m-d 00:00:00', strtotime('this week')));
  191. $lists = Base::DBC2A(DB::table('project_task')
  192. ->select(['title', 'completedate'])
  193. ->where('username', $user['username'])
  194. ->where('complete', 1)
  195. ->where('delete', 0)
  196. ->whereBetween('completedate', [$startTime, time()])
  197. ->orderBy('completedate')
  198. ->orderBy('id')
  199. ->get());
  200. foreach ($lists as $item) {
  201. $pre = $type == '周报' ? ('<span>[周' . ['日', '一', '二', '三', '四', '五', '六'][date('w')] . ']</span>&nbsp;') : '';
  202. $completeContent .= '<li>' . $pre . $item['title'] . '</li>';
  203. }
  204. if (empty($completeContent)) {
  205. $completeContent = '<li>&nbsp;</li>';
  206. }
  207. //未完成
  208. $unfinishedContent = '';
  209. $finishTime = $type == '日报' ? strtotime(date('Y-m-d 23:59:59')) : strtotime(date('Y-m-d 23:59:59', strtotime('last day next week')));
  210. $lists = Base::DBC2A(DB::table('project_task')
  211. ->select(['title', 'enddate'])
  212. ->where('username', $user['username'])
  213. ->where('complete', 0)
  214. ->where('delete', 0)
  215. ->where('startdate', '>', 0)
  216. ->where('enddate', '<', $finishTime)
  217. ->orderBy('id')
  218. ->get());
  219. foreach ($lists as $item) {
  220. $pre = $item['enddate'] > 0 && $item['enddate'] < time() ? '<span style="color:#ff0000;">[超期]</span>&nbsp;' : '';
  221. $unfinishedContent .= '<li>' . $pre . $item['title'] . '</li>';
  222. }
  223. if (empty($unfinishedContent)) {
  224. $unfinishedContent = '<li>&nbsp;</li>';
  225. }
  226. //
  227. $reportDetail['title'] = ($user['nickname'] ?: $user['username']) . '的' . $type . '[' . $dateTitle . ']';
  228. $reportDetail['ccuser'] = '';
  229. $reportDetail['content'] = '<h2>已完成工作</h2><ol>' . $completeContent . '</ol><h2>未完成的工作</h2><ol>' . $unfinishedContent . '</ol>';
  230. $reportDetail['status'] = '未保存';
  231. } else {
  232. $reportDetail['ccuser'] = implode(',', Base::string2array($reportDetail['ccuser']));
  233. if (!isset($reportDetail['content'])) {
  234. $reportDetail['content'] = DB::table('report_content')->select(['content'])->where('rid', $reportDetail['id'])->value('content');
  235. }
  236. }
  237. $reportDetail['ccuserAgain'] = isset($reportDetail['ccuserAgain']) ? $reportDetail['ccuserAgain'] : false;
  238. $reportDetail['ccuserArray'] = explode(',', $reportDetail['ccuser']);
  239. return Base::retSuccess($act == 'submit' ? '保存成功!' : 'success', $reportDetail);
  240. }
  241. /**
  242. * 我的汇报
  243. *
  244. * @apiParam {Number} [page] 当前页,默认:1
  245. * @apiParam {Number} [pagesize] 每页显示数量,默认:20,最大:100
  246. */
  247. public function my()
  248. {
  249. $user = Users::authE();
  250. if (Base::isError($user)) {
  251. return $user;
  252. } else {
  253. $user = $user['data'];
  254. }
  255. //
  256. $whereArray = [];
  257. $whereArray[] = ['username', '=', $user['username']];
  258. if (trim(Request::input('username'))) {
  259. $whereArray[] = ['username', '=', trim(Request::input('username'))];
  260. }
  261. if (in_array(trim(Request::input('type')), ['日报', '周报'])) {
  262. $whereArray[] = ['type', '=', trim(Request::input('type'))];
  263. }
  264. $indate = Request::input('indate');
  265. if (is_array($indate)) {
  266. if ($indate[0] > 0) $whereArray[] = ['indate', '>=', Base::dayTimeF($indate[0])];
  267. if ($indate[1] > 0) $whereArray[] = ['indate', '<=', Base::dayTimeE($indate[1])];
  268. }
  269. //
  270. $orderBy = '`indate` DESC,`id` DESC';
  271. $sorts = Base::json2array(Request::input('sorts'));
  272. if (in_array($sorts['order'], ['asc', 'desc'])) {
  273. switch ($sorts['key']) {
  274. case 'date':
  275. case 'indate':
  276. $orderBy = '`' . $sorts['key'] . '` ' . $sorts['order'] . ',`id` DESC';
  277. break;
  278. }
  279. }
  280. //
  281. $lists = DB::table('report_lists')
  282. ->where($whereArray)
  283. ->orderByRaw($orderBy)
  284. ->paginate(Base::getPaginate(100, 20));
  285. $lists = Base::getPageList($lists);
  286. if ($lists['total'] == 0) {
  287. return Base::retError('未找到任何相关的汇报', $lists);
  288. }
  289. foreach ($lists['lists'] AS $key => $item) {
  290. $lists['lists'][$key]['ccuser'] = Base::string2array($item['ccuser']);
  291. }
  292. return Base::retSuccess('success', $lists);
  293. }
  294. /**
  295. * 我的汇报
  296. *
  297. * @apiParam {String} [username] 汇报者用户名
  298. * @apiParam {Array} [indate] 汇报时间
  299. * @apiParam {String} [type] 类型
  300. * - 日报
  301. * - 周报
  302. * @apiParam {Object} [sorts] 排序方式,格式:{key:'', order:''}
  303. * - key: title|username|indate
  304. * - order: asc|desc
  305. * @apiParam {Number} [page] 当前页,默认:1
  306. * @apiParam {Number} [pagesize] 每页显示数量,默认:20,最大:100
  307. */
  308. public function receive()
  309. {
  310. $user = Users::authE();
  311. if (Base::isError($user)) {
  312. return $user;
  313. } else {
  314. $user = $user['data'];
  315. }
  316. //
  317. $whereArray = [];
  318. $whereArray[] = ['report_ccuser.username', '=', $user['username']];
  319. $whereArray[] = ['report_ccuser.cc', '=', 1];
  320. if (trim(Request::input('username'))) {
  321. $whereArray[] = ['report_lists.username', '=', trim(Request::input('username'))];
  322. }
  323. if (in_array(trim(Request::input('type')), ['日报', '周报'])) {
  324. $whereArray[] = ['report_lists.type', '=', trim(Request::input('type'))];
  325. }
  326. $indate = Request::input('indate');
  327. if (is_array($indate)) {
  328. if ($indate[0] > 0) $whereArray[] = ['report_lists.indate', '>=', Base::dayTimeF($indate[0])];
  329. if ($indate[1] > 0) $whereArray[] = ['report_lists.indate', '<=', Base::dayTimeE($indate[1])];
  330. }
  331. //
  332. $builder = DB::table('report_lists')
  333. ->join('report_ccuser', 'report_lists.id', '=', 'report_ccuser.rid')
  334. ->select(['report_lists.*', 'report_ccuser.indate as senddate'])
  335. ->where($whereArray);
  336. $sorts = Base::json2array(Request::input('sorts'));
  337. if (in_array($sorts['order'], ['asc', 'desc'])) {
  338. switch ($sorts['key']) {
  339. case 'title':
  340. case 'username':
  341. $builder->orderBy($sorts['key'], $sorts['order']);
  342. break;
  343. case 'indate':
  344. $builder->orderBy('report_ccuser.indate', $sorts['order']);
  345. break;
  346. }
  347. } else {
  348. $builder->orderByDesc('report_ccuser.indate');
  349. $builder->orderByDesc('report_ccuser.id');
  350. }
  351. //
  352. $builder->orderByDesc('date');
  353. $lists = $builder->paginate(Base::getPaginate(100, 20));
  354. $lists = Base::getPageList($lists);
  355. if ($lists['total'] == 0) {
  356. return Base::retError('未找到任何相关的汇报', $lists);
  357. }
  358. foreach ($lists['lists'] AS $key => $item) {
  359. $lists['lists'][$key]['ccuser'] = Base::string2array($item['ccuser']);
  360. }
  361. return Base::retSuccess('success', $lists);
  362. }
  363. /**
  364. * 获取我上次抄送的人
  365. * @return array|mixed
  366. */
  367. public function prevcc()
  368. {
  369. $user = Users::authE();
  370. if (Base::isError($user)) {
  371. return $user;
  372. } else {
  373. $user = $user['data'];
  374. }
  375. //
  376. $rid = DB::table('report_ccuser')
  377. ->join('report_lists', 'report_lists.id', '=', 'report_ccuser.rid')
  378. ->where('report_lists.username', $user['username'])
  379. ->orderByDesc('report_ccuser.id')
  380. ->value('rid');
  381. if (empty($rid)) {
  382. return Base::retError('没有相关数据!');
  383. }
  384. $lists = Base::DBC2A(DB::table('report_ccuser')->select(['username'])->where('rid', $rid)->pluck('username'));
  385. if (empty($lists)) {
  386. return Base::retError('没有相关数据!');
  387. }
  388. return Base::retSuccess('success', [
  389. 'lists' => $lists
  390. ]);
  391. }
  392. }