JsOrderController.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Administrator
  5. * Date: 2020-09-11
  6. * Time: 11:17
  7. */
  8. namespace app\back\controller;
  9. use app\back\model\AgencyModel;
  10. use app\back\model\RegionModel;
  11. use app\back\model\JsModel;
  12. use app\back\model\JsOrderModel;
  13. use app\back\model\UserModel;
  14. use Cassandra\Map;
  15. use cmf\controller\AdminBaseController;
  16. use app\back\model\JsProjectModel;
  17. use JPush\Client;
  18. use think\Db;
  19. class JsOrderController extends AdminBaseController
  20. {
  21. /**
  22. * @return mixed
  23. * 实时订单
  24. */
  25. public function index()
  26. {
  27. $id = cmf_get_current_admin_id();
  28. $admin = UserModel::find($id);
  29. $userObject = UserModel::where('mobile', $admin->user_login)->where('user_type', 2)->select();
  30. $userArray = $userObject->toArray();
  31. if (count($userArray)) {
  32. $user = $userObject[0];
  33. }
  34. $agency_id = 0;
  35. if (isset($user->agency_id) && !empty($user->agency_id)) {
  36. $agency_id = (int)$user->agency_id;
  37. }
  38. $where = [];
  39. //如果有 代理 id 表示 是有区县代理的
  40. if ($agency_id) {
  41. $agency = AgencyModel::find($agency_id);
  42. //需要判断 renk_id 是 1 还是 2 1级代理是地级市 2级代理是区县级
  43. if ($agency->renk_id === 1) {
  44. $region = RegionModel::where('id', $agency->city_id)->select()[0];
  45. $where['city'] = $region->cityName;
  46. } else {
  47. $region = RegionModel::where('id', $agency->district_id)->select()[0];
  48. $where['district'] = $region->cityName;
  49. }
  50. }
  51. //$where = [];
  52. $pw = [];
  53. $where['type'] = 1;
  54. $page = $this->request->param('page', 1);
  55. $JsOrderModel = new JsOrderModel();
  56. $district = $this->request->param('district');
  57. if ($district) {
  58. $this->assign('district', $district);
  59. }
  60. $project_id = $this->request->param('project_id');
  61. $keyword = $this->request->param('keyword');
  62. if ($keyword) {
  63. $user_ids = UserModel::where('user_nickname|mobile', 'like', '%' . $keyword . '%')->field('id')->select();
  64. $user_new_ids = [];
  65. foreach ($user_ids as $vo) {
  66. $user_new_ids[] = $vo['id'];
  67. }
  68. $where['user_id'] = $user_new_ids;
  69. $this->assign('keyword', $keyword);
  70. }
  71. if ($project_id) {
  72. $where['project_id'] = $project_id;
  73. $pw['project_id'] = $project_id;
  74. $this->assign('project_id', $project_id);
  75. }
  76. $order_sn = $this->request->param('order_sn');
  77. if ($order_sn) {
  78. $this->assign('order_sn', $order_sn);
  79. $pw['order_sn'] = $order_sn;
  80. }
  81. $status = $this->request->param('status', '');
  82. if ($status !== '') {
  83. $where['status'] = $status;
  84. $pw['status'] = $status;
  85. $this->assign('status', $status);
  86. }
  87. $list = $JsOrderModel->getList($where, $page, 'index', $pw, $order_sn, $district);
  88. if ($agency_id) {
  89. foreach ($list as $k => $v) {
  90. $mobile = $v->mobile;
  91. if ($mobile) {
  92. $startStr = substr($mobile, 0, 3);
  93. $endStr = substr($mobile, -3);
  94. $list[$k]->mobile = $startStr . '*****' . $endStr;
  95. }
  96. $xiadan_user = $v->xiadan_user;
  97. if ($xiadan_user['mobile']) {
  98. $xiadan_user_mobile = $xiadan_user['mobile'];
  99. $startStr = substr($xiadan_user_mobile, 0, 3);
  100. $endStr = substr($xiadan_user_mobile, -3);
  101. $list[$k]->xiadan_user['mobile'] = $startStr . '*****' . $endStr;
  102. }
  103. $js = $v->js;
  104. if ($js['phone']) {
  105. $js_phone = $js['phone'];
  106. $startStr = substr($js_phone, 0, 3);
  107. $endStr = substr($js_phone, -3);
  108. $list[$k]->js['phone'] = $startStr . '*****' . $endStr;
  109. }
  110. }
  111. }
  112. // dump($list->toArray());
  113. $JsProjectModel = new JsProjectModel();
  114. $js_project_list = $JsProjectModel::field('id,name')->select();
  115. // dump($js_project_list->toArray());
  116. $this->assign('js_project_list', $js_project_list);
  117. $this->assign('list', $list);
  118. $this->assign('page', $list->render());
  119. return $this->fetch();
  120. }
  121. /**
  122. * @return mixed
  123. * 预约订单
  124. */
  125. public function make()
  126. {
  127. $id = cmf_get_current_admin_id();
  128. $admin = UserModel::find($id);
  129. $userObject = UserModel::where('mobile', $admin->user_login)->where('user_type', 2)->select();
  130. $userArray = $userObject->toArray();
  131. if (count($userArray)) {
  132. $user = $userObject[0];
  133. }
  134. $agency_id = 0;
  135. if (isset($user->agency_id) && !empty($user->agency_id)) {
  136. $agency_id = (int)$user->agency_id;
  137. }
  138. $where = [];
  139. //如果有 代理 id 表示 是有区县代理的
  140. if ($agency_id) {
  141. $agency = AgencyModel::find($agency_id);
  142. //需要判断 renk_id 是 1 还是 2 1级代理是地级市 2级代理是区县级
  143. if ($agency->renk_id === 1) {
  144. $region = RegionModel::where('id', $agency->city_id)->select()[0];
  145. $where['city'] = $region->cityName;
  146. } else {
  147. $region = RegionModel::where('id', $agency->district_id)->select()[0];
  148. $where['district'] = $region->cityName;
  149. }
  150. }
  151. //$where = [];
  152. $pw = [];
  153. $where['type'] = 2;
  154. $page = $this->request->param('page', 1);
  155. $JsOrderModel = new JsOrderModel();
  156. $project_id = $this->request->param('project_id');
  157. $keyword = $this->request->param('keyword');
  158. if ($keyword) {
  159. $user_ids = UserModel::where('user_nickname|mobile', 'like', '%' . $keyword . '%')->field('id')->select();
  160. $user_new_ids = [];
  161. foreach ($user_ids as $vo) {
  162. $user_new_ids[] = $vo['id'];
  163. }
  164. $where['user_id'] = $user_new_ids;
  165. $this->assign('keyword', $keyword);
  166. }
  167. if ($project_id) {
  168. $where['project_id'] = $project_id;
  169. $pw['project_id'] = $project_id;
  170. $this->assign('project_id', $project_id);
  171. }
  172. $order_sn = $this->request->param('order_sn');
  173. if ($order_sn) {
  174. $this->assign('order_sn', $order_sn);
  175. $pw['order_sn'] = $order_sn;
  176. }
  177. $status = $this->request->param('status', '');
  178. if ($status !== '') {
  179. $where['status'] = $status;
  180. $pw['status'] = $status;
  181. $this->assign('status', $status);
  182. }
  183. $district = $this->request->param('district');
  184. if ($district) {
  185. $this->assign('district', $district);
  186. }
  187. $list = $JsOrderModel->getList($where, $page, 'index', $pw, $order_sn, $district);
  188. if ($agency_id) {
  189. foreach ($list as $k => $v) {
  190. $mobile = $v->mobile;
  191. if ($mobile) {
  192. $startStr = substr($mobile, 0, 3);
  193. $endStr = substr($mobile, -3);
  194. $list[$k]->mobile = $startStr . '*****' . $endStr;
  195. }
  196. $xiadan_user = $v->xiadan_user;
  197. if ($xiadan_user['mobile']) {
  198. $xiadan_user_mobile = $xiadan_user['mobile'];
  199. $startStr = substr($xiadan_user_mobile, 0, 3);
  200. $endStr = substr($xiadan_user_mobile, -3);
  201. $list[$k]->xiadan_user['mobile'] = $startStr . '*****' . $endStr;
  202. }
  203. $js = $v->js;
  204. if ($js['phone']) {
  205. $js_phone = $js['phone'];
  206. $startStr = substr($js_phone, 0, 3);
  207. $endStr = substr($js_phone, -3);
  208. $list[$k]->js['phone'] = $startStr . '*****' . $endStr;
  209. }
  210. }
  211. }
  212. $JsProjectModel = new JsProjectModel();
  213. $js_project_list = $JsProjectModel::field('id,name')->select();
  214. $this->assign('js_project_list', $js_project_list);
  215. $this->assign('list', $list);
  216. $this->assign('page', $list->render());
  217. return $this->fetch();
  218. }
  219. public function delete()
  220. {
  221. $id = $this->request->param('id');
  222. JsOrderModel::destroy($id);
  223. $this->success('删除成功');
  224. }
  225. /**
  226. * @throws \think\Exception
  227. * @throws \think\db\exception\DataNotFoundException
  228. * @throws \think\db\exception\ModelNotFoundException
  229. * @throws \think\exception\DbException
  230. * @throws \think\exception\PDOException
  231. * 后台完成订单服务
  232. */
  233. public function finish()
  234. {
  235. $ids = $this->request->param('ids/a');
  236. $JsOrderModel = new JsOrderModel();
  237. JsOrderModel::where('id', 'in', $ids)->update(['finish_time' => time(), 'status' => 3]);
  238. $list = $JsOrderModel::where('id', 'in', $ids)->field('id,user_id,jiedan_js_id,order_price')->select();
  239. $info = cmf_get_option('jspus');
  240. foreach ($list as $vo) {
  241. $url = '/pages/ddxq/ddxq?id=' . $vo['id'];
  242. $jsuserid = $js_user_id = Db::name('js')->where('id', $vo['jiedan_js_id'])->value('user_id');
  243. JsModel::where('id', $vo['jiedan_js_id'])->setInc('jiedan_num', 1);
  244. if ($vo['order_price'] > 0) {
  245. UserModel::where('id', $jsuserid)->setInc('balance', $vo['order_price']);
  246. $log['user_id'] = $jsuserid;
  247. $log['money'] = $vo['order_price'];
  248. $log['type'] = 8;
  249. $log['remark'] = '完成服务获得金额';
  250. $log['add_time'] = time();
  251. $log['obj_id'] = $vo['id'];
  252. if ($log['money'] != 0) {
  253. Db::name('user_money_log')->insert($log);
  254. }
  255. }
  256. $js_user_id .= 'js';
  257. $js_client = new Client($info['js_app_key'], $info['js_master_secret']);
  258. $client = new Client($info['app_key'], $info['master_secret']);
  259. $extras = [
  260. 'id' => $vo['id'],
  261. 'flag' => '用户',
  262. 'url' => $url
  263. ];
  264. $array = [
  265. 'extras' => $extras,
  266. ];
  267. $text = '服务已被后台指定结束';
  268. $platform = array('ios', 'android');
  269. $user_id = $vo['user_id'] . 'js';
  270. $client->push()
  271. ->setPlatform($platform)
  272. ->setNotificationAlert($text)
  273. ->iosNotification($text, $array)
  274. ->androidNotification($text, $array)
  275. ->addAlias("{$user_id}")
  276. ->send();
  277. $extras = [
  278. 'id' => $vo['id'],
  279. 'flag' => '技师端',
  280. 'url' => $url
  281. ];
  282. $array = [
  283. 'extras' => $extras,
  284. ];
  285. $text = '服务已被后台指定结束';
  286. $js_client->push()
  287. ->setPlatform($platform)
  288. ->setNotificationAlert($text)
  289. ->iosNotification($text, $array)
  290. ->androidNotification($text, $array)
  291. ->addAlias("{$js_user_id}")
  292. ->send();
  293. $this->fenxiao($vo['id'], 1);
  294. $this->fenxiao($vo['id'], 2);
  295. }
  296. $this->success('操作成功');
  297. }
  298. public function getJsList($lat = 1, $lng = 1)
  299. {
  300. $url = 'http://116.255.226.95:82/api/js/getList?lng=' . $lng . '&lat=' . $lat;
  301. $this->assign('list', []);
  302. return $this->fetch('jslist');
  303. }
  304. public function qdlist()
  305. {
  306. $id = $this->request->param('id');
  307. $list = Db::name('js_order_grab')->where('order_id=' . $id)->order('id asc')->select()->toArray();
  308. if (!empty($list) && is_array($list)) {
  309. foreach ($list as $key => $val) {
  310. $js_info = JsModel::get($val['js_id'])->toArray();
  311. $list[$key]['js_name'] = $js_info['name'];
  312. $list[$key]['js_mobile'] = $js_info['phone'];
  313. }
  314. }
  315. $this->assign('list', $list);
  316. return $this->fetch();
  317. }
  318. public function fenxiao($order_id, $type)
  319. {
  320. $orderInfo = JsOrderModel::get($order_id);
  321. $userModel = new UserModel();
  322. if ($type == 1) {
  323. $log['remark'] = '好友下单';
  324. $price = $orderInfo['price'];
  325. $user_info = UserModel::get($orderInfo['user_id']);
  326. $agency_info = AgencyModel::where(['district_id' => $user_info['district_id'], 'renk_id' => 2])->find();
  327. $user_agency_id = Db::name('user')->where(function ($query) use ($agency_info){
  328. $query->where('agency_id',$agency_info['id']);
  329. $query->whereOr('agency_id','like',$agency_info['id'].',%');
  330. $query->whereOr('agency_id','like','%,'.$agency_info['id']);
  331. $query->whereOr('agency_id','like','%,'.$agency_info['id'].',%');
  332. })->value('id');
  333. $money = round($price * $agency_info['ratio'] / 100, 2);
  334. $user_id = $orderInfo['user_id'];
  335. if ($money > 0 && $user_agency_id) {
  336. UserModel::where('id', $user_agency_id)->setInc('balance', $money);
  337. $log['user_id'] = $user_agency_id;
  338. $log['money'] = $money;
  339. $log['type'] = 5;
  340. $log['add_time'] = time();
  341. $log['obj_id'] = $order_id;
  342. $log['is_type'] = $type;
  343. $log['xia_user_id'] = $user_info['id'];
  344. $log['remark'] = '用户下单,区代提成';
  345. Db::name('user_money_log')->insert($log);
  346. }
  347. $user_parent_id = $userModel::get($user_id)['parent_id'];
  348. $user_parent_id1 = $userModel::get($user_id)['parent_id1'];
  349. if ($user_parent_id) {
  350. $user_parent_info = UserModel::get($user_parent_id);
  351. $distributorInfo = Db::name('distributor')->where('id', $user_parent_info['distributor_id'])->find();
  352. $money = $price * $distributorInfo['yi_ratio'] / 100;
  353. if ($money > 0) {
  354. UserModel::where('id', $user_parent_id)->setInc('balance', $money);
  355. $log['user_id'] = $user_parent_id;
  356. $log['money'] = $money;
  357. $log['type'] = 3;
  358. $log['add_time'] = time();
  359. $log['obj_id'] = $order_id;
  360. $log['is_type'] = $type;
  361. $log['xia_user_id'] = $user_id;
  362. $log['remark'] = '一级好友下单提成';
  363. Db::name('user_money_log')->insert($log);
  364. }
  365. }
  366. if ($user_parent_id1) {
  367. $user_parent_info1 = UserModel::get($user_parent_id1);
  368. $distributorInfo1 = Db::name('distributor')->where('id', $user_parent_info1['distributor_id'])->find();
  369. $money = $price * $distributorInfo1['er_ratio'] / 100;
  370. if ($money > 0) {
  371. UserModel::where('id', $user_parent_id1)->setInc('balance', $money);
  372. $log['user_id'] = $user_parent_id1;
  373. $log['money'] = $money;
  374. $log['type'] = 4;
  375. $log['add_time'] = time();
  376. $log['obj_id'] = $order_id;
  377. $log['xia_user_id'] = $user_id;
  378. $log['is_type'] = $type;
  379. $log['remark'] = '二级好友下单提成';
  380. Db::name('user_money_log')->insert($log);
  381. }
  382. }
  383. } else {
  384. if (!$orderInfo['jiedan_js_id']) {
  385. return false;
  386. }
  387. $user_id = Db::name('js')->where('id', $orderInfo['jiedan_js_id'])->value('user_id');
  388. $log['remark'] = '好友接单';
  389. $price = $orderInfo['order_price'];
  390. $user_parent_id = $userModel::get($user_id)['parent_id'];
  391. $user_parent_id1 = $userModel::get($user_id)['parent_id1'];
  392. // if($user_parent_id){
  393. // $user_parent_info = UserModel::get($user_parent_id);
  394. // $distributorInfo = Db::name('distributor')->where('id',$user_parent_info['distributor_id'])->find();
  395. // $money = $price * $distributorInfo['yi_ratio']/100;
  396. // if($money > 0){
  397. // $log['user_id'] = $user_parent_id;
  398. // $log['money'] = $money;
  399. // $log['type'] = 3;
  400. // $log['add_time'] = time();
  401. // $log['obj_id'] = $order_id;
  402. // $log['is_type'] = $type;
  403. // $log['xia_user_id'] = $user_id;
  404. // Db::name('user_money_log')->insert($log);
  405. // }
  406. // }
  407. // if($user_parent_id1){
  408. // $user_parent_info1 = UserModel::get($user_parent_id1);
  409. // $distributorInfo1 = Db::name('distributor')->where('id',$user_parent_info1['distributor_id'])->find();
  410. // $money = $price * $distributorInfo1['er_ratio']/100;
  411. // if($money > 0){
  412. // $log['user_id'] = $user_parent_id1;
  413. // $log['money'] = $money;
  414. // $log['type'] = 4;
  415. // $log['add_time'] = time();
  416. // $log['obj_id'] = $order_id;
  417. // $log['xia_user_id'] = $user_id;
  418. // $log['is_type'] = $type;
  419. // Db::name('user_money_log')->insert($log);
  420. // }
  421. // }
  422. }
  423. }
  424. }