ReportController.php 14 KB

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