ReportController.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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. * 获取模板、保存、发送、删除
  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(Request::input('id'));
  64. $act = trim(Request::input('act'));
  65. $type = trim(Request::input('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(['rid' => $reportDetail['id'], 'username' => $cuser], ['cc' => 1]);
  106. }
  107. DB::table('report_lists')->where('id', $reportDetail['id'])->update([
  108. 'status' => '已发送',
  109. 'ccuser' => Base::array2string($ccuser)
  110. ]);
  111. $reportDetail['ccuser'] = implode(',', $ccuser);
  112. $reportDetail['ccuserArray'] = explode(',', $reportDetail['ccuser']);
  113. return Base::retSuccess('发送成功!', array_merge($reportDetail, [
  114. 'ccuserAgain' => $reportDetail['status'] == '已发送'
  115. ]));
  116. } elseif ($act == 'delete') {
  117. if (empty($reportDetail)) {
  118. return Base::retError('没有相关的数据或已被删除!');
  119. }
  120. if ($reportDetail['status'] == '已发送') {
  121. return Base::retError('汇报已发送,无法删除!');
  122. }
  123. DB::table('report_lists')->where('id', $reportDetail['id'])->delete();
  124. DB::table('report_ccuser')->where('rid', $reportDetail['id'])->delete();
  125. DB::table('report_content')->where('rid', $reportDetail['id'])->delete();
  126. return Base::retSuccess('删除成功!');
  127. } elseif ($act == 'submit') {
  128. $D = Base::getContentsParse('D');
  129. if (mb_strlen($D['title']) < 2 || mb_strlen($D['title']) > 100) {
  130. return Base::retError('标题限制2-100个字!');
  131. }
  132. if (empty($reportDetail)) {
  133. DB::table('report_lists')->insert([
  134. 'username' => $user['username'],
  135. 'title' => $D['title'],
  136. 'type' => $type,
  137. 'status' => '未发送',
  138. 'date' => $type=='日报'?date("Ymd"):date("W"),
  139. 'indate' => Base::time(),
  140. ]);
  141. $reportDetail = Base::DBC2A(DB::table('report_lists')->where($whereArray)->first());
  142. }
  143. if (empty($reportDetail)) {
  144. return Base::retError('系统繁忙,请稍后再试!');
  145. }
  146. //
  147. $D['ccuser'] = explode(",", $D['ccuser']);
  148. $send = $reportDetail['status'] == '已发送' ? 1 : intval(Request::input('send'));
  149. $ccuserAgain = $reportDetail['status'] == '已发送';
  150. if ($send) {
  151. DB::table('report_ccuser')->where(['rid' => $reportDetail['id']])->update(['cc' => 0]);
  152. foreach ($D['ccuser'] AS $ck => $cuser) {
  153. if (!$cuser) {
  154. unset($D['ccuser'][$ck]);
  155. continue;
  156. }
  157. DB::table('report_ccuser')->updateOrInsert(['rid' => $reportDetail['id'], 'username' => $cuser], ['cc' => 1]);
  158. }
  159. $reportDetail['status'] = '已发送';
  160. }
  161. //
  162. DB::table('report_lists')->where('id', $reportDetail['id'])->update([
  163. 'title' => $D['title'],
  164. 'status' => $send ? '已发送' : '未发送',
  165. 'ccuser' => Base::array2string($D['ccuser'])
  166. ]);
  167. DB::table('report_content')->updateOrInsert(['rid' => $reportDetail['id']], ['content' => $D['content']]);
  168. //
  169. $reportDetail = array_merge($reportDetail, [
  170. 'ccuserAgain' => $ccuserAgain,
  171. 'ccuser' => $D['ccuser'],
  172. 'title' => $D['title'],
  173. 'content' => $D['content'],
  174. ]);
  175. }
  176. if (empty($reportDetail)) {
  177. //已完成
  178. $completeContent = '';
  179. $startTime = $type == '日报' ? strtotime(date('Y-m-d 00:00:00')) : strtotime(date('Y-m-d 00:00:00', strtotime('this week')));
  180. $lists = Base::DBC2A(DB::table('project_task')
  181. ->select(['title', 'completedate'])
  182. ->where('username', $user['username'])
  183. ->where('complete', 1)
  184. ->where('delete', 0)
  185. ->whereBetween('completedate', [$startTime, time()])
  186. ->orderBy('completedate')
  187. ->orderBy('id')
  188. ->get());
  189. foreach ($lists as $item) {
  190. $pre = $type == '周报' ? ('<span>[周' . ['日', '一', '二', '三', '四', '五', '六'][date('w')] . ']</span>&nbsp;') : '';
  191. $completeContent .= '<li>' . $pre . $item['title'] . '</li>';
  192. }
  193. if (empty($completeContent)) {
  194. $completeContent = '<li>&nbsp;</li>';
  195. }
  196. //未完成
  197. $unfinishedContent = '';
  198. $finishTime = $type == '日报' ? strtotime(date('Y-m-d 23:59:59')) : strtotime(date('Y-m-d 23:59:59', strtotime('last day next week')));
  199. $lists = Base::DBC2A(DB::table('project_task')
  200. ->select(['title', 'enddate'])
  201. ->where('username', $user['username'])
  202. ->where('complete', 0)
  203. ->where('delete', 0)
  204. ->where('startdate', '>', 0)
  205. ->where('enddate', '<', $finishTime)
  206. ->orderBy('id')
  207. ->get());
  208. foreach ($lists as $item) {
  209. $pre = $item['enddate'] > 0 && $item['enddate'] < time() ? '<span style="color:#ff0000;">[超期]</span>&nbsp;' : '';
  210. $unfinishedContent .= '<li>' . $pre . $item['title'] . '</li>';
  211. }
  212. if (empty($unfinishedContent)) {
  213. $unfinishedContent = '<li>&nbsp;</li>';
  214. }
  215. //
  216. $reportDetail['title'] = ($user['nickname'] ?: $user['username']) . '的' . $type . '[' . $dateTitle . ']';
  217. $reportDetail['ccuser'] = '';
  218. $reportDetail['content'] = '<h2>已完成工作</h2><ol>' . $completeContent . '</ol><h2>未完成的工作</h2><ol>' . $unfinishedContent . '</ol>';
  219. $reportDetail['status'] = '未保存';
  220. } else {
  221. $reportDetail['ccuser'] = implode(',', Base::string2array($reportDetail['ccuser']));
  222. if (!isset($reportDetail['content'])) {
  223. $reportDetail['content'] = DB::table('report_content')->select(['content'])->where('rid', $reportDetail['id'])->value('content');
  224. }
  225. }
  226. $reportDetail['ccuserAgain'] = isset($reportDetail['ccuserAgain']) ? $reportDetail['ccuserAgain'] : false;
  227. $reportDetail['ccuserArray'] = explode(',', $reportDetail['ccuser']);
  228. return Base::retSuccess($act == 'submit' ? '保存成功!' : 'success', $reportDetail);
  229. }
  230. /**
  231. * 我的汇报
  232. *
  233. * @apiParam {Number} [page] 当前页,默认:1
  234. * @apiParam {Number} [pagesize] 每页显示数量,默认:20,最大:100
  235. */
  236. public function my()
  237. {
  238. $user = Users::authE();
  239. if (Base::isError($user)) {
  240. return $user;
  241. } else {
  242. $user = $user['data'];
  243. }
  244. //
  245. $whereArray = [];
  246. $whereArray[] = ['username', '=', $user['username']];
  247. if (trim(Request::input('username'))) {
  248. $whereArray[] = ['username', '=', trim(Request::input('username'))];
  249. }
  250. if (in_array(trim(Request::input('type')), ['日报', '周报'])) {
  251. $whereArray[] = ['type', '=', trim(Request::input('type'))];
  252. }
  253. $indate = Request::input('indate');
  254. if (is_array($indate)) {
  255. if ($indate[0] > 0) $whereArray[] = ['indate', '>=', Base::dayTimeF($indate[0])];
  256. if ($indate[1] > 0) $whereArray[] = ['indate', '<=', Base::dayTimeE($indate[1])];
  257. }
  258. //
  259. $orderBy = '`id` DESC';
  260. $sorts = Base::json2array(Request::input('sorts'));
  261. if (in_array($sorts['order'], ['asc', 'desc'])) {
  262. switch ($sorts['key']) {
  263. case 'indate':
  264. $orderBy = '`' . $sorts['key'] . '` ' . $sorts['order'] . ',`id` DESC';
  265. break;
  266. }
  267. }
  268. //
  269. $lists = DB::table('report_lists')
  270. ->where($whereArray)
  271. ->orderByRaw($orderBy)
  272. ->paginate(Min(Max(Base::nullShow(Request::input('pagesize'), 10), 1), 100));
  273. $lists = Base::getPageList($lists);
  274. if ($lists['total'] == 0) {
  275. return Base::retError('未找到任何相关的汇报', $lists);
  276. }
  277. foreach ($lists['lists'] AS $key => $item) {
  278. $lists['lists'][$key]['ccuser'] = Base::string2array($item['ccuser']);
  279. }
  280. return Base::retSuccess('success', $lists);
  281. }
  282. /**
  283. * 我的汇报
  284. *
  285. * @apiParam {String} [username] 汇报者用户名
  286. * @apiParam {Array} [indate] 汇报时间
  287. * @apiParam {String} [type] 类型
  288. * - 日报
  289. * - 周报
  290. * @apiParam {Object} [sorts] 排序方式,格式:{key:'', order:''}
  291. * - key: title|username|indate
  292. * - order: asc|desc
  293. * @apiParam {Number} [page] 当前页,默认:1
  294. * @apiParam {Number} [pagesize] 每页显示数量,默认:20,最大:100
  295. */
  296. public function receive()
  297. {
  298. $user = Users::authE();
  299. if (Base::isError($user)) {
  300. return $user;
  301. } else {
  302. $user = $user['data'];
  303. }
  304. //
  305. $whereArray = [];
  306. $whereArray[] = ['report_ccuser.username', '=', $user['username']];
  307. $whereArray[] = ['report_ccuser.cc', '=', 1];
  308. if (trim(Request::input('username'))) {
  309. $whereArray[] = ['report_lists.username', '=', trim(Request::input('username'))];
  310. }
  311. if (in_array(trim(Request::input('type')), ['日报', '周报'])) {
  312. $whereArray[] = ['report_lists.type', '=', trim(Request::input('type'))];
  313. }
  314. $indate = Request::input('indate');
  315. if (is_array($indate)) {
  316. if ($indate[0] > 0) $whereArray[] = ['report_lists.indate', '>=', Base::dayTimeF($indate[0])];
  317. if ($indate[1] > 0) $whereArray[] = ['report_lists.indate', '<=', Base::dayTimeE($indate[1])];
  318. }
  319. //
  320. $orderBy = '`id` DESC';
  321. $sorts = Base::json2array(Request::input('sorts'));
  322. if (in_array($sorts['order'], ['asc', 'desc'])) {
  323. switch ($sorts['key']) {
  324. case 'title':
  325. case 'username':
  326. case 'indate':
  327. $orderBy = '`' . $sorts['key'] . '` ' . $sorts['order'] . ',`id` DESC';
  328. break;
  329. }
  330. }
  331. //
  332. $lists = DB::table('report_lists')
  333. ->join('report_ccuser', 'report_lists.id', '=', 'report_ccuser.rid')
  334. ->select(['report_lists.*'])
  335. ->where($whereArray)
  336. ->orderByRaw($orderBy)
  337. ->paginate(Min(Max(Base::nullShow(Request::input('pagesize'), 10), 1), 100));
  338. $lists = Base::getPageList($lists);
  339. if ($lists['total'] == 0) {
  340. return Base::retError('未找到任何相关的汇报', $lists);
  341. }
  342. foreach ($lists['lists'] AS $key => $item) {
  343. $lists['lists'][$key]['ccuser'] = Base::string2array($item['ccuser']);
  344. }
  345. return Base::retSuccess('success', $lists);
  346. }
  347. }