ReportController.php 15 KB

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