MobileOrder.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  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('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. $list = $this->model
  66. ->with(['info','operation','operation.admin','admin','admin.admin','refund_log','refund_log.refunduser'])
  67. ->where($where)
  68. ->where($map)
  69. ->where('type',1)
  70. ->order($sort, $order)
  71. ->paginate($limit);
  72. foreach ($list as $row) {
  73. $row['pay_link']=h5_link("oder-detail/{$row['id']}");
  74. $row->append(['phone_order_count']);
  75. }
  76. $extend=[];
  77. $extend['total']=$this->model->where($where)->where($map)->where('type',1)->sum('amount');
  78. $extend['alipay']=$this->model->where($where)->where($map)->where('type',1)->where('pay_type',1)->sum('amount');
  79. $extend['wechat']=$this->model->where($where)->where($map)->where('type',1)->where('pay_type',2)->sum('amount');
  80. $extend['jd']=$this->model->where($where)->where($map)->where('type',1)->where('pay_type',3)->sum('amount');
  81. $extend['di']=$this->model->where($where)->where($map)->where('type',1)->sum('amount_di');
  82. $extend['profit']=$this->model->where($where)->where($map)->where('type',1)->sum('amount_profit');
  83. $extend['refund']=$this->model->where($where)->where($map)->where('type',1)->sum('amount_refund');
  84. $result = array("total" => $list->total(), "rows" => $list->items(),'extend'=>$extend);
  85. $result['link']=$this->request->url().'&export=1';
  86. return json($result);
  87. }
  88. $export=input('export');
  89. if($export){
  90. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  91. $list = $this->model
  92. ->with(['info','operation','operation.admin','admin','admin.admin'])
  93. ->where($where)
  94. ->where($map)
  95. ->where('type',1)
  96. ->order($sort, $order)
  97. ->paginate($limit);
  98. return MobileOrderExport::export($list,'admin');
  99. }
  100. $this->assign('admins',Admin::getAdmins());
  101. return $this->view->fetch();
  102. }
  103. protected function buildparams($searchfields = null, $relationSearch = null)
  104. {
  105. $searchfields = is_null($searchfields) ? $this->searchFields : $searchfields;
  106. $relationSearch = is_null($relationSearch) ? $this->relationSearch : $relationSearch;
  107. $search = $this->request->get("search", '');
  108. $filter = $this->request->get("filter", '');
  109. $op = $this->request->get("op", '', 'trim');
  110. $sort = $this->request->get("sort", !empty($this->model) && $this->model->getPk() ? $this->model->getPk() : 'id');
  111. $order = $this->request->get("order", "DESC");
  112. $offset = $this->request->get("offset/d", 0);
  113. $limit = $this->request->get("limit/d", 999999);
  114. //新增自动计算页码
  115. $page = $limit ? intval($offset / $limit) + 1 : 1;
  116. if ($this->request->has("page")) {
  117. $page = $this->request->get("page/d", 1);
  118. }
  119. $this->request->get([config('paginate.var_page') => $page]);
  120. $filter = (array)json_decode($filter, true);
  121. $op = (array)json_decode($op, true);
  122. $filter = $filter ? $filter : [];
  123. $where = [];
  124. $alias = [];
  125. $bind = [];
  126. $name = '';
  127. $aliasName = '';
  128. if (!empty($this->model) && $this->relationSearch) {
  129. $name = $this->model->getTable();
  130. $alias[$name] = Loader::parseName(basename(str_replace('\\', '/', get_class($this->model))));
  131. $aliasName = $alias[$name] . '.';
  132. }
  133. $sortArr = explode(',', $sort);
  134. foreach ($sortArr as $index => & $item) {
  135. $item = stripos($item, ".") === false ? $aliasName . trim($item) : $item;
  136. }
  137. unset($item);
  138. $sort = implode(',', $sortArr);
  139. $adminIds = $this->getDataLimitAdminIds();
  140. if (is_array($adminIds)) {
  141. $where[] = [$aliasName . $this->dataLimitField, 'in', $adminIds];
  142. }
  143. if ($search) {
  144. $searcharr = is_array($searchfields) ? $searchfields : explode(',', $searchfields);
  145. foreach ($searcharr as $k => &$v) {
  146. $v = stripos($v, ".") === false ? $aliasName . $v : $v;
  147. }
  148. unset($v);
  149. $where[] = [implode("|", $searcharr), "LIKE", "%{$search}%"];
  150. }
  151. $index = 0;
  152. foreach ($filter as $k => $v) {
  153. if (!preg_match('/^[a-zA-Z0-9_\-\.]+$/', $k)) {
  154. continue;
  155. }
  156. $sym = isset($op[$k]) ? $op[$k] : '=';
  157. if (stripos($k, ".") === false) {
  158. $k = $aliasName . $k;
  159. }
  160. $v = !is_array($v) ? trim($v) : $v;
  161. $sym = strtoupper(isset($op[$k]) ? $op[$k] : $sym);
  162. //null和空字符串特殊处理
  163. if (!is_array($v)) {
  164. if (in_array(strtoupper($v), ['NULL', 'NOT NULL'])) {
  165. $sym = strtoupper($v);
  166. }
  167. if (in_array($v, ['""', "''"])) {
  168. $v = '';
  169. $sym = '=';
  170. }
  171. }
  172. switch ($sym) {
  173. case '=':
  174. case '<>':
  175. $where[] = [$k, $sym, (string)$v];
  176. break;
  177. case 'LIKE':
  178. case 'NOT LIKE':
  179. case 'LIKE %...%':
  180. case 'NOT LIKE %...%':
  181. $where[] = [$k, trim(str_replace('%...%', '', $sym)), "%{$v}%"];
  182. break;
  183. case '>':
  184. case '>=':
  185. case '<':
  186. case '<=':
  187. $where[] = [$k, $sym, intval($v)];
  188. break;
  189. case 'FINDIN':
  190. case 'FINDINSET':
  191. case 'FIND_IN_SET':
  192. $v = is_array($v) ? $v : explode(',', str_replace(' ', ',', $v));
  193. $findArr = array_values($v);
  194. foreach ($findArr as $idx => $item) {
  195. $bindName = "item_" . $index . "_" . $idx;
  196. $bind[$bindName] = $item;
  197. $where[] = "FIND_IN_SET(:{$bindName}, `" . str_replace('.', '`.`', $k) . "`)";
  198. }
  199. break;
  200. case 'IN':
  201. case 'IN(...)':
  202. case 'NOT IN':
  203. case 'NOT IN(...)':
  204. $where[] = [$k, str_replace('(...)', '', $sym), is_array($v) ? $v : explode(',', $v)];
  205. break;
  206. case 'BETWEEN':
  207. case 'NOT BETWEEN':
  208. $arr = array_slice(explode(',', $v), 0, 2);
  209. if (stripos($v, ',') === false || !array_filter($arr)) {
  210. continue 2;
  211. }
  212. //当出现一边为空时改变操作符
  213. if ($arr[0] === '') {
  214. $sym = $sym == 'BETWEEN' ? '<=' : '>';
  215. $arr = $arr[1];
  216. } elseif ($arr[1] === '') {
  217. $sym = $sym == 'BETWEEN' ? '>=' : '<';
  218. $arr = $arr[0];
  219. }
  220. $where[] = [$k, $sym, $arr];
  221. break;
  222. case 'RANGE':
  223. case 'NOT RANGE':
  224. $v = str_replace(' - ', ',', $v);
  225. $arr = array_slice(explode(',', $v), 0, 2);
  226. if (stripos($v, ',') === false || !array_filter($arr)) {
  227. continue 2;
  228. }
  229. //当出现一边为空时改变操作符
  230. if ($arr[0] === '') {
  231. $sym = $sym == 'RANGE' ? '<=' : '>';
  232. $arr = $arr[1];
  233. } elseif ($arr[1] === '') {
  234. $sym = $sym == 'RANGE' ? '>=' : '<';
  235. $arr = $arr[0];
  236. }
  237. $tableArr = explode('.', $k);
  238. if (count($tableArr) > 1 && $tableArr[0] != $name && !in_array($tableArr[0], $alias) && !empty($this->model)) {
  239. //修复关联模型下时间无法搜索的BUG
  240. $relation = Loader::parseName($tableArr[0], 1, false);
  241. $alias[$this->model->$relation()->getTable()] = $tableArr[0];
  242. }
  243. $where[] = [$k, str_replace('RANGE', 'BETWEEN', $sym) . ' TIME', $arr];
  244. break;
  245. case 'NULL':
  246. case 'IS NULL':
  247. case 'NOT NULL':
  248. case 'IS NOT NULL':
  249. $where[] = [$k, strtolower(str_replace('IS ', '', $sym))];
  250. break;
  251. default:
  252. break;
  253. }
  254. $index++;
  255. }
  256. if (!empty($this->model)) {
  257. $this->model->alias($alias);
  258. }
  259. $model = $this->model;
  260. $where = function (Query $query) use ($where, $alias, $bind, &$model) {
  261. if (!empty($model)) {
  262. $model->alias($alias);
  263. $model->bind($bind);
  264. }
  265. foreach ($where as $k => $v) {
  266. if (is_array($v)) {
  267. if($v[0]=='rules.rule'){
  268. $query->whereExists("select * from mobile_order_rules where mobile_order_rules.mobile_order_id=mobile_order.id and rule='{$v[2]}'");
  269. continue;
  270. }elseif ($v[0]=='admin.admin_id'){
  271. $query->whereExists("select * from mobile_order_admin where mobile_order_admin.mobile_order_id=mobile_order.id and admin_id={$v[2]}");
  272. continue;
  273. }
  274. call_user_func_array([$query, 'where'], $v);
  275. } else {
  276. $query->where($v);
  277. }
  278. }
  279. };
  280. return [$where, $sort, $order, $offset, $limit, $page, $alias, $bind];
  281. }
  282. public function edit($ids = null)
  283. {
  284. $model=$this->model->find($ids);
  285. $originStatus=$model['status'];
  286. $row=$model->toArray();
  287. if($this->request->isGet()){
  288. $row['address']=$model->originData()['address'];
  289. $row['city']=\app\common\model\Area::getNameString($row['city'],'/');
  290. $this->assign('row',$row);
  291. return view();
  292. }else{
  293. $data=input('row/a');
  294. if(isset($data['city'])){
  295. $data['city']=\app\common\model\Area::whereIn('name|shortname',str_replace('/',',',$data['city']))->column('id');
  296. }
  297. $this->validate($data,[
  298. 'status'=>'in:'.implode(',',array_keys(\app\common\model\MobileOrder::$status))
  299. ]);
  300. foreach ($data as $key=>$value){
  301. $model[$key]=$value;
  302. }
  303. MobileOrderAdmin::add($model,$originStatus!=$model['status'],$this->auth->id);
  304. $model->save();
  305. $this->success('');
  306. }
  307. }
  308. public function refund($ids){
  309. $model=$this->model->find($ids);
  310. $this->assign('row',$model);
  311. if($this->request->isGet()){
  312. return view();
  313. }else{
  314. $data=input('row/a');
  315. $this->validate($data,RefundValidate::class);
  316. Db::startTrans();
  317. $model=$this->model->where('id',$ids)->lock(true)->findOrFail();
  318. $model->makeRefund('admin',Admin::find($this->auth->id),$data);
  319. Db::commit();
  320. $this->success();
  321. }
  322. }
  323. public function status(){
  324. return \app\common\model\MobileOrder::$status;
  325. }
  326. public function pay_type(){
  327. return \app\common\model\MobileOrder::$payTypes;
  328. }
  329. public function add_operation(){
  330. $id=input('ids/d');
  331. if($this->request->isGet()){
  332. return view();
  333. }else{
  334. $content=input('row.content');
  335. $this->validate(compact('content'),[
  336. 'content'=>'max:250',
  337. ]);
  338. MobileOrderOperation::create([
  339. 'mobile_order_id'=>$id,
  340. 'admin_id'=>$this->auth->id,
  341. 'content'=>$content,
  342. ]);
  343. $this->success();
  344. }
  345. }
  346. public function send(){
  347. $id=input('ids/d');
  348. if($this->request->isGet()){
  349. $row=$this->model->find($id);
  350. $this->assign('row',$row);
  351. return view();
  352. }else{
  353. $data=input('row/a');
  354. Db::startTrans();
  355. $row=$this->model->lock(true)->findOrFail($id);
  356. $row->dealSend($data['trans_no'],$data['trans_id']);
  357. Db::commit();
  358. $this->success();
  359. }
  360. }
  361. #展示开卡资料
  362. public function show_open_data($id){
  363. $row=$this->model->find($id);
  364. return view('',compact('row'));
  365. }
  366. public function see_logistics($ids){
  367. $order=$this->model->find($ids);
  368. $data=TransferCheck::instance()->setNo($order['trans_no'])->setName($order['name'])->setLogisticsCompany(LogisticsCompany::get($order['trans_id']))->get();
  369. $this->assign('data',$data);
  370. return view('mobile_order/see_logistics');
  371. }
  372. }