Skill.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. <?php
  2. namespace app\api\model\service;
  3. use think\Model;
  4. use think\Db;
  5. use think\Exception;
  6. use traits\model\SoftDelete;
  7. class Skill extends Model
  8. {
  9. use SoftDelete;
  10. // 表名
  11. protected $name = 'service_skill';
  12. // 自动写入时间戳字段
  13. protected $autoWriteTimestamp = 'int';
  14. // 定义时间戳字段名
  15. protected $createTime = 'createtime';
  16. protected $updateTime = 'updatetime';
  17. protected $deleteTime = 'deletetime';
  18. /**
  19. * 查询附近技师
  20. * @param $get
  21. * @return bool|\PDOStatement|string|\think\Collection
  22. * @throws \think\db\exception\DataNotFoundException
  23. * @throws \think\db\exception\ModelNotFoundException
  24. * @throws \think\exception\DbException
  25. */
  26. public static function nearSkill($get)
  27. {
  28. $lng = $get['lng'];
  29. $lat = $get['lat'];
  30. $orderBy = 'distance asc';
  31. $earth = 6378.137;
  32. $pi = 3.1415926535898;
  33. $list = self::where(['city' => $get['city'], 'state' => 1, 'is_rest' => 0, 'accept_nums' => ['>', 0]])
  34. ->field("id,user_id,name,skill_cate_id,user_image,image,city,age,(2*$earth*ASIN(SQRT(POW(SIN($pi*(" . $lat . "-lat)/360),2)+COS($pi*" . $lat . "/180)*COS(lat*$pi/180)*POW(SIN($pi*(" . $lng . "-lng)/360),2)))) as distance")
  35. ->order($orderBy)
  36. ->page($get['page'])
  37. ->limit($get['limit'])
  38. ->select();
  39. return $list;
  40. }
  41. /**
  42. * 查询服务人员列表
  43. * @param $get
  44. * @return bool|\PDOStatement|string|\think\Collection
  45. * @throws \think\db\exception\DataNotFoundException
  46. * @throws \think\db\exception\ModelNotFoundException
  47. * @throws \think\exception\DbException
  48. */
  49. public static function searchSkill($get)
  50. {
  51. extract($get);
  52. $lng = $get['lng'];
  53. $lat = $get['lat'];
  54. $orderBy = 'distance asc';
  55. $earth = 6378.137;
  56. $pi = 3.1415926535898;
  57. $where['state'] = 1;
  58. $where['accept_nums'] = ['>', 0];
  59. if (isset($name) && $name != '') {
  60. $where['name'] = ['like', "%$name%"];
  61. }
  62. if (isset($city) && $city != '') {
  63. $where['city'] = $city;
  64. }
  65. if (isset($category_id) && $category_id != '') {
  66. $where['category_id'] = $category_id;
  67. }
  68. if (isset($skill_cate_id) && $skill_cate_id != '') {
  69. $where['skill_cate_id'] = $skill_cate_id;
  70. }
  71. if (isset($goods_id) && $goods_id != '') {
  72. $where[] = ['exp', Db::raw("FIND_IN_SET('$goods_id',goods_ids)")];
  73. }
  74. if (isset($shop_id) && $shop_id != '') {
  75. $where['shop_id'] = $shop_id;
  76. }
  77. $list = self::where($where)
  78. ->field("id,user_id,name,skill_cate_id,shop_id,user_image,image,is_rest,city,age,(2*$earth*ASIN(SQRT(POW(SIN($pi*(" . $lat . "-lat)/360),2)+COS($pi*" . $lat . "/180)*COS(lat*$pi/180)*POW(SIN($pi*(" . $lng . "-lng)/360),2)))) as distance")
  79. ->order($orderBy)
  80. ->page($get['page'])
  81. ->limit($get['limit'])
  82. ->select();
  83. return $list;
  84. }
  85. public static function getCount($params)
  86. {
  87. $params['state'] = 1;
  88. return self::where($params)->count();
  89. }
  90. public static function skillInfo($id)
  91. {
  92. return self::where(['id' => $id])->field('id,user_id,name,shop_id,sex,age,images,goods_ids,mobile,image,edu,nation,height,weight,exper,lng,lat,is_rest,front_image,certificate_image,health_image')->find();
  93. }
  94. public static function getOrderSkill($skill_id)
  95. {
  96. return self::where(['id' => $skill_id])->field('id,image,name,mobile')->find();
  97. }
  98. public static function getSkillScore($skill_id)
  99. {
  100. $data['average_score'] = Comment::getCommentScore(['skill_id' => $skill_id]);
  101. $data['good_comment_percent'] = Comment::skillGoodPercent(['skill_id' => $skill_id]);
  102. return $data;
  103. }
  104. /**
  105. * 服务人员详情
  106. * @param $id
  107. * @param $uid
  108. * @return array|bool|\PDOStatement|string|Model
  109. * @throws \think\db\exception\DataNotFoundException
  110. * @throws \think\db\exception\ModelNotFoundException
  111. * @throws \think\exception\DbException
  112. */
  113. public static function getInfo($id, $uid)
  114. {
  115. $info = db('service_skill')->where(['id' => $id])->field('id,user_id,name,shop_id,sex,age,image,images,goods_ids,edu,nation,exper,lng,lat,is_rest,front_image,certificate_image,health_image')->find();
  116. if (!$info['id']) {
  117. return false;
  118. }
  119. $info['goodsList'] = Goods::getSkillGoods($info['goods_ids']);
  120. $info['followState'] = $uid ? Follow::getState(['follow_id' => $id, 'user_id' => $uid,'type' => 0]) : 0;
  121. $info['images'] = $info['images']?explode(',', $info['images']):'';
  122. $info['shopname'] = $info['shop_id'] ? Shop::getName($info['shop_id']) : '';
  123. return $info;
  124. }
  125. public static function getSkillInfo($params)
  126. {
  127. $skill = self::where(['id' => $params['id'], 'state' => 1])->field('id,name,shop_id,image,skill_cate_id,age,lng,lat,is_rest')->find();
  128. if (!$skill) {
  129. return false;
  130. }
  131. $skill['skillCate'] = SkillCate::getName($skill['skill_cate_id']);
  132. $skill['average_score'] = Comment::getCommentScore(['skill_id' => $skill['id']]);
  133. $skill['skillTime'] = SkillTime::getSkillTime($skill['id']);
  134. $skill['shopname'] = $skill['shop_id'] ? Shop::getName($skill['shop_id']) : '';
  135. $skill['distance'] = \addons\service\library\Common::distance($params['lng'], $params['lat'], $skill['lng'], $skill['lat']);
  136. $skill['commentCount'] = Comment::getCount(['skill_id' => $skill['id'], 'state' => 1]);
  137. $skill['good_comment_percent'] = Comment::skillGoodPercent(['skill_id' => $skill['id']]);
  138. return $skill;
  139. }
  140. /**
  141. * 获得服务者商户
  142. * @param $uid
  143. * @return array|bool|\PDOStatement|string|Model
  144. * @throws \think\db\exception\DataNotFoundException
  145. * @throws \think\db\exception\ModelNotFoundException
  146. * @throws \think\exception\DbException
  147. */
  148. public static function getSkillShop($uid)
  149. {
  150. $info = self::where(['user_id' => $uid])->field('id,name,shop_id,code')->find();
  151. $info['shopInfo'] = $info['shop_id'] ? Shop::getShop($info['shop_id']) : '';
  152. return $info;
  153. }
  154. public static function getShopSkill($params,$page)
  155. {
  156. $list = Skill::where($params)->field('id,ensure_price,image,percent,name,sex,city,district,address,lng,lat')->order('id desc')->page($page)->limit(10)->select();
  157. foreach ($list as $key=>$value)
  158. {
  159. $list[$key]['orderCount'] = Order::getOrderCount(['skill_id'=>$value['id'],'status'=>['in',[2,3,4]]]);
  160. $list[$key]['orderList'] = Order::getOrder(['skill_id'=>$value['id'],'status'=>['in',[2,3,4]]]);
  161. $list[$key]['average_score'] = Comment::getCommentScore(['skill_id' => $value['id']]);
  162. $list[$key]['good_comment_percent'] = Comment::skillGoodPercent(['skill_id' => $value['id']]);
  163. }
  164. return $list;
  165. }
  166. public static function getShopSkillList($params,$page)
  167. {
  168. $list = Skill::where($params)->field('id,ensure_price,image,percent,name,sex,city,district,address,lng,lat,user_id,is_rest')->order('id desc')->page($page)->limit(10)->select();
  169. foreach ($list as $key=>$value)
  170. {
  171. $list[$key]['skillDetail'] = self::skillShopDetail(['id'=>$value['id'],'shop_id'=>$params['shop_id'],'user_id'=>$value['user_id']]);
  172. }
  173. return $list;
  174. }
  175. public static function skillShopDetail($params)
  176. {
  177. $data = [];
  178. $data['average_score'] = Comment::getCommentScore(['skill_id' => $params['id']]);
  179. $data['shopMoney'] = UserInfo::where(['user_id'=>$params['user_id']])->value('shop_money');
  180. $data['orderCount'] = Order::getOrderCount(['skill_id'=>$params['id'],'shop_id'=>$params['shop_id'],'is_settle'=>2]);
  181. $data['orderPrice'] = Order::getOrderPrice(['skill_id'=>$params['id'],'shop_id'=>$params['shop_id'],'is_settle'=>2],'settle_price');
  182. $data['withdrawPrice'] = Withdraw::where(['user_id'=>$params['user_id'],'shop_id'=>$params['shop_id'],'state'=>2])->sum('num');
  183. $data['notServiceCount'] = Order::getOrderCount(['skill_id'=>$params['id'],'shop_id'=>$params['shop_id'],'status'=>['in',[2,3,4,5]],'is_service'=>['in',[0,-1]]]);
  184. $data['notSettleCount'] = Order::getOrderCount(['skill_id'=>$params['id'],'shop_id'=>$params['shop_id'],'is_settle'=>1]);
  185. return $data;
  186. }
  187. /**
  188. * 检测服务人员可接单状态
  189. * @param $id
  190. * @return array|bool|\PDOStatement|string|Model
  191. * @throws \think\db\exception\DataNotFoundException
  192. * @throws \think\db\exception\ModelNotFoundException
  193. * @throws \think\exception\DbException
  194. */
  195. public static function checkAcceptState($id,$goods_id)
  196. {
  197. $info = self::where(['id'=>$id,'state'=>1,'is_rest'=>0])->field('id,name,goods_ids,accept_nums')->find();
  198. if(!$info)
  199. {
  200. throw new Exception('当前服务者无法接单');
  201. }
  202. if(!in_array($goods_id,explode(',',$info['goods_ids']))){
  203. throw new Exception('当前服务者无此服务项目');
  204. }
  205. return $info;
  206. }
  207. public static function money($money, $user_id, $memo)
  208. {
  209. Db::startTrans();
  210. try {
  211. $skill = self::where('user_id',$user_id)->lock(true)->find();
  212. if ($skill && $money != 0) {
  213. $before = $skill->ensure_price;
  214. $after = function_exists('bcadd') ? bcadd($skill->ensure_price, $money, 2) : $skill->ensure_price + $money;
  215. //更新会员信息
  216. $skill->save(['ensure_price' => $after]);
  217. //写入日志
  218. EnsureLog::create(['user_id' => $user_id, 'money' => $money, 'type'=>0, 'memo' => $memo]);
  219. }
  220. Db::commit();
  221. } catch (\Exception $e) {
  222. Db::rollback();
  223. }
  224. }
  225. }