StaffSignIn.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. namespace addons\qingdongams\model;
  3. use think\Exception;
  4. use think\Model;
  5. use think\Session;
  6. use traits\model\SoftDelete;
  7. /**
  8. *员工外勤签到
  9. */
  10. class StaffSignIn Extends Model {
  11. use SoftDelete;
  12. const WORKORDER_TYPE = 'workorder';//工单
  13. const EVENT_TYPE = 'event';//日程
  14. const CUSTOMER_TYPE = 'customer';//客户
  15. const PROOFING_TYPE = 'proofing';//客户
  16. const ORDER_TYPE = 'order';//订单
  17. protected $name = 'qingdongams_staff_sign_in';
  18. // 开启自动写入时间戳字段
  19. protected $autoWriteTimestamp = 'int';
  20. // 定义时间戳字段名
  21. protected $createTime = 'createtime';
  22. protected $updateTime = 'updatetime';
  23. protected $deleteTime = 'deletetime';
  24. //获取创建时间
  25. public function getCreatetimeAttr($value) {
  26. return date('Y-m-d H:i:s', $value);
  27. }
  28. public function getDistanceAttr($value) {
  29. return float_number($value);
  30. }
  31. //
  32. public function getFileIdsAttr($value) {
  33. $files = explode(',', $value);
  34. $result = [];
  35. foreach ($files as $fid) {
  36. if ($fid) {
  37. $result[] = cdnurl(File::getUrl($fid), true);
  38. }
  39. }
  40. return $result;
  41. }
  42. //获取日程状态
  43. public function getRelationProcessAttr($value){
  44. $process=[0=>'未开始',1=>'出发签到',2=>'到达签到',3=>'完成签到',4=>'返程签到'];
  45. return $process[$value]??$value;
  46. }
  47. //签到
  48. public static function createSignIn($params) {
  49. $staff = Staff::info();
  50. $params['staff_id'] = $staff->id;
  51. $model = new self;
  52. $lastSignIn=$model->where(['staff_id'=>$staff->id])->order('id desc')->field('id,lng,lat,location')->find();
  53. if (empty($lastSignIn['lng']) || empty($lastSignIn['lat']) || empty($params['lng'])|| empty($params['lat'])) {
  54. $params['trip_distance'] = 0;
  55. } else {
  56. $params['trip_distance'] = getdistance($lastSignIn['lng'], $lastSignIn['lat'], $params['lng'], $params['lat']);
  57. }
  58. // 调用当前模型对应的User验证器类进行数据验证
  59. $result = $model->allowField(true)->save($params);
  60. if(!$result){
  61. throw new Exception('创建签到记录失败');
  62. }
  63. $lastId = $model->getLastInsID();
  64. if (isset($params['reminds_id'])) {//发送通知
  65. $staff_ids = explode(',', $params['reminds_id']);
  66. foreach ($staff_ids as $staff_id) {
  67. //发送通知
  68. Message::addMessage(Message::SIGN_TYPE, $lastId, $staff_id, $staff->id, $staff->name . '有新的工作动态,由您审阅!');
  69. }
  70. }
  71. return true;
  72. }
  73. //创建签到信息
  74. public static function quickSignIn($customer_id, $content, $params=[])
  75. {
  76. $customer = Customer::where(['id' => $customer_id])->field('lng,lat')->find();
  77. $lng=$params['lng']??'';
  78. $lat=$params['lat']??'';
  79. if (empty($lng) && empty($lat)) {
  80. $distance = '';
  81. } elseif (empty($customer['lng']) && empty($customer['lat'])) {
  82. $distance = '';
  83. } else {
  84. $distance = getdistance($customer['lng'], $customer['lat'], $lng, $lat);
  85. }
  86. $staff = Staff::info();
  87. $data = [
  88. 'content' => $content,
  89. 'customer_id' => $customer_id,
  90. 'relation_type' => $params['relation_type']??'',
  91. 'relation_id' => $params['relation_id']??'',
  92. 'relation_process' => $params['relation_process']??'工单备注',
  93. 'lng' => $lng,
  94. 'lat' => $lat,
  95. 'distance' => $distance,
  96. 'location' => $params['location']??'',
  97. 'file_ids' => $params['file_ids']??'',
  98. 'staff_id'=>$staff->id,
  99. ];
  100. $Enent = new self;
  101. $result = $Enent->allowField(true)->save($data);
  102. if (false === $result) {
  103. // 验证失败 输出错误信息
  104. throw new Exception($Enent->getError());
  105. }
  106. return true;
  107. }
  108. //员工
  109. public function staff() {
  110. return $this->belongsTo(Staff::class, 'staff_id', 'id')->field('id,name,img,post');
  111. }
  112. //客户
  113. public function customer() {
  114. return $this->belongsTo(Customer::class, 'customer_id', 'id')->field('id,name');
  115. }
  116. //是否跟进
  117. public function read() {
  118. return $this->hasMany(StaffSignInRead::class, 'sign_in_id', 'id')->field('sign_in_id,createtime');
  119. }
  120. //评论次数
  121. public function commentNumber(){
  122. return $this->belongsTo(Comment::class,'id','relation_id')->where(['relation_type'=>Comment::SIGN_TYPE])->group('relation_id')->field('relation_id,count(*) as comment')->bind('comment');
  123. }
  124. //关联工单
  125. public function workorder() {
  126. return $this->belongsTo(Workorder::class, 'relation_id', 'id')->field('id,title');
  127. }
  128. //关联日程
  129. public function eventTitle() {
  130. return $this->belongsTo(Event::class, 'relation_id', 'id')->field('id,title');
  131. }
  132. }