ReportController.php 14 KB

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