DriverService.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2017~2021 LARAVEL研发中心
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://www.laravel.cn
  8. // +----------------------------------------------------------------------
  9. // | Author: laravel开发员 <laravel.qq.com>
  10. // +----------------------------------------------------------------------
  11. namespace App\Services\Common;
  12. use App\Helpers\Jwt;
  13. use App\Models\ActionLogModel;
  14. use App\Models\DriverModel;
  15. use App\Models\DriverProfileModel;
  16. use App\Services\BaseService;
  17. use App\Services\RedisService;
  18. use App\Services\SmsService;
  19. use Illuminate\Support\Facades\DB;
  20. use phpQrcode\QRcode;
  21. /**
  22. * 司机管理-服务类
  23. * @author laravel开发员
  24. * @since 2020/11/11
  25. * Class DriverService
  26. * @package App\Services\Api
  27. */
  28. class DriverService extends BaseService
  29. {
  30. // 静态对象
  31. protected static $instance = null;
  32. /**
  33. * 构造函数
  34. * @author laravel开发员
  35. * @since 2020/11/11
  36. * DriverService constructor.
  37. */
  38. public function __construct()
  39. {
  40. $this->model = new DriverModel();
  41. }
  42. /**
  43. * 静态入口
  44. */
  45. public static function make()
  46. {
  47. if (!self::$instance) {
  48. self::$instance = new static();
  49. }
  50. return self::$instance;
  51. }
  52. /**
  53. * @param $params
  54. * @param int $pageSize
  55. * @return array
  56. */
  57. public function getDataList($params, $pageSize = 15)
  58. {
  59. $where = ['a.mark' => 1];
  60. $list = $this->model->with(['carCategory','brand','profile','level'])->from('driver as a')
  61. ->leftJoin('member as b', 'b.id', '=', 'a.user_id')
  62. ->where($where)
  63. ->where(function ($query) use($params){
  64. $keyword = isset($params['keyword'])? $params['keyword'] : '';
  65. if($keyword){
  66. $query->where('a.realname','like',"%{$keyword}%")->orWhere('a.mobile','like',"%{$keyword}%")->orWhere('a.car_number','like',"%{$keyword}%");
  67. }
  68. $status = isset($params['status'])? $params['status'] : 0;
  69. $type = isset($params['type'])? $params['type'] : 0;
  70. if($status && is_array($status)){
  71. $query->whereIn('a.status', $status);
  72. }else if($status){
  73. $query->where('a.status', $status);
  74. }
  75. if($type == 2){
  76. $query->whereIn('a.status', [2,4]);
  77. }
  78. })
  79. ->select(['a.*','b.nickname','b.username'])
  80. ->orderBy('a.create_time','desc')
  81. ->orderBy('a.id','desc')
  82. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  83. $list = $list? $list->toArray() :[];
  84. if($list){
  85. foreach($list['data'] as &$item){
  86. $item['create_time'] = $item['create_time']? datetime($item['create_time'],'Y-m-d H:i:s') : '';
  87. $item['avatar'] = isset($item['avatar']) && $item['avatar']? get_image_url($item['avatar']) : '';
  88. // 认证资料
  89. $item['bank_account'] = ['bank_name'=>'','bank_card'=>'','realname'=>''];
  90. if(isset($item['profile'])){
  91. $item['profile'] = $item['profile']? $item['profile'] : [];
  92. if($item['profile']){
  93. $item['profile']['alipay_qrcode'] = $item['profile']['alipay_qrcode']? get_image_url($item['profile']['alipay_qrcode']) : '';
  94. $item['profile']['wxpay_qrcode'] = $item['profile']['wxpay_qrcode']? get_image_url($item['profile']['wxpay_qrcode']) : '';
  95. $item['bank_account'] = $item['profile']['bank_account']? json_decode($item['profile']['bank_account']) : ['bank_name'=>'','bank_card'=>'','realname'=>''];
  96. $item['profile']['idcard_img'] = $item['profile']['idcard_img']? get_image_url($item['profile']['idcard_img']) : '';
  97. $item['profile']['driver_license'] = $item['profile']['driver_license']? get_image_url($item['profile']['driver_license']) : '';
  98. $item['profile']['driving_license'] = $item['profile']['driving_license']? get_image_url($item['profile']['driving_license']) : '';
  99. $item['profile']['qualification_certificate'] = $item['profile']['qualification_certificate']? get_image_url($item['profile']['qualification_certificate']) : '';
  100. $item['profile']['transport_certificate'] = $item['profile']['transport_certificate']? get_image_url($item['profile']['transport_certificate']) : '';
  101. $item['profile']['no_crime_certificate'] = $item['profile']['no_crime_certificate']? get_image_url($item['profile']['no_crime_certificate']) : '';
  102. }
  103. }
  104. $item['level_name'] = isset($item['level']) && $item['level']? $item['level']['name'] : '';
  105. $item['level'] = isset($item['level']) && $item['level']? $item['level']['id'] : 0;
  106. $vipExpired = isset($item['vip_expired'])? $item['vip_expired'] : '';
  107. if($vipExpired && date('Y-m-d H:i:s')>$vipExpired){
  108. $item['level'] = 1;
  109. $item['level_name'] = '普通会员';
  110. }
  111. }
  112. }
  113. return [
  114. 'pageSize'=> $pageSize,
  115. 'total'=>isset($list['total'])? $list['total'] : 0,
  116. 'list'=> isset($list['data'])? $list['data'] : []
  117. ];
  118. }
  119. /**
  120. * 获取资料详情
  121. * @param $where
  122. * @param array $field
  123. */
  124. public function getInfo($where, $type = 'info', array $field = [])
  125. {
  126. $defaultField = ['id','user_id', 'realname', 'mobile','car_number', 'balance','car_cate_id','car_brand_id','vip_expired','level','is_auth','order_count','online_time','receive_order','lock_type','driver_type','is_online', 'status', 'avatar'];
  127. $field = $field ? $field : $defaultField;
  128. if (is_array($where)) {
  129. $info = $this->model->with(['level','profile'])->where($where)->select($field)->first();
  130. } else {
  131. $info = $this->model->with(['level','profile'])->where(['id' => (int)$where])->select($field)->first();
  132. }
  133. $info = $info ? $info->toArray() : [];
  134. if ($info) {
  135. if(isset($info['avatar'])){
  136. $info['avatar'] = $info['avatar'] ? get_image_url($info['avatar']) : '';
  137. }
  138. if(isset($info['balance'])){
  139. $info['balance'] = moneyFormat($info['balance'], 2);
  140. }
  141. if(isset($info['tz_score'])){
  142. $info['tz_score'] = moneyFormat($info['tz_score'], 2);
  143. }
  144. if(isset($info['mobile'])){
  145. $info['mobile'] = $info['mobile']? $info['mobile'] : '';
  146. $info['mobile_text'] = $info['mobile']? format_mobile($info['mobile']) : '';
  147. }
  148. if(isset($info['level'])){
  149. $info['level'] = $info['level']? $info['level'] : [];
  150. }
  151. // 认证资料
  152. $info['bank_account'] = [];
  153. if(isset($info['profile'])){
  154. $info['profile'] = $info['profile']? $info['profile'] : [];
  155. if($info['profile']){
  156. $info['profile']['idcard_img'] = $info['profile']['idcard_img']? get_image_url($info['profile']['idcard_img']) : '';
  157. $info['profile']['driver_license'] = $info['profile']['driver_license']? get_image_url($info['profile']['driver_license']) : '';
  158. $info['profile']['driving_license'] = $info['profile']['driving_license']? get_image_url($info['profile']['driving_license']) : '';
  159. $info['profile']['qualification_certificate'] = $info['profile']['qualification_certificate']? get_image_url($info['profile']['qualification_certificate']) : '';
  160. $info['profile']['transport_certificate'] = $info['profile']['transport_certificate']? get_image_url($info['profile']['transport_certificate']) : '';
  161. $info['profile']['no_crime_certificate'] = $info['profile']['no_crime_certificate']? get_image_url($info['profile']['no_crime_certificate']) : '';
  162. }
  163. }
  164. }
  165. return $info;
  166. }
  167. /**
  168. * 按日期统计注册司机数
  169. * @param string $beginAt 开始时间
  170. * @param string $endAt 结束时间
  171. * @param int[] $status 状态:数组或数值
  172. * @return mixed
  173. */
  174. public function getRegisterCount($beginAt='', $endAt='', $status=[2,4])
  175. {
  176. $where = ['mark' => 1];
  177. return $this->model->where($where)->where(function($query) use($beginAt,$endAt,$status){
  178. if($beginAt && $endAt){
  179. $query->whereBetween('create_time', [strtotime($beginAt), strtotime($endAt)]);
  180. }else if($beginAt){
  181. $query->where('create_time','>=', strtotime($beginAt));
  182. }
  183. if($status && is_array($status)){
  184. $query->whereIn('status',$status);
  185. }else if($status){
  186. $query->where('status',$status);
  187. }
  188. })->count('id');
  189. }
  190. /**
  191. * 添加会编辑会员
  192. * @return array
  193. * @since 2020/11/11
  194. * @author laravel开发员
  195. */
  196. public function edit()
  197. {
  198. // 请求参数
  199. $data = request()->all();
  200. // 头像处理
  201. $avatar = trim($data['avatar']);
  202. if ($avatar) {
  203. $data['avatar'] = get_image_path($avatar);
  204. }
  205. $profile = isset($data['profile'])? $data['profile'] : [];
  206. if($profile){
  207. if(isset($profile['idcard_img'])){
  208. $profile['idcard_img'] = get_image_path($profile['idcard_img']);
  209. }
  210. if(isset($profile['wxpay_qrcode'])){
  211. $profile['wxpay_qrcode'] = get_image_path($profile['wxpay_qrcode']);
  212. }
  213. if(isset($profile['alipay_qrcode'])){
  214. $profile['alipay_qrcode'] = get_image_path($profile['alipay_qrcode']);
  215. }
  216. if(isset($data['bank_account'])){
  217. $profile['bank_account'] = $data['bank_account']? json_encode($data['bank_account'], 256) : '';
  218. }
  219. if(isset($profile['driver_license'])){
  220. $profile['driver_license'] = get_image_path($profile['driver_license']);
  221. }
  222. if(isset($profile['driving_license'])){
  223. $profile['driving_license'] = get_image_path($profile['driving_license']);
  224. }
  225. if(!empty($profile['qualification_certificate'])){
  226. $profile['qualification_certificate'] = get_image_path($profile['qualification_certificate']);
  227. }
  228. if(!empty($profile['transport_certificate'])){
  229. $profile['transport_certificate'] = get_image_path($profile['transport_certificate']);
  230. }
  231. if(!empty($profile['no_crime_certificate'])){
  232. $profile['no_crime_certificate'] = get_image_path($profile['no_crime_certificate']);
  233. }
  234. }
  235. // 手机号唯一
  236. $id = isset($data['id'])? $data['id'] : 0;
  237. $mobile = isset($data['mobile'])? trim($data['mobile']) : '';
  238. $checkId = $this->model->where(['mobile'=>$mobile,'mark'=>1])->value('id');
  239. if($checkId && ($id != $checkId)){
  240. return message('手机号已存在',false);
  241. }
  242. unset($data['profile']);
  243. DB::beginTransaction();
  244. $result = parent::edit($data); // TODO: Change the autogenerated stub
  245. $success = isset($result['success'])? $result['success'] : '';
  246. $id = isset($result['data']['id'])? $result['data']['id'] : 0;
  247. if($success != true || !$id){
  248. DB::rollBack();
  249. return message('保存失败',false);
  250. }
  251. $profile['driver_id'] = $id;
  252. if(DriverProfileModel::where(['driver_id'=> $id])->value('id')){
  253. if (!DriverProfileModel::where(['driver_id' => $id])->update($profile)) {
  254. DB::rollBack();
  255. return message('保存失败',false);
  256. }
  257. }else{
  258. if(!DriverProfileModel::insertgetId($profile)){
  259. DB::rollBack();
  260. return message('保存失败',false);
  261. }
  262. }
  263. // 设置日志标题
  264. ActionLogModel::setTitle("修改或审核司机信息");
  265. ActionLogModel::record();
  266. DB::commit();
  267. return $result;
  268. }
  269. /**
  270. * 选项
  271. * @return array
  272. */
  273. public function options()
  274. {
  275. // 获取参数
  276. $param = request()->all();
  277. // 用户ID
  278. $keyword = getter($param, "keyword");
  279. $userId = getter($param, "driver_id");
  280. $datas = $this->model->where(['status'=> 2,'mark'=>1])
  281. ->where(function($query) use($userId){
  282. if($userId){
  283. $query->whereNotIn('id', [$userId]);
  284. }
  285. })
  286. ->where(function($query) use($keyword){
  287. if($keyword){
  288. $query->where('realname','like',"%{$keyword}%")->orWhere('mobile','like',"%{$keyword}%");
  289. }
  290. })
  291. ->select(['id','mobile','realname','car_number','status'])
  292. ->get();
  293. return $datas? $datas->toArray() : [];
  294. }
  295. /**
  296. * 删除
  297. * @return array
  298. */
  299. public function delete()
  300. {
  301. // 设置日志标题
  302. ActionLogModel::setTitle("删除司机信息");
  303. ActionLogModel::record();
  304. $this->model->where('mark',0)->where('update_time','<=', time() - 15*86400)->delete();
  305. return parent::delete(); // TODO: Change the autogenerated stub
  306. }
  307. }