Daily.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. <?php
  2. namespace addons\qingdongams\controller;
  3. use addons\qingdongams\model\Daily as DailyModel;
  4. use addons\qingdongams\model\DailyDraft;
  5. use addons\qingdongams\model\DailyRead;
  6. use addons\qingdongams\model\Form;
  7. use addons\qingdongams\model\FormField;
  8. use addons\qingdongams\model\Staff;
  9. use addons\qingdongams\model\Message;
  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. $params = $this->request->post();
  21. $result = FormField::checkFields(DailyModel::getTypeName($params['type']), $params);
  22. if ($result !== true) {
  23. $this->error($result);
  24. }
  25. Db::startTrans();
  26. try {
  27. $params['create_staff_id'] = $this->auth->id;
  28. $dailyId = DailyModel::createDaily($params);
  29. Db::commit();
  30. } catch (Exception $e) {
  31. Db::rollback();
  32. $this->error($e->getMessage());
  33. }
  34. $this->success('创建工作报告成功');
  35. }
  36. //获取
  37. public function getList() {
  38. $limit = input("limit/d", 10);
  39. $is_read = input('is_read', 0);
  40. $staff_id = input('staff_id', 0);
  41. $type = input('type', 0);
  42. $where = [];
  43. $where['create_staff_id'] = ['in', Staff::getMyStaffIds()];
  44. if($staff_id){
  45. $where['create_staff_id']=$staff_id;
  46. }
  47. if($type){
  48. $where['type']=$type;
  49. }
  50. $staff_id = $this->auth->id;
  51. if ($is_read == 1) {//已读
  52. $ids = DailyRead::where(['staff_id' => $staff_id])->column('daily_id');
  53. $where['id'] = ['in', $ids];
  54. } elseif ($is_read == 2) {//未读
  55. $ids = DailyRead::where(['staff_id' => $staff_id])->column('daily_id');
  56. $where['id'] = ['not in', $ids];
  57. }
  58. $records = DailyModel::where($where)->with([
  59. 'staff',
  60. 'read' => function ($query) use ($staff_id) {
  61. $query->where(['staff_id' => $staff_id]);
  62. }
  63. ])->order('id desc')->paginate($limit)->toArray();
  64. $data = $records['data'];
  65. foreach ($data as $k => $v) {
  66. if (!empty($v['read'])) {
  67. $v['is_read'] = 1;
  68. } else {
  69. $v['is_read'] = 0;
  70. }
  71. $data[$k] = $v;
  72. }
  73. $this->success('请求成功', [
  74. 'total' => $records['total'],
  75. 'per_page' => $records['per_page'],
  76. 'current_page' => $records['current_page'],
  77. 'last_page' => $records['last_page'],
  78. 'data' => $data
  79. ]);
  80. $this->success('请求成功', $records);
  81. }
  82. //获取详情
  83. public function getDailyDetail() {
  84. $id = input('id');
  85. if (empty($id)) {
  86. $this->error('参数不能为空');
  87. }
  88. $record = DailyModel::where(['id' => $id])->with([
  89. 'staff',
  90. ])->find();
  91. if (empty($record)) {
  92. $this->error('记录不存在');
  93. }
  94. $record = $record->toArray();
  95. $reminds_id = $record['reminds_id'];
  96. $reminds_id = explode(',', $reminds_id);
  97. $names = \addons\qingdongams\model\Staff::where(['id' => ['in', $reminds_id],'status'=>1])->column('name');
  98. $record['staff_name'] = implode(',', $names);
  99. $other = $record['other']?:[];
  100. $record = array_merge($record, $other);
  101. $record = Form::getDataDetail(DailyModel::getTypeName($record['type']), $record);
  102. //标记通知已读
  103. //添加阅读记录
  104. DailyRead::addRead($id, $this->auth->id);
  105. //标记通知已读
  106. Message::setRead(Message::DAILY_TYPE, $id, $this->auth->id);
  107. $this->success('请求成功', $record);
  108. }
  109. //获取工作报告已读 未读
  110. public function getDailyRead()
  111. {
  112. $id = input('id');
  113. if (empty($id)) {
  114. $this->error('参数错误');
  115. }
  116. $daily = DailyModel::where(['id' => $id])->find();
  117. if (empty($daily)) {
  118. $this->error('记录不存在');
  119. }
  120. $create_staff_id = $daily['create_staff_id'];
  121. $read = DailyRead::where(['daily_id' => $id])->group('staff_id')->field('id,staff_id')->with(['staff'])->select();
  122. $staffIds = [];
  123. foreach ($read as $v) {
  124. $staffIds[] = $v['staff_id'];
  125. }
  126. //全部可看数据的人
  127. $allids = explode(',', $daily->reminds_id);
  128. $not_ids = array_diff($allids, $staffIds);
  129. $notRead = Staff::where(['id' => ['in', $not_ids],'status'=>1])
  130. ->field('id,name,img')->select();
  131. $this->success('请求成功', ['read' => $read, 'not_read' => $notRead]);
  132. }
  133. //草稿创建成功
  134. public function daily_draft()
  135. {
  136. $params = $this->request->post();
  137. Db::startTrans();
  138. try {
  139. $params['create_staff_id'] = $this->auth->id;
  140. //自定义字段
  141. $other = [];
  142. foreach ($params as $name => $val) {
  143. if(is_array($val)){
  144. $other[$name] = implode(',',$val);
  145. }else{
  146. $other[$name] = $val;
  147. }
  148. unset($params[$name]);
  149. }
  150. $params['other'] = json_encode($other);
  151. $where['create_staff_id'] = $this->auth->id;
  152. $where['type'] = $params['type'];
  153. $info = DailyDraft::where($where)->find();
  154. if ($info) {
  155. $result = DailyDraft::where(array('id' => $info['id']))->update($params);
  156. } else {
  157. $result = DailyDraft::create($params);
  158. }
  159. if (false === $result) {
  160. // 验证失败 输出错误信息
  161. throw new Exception('草稿保存失败');
  162. }
  163. Db::commit();
  164. } catch (Exception $e) {
  165. Db::rollback();
  166. $this->error($e->getMessage());
  167. }
  168. $this->success('草稿保存成功');
  169. }
  170. //获取详情
  171. public function getDailyDraftDetail()
  172. {
  173. $type = input('type');
  174. if (!$type) {
  175. $this->error('参数不正确');
  176. }
  177. $where['create_staff_id'] = $this->auth->id;
  178. $where['type'] = $type;
  179. $record = DailyDraft::where($where)->find();
  180. if (empty($record)) {
  181. $this->success('记录不存在');
  182. }
  183. switch ($record['type']) {
  184. case '日报':
  185. $record['type_index'] = 0;
  186. break;
  187. case '周报':
  188. $record['type_index'] = 1;
  189. break;
  190. case '月报':
  191. $record['type_index'] = 2;
  192. break;
  193. case '季报':
  194. $record['type_index'] = 3;
  195. break;
  196. case '年报':
  197. $record['type_index'] = 4;
  198. break;
  199. default:
  200. $record['type_index'] = 0;
  201. break;
  202. }
  203. $record = $record->toArray();
  204. $reminds_id = $record['reminds_id'];
  205. $reminds_id = explode(',', $reminds_id);
  206. $names = Staff::where(['id' => ['in', $reminds_id],'status'=>1])->field('id,img,name,post')->select();
  207. $record['staff_info'] = $names;
  208. $other = $record['other']?:[];
  209. $record = array_merge($record, $other);
  210. $this->success('请求成功', $record);
  211. }
  212. //修改工作报告
  213. public function updateDaily()
  214. {
  215. $params = $this->request->post();
  216. if(empty($params['id'])){
  217. $this->error('参数不能为空');
  218. }
  219. Db::startTrans();
  220. try {
  221. $dailyId = DailyModel::updateDaily($params);
  222. Db::commit();
  223. } catch (Exception $e) {
  224. Db::rollback();
  225. $this->error($e->getMessage());
  226. }
  227. $this->success('创建工作报告成功');
  228. }
  229. }