ReportController.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  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. use App\Task;
  9. /**
  10. * @apiDefine report
  11. *
  12. * 汇报
  13. */
  14. class ReportController extends Controller
  15. {
  16. public function __invoke($method, $action = '')
  17. {
  18. $app = $method ? $method : 'main';
  19. if ($action) {
  20. $app .= "__" . $action;
  21. }
  22. return (method_exists($this, $app)) ? $this->$app() : Base::ajaxError("404 not found (" . str_replace("__", "/", $app) . ").");
  23. }
  24. /**
  25. * 获取内容
  26. *
  27. * @apiParam {Number} id 数据ID
  28. */
  29. public function content()
  30. {
  31. $row = Base::DBC2A(DB::table('report_content')->select(['rid', 'content'])->where('rid', intval(Request::input('id')))->first());
  32. if (empty($row)) {
  33. return Base::retError('内容不存在或已被删除!');
  34. }
  35. return Base::retSuccess('success', $row);
  36. }
  37. /**
  38. * {post} 获取模板、保存、发送、删除
  39. *
  40. * @apiParam {String} type 类型
  41. * - 日报
  42. * - 周报
  43. * @apiParam {Number} [id] 数据ID
  44. * @apiParam {String} [act] 请求方式
  45. * - submit: 保存
  46. * - send: 仅发送
  47. * - delete: 删除汇报
  48. * - else: 获取
  49. * @apiParam {String} [send] 是否发送(1:是),仅act=submit且未发送过的有效
  50. * @apiParam {Object} [D] Request Payload 提交
  51. * - title: 标题
  52. * - ccuser: 抄送人
  53. * - content: 内容
  54. */
  55. public function template()
  56. {
  57. $user = Users::authE();
  58. if (Base::isError($user)) {
  59. return $user;
  60. } else {
  61. $user = $user['data'];
  62. }
  63. //
  64. $id = intval(Base::getPostValue('id'));
  65. $act = trim(Base::getPostValue('act'));
  66. $type = trim(Base::getPostValue('type'));
  67. if (!in_array($type, ['日报', '周报'])) {
  68. return Base::retError('参数错误!');
  69. }
  70. $dateTitle = "";
  71. //
  72. $whereArray = [];
  73. $whereArray[] = ['username', '=', $user['username']];
  74. if ($id > 0) {
  75. $whereArray[] = ['id', '=', $id];
  76. } else {
  77. switch ($type) {
  78. case "日报":
  79. $whereArray[] = ['type', '=', '日报'];
  80. $whereArray[] = ['date', '=', date("Ymd")];
  81. $dateTitle = date("Y-m-d");
  82. break;
  83. case "周报":
  84. $whereArray[] = ['type', '=', '周报'];
  85. $whereArray[] = ['date', '=', date("W")];
  86. $dateTitle = date("Y年m月") . "第" . Base::getMonthWeek() . "周";
  87. break;
  88. }
  89. }
  90. //
  91. $reportDetail = Base::DBC2A(DB::table('report_lists')->where($whereArray)->first());
  92. if ($id > 0 && empty($reportDetail)) {
  93. return Base::retError('没有相关的数据!');
  94. }
  95. if ($act == 'send') {
  96. if (empty($reportDetail)) {
  97. return Base::retError('没有相关的数据或已被删除!');
  98. }
  99. $ccuser = Base::string2array($reportDetail['ccuser']);
  100. DB::table('report_ccuser')->where(['rid' => $reportDetail['id']])->update(['cc' => 0]);
  101. foreach ($ccuser AS $ck => $cuser) {
  102. if (!$cuser) {
  103. unset($ccuser[$ck]);
  104. continue;
  105. }
  106. DB::table('report_ccuser')->updateOrInsert([
  107. 'rid' => $reportDetail['id'],
  108. 'username' => $cuser,
  109. 'indate' => Base::time()
  110. ], [
  111. 'cc' => 1
  112. ]);
  113. }
  114. DB::table('report_lists')->where('id', $reportDetail['id'])->update([
  115. 'status' => '已发送',
  116. 'ccuser' => Base::array2string($ccuser)
  117. ]);
  118. $reportDetail['ccuser'] = implode(',', $ccuser);
  119. $reportDetail['ccuserArray'] = explode(',', $reportDetail['ccuser']);
  120. return Base::retSuccess('发送成功!', array_merge($reportDetail, [
  121. 'ccuserAgain' => $reportDetail['status'] == '已发送'
  122. ]));
  123. } elseif ($act == 'delete') {
  124. if (empty($reportDetail)) {
  125. return Base::retError('没有相关的数据或已被删除!');
  126. }
  127. if ($reportDetail['status'] == '已发送') {
  128. return Base::retError('汇报已发送,无法删除!');
  129. }
  130. DB::table('report_lists')->where('id', $reportDetail['id'])->delete();
  131. DB::table('report_ccuser')->where('rid', $reportDetail['id'])->delete();
  132. DB::table('report_content')->where('rid', $reportDetail['id'])->delete();
  133. return Base::retSuccess('删除成功!');
  134. } elseif ($act == 'submit') {
  135. if (mb_strlen(Base::getPostValue('title')) < 2 || mb_strlen(Base::getPostValue('title')) > 100) {
  136. return Base::retError('标题限制2-100个字!');
  137. }
  138. if (empty($reportDetail)) {
  139. DB::table('report_lists')->insert([
  140. 'username' => $user['username'],
  141. 'title' => Base::getPostValue('title'),
  142. 'type' => $type,
  143. 'status' => '未发送',
  144. 'date' => $type=='日报'?date("Ymd"):date("W"),
  145. 'indate' => Base::time(),
  146. ]);
  147. $reportDetail = Base::DBC2A(DB::table('report_lists')->where($whereArray)->first());
  148. }
  149. if (empty($reportDetail)) {
  150. return Base::retError('系统繁忙,请稍后再试!');
  151. }
  152. //
  153. $ccuserArr = explode(",", Base::getPostValue('ccuser'));
  154. $send = $reportDetail['status'] == '已发送' ? 1 : intval(Base::getPostValue('send'));
  155. $ccuserAgain = $reportDetail['status'] == '已发送';
  156. if ($send) {
  157. DB::table('report_ccuser')->where(['rid' => $reportDetail['id']])->update(['cc' => 0]);
  158. foreach ($ccuserArr AS $ck => $cuser) {
  159. if (!$cuser) {
  160. unset($ccuserArr[$ck]);
  161. continue;
  162. }
  163. DB::table('report_ccuser')->updateOrInsert([
  164. 'rid' => $reportDetail['id'],
  165. 'username' => $cuser,
  166. 'indate' => Base::time()
  167. ], [
  168. 'cc' => 1
  169. ]);
  170. }
  171. $reportDetail['status'] = '已发送';
  172. }
  173. //
  174. DB::table('report_lists')->where('id', $reportDetail['id'])->update([
  175. 'title' => Base::getPostValue('title'),
  176. 'status' => $send ? '已发送' : '未发送',
  177. 'ccuser' => Base::array2string($ccuserArr)
  178. ]);
  179. DB::table('report_content')->updateOrInsert(['rid' => $reportDetail['id']], ['content' => Base::getPostValue('content')]);
  180. //
  181. $reportDetail = array_merge($reportDetail, [
  182. 'ccuserAgain' => $ccuserAgain,
  183. 'ccuser' => $ccuserArr,
  184. 'title' => Base::getPostValue('title'),
  185. 'content' => Base::getPostValue('content'),
  186. ]);
  187. }
  188. if (empty($reportDetail)) {
  189. $startTime = $type == '日报' ? strtotime(date('Y-m-d 00:00:00')) : strtotime(date('Y-m-d 00:00:00', strtotime('this week')));
  190. $finishTime = $type == '日报' ? strtotime(date('Y-m-d 23:59:59')) : strtotime(date('Y-m-d 23:59:59', strtotime('last day next week')));
  191. //zmw 修改
  192. $list = Task::where(function($query) use ($user){
  193. return $query->where('username',$user['username'])->orwhere('createuser',$user['username']);
  194. })->where('delete',0)->with(['subtask'=>function($query) {$query->where('delete',0);}])->get()->toArray();
  195. $completeContent = '';//已完成的任务
  196. $unfinishedContent = '';//未完成的任务
  197. foreach ($list as $k => $v){
  198. if($v['complete'] == 1 && $v['completedate'] >= $startTime && $v['completedate'] <= $finishTime){
  199. //父任务标记已完成,不管子任务是否有完成,统一放到已完成内
  200. $pre = $type == '周报' ? ('<span>[周' . ['日', '一', '二', '三', '四', '五', '六'][date('w')] . ']</span>&nbsp;') : '';
  201. $completeContent .= '<li>' . $pre . $v['title'] . '</li>';
  202. if(count($v['subtask']) > 0){
  203. $completeContent .= '<ol>';
  204. foreach ($v['subtask'] as $key => $value){
  205. $completeContent .= '<li>' . $value['detail'] . '</li>';
  206. }
  207. $completeContent .= '</ol>';
  208. }
  209. }
  210. if($v['complete'] == 0 && $v['enddate'] <= $finishTime){
  211. //父任务未标记已完成,可能实际已完成,但忘记置父任务状态,也可能后续还会增加子任务,故统一放未完成,对父任务未完成,但下面已经完成的子任务做删除线
  212. $pre = $v['enddate'] > 0 && $v['enddate'] < time() ? '<span style="color:#ff0000;">[超期]</span>&nbsp;' : '';
  213. $unfinishedContent .= '<li>' . $pre . $v['title'] . '</li>';
  214. if(count($v['subtask']) > 0){
  215. $unfinishedContent .= '<ol>';
  216. foreach ($v['subtask'] as $key => $value){
  217. $unfinishedContent .= '<li>';
  218. if($value['status'] == 'complete'){
  219. $unfinishedContent .= "<span style='text-decoration: line-through;'>" .$value['detail'] . "</span>";
  220. }else{
  221. $unfinishedContent .= $value['detail'];
  222. }
  223. $unfinishedContent .= '</li>';
  224. }
  225. $unfinishedContent .= '</ol>';
  226. }
  227. }
  228. }
  229. if (empty($completeContent)) {
  230. $completeContent = '<li>&nbsp;</li>';
  231. }
  232. if (empty($unfinishedContent)) {
  233. $unfinishedContent = '<li>&nbsp;</li>';
  234. }
  235. //
  236. $reportDetail['title'] = ($user['nickname'] ?: $user['username']) . '的' . $type . '[' . $dateTitle . ']';
  237. $reportDetail['ccuser'] = '';
  238. $reportDetail['content'] = '<h2>已完成工作</h2><ol>' . $completeContent . '</ol><h2>未完成的工作</h2><ol>' . $unfinishedContent . '</ol>';
  239. $reportDetail['status'] = '未保存';
  240. } else {
  241. $reportDetail['ccuser'] = implode(',', Base::string2array($reportDetail['ccuser']));
  242. if (!isset($reportDetail['content'])) {
  243. $reportDetail['content'] = DB::table('report_content')->select(['content'])->where('rid', $reportDetail['id'])->value('content');
  244. }
  245. }
  246. $reportDetail['ccuserAgain'] = isset($reportDetail['ccuserAgain']) ? $reportDetail['ccuserAgain'] : false;
  247. $reportDetail['ccuserArray'] = explode(',', $reportDetail['ccuser']);
  248. return Base::retSuccess($act == 'submit' ? '保存成功!' : 'success', $reportDetail);
  249. }
  250. /**
  251. * 我的汇报
  252. *
  253. * @apiParam {Number} [page] 当前页,默认:1
  254. * @apiParam {Number} [pagesize] 每页显示数量,默认:20,最大:100
  255. */
  256. public function my()
  257. {
  258. $user = Users::authE();
  259. if (Base::isError($user)) {
  260. return $user;
  261. } else {
  262. $user = $user['data'];
  263. }
  264. //
  265. $whereArray = [];
  266. $whereArray[] = ['username', '=', $user['username']];
  267. if (trim(Request::input('username'))) {
  268. $whereArray[] = ['username', '=', trim(Request::input('username'))];
  269. }
  270. if (in_array(trim(Request::input('type')), ['日报', '周报'])) {
  271. $whereArray[] = ['type', '=', trim(Request::input('type'))];
  272. }
  273. $indate = Request::input('indate');
  274. if (is_array($indate)) {
  275. if ($indate[0] > 0) $whereArray[] = ['indate', '>=', Base::dayTimeF($indate[0])];
  276. if ($indate[1] > 0) $whereArray[] = ['indate', '<=', Base::dayTimeE($indate[1])];
  277. }
  278. //
  279. $orderBy = '`indate` DESC,`id` DESC';
  280. $sorts = Base::json2array(Request::input('sorts'));
  281. if (in_array($sorts['order'], ['asc', 'desc'])) {
  282. switch ($sorts['key']) {
  283. case 'date':
  284. case 'indate':
  285. $orderBy = '`' . $sorts['key'] . '` ' . $sorts['order'] . ',`id` DESC';
  286. break;
  287. }
  288. }
  289. //
  290. $lists = DB::table('report_lists')
  291. ->where($whereArray)
  292. ->orderByRaw($orderBy)
  293. ->paginate(Base::getPaginate(100, 20));
  294. $lists = Base::getPageList($lists);
  295. if ($lists['total'] == 0) {
  296. return Base::retError('未找到任何相关的汇报', $lists);
  297. }
  298. foreach ($lists['lists'] AS $key => $item) {
  299. $lists['lists'][$key]['ccuser'] = Base::string2array($item['ccuser']);
  300. }
  301. return Base::retSuccess('success', $lists);
  302. }
  303. /**
  304. * 我的汇报
  305. *
  306. * @apiParam {String} [username] 汇报者用户名
  307. * @apiParam {Array} [indate] 汇报时间
  308. * @apiParam {String} [type] 类型
  309. * - 日报
  310. * - 周报
  311. * @apiParam {Object} [sorts] 排序方式,格式:{key:'', order:''}
  312. * - key: title|username|indate
  313. * - order: asc|desc
  314. * @apiParam {Number} [page] 当前页,默认:1
  315. * @apiParam {Number} [pagesize] 每页显示数量,默认:20,最大:100
  316. */
  317. public function receive()
  318. {
  319. $user = Users::authE();
  320. if (Base::isError($user)) {
  321. return $user;
  322. } else {
  323. $user = $user['data'];
  324. }
  325. //
  326. $whereArray = [];
  327. $whereArray[] = ['report_ccuser.username', '=', $user['username']];
  328. $whereArray[] = ['report_ccuser.cc', '=', 1];
  329. if (trim(Request::input('username'))) {
  330. $whereArray[] = ['report_lists.username', '=', trim(Request::input('username'))];
  331. }
  332. if (in_array(trim(Request::input('type')), ['日报', '周报'])) {
  333. $whereArray[] = ['report_lists.type', '=', trim(Request::input('type'))];
  334. }
  335. $indate = Request::input('indate');
  336. if (is_array($indate)) {
  337. if ($indate[0] > 0) $whereArray[] = ['report_lists.indate', '>=', Base::dayTimeF($indate[0])];
  338. if ($indate[1] > 0) $whereArray[] = ['report_lists.indate', '<=', Base::dayTimeE($indate[1])];
  339. }
  340. //
  341. $builder = DB::table('report_lists')
  342. ->join('report_ccuser', 'report_lists.id', '=', 'report_ccuser.rid')
  343. ->select(['report_lists.*', 'report_ccuser.indate as senddate'])
  344. ->where($whereArray);
  345. $sorts = Base::json2array(Request::input('sorts'));
  346. if (in_array($sorts['order'], ['asc', 'desc'])) {
  347. switch ($sorts['key']) {
  348. case 'title':
  349. case 'username':
  350. $builder->orderBy($sorts['key'], $sorts['order']);
  351. break;
  352. case 'indate':
  353. $builder->orderBy('report_ccuser.indate', $sorts['order']);
  354. break;
  355. }
  356. } else {
  357. $builder->orderByDesc('report_ccuser.indate');
  358. $builder->orderByDesc('report_ccuser.id');
  359. }
  360. //
  361. $builder->orderByDesc('date');
  362. $lists = $builder->paginate(Base::getPaginate(100, 20));
  363. $lists = Base::getPageList($lists);
  364. if ($lists['total'] == 0) {
  365. return Base::retError('未找到任何相关的汇报', $lists);
  366. }
  367. foreach ($lists['lists'] AS $key => $item) {
  368. $lists['lists'][$key]['ccuser'] = Base::string2array($item['ccuser']);
  369. }
  370. return Base::retSuccess('success', $lists);
  371. }
  372. /**
  373. * 获取我上次抄送的人
  374. * @return array|mixed
  375. */
  376. public function prevcc()
  377. {
  378. $user = Users::authE();
  379. if (Base::isError($user)) {
  380. return $user;
  381. } else {
  382. $user = $user['data'];
  383. }
  384. //
  385. $rid = DB::table('report_ccuser')
  386. ->join('report_lists', 'report_lists.id', '=', 'report_ccuser.rid')
  387. ->where('report_lists.username', $user['username'])
  388. ->orderByDesc('report_ccuser.id')
  389. ->value('rid');
  390. if (empty($rid)) {
  391. return Base::retError('没有相关数据!');
  392. }
  393. $lists = Base::DBC2A(DB::table('report_ccuser')->select(['username'])->where('rid', $rid)->pluck('username'));
  394. if (empty($lists)) {
  395. return Base::retError('没有相关数据!');
  396. }
  397. return Base::retSuccess('success', [
  398. 'lists' => $lists
  399. ]);
  400. }
  401. }