MobileOrder.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  1. <?php
  2. namespace app\admin\controller;
  3. use app\admin\model\Admin;
  4. use app\admin\model\MobileOrderAdmin;
  5. use app\common\controller\Backend;
  6. use app\common\model\LogisticsCompany;
  7. use app\common\model\MobileOrderOperation;
  8. use app\common\model\MobileOrderRules;
  9. use app\common\service\MobileOrderExport;
  10. use app\common\service\Refund;
  11. use app\common\service\SmsSend;
  12. use app\common\service\TransferCheck;
  13. use app\common\validate\RefundValidate;
  14. use think\Db;
  15. use think\db\Query;
  16. use think\Loader;
  17. /**
  18. *
  19. *
  20. * @icon fa fa-circle-o
  21. */
  22. class MobileOrder extends Backend
  23. {
  24. protected $noNeedRight=['status','pay_type'];
  25. /**
  26. * MobileOrder模型对象
  27. * @var \app\admin\model\MobileOrder
  28. */
  29. protected $model = null;
  30. public function _initialize()
  31. {
  32. parent::_initialize();
  33. $this->model = new \app\admin\model\MobileOrder;
  34. $this->assign('status',\app\common\model\MobileOrder::$status);
  35. $this->assign('logistics_list',json_encode(LogisticsCompany::all()));
  36. }
  37. public function import()
  38. {
  39. $this->error('not support');
  40. }
  41. /**
  42. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  43. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  44. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  45. */
  46. /**
  47. * 查看
  48. */
  49. public function index()
  50. {
  51. //当前是否为关联查询
  52. $this->relationSearch = false;
  53. //设置过滤方法
  54. $this->request->filter(['strip_tags', 'trim']);
  55. $map=[];
  56. if($this->admin('is_sub')){
  57. $map['s_id']=$this->admin('id');
  58. }
  59. if ($this->request->isAjax()) {
  60. //如果发送的来源是Selectpage,则转发到Selectpage
  61. if ($this->request->request('keyField')) {
  62. return $this->selectpage();
  63. }
  64. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  65. if($this->admin('is_manager')){
  66. $relation=['info','operation','operation.admin','admin','admin.admin','refund_log','refund_log.refunduser','sub_remark','sub_remark.admin','anchor'];
  67. }else{
  68. $relation=['info','admin','admin.admin','refund_log','refund_log.refunduser','sub_remark'=>function(Query $query){
  69. $query->where('sub_admin_id',$this->auth->id);
  70. },'sub_remark.admin','anchor','sub_anchor'=>function(Query $query){
  71. $query->where('sub_admin_id',$this->auth->id);
  72. },'sub_anchor.mobileAnchor'];
  73. }
  74. $list = $this->model
  75. ->with($relation)
  76. ->where($where)
  77. ->where($map)
  78. ->where('type',1)
  79. ->order($sort, $order)
  80. ->paginate($limit);
  81. foreach ($list as $row) {
  82. $row['pay_link']=h5_link("oder-detail/{$row['id']}");
  83. $row->append(['phone_order_count']);
  84. }
  85. $extend=[];
  86. $extend['total']=$this->model->where($where)->where($map)->where('type',1)->sum('amount');
  87. $extend['alipay']=$this->model->where($where)->where($map)->where('type',1)->where('pay_type',1)->sum('amount');
  88. $extend['wechat']=$this->model->where($where)->where($map)->where('type',1)->where('pay_type',2)->sum('amount');
  89. $extend['jd']=$this->model->where($where)->where($map)->where('type',1)->where('pay_type',3)->sum('amount');
  90. $extend['di']=$this->model->where($where)->where($map)->where('type',1)->sum('amount_di');
  91. $extend['profit']=$this->model->where($where)->where($map)->where('type',1)->sum('amount_profit');
  92. $extend['refund']=$this->model->where($where)->where($map)->where('type',1)->sum('amount_refund');
  93. $result = array("total" => $list->total(), "rows" => $list->items(),'extend'=>$extend);
  94. $result['link']=$this->request->url().'&export=1';
  95. return json($result);
  96. }
  97. $export=input('export');
  98. if($export){
  99. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  100. $list = $this->model
  101. ->with(['info','operation','operation.admin','admin','admin.admin'])
  102. ->where($where)
  103. ->where($map)
  104. ->where('type',1)
  105. ->order($sort, $order)
  106. ->paginate($limit);
  107. return MobileOrderExport::export($list,$this->admin(),'admin');
  108. }
  109. $this->assign('admins',Admin::getAdmins());
  110. return $this->view->fetch();
  111. }
  112. protected function buildparams($searchfields = null, $relationSearch = null)
  113. {
  114. $searchfields = is_null($searchfields) ? $this->searchFields : $searchfields;
  115. $relationSearch = is_null($relationSearch) ? $this->relationSearch : $relationSearch;
  116. $search = $this->request->get("search", '');
  117. $filter = $this->request->get("filter", '');
  118. $op = $this->request->get("op", '', 'trim');
  119. $sort = $this->request->get("sort", !empty($this->model) && $this->model->getPk() ? $this->model->getPk() : 'id');
  120. $order = $this->request->get("order", "DESC");
  121. $offset = $this->request->get("offset/d", 0);
  122. $limit = $this->request->get("limit/d", 999999);
  123. //新增自动计算页码
  124. $page = $limit ? intval($offset / $limit) + 1 : 1;
  125. if ($this->request->has("page")) {
  126. $page = $this->request->get("page/d", 1);
  127. }
  128. $this->request->get([config('paginate.var_page') => $page]);
  129. $filter = (array)json_decode($filter, true);
  130. $op = (array)json_decode($op, true);
  131. $filter = $filter ? $filter : [];
  132. $where = [];
  133. $alias = [];
  134. $bind = [];
  135. $name = '';
  136. $aliasName = '';
  137. if (!empty($this->model) && $this->relationSearch) {
  138. $name = $this->model->getTable();
  139. $alias[$name] = Loader::parseName(basename(str_replace('\\', '/', get_class($this->model))));
  140. $aliasName = $alias[$name] . '.';
  141. }
  142. $sortArr = explode(',', $sort);
  143. foreach ($sortArr as $index => & $item) {
  144. $item = stripos($item, ".") === false ? $aliasName . trim($item) : $item;
  145. }
  146. unset($item);
  147. $sort = implode(',', $sortArr);
  148. $adminIds = $this->getDataLimitAdminIds();
  149. if (is_array($adminIds)) {
  150. $where[] = [$aliasName . $this->dataLimitField, 'in', $adminIds];
  151. }
  152. if ($search) {
  153. $searcharr = is_array($searchfields) ? $searchfields : explode(',', $searchfields);
  154. foreach ($searcharr as $k => &$v) {
  155. $v = stripos($v, ".") === false ? $aliasName . $v : $v;
  156. }
  157. unset($v);
  158. $where[] = [implode("|", $searcharr), "LIKE", "%{$search}%"];
  159. }
  160. $index = 0;
  161. foreach ($filter as $k => $v) {
  162. if (!preg_match('/^[a-zA-Z0-9_\-\.]+$/', $k)) {
  163. continue;
  164. }
  165. $sym = isset($op[$k]) ? $op[$k] : '=';
  166. if (stripos($k, ".") === false) {
  167. $k = $aliasName . $k;
  168. }
  169. $v = !is_array($v) ? trim($v) : $v;
  170. $sym = strtoupper(isset($op[$k]) ? $op[$k] : $sym);
  171. //null和空字符串特殊处理
  172. if (!is_array($v)) {
  173. if (in_array(strtoupper($v), ['NULL', 'NOT NULL'])) {
  174. $sym = strtoupper($v);
  175. }
  176. if (in_array($v, ['""', "''"])) {
  177. $v = '';
  178. $sym = '=';
  179. }
  180. }
  181. switch ($sym) {
  182. case '=':
  183. case '<>':
  184. $where[] = [$k, $sym, (string)$v];
  185. break;
  186. case 'LIKE':
  187. case 'NOT LIKE':
  188. case 'LIKE %...%':
  189. case 'NOT LIKE %...%':
  190. $where[] = [$k, trim(str_replace('%...%', '', $sym)), "%{$v}%"];
  191. break;
  192. case '>':
  193. case '>=':
  194. case '<':
  195. case '<=':
  196. $where[] = [$k, $sym, intval($v)];
  197. break;
  198. case 'FINDIN':
  199. case 'FINDINSET':
  200. case 'FIND_IN_SET':
  201. $v = is_array($v) ? $v : explode(',', str_replace(' ', ',', $v));
  202. $findArr = array_values($v);
  203. foreach ($findArr as $idx => $item) {
  204. $bindName = "item_" . $index . "_" . $idx;
  205. $bind[$bindName] = $item;
  206. $where[] = "FIND_IN_SET(:{$bindName}, `" . str_replace('.', '`.`', $k) . "`)";
  207. }
  208. break;
  209. case 'IN':
  210. case 'IN(...)':
  211. case 'NOT IN':
  212. case 'NOT IN(...)':
  213. $where[] = [$k, str_replace('(...)', '', $sym), is_array($v) ? $v : explode(',', $v)];
  214. break;
  215. case 'BETWEEN':
  216. case 'NOT BETWEEN':
  217. $arr = array_slice(explode(',', $v), 0, 2);
  218. if (stripos($v, ',') === false || !array_filter($arr)) {
  219. continue 2;
  220. }
  221. //当出现一边为空时改变操作符
  222. if ($arr[0] === '') {
  223. $sym = $sym == 'BETWEEN' ? '<=' : '>';
  224. $arr = $arr[1];
  225. } elseif ($arr[1] === '') {
  226. $sym = $sym == 'BETWEEN' ? '>=' : '<';
  227. $arr = $arr[0];
  228. }
  229. $where[] = [$k, $sym, $arr];
  230. break;
  231. case 'RANGE':
  232. case 'NOT RANGE':
  233. $v = str_replace(' - ', ',', $v);
  234. $arr = array_slice(explode(',', $v), 0, 2);
  235. if (stripos($v, ',') === false || !array_filter($arr)) {
  236. continue 2;
  237. }
  238. //当出现一边为空时改变操作符
  239. if ($arr[0] === '') {
  240. $sym = $sym == 'RANGE' ? '<=' : '>';
  241. $arr = $arr[1];
  242. } elseif ($arr[1] === '') {
  243. $sym = $sym == 'RANGE' ? '>=' : '<';
  244. $arr = $arr[0];
  245. }
  246. $tableArr = explode('.', $k);
  247. if (count($tableArr) > 1 && $tableArr[0] != $name && !in_array($tableArr[0], $alias) && !empty($this->model)) {
  248. //修复关联模型下时间无法搜索的BUG
  249. $relation = Loader::parseName($tableArr[0], 1, false);
  250. $alias[$this->model->$relation()->getTable()] = $tableArr[0];
  251. }
  252. $where[] = [$k, str_replace('RANGE', 'BETWEEN', $sym) . ' TIME', $arr];
  253. break;
  254. case 'NULL':
  255. case 'IS NULL':
  256. case 'NOT NULL':
  257. case 'IS NOT NULL':
  258. $where[] = [$k, strtolower(str_replace('IS ', '', $sym))];
  259. break;
  260. default:
  261. break;
  262. }
  263. $index++;
  264. }
  265. if (!empty($this->model)) {
  266. $this->model->alias($alias);
  267. }
  268. $model = $this->model;
  269. $where = function (Query $query) use ($where, $alias, $bind, &$model) {
  270. if (!empty($model)) {
  271. $model->alias($alias);
  272. $model->bind($bind);
  273. }
  274. foreach ($where as $k => $v) {
  275. if (is_array($v)) {
  276. if($v[0]=='rules.rule'){
  277. $query->whereExists("select * from mobile_order_rules where mobile_order_rules.mobile_order_id=mobile_order.id and rule='{$v[2]}'");
  278. continue;
  279. }elseif ($v[0]=='admin.admin_id'){
  280. $query->whereExists("select * from mobile_order_admin where mobile_order_admin.mobile_order_id=mobile_order.id and admin_id={$v[2]}");
  281. continue;
  282. }elseif ($v[0]=='mobile_anchor_id' && $this->admin('is_sub')){
  283. $query->whereExists("select * from mobile_order_sub_anchor where mobile_order_sub_anchor.mobile_order_id=mobile_order.id and sub_admin_id={$this->auth->id}");
  284. continue;
  285. }
  286. call_user_func_array([$query, 'where'], $v);
  287. } else {
  288. $query->where($v);
  289. }
  290. }
  291. };
  292. return [$where, $sort, $order, $offset, $limit, $page, $alias, $bind];
  293. }
  294. public function edit($ids = null)
  295. {
  296. $model=$this->model->find($ids);
  297. $originStatus=$model['status'];
  298. $row=$model->toArray();
  299. if($this->request->isGet()){
  300. $row['address']=$model->originData()['address'];
  301. $row['city']=\app\common\model\Area::getNameString($row['city'],'/');
  302. $this->assign('row',$row);
  303. return view();
  304. }else{
  305. $field=[
  306. 'name',
  307. 'phone',
  308. 'id_no',
  309. 'address',
  310. 'trans_no',
  311. 'trans_id',
  312. 'status',
  313. ];
  314. $data=input('row/a');
  315. Db::startTrans();
  316. if(isset($data['city'])){
  317. $data['city']=\app\common\model\Area::whereIn('name|shortname',str_replace('/',',',$data['city']))->column('id');
  318. }
  319. $this->validate($data,[
  320. 'status'=>'in:'.implode(',',array_keys(\app\common\model\MobileOrder::$status))
  321. ]);
  322. foreach ($data as $key=>$value){
  323. if(in_array($key,$field)) {
  324. $model[$key] = $value;
  325. }
  326. }
  327. MobileOrderAdmin::add($model,$originStatus!=$model['status'],$this->auth->id);
  328. $model->save();
  329. Db::commit();
  330. $this->success('');
  331. }
  332. }
  333. public function refund($ids){
  334. $model=$this->model->find($ids);
  335. $this->assign('row',$model);
  336. if($this->request->isGet()){
  337. return view();
  338. }else{
  339. $data=input('row/a');
  340. $this->validate($data,RefundValidate::class);
  341. Db::startTrans();
  342. $model=$this->model->where('id',$ids)->lock(true)->findOrFail();
  343. $model->makeRefund('admin',Admin::find($this->auth->id),$data);
  344. Db::commit();
  345. $this->success();
  346. }
  347. }
  348. public function status(){
  349. return \app\common\model\MobileOrder::$status;
  350. }
  351. public function pay_type(){
  352. return \app\common\model\MobileOrder::$payTypes;
  353. }
  354. public function add_operation(){
  355. $id=input('ids/d');
  356. if($this->request->isGet()){
  357. return view();
  358. }else{
  359. $content=input('row.content');
  360. $this->validate(compact('content'),[
  361. 'content'=>'max:250',
  362. ]);
  363. MobileOrderOperation::create([
  364. 'mobile_order_id'=>$id,
  365. 'admin_id'=>$this->auth->id,
  366. 'content'=>$content,
  367. ]);
  368. $this->success();
  369. }
  370. }
  371. public function send(){
  372. $id=input('ids/d');
  373. if($this->request->isGet()){
  374. $row=$this->model->find($id);
  375. $this->assign('row',$row);
  376. return view();
  377. }else{
  378. $data=input('row/a');
  379. Db::startTrans();
  380. $row=$this->model->lock(true)->findOrFail($id);
  381. $row->dealSend($data['trans_no'],$data['trans_id']);
  382. Db::commit();
  383. $this->success();
  384. }
  385. }
  386. #展示开卡资料
  387. public function show_open_data($id){
  388. $row=$this->model->find($id);
  389. return view('',compact('row'));
  390. }
  391. public function see_logistics($ids){
  392. $order=$this->model->find($ids);
  393. $data=TransferCheck::instance()->setNo($order['trans_no'])->setName($order['name'])->setLogisticsCompany(LogisticsCompany::get($order['trans_id']))->get();
  394. $this->assign('data',$data);
  395. return view('mobile_order/see_logistics');
  396. }
  397. #备注
  398. public function sub_remark($id){
  399. $order=$this->model->find($id);
  400. if($this->admin('is_sub') && $this->auth->id!=$order['s_id']){
  401. $this->error('操作失败(a)');
  402. }
  403. if($this->request->isGet()){
  404. return view();
  405. }else{
  406. $data=input('row/a');
  407. $this->validate($data,[
  408. 'content|备注'=>['require','max:50'],
  409. ]);
  410. $order->subRemark()->save([
  411. 'sub_admin_id'=>$this->auth->id,
  412. 'content'=>$data['content'],
  413. ]);
  414. $this->success();
  415. }
  416. }
  417. public function set_anchor($ids){
  418. $order=$this->model->find($ids);
  419. if(!$order){
  420. $this->error('订单不存在');
  421. }
  422. if($this->request->isGet()){
  423. if($this->admin('is_manager')){
  424. $anchor_id=$order['mobile_anchor_id'];
  425. }else{
  426. $anchor_id=$order->subAnchor()->where('sub_admin_id',$this->admin('id'))->value('sub_mobile_anchor_id');
  427. if($anchor_id){
  428. exit('无法再次设置主播');
  429. }
  430. }
  431. $this->assign('mobile_anchor_id',$anchor_id);
  432. return view('sub_anchor');
  433. }else{
  434. $data=input('row/a');
  435. $this->validate($data,[
  436. 'mobile_anchor_id'=>['require'],
  437. ]);
  438. if(!\app\admin\model\MobileAnchor::where('admin_id',$this->admin('id'))->find($data['mobile_anchor_id'])){
  439. $this->error('主播不存在');
  440. }
  441. if($this->admin('is_manager')){
  442. $order['mobile_anchor_id']=$data['mobile_anchor_id'];
  443. $order->save();
  444. $this->success();
  445. }else{
  446. $subAnchor=$order->subAnchor()->where('sub_admin_id',$this->admin('id'))->find();
  447. if(!$subAnchor){
  448. $order->subAnchor()->save([
  449. 'sub_admin_id'=>$this->admin('id'),
  450. 'sub_mobile_anchor_id'=>$data['mobile_anchor_id'],
  451. ]);
  452. $this->success();
  453. }else{
  454. $this->error('不能再次修改');
  455. }
  456. }
  457. }
  458. }
  459. }