Event.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. <?php
  2. namespace addons\qingdong\controller;
  3. use addons\qingdong\model\Event as EventModel;
  4. use addons\qingdong\model\File;
  5. use addons\qingdong\model\Form;
  6. use addons\qingdong\model\StaffSignIn;
  7. use addons\qingdong\model\Message;
  8. use addons\qingdong\model\Staff;
  9. use think\Db;
  10. use think\Exception;
  11. /**
  12. * 日程接口
  13. */
  14. class Event extends StaffApi
  15. {
  16. protected $noNeedLogin = [];
  17. protected $noNeedRight = [];
  18. //获取日程
  19. public function getEvent()
  20. {
  21. //开始时间
  22. $start_day = input('start_day');
  23. //结束时间
  24. $end_day = input('end_day');;
  25. if (empty($start_day) || empty($end_day)) {
  26. $this->error('开始时间或结束时间不能为空');
  27. }
  28. if ($end_day < $start_day) {
  29. $this->error('开始时间不能小于结束时间');
  30. }
  31. $this->success('请求成功', EventModel::getTimeList($start_day, $end_day));
  32. }
  33. //添加日程
  34. public function addEvent()
  35. {
  36. $params = $this->request->post();
  37. // 表单验证
  38. if (($result = $this->qingdongValidate($params, get_class(), 'create')) !== true) {
  39. $this->error($result);
  40. }
  41. Db::startTrans();
  42. try {
  43. $params['type'] = 1;//日程
  44. $result = EventModel::createEvent($params);
  45. Db::commit();
  46. } catch (Exception $e) {
  47. Db::rollback();
  48. $this->error($e->getMessage());
  49. }
  50. if ($result) {
  51. $this->success('添加日程成功');
  52. }
  53. }
  54. //变更状态
  55. public function changeStatus()
  56. {
  57. $id = input('id');
  58. $status = input('status', 0);
  59. if (empty($id)) {
  60. $this->error('参数错误');
  61. }
  62. Db::startTrans();
  63. try {
  64. EventModel::changeStatus($id, $status);
  65. Db::commit();
  66. } catch (Exception $e) {
  67. Db::rollback();
  68. $this->error($e->getMessage());
  69. }
  70. $this->success('变更成功');
  71. }
  72. //
  73. public function getList()
  74. {
  75. $relation_type = input('relation_type', '', 'intval');// 1客户 2联系人 3合同 4线索
  76. $relation_id = input('relation_id', '', 'intval');
  77. $limit = input("limit/d", 10);
  78. $where = [];
  79. $where['relation_type'] = $relation_type;
  80. $where['relation_id'] = $relation_id;
  81. $where['staff_id'] = $this->auth->id;
  82. $records = EventModel::where($where)->with(['staff'])->field('id,staff_id,type,title,start_time,end_time,status,auto_end,level,remindtype,remark,color,createtime')->order('id desc')->paginate($limit);
  83. $this->success('请求成功', $records);
  84. }
  85. //添加签到
  86. public function addSign()
  87. {
  88. $params = $this->request->post();
  89. $location = input('location');
  90. $lng = input('lng');
  91. $lat = input('lat');
  92. $files = input('file_ids');
  93. $customer_id = input('customer_id');
  94. $content = input('content');
  95. if (empty($lng) || empty($lat)) {
  96. $this->error('地理位置不能为空');
  97. }
  98. //自定义字段
  99. $other = [];
  100. foreach ($params as $name => $val) {
  101. if (strstr( $name,'other_') !== false) {
  102. if(is_array($val)){
  103. $other[$name] = implode(',',$val);
  104. }else{
  105. $other[$name] = $val;
  106. }
  107. unset($params[$name]);
  108. }
  109. }
  110. $data = [
  111. 'location' => $location,
  112. 'lng' => $lng,
  113. 'lat' => $lat,
  114. 'file_ids' => $files,
  115. 'customer_id' => $customer_id,
  116. 'staff_id' => $this->auth->id,
  117. 'other' => json_encode($other, JSON_UNESCAPED_UNICODE)
  118. ];
  119. $model = new StaffSignIn();
  120. if ($result = $model->save($data)) {
  121. $lastId = $model->getLastInsID();
  122. //通知上级
  123. Message::addMessage(Message::SIGN_TYPE, $lastId, $this->auth->parent_id, $this->auth->id);
  124. $this->success('签到成功');
  125. }
  126. $this->error('签到失败');
  127. }
  128. //获取签到信息
  129. public function getSign()
  130. {
  131. $customer_id = input('customer_id', 0, 'intval');
  132. $limit = input("limit/d", 10);
  133. $where = [];
  134. $type = input('type', 0);// 0 全部 1 我创建 2 下属创建
  135. if ($type == 1) {//我的客户
  136. $where['staff_id'] = $this->auth->id;
  137. } elseif ($type == 2) {//下属负责的客户
  138. $where['staff_id'] = ['in', Staff::getLowerStaffId()];
  139. }else{
  140. $where['staff_id'] = ['in', Staff::getMyStaffIds()];
  141. }
  142. if ($customer_id) {
  143. $where['customer_id'] = $customer_id;
  144. }
  145. $staffSign = StaffSignIn::where($where)->with(['staff', 'customer'])
  146. ->order('id desc')->paginate($limit);
  147. //标记通知已读
  148. Message::setRead(Message::SIGN_TYPE,0, $this->auth->id);
  149. $this->success('请求成功', $staffSign);
  150. }
  151. /**
  152. * 获取签到详情
  153. */
  154. public function getSignDetail(){
  155. $id = input('id', 0, 'intval');
  156. $staffSign = StaffSignIn::where(['id'=>$id])->with(['staff', 'customer'])->find();
  157. if(empty($staffSign)){
  158. $this->error('数据不存在');
  159. }
  160. if($staffSign['other']){
  161. $other = json_decode($staffSign['other'],true);
  162. $form = Form::getDataValue('signin');
  163. foreach($form as $k=>$v){
  164. if($v['component'] == 'uploadImage' || $v['component'] == 'uploadFile'){
  165. $other[$v['id'].'_str'] = '';
  166. if(key_exists($v['id'],$other)){
  167. if(isset($other[$v['id']]) && $other[$v['id']]){
  168. $whereT['id'] = array('in',$other[$v['id']]);
  169. $fileinfo = File::where($whereT)->field('id,name,file_path,save_name')->select();
  170. if($fileinfo){
  171. $other[$v['id']] = $fileinfo;
  172. $fileinfodata = '';
  173. foreach($fileinfo as $kss=>$vss){
  174. $fileinfodata = $vss['save_name'].','.$fileinfodata;
  175. }
  176. $other[$v['id'].'_str'] = rtrim($fileinfodata,',');
  177. }
  178. }
  179. }
  180. }
  181. }
  182. $staffSign['other'] = $other;
  183. }
  184. $this->success('请求成功',$staffSign);
  185. }
  186. }