Daily.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. <?php
  2. namespace addons\qingdong\controller;
  3. use addons\qingdong\model\Daily as DailyModel;
  4. use addons\qingdong\model\DailyRead;
  5. use addons\qingdong\model\File;
  6. use addons\qingdong\model\Form;
  7. use addons\qingdong\model\Staff;
  8. use addons\qingdong\model\Message;
  9. use addons\qingdong\model\DailyDraft;
  10. use think\Db;
  11. use think\Exception;
  12. /**
  13. * 工作报告接口
  14. */
  15. class Daily extends StaffApi {
  16. protected $noNeedLogin = [];
  17. protected $noNeedRight = [];
  18. //创建
  19. public function createDaily()
  20. {
  21. $params = $this->request->post();
  22. Db::startTrans();
  23. try {
  24. $params['create_staff_id'] = $this->auth->id;
  25. $dailyId = DailyModel::createDaily($params);
  26. Db::commit();
  27. } catch (Exception $e) {
  28. Db::rollback();
  29. $this->error($e->getMessage());
  30. }
  31. $this->success('创建工作报告成功');
  32. }
  33. //获取
  34. public function getList()
  35. {
  36. $limit = input("limit/d", 10);
  37. $is_read = input('is_read', 0);
  38. $staff_ids = input('staff_id', 0);
  39. $type = input('type', 0);
  40. $times = input('times', '');
  41. $where = [];
  42. $where['create_staff_id'] = ['in', Staff::getMyStaffIds()];
  43. if ($staff_ids) {
  44. $where['create_staff_id'] = $staff_ids;
  45. }
  46. if ($type) {
  47. $where['type'] = $type;
  48. }
  49. if ($times) {//筛选时间
  50. $times = explode(',', $times);
  51. $where['createtime'] = ['between', [strtotime($times[0]), strtotime($times[1]) + 86400 - 1]];
  52. }
  53. $staff_id = $this->auth->id;
  54. if ($is_read == 1) {//已读
  55. $ids = DailyRead::where(['staff_id' => $staff_id])->column('daily_id');
  56. $where['id'] = ['in', $ids];
  57. } elseif ($is_read == 2) {//未读
  58. $ids = DailyRead::where(['staff_id' => $staff_id])->column('daily_id');
  59. $where['id'] = ['not in', $ids];
  60. }
  61. $followWhere = [];
  62. $elseWhere = [];
  63. if (!$staff_ids) {
  64. $followWhere[] = ['exp', Db::raw('FIND_IN_SET(' . $this->auth->id . ',reminds_id)')];
  65. if ($type) {
  66. $elseWhere['type'] = ['eq', $type];
  67. }
  68. if ($is_read == 1) {//已读
  69. $elseWhere['id'] = ['in', $ids];
  70. } elseif ($is_read == 2) {//未读
  71. $elseWhere['id'] = ['not in', $ids];
  72. }
  73. }
  74. $records = DailyModel::where($where)->whereOr(function ($query) use ($followWhere, $elseWhere) {
  75. $query->where($followWhere)->where($elseWhere);
  76. })->with([
  77. 'staff',
  78. 'read' => function ($query) use ($staff_id) {
  79. $query->where(['staff_id' => $staff_id]);
  80. }
  81. ])->order('id desc')->paginate($limit)->toArray();
  82. $data = $records['data'];
  83. foreach ($data as $k => $v) {
  84. if (!empty($v['read'])) {
  85. $v['is_read'] = 1;
  86. } else {
  87. $v['is_read'] = 0;
  88. }
  89. $data[$k] = $v;
  90. }
  91. $this->success('请求成功', [
  92. 'total' => $records['total'],
  93. 'per_page' => $records['per_page'],
  94. 'current_page' => $records['current_page'],
  95. 'last_page' => $records['last_page'],
  96. 'data' => $data
  97. ]);
  98. $this->success('请求成功', $records);
  99. }
  100. //获取详情
  101. public function getDailyDetail()
  102. {
  103. $id = input('id');
  104. if (empty($id)) {
  105. $this->error('参数不能为空');
  106. }
  107. $record = DailyModel::where(['id' => $id])->with([
  108. 'staff',
  109. ])->find();
  110. if (empty($record)) {
  111. $this->error('记录不存在');
  112. }
  113. $record = $record->toArray();
  114. $reminds_id = $record['reminds_id'];
  115. $reminds_id = explode(',', $reminds_id);
  116. $names = Staff::where(['id' => ['in', $reminds_id]])->column('name');
  117. $record['staff_name'] = implode(',', $names);
  118. if ($record['other']) {
  119. if(is_array($record['other'])){
  120. $other = $record['other'];
  121. }else{
  122. $other = json_decode($record['other'], true);
  123. }
  124. } else {
  125. $other = [];
  126. }
  127. $record = array_merge($record, $other);
  128. switch($record['type']){
  129. case '日报':
  130. $type = 'daily';
  131. break;
  132. case '周报':
  133. $type ='weekly';
  134. break;
  135. case '月报':
  136. $type = 'monthly';
  137. break;
  138. case '季报':
  139. $type = 'quarterly';
  140. break;
  141. case '年报':
  142. $type = 'yearly';
  143. break;
  144. default:
  145. $type = 'daily';
  146. break;
  147. }
  148. $form = Form::getDataValue($type,$record);
  149. foreach($form as $k=>$v){
  150. if($v['component'] == 'uploadImage' || $v['component'] == 'uploadFile'){
  151. if(key_exists($v['id'],$record)){
  152. if(isset($record[$v['id']]) && $record[$v['id']]){
  153. $whereT['id'] = array('in',$record[$v['id']]);
  154. $fileinfo = File::where($whereT)->field('id,name,file_path')->select();
  155. if($fileinfo){
  156. $record[$v['id']] = $fileinfo;
  157. }
  158. }
  159. }
  160. }
  161. }
  162. //标记通知已读
  163. Message::setRead(Message::DAILY_TYPE, $id, $this->auth->id);
  164. //添加阅读记录
  165. DailyRead::addRead($id, $this->auth->id);
  166. $this->success('请求成功', $record);
  167. }
  168. //获取工作报告已读 未读
  169. public function getDailyRead()
  170. {
  171. $id = input('id');
  172. if (empty($id)) {
  173. $this->error('参数错误');
  174. }
  175. $daily = DailyModel::where(['id' => $id])->find();
  176. if (empty($daily)) {
  177. $this->error('记录不存在');
  178. }
  179. $create_staff_id = $daily['create_staff_id'];
  180. $read = DailyRead::where(['daily_id' => $id])->group('staff_id')->field('id,staff_id')->with(['staff'])->select();
  181. $staffIds = [];
  182. foreach ($read as $v) {
  183. $staffIds[] = $v['staff_id'];
  184. }
  185. //全部可看数据的人
  186. $allids = explode(',', $daily->reminds_id);
  187. $not_ids = array_diff($allids, $staffIds);
  188. $notRead = Staff::where(['id' => ['in', $not_ids]])
  189. ->field('id,name,img')->select();
  190. $this->success('请求成功', ['read' => $read, 'not_read' => $notRead]);
  191. }
  192. //草稿创建
  193. public function daily_draft()
  194. {
  195. $params = $this->request->post();
  196. Db::startTrans();
  197. try {
  198. $params['create_staff_id'] = $this->auth->id;
  199. //自定义字段
  200. $other = [];
  201. foreach ($params as $name => $val) {
  202. if (strstr($name, 'other_') !== false) {
  203. if(is_array($val)){
  204. $other[$name] = implode(',',$val);
  205. }else{
  206. $other[$name] = $val;
  207. }
  208. unset($params[$name]);
  209. }
  210. }
  211. $params['other'] = json_encode($other);
  212. $where['create_staff_id'] = $this->auth->id;
  213. $where['type'] = $params['type'];
  214. $info = DailyDraft::where($where)->find();
  215. if ($info) {
  216. $result = DailyDraft::where(array('id' => $info['id']))->update($params);
  217. } else {
  218. $result = DailyDraft::create($params);
  219. }
  220. if (false === $result) {
  221. // 验证失败 输出错误信息
  222. throw new Exception('草稿保存失败');
  223. }
  224. Db::commit();
  225. } catch (Exception $e) {
  226. Db::rollback();
  227. $this->error($e->getMessage());
  228. }
  229. $this->success('草稿保存成功');
  230. }
  231. //获取详情
  232. public function getDailyDraftDetail()
  233. {
  234. $type = input('type');
  235. if (!$type) {
  236. $this->error('参数不正确');
  237. }
  238. $where['create_staff_id'] = $this->auth->id;
  239. $where['type'] = $type;
  240. $record = DailyDraft::where($where)->find();
  241. if (empty($record)) {
  242. $this->success('记录不存在');
  243. }
  244. switch ($record['type']) {
  245. case '日报':
  246. $record['type_index'] = 0;
  247. break;
  248. case '周报':
  249. $record['type_index'] = 1;
  250. break;
  251. case '月报':
  252. $record['type_index'] = 2;
  253. break;
  254. case '季报':
  255. $record['type_index'] = 3;
  256. break;
  257. case '年报':
  258. $record['type_index'] = 4;
  259. break;
  260. default:
  261. $record['type_index'] = 0;
  262. break;
  263. }
  264. $record = $record->toArray();
  265. $reminds_id = $record['reminds_id'];
  266. $reminds_id = explode(',', $reminds_id);
  267. $names = Staff::where(['id' => ['in', $reminds_id]])->field('id,img,name,post')->select();
  268. $record['staff_info'] = $names;
  269. if ($record['other']) {
  270. $other = json_decode($record['other'], true);
  271. } else {
  272. $other = [];
  273. }
  274. $record = array_merge($record, $other);
  275. $this->success('请求成功', $record);
  276. }
  277. //修改工作报告
  278. public function updateDaily()
  279. {
  280. $params = $this->request->post();
  281. if(empty($params['id'])){
  282. $this->error('参数不能为空');
  283. }
  284. Db::startTrans();
  285. try {
  286. $dailyId = DailyModel::updateDaily($params);
  287. Db::commit();
  288. } catch (Exception $e) {
  289. Db::rollback();
  290. $this->error($e->getMessage());
  291. }
  292. $this->success('创建工作报告成功');
  293. }
  294. }