Event.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. <?php
  2. namespace addons\qingdongams\model;
  3. use think\Exception;
  4. use think\Model;
  5. use traits\model\SoftDelete;
  6. /**
  7. *日程提醒
  8. */
  9. class Event Extends Model {
  10. use SoftDelete;
  11. const CUSTOMER_TYPE = 1;//客户
  12. const CONTACTS_TYPE = 2;//联系人
  13. const CONTRACT_TYPE = 3;//合同
  14. const LEADS_TYPE = 4;//线索
  15. const RECEIVABLES_TYPE = 5;//回款记录
  16. protected $name = 'qingdongams_event';
  17. // 开启自动写入时间戳字段
  18. protected $autoWriteTimestamp = 'int';
  19. // 定义时间戳字段名
  20. protected $createTime = 'createtime';
  21. protected $updateTime = 'updatetime';
  22. protected $deleteTime = 'deletetime';
  23. protected $append = [
  24. 'show_staff_data',
  25. ];
  26. protected static function init()
  27. {
  28. self::beforeUpdate(function ($model) {
  29. if(isset($model->status)){
  30. if ($model->status == 2) {
  31. $model->actual_end_time=time();
  32. }
  33. }
  34. });
  35. }
  36. public function getShowStaffDataAttr($value,$data)
  37. {
  38. if (!isset($data['show_staff_ids'])) {
  39. return '';
  40. }
  41. $ids=explode(',',$data['show_staff_ids']);
  42. return Staff::where(['id'=>['in',$ids]])->field('id,name,img,post,mobile')->select();
  43. }
  44. //评论
  45. public function comments()
  46. {
  47. return $this->hasMany(Comment::class, 'relation_id', 'id')->where(['relation_type'=>'assist_event'])->with('staff')->field('id,relation_id,content,staff_id,file_ids,createtime');
  48. }
  49. public function getCreatetimeAttr($value) {
  50. return date('Y-m-d H:i:s', $value);
  51. }
  52. //获取紧要程度
  53. public static function getLevel($level) {
  54. $levels = [0 => '普通', 1 => '重要', 2 => '紧急', 4 => '重要且紧急'];
  55. return $levels[$level] ?? "普通";
  56. }
  57. //获取日程状态
  58. public static function getStatus($status) {
  59. $statuss = [0 => '未开始', 1 => '进行中', 2 => '已结束', 3 => '任务终止'];
  60. return $statuss[$status] ?? "未开始";
  61. }
  62. //创建日程
  63. public static function createEvent($params)
  64. {
  65. $staff = Staff::info();
  66. if (isset($params['event_type'])) {
  67. if(isset($params['event_type'][2]) && in_array($params['event_type'][2],['上门安装','上门维修']) ){
  68. if($params['event_type'][1] != '上门'){
  69. unset($params['event_type'][2]);
  70. }
  71. }
  72. $params['event_type'] = implode('_', $params['event_type']);
  73. $params['customer_id'] = $params['relation_id'];
  74. $params['event_type'] = trim($params['event_type'], '_');
  75. }
  76. if (empty($params['staff_id'])) {
  77. $params['staff_id'] = $staff->id;
  78. }
  79. $params['create_staff_id'] = $staff->id;
  80. $params['show_staff_ids'] = $params['staff_id'];
  81. if (empty($params['relation_type'])) {
  82. $params['relation_type'] = StaffSignIn::EVENT_TYPE;
  83. }
  84. if (isset($params['event_type'])) {
  85. $params['owner_staff_id'] = $params['staff_id'];
  86. if ($params['event_type'] == '设计工单') {
  87. $workorderModel = new Workorder();
  88. $content = '创建设计工单';
  89. $params['workorder_type'] = '设计工单';
  90. $params['workorder_number'] = 'S' . date('ymd') . rand(100, 999);
  91. $workorderLastid = $workorderModel::createWorkorder($params);
  92. $signin['relation_type'] = StaffSignIn::WORKORDER_TYPE;
  93. $signin['relation_id'] = $workorderLastid;
  94. //创建跟进记录
  95. StaffSignIn::quickSignIn($params['customer_id'], $content, $signin);
  96. $params['relation_type'] = StaffSignIn::WORKORDER_TYPE;
  97. $params['relation_id'] = $workorderLastid;
  98. } else if ($params['event_type'] == '售后服务_返厂维修') {
  99. $workorderModel = new Workorder();
  100. $content = '创建售后服务-返厂维修';
  101. $params['workorder_type'] = '返厂维修';
  102. $params['workorder_number'] = 'S' . date('ymd') . rand(100, 999);
  103. $workorderLastid = $workorderModel::createWorkorder($params);
  104. $signin['relation_type'] = StaffSignIn::WORKORDER_TYPE;
  105. $signin['relation_id'] = $workorderLastid;
  106. //创建跟进记录
  107. StaffSignIn::quickSignIn($params['customer_id'], $content, $signin);
  108. $params['relation_type'] = StaffSignIn::WORKORDER_TYPE;
  109. $params['relation_id'] = $workorderLastid;
  110. } else if ($params['event_type'] == '售后服务_上门_上门安装') {
  111. $workorderModel = new Workorder();
  112. $content = '创建服务-上门安装';
  113. $params['workorder_type'] = '上门安装';
  114. $params['workorder_number'] = 'S' . date('ymd') . rand(100, 999);
  115. $workorderLastid = $workorderModel::createWorkorder($params);
  116. $signin['relation_type'] = StaffSignIn::WORKORDER_TYPE;
  117. $signin['relation_id'] = $workorderLastid;
  118. //创建跟进记录
  119. StaffSignIn::quickSignIn($params['customer_id'], $content, $signin);
  120. $params['relation_type'] = StaffSignIn::WORKORDER_TYPE;
  121. $params['relation_id'] = $workorderLastid;
  122. } else if ($params['event_type'] == '售后服务_上门_上门维修') {
  123. $workorderModel = new Workorder();
  124. $content = '创建服务-上门维修';
  125. $params['workorder_type'] = '上门维修';
  126. $params['workorder_number'] = 'S' . date('ymd') . rand(100, 999);
  127. $workorderLastid = $workorderModel::createWorkorder($params);
  128. $signin['relation_type'] = StaffSignIn::WORKORDER_TYPE;
  129. $signin['relation_id'] = $workorderLastid;
  130. //创建跟进记录
  131. StaffSignIn::quickSignIn($params['customer_id'], $content, $signin);
  132. $params['relation_type'] = StaffSignIn::WORKORDER_TYPE;
  133. $params['relation_id'] = $workorderLastid;
  134. }
  135. }
  136. if (isset($params['product_part'])) {
  137. unset($params['product_part']);
  138. }
  139. $Model = new self;
  140. // 调用当前模型对应的User验证器类进行数据验证
  141. $result = $Model->allowField(true)->save($params);
  142. if (false === $result) {
  143. // 验证失败 输出错误信息
  144. throw new Exception($Model->getError());
  145. }
  146. $lastId = $Model->getLastInsID();
  147. // JointFollow::initJointFollow( JointFollow::EVENT_TYPE,$lastId,$params['staff_id'],$params['show_staff_ids']);
  148. $data = [
  149. 'staff_id' => $staff->id,
  150. 'customer_id' => $params['customer_id']??0,//客户id
  151. 'relation_process' => '创建任务',//日程类型
  152. 'relation_type' => StaffSignIn::EVENT_TYPE,//日程类型
  153. 'relation_id' => $lastId,//签到关联id
  154. 'content' => '创建任务',
  155. ];
  156. StaffSignIn::createSignIn($data);
  157. if (isset($params['type']) && $params['type'] == 4) {//任务
  158. $staff = Staff::info();
  159. //发送通知
  160. Message::addMessage(Message::ASSIST_EVENT_TYPE, $lastId, $params['staff_id'], $staff->id,
  161. $staff->name . '邀请您协助工作,请您及时查看');
  162. } else {
  163. $staff = Staff::info();
  164. if ($params['staff_id'] != $staff->id && $params['relation_type'] == StaffSignIn::EVENT_TYPE) {
  165. //发送通知
  166. Message::addMessage(Message::EVENT_TYPE, $lastId, $params['staff_id'], $staff->id,
  167. $staff->name . '指派给您一项任务,请您及时审阅');
  168. }
  169. }
  170. return true;
  171. }
  172. //修改日程信息
  173. public static function editEvent($params)
  174. {
  175. $staff = Staff::info();
  176. $params['status'] = 0;
  177. $row=self::get($params['id']);
  178. if(empty($params['staff_id']) ){
  179. $params['staff_id'] = $staff->id;
  180. //发送通知
  181. Message::addMessage(Message::WORKORDER_TYPE, $row->id,
  182. $params['staff_id'], $staff->id,
  183. $staff->name.'《'.$row->title.'》指派给您一件任务,请您及时审阅');
  184. }
  185. $Model = new self;
  186. // 调用当前模型对应的User验证器类进行数据验证
  187. $result = $Model->allowField(true)->save($params,['id'=>$params['id']]);
  188. if (false === $result) {
  189. // 验证失败 输出错误信息
  190. throw new Exception($Model->getError());
  191. }
  192. return true;
  193. }
  194. /**
  195. *变更状态
  196. */
  197. public static function changeStatus($id, $status) {
  198. $model = new self();
  199. if ($model->save(['status' => $status], ['id' => $id]) == false) {
  200. throw new Exception('变更失败');
  201. }
  202. return true;
  203. }
  204. //根据时间获取列表
  205. public static function getTimeList($start_time, $end_time) {
  206. $staff = Staff::info();
  207. $model = new self();
  208. $start_time = $start_time . ' 00:00:00';
  209. $end_time = $end_time . ' 23:59:59';
  210. $events = $model->where([
  211. 'staff_id' => $staff->id,
  212. ])->where(function ($query) use ($start_time, $end_time) {
  213. $query->where(['start_time' => ['between', [$start_time, $end_time]]])->whereOr([
  214. 'end_time' => [
  215. 'between',
  216. [
  217. $start_time,
  218. $end_time
  219. ]
  220. ]
  221. ]);
  222. })->select();
  223. $data = [];
  224. $start_time = strtotime($start_time);
  225. $end_time = strtotime($end_time);
  226. for ($start_time; $start_time <= $end_time; $start_time = strtotime('+1 day', $start_time)) {
  227. $time = date('Y-m-d', $start_time);
  228. $data[$time] = [];
  229. foreach ($events as $k => $v) {
  230. if (strtotime($v['start_time']) <= $start_time + 86400 - 1 && strtotime($v['end_time']) >= $start_time) {
  231. $data[$time][] = $v;
  232. }
  233. }
  234. }
  235. foreach ($data as &$times) {
  236. foreach ($times as &$ves) {
  237. $ves['start_time'] = date('Y-m-d H:i', strtotime($ves['start_time']));
  238. $ves['end_time'] = date('Y-m-d H:i', strtotime($ves['end_time']));
  239. }
  240. }
  241. return $data;
  242. }
  243. /**
  244. * 修改任务状态
  245. * @desc 备注
  246. * @update_date 2021/7/16 更新时间
  247. * @author zhangwei
  248. */
  249. public static function updateProcess($id, $process,$last_event='') {
  250. $staff=Staff::info();
  251. $model = new self;
  252. $save = ['process' => $process];
  253. $row=$model::get($id);
  254. if ($process == 1) {//开始
  255. $save['status'] = 1;
  256. $save['actual_start_time'] = time();
  257. if($row['event_type'] == '客户服务_电话' || $row['event_type'] == '客户服务_微信' || $row['event_type'] == '售后服务_电话'){
  258. $save['process'] = 2;
  259. }
  260. }
  261. if ($process == 2) {//到访时间
  262. $save['actual_arrive_time'] = time();
  263. }
  264. if ($process == 3) {//完成
  265. if(empty($row)){
  266. throw new Exception('日程不存在');
  267. }
  268. $save['last_event']=$last_event;
  269. if($last_event != '返程'){//如果是拜访下一家 则任务结束
  270. $save['status'] = 2;
  271. }
  272. $save['actual_end_time'] = time();//结束时间
  273. //日程工作时长
  274. $use_time= $save['actual_end_time'] -$row->actual_start_time;
  275. $save['use_time'] = intval(($use_time%3600)/60);
  276. if($row->type == 2){//任务完成
  277. //发送通知
  278. Message::addMessage(Message::EVENT_TYPE, $id, $row->create_staff_id, $row->id,$staff->name.'刚刚完成一个任务,指定由您处理');
  279. }
  280. }
  281. if($process == 4){//返程签到
  282. $save['status'] = 2;
  283. }
  284. $result = $model->save($save, ['id' => $id]);
  285. if ($result == false) {
  286. // 验证失败 输出错误信息
  287. throw new Exception($model->getError());
  288. }
  289. return true;
  290. }
  291. //负责人
  292. public function staff() {
  293. return $this->hasOne(Staff::class, 'id', 'staff_id')->field('id,name,img,post,department_id');
  294. }
  295. //客户
  296. public function customer() {
  297. return $this->hasOne(Customer::class, 'id', 'customer_id')->field('id,name');
  298. }
  299. //联系人
  300. public function contacts() {
  301. return $this->hasOne(Contacts::class, 'customer_id', 'customer_id')->field('id,name,mobile,customer_id,mobilecode,region');
  302. }
  303. }