ReportController.php 12 KB

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