123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389 |
- <?php
- namespace app\admin\controller;
- use app\admin\model\Admin;
- use app\admin\model\MobileOrderAdmin;
- use app\common\controller\Backend;
- use app\common\model\LogisticsCompany;
- use app\common\model\MobileOrderOperation;
- use app\common\model\MobileOrderRules;
- use app\common\service\MobileOrderExport;
- use app\common\service\Refund;
- use app\common\service\SmsSend;
- use app\common\service\TransferCheck;
- use app\common\validate\RefundValidate;
- use think\Db;
- use think\db\Query;
- use think\Loader;
- /**
- *
- *
- * @icon fa fa-circle-o
- */
- class MobileOrder extends Backend
- {
- protected $noNeedRight=['status','pay_type'];
- /**
- * MobileOrder模型对象
- * @var \app\admin\model\MobileOrder
- */
- protected $model = null;
- public function _initialize()
- {
- parent::_initialize();
- $this->model = new \app\admin\model\MobileOrder;
- $this->assign('status',\app\common\model\MobileOrder::$status);
- $this->assign('logistics_list',json_encode(LogisticsCompany::all()));
- }
- public function import()
- {
- $this->error('not support');
- }
- /**
- * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
- * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
- * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
- */
-
- /**
- * 查看
- */
- public function index()
- {
- //当前是否为关联查询
- $this->relationSearch = false;
- //设置过滤方法
- $this->request->filter(['strip_tags', 'trim']);
- $map=[];
- if($this->admin('sub')){
- $map['s_id']=$this->admin('id');
- }
- if ($this->request->isAjax()) {
- //如果发送的来源是Selectpage,则转发到Selectpage
- if ($this->request->request('keyField')) {
- return $this->selectpage();
- }
- list($where, $sort, $order, $offset, $limit) = $this->buildparams();
- $list = $this->model
- ->with(['info','operation','operation.admin','admin','admin.admin','refund_log','refund_log.refunduser'])
- ->where($where)
- ->where($map)
- ->where('type',1)
- ->order($sort, $order)
- ->paginate($limit);
- foreach ($list as $row) {
- $row['pay_link']=h5_link("oder-detail/{$row['id']}");
- $row->append(['phone_order_count']);
- }
- $extend=[];
- $extend['total']=$this->model->where($where)->where($map)->where('type',1)->sum('amount');
- $extend['alipay']=$this->model->where($where)->where($map)->where('type',1)->where('pay_type',1)->sum('amount');
- $extend['wechat']=$this->model->where($where)->where($map)->where('type',1)->where('pay_type',2)->sum('amount');
- $extend['jd']=$this->model->where($where)->where($map)->where('type',1)->where('pay_type',3)->sum('amount');
- $extend['di']=$this->model->where($where)->where($map)->where('type',1)->sum('amount_di');
- $extend['profit']=$this->model->where($where)->where($map)->where('type',1)->sum('amount_profit');
- $extend['refund']=$this->model->where($where)->where($map)->where('type',1)->sum('amount_refund');
- $result = array("total" => $list->total(), "rows" => $list->items(),'extend'=>$extend);
- $result['link']=$this->request->url().'&export=1';
- return json($result);
- }
- $export=input('export');
- if($export){
- list($where, $sort, $order, $offset, $limit) = $this->buildparams();
- $list = $this->model
- ->with(['info','operation','operation.admin','admin','admin.admin'])
- ->where($where)
- ->where($map)
- ->where('type',1)
- ->order($sort, $order)
- ->paginate($limit);
- return MobileOrderExport::export($list,'admin');
- }
- $this->assign('admins',Admin::getAdmins());
- return $this->view->fetch();
- }
- protected function buildparams($searchfields = null, $relationSearch = null)
- {
- $searchfields = is_null($searchfields) ? $this->searchFields : $searchfields;
- $relationSearch = is_null($relationSearch) ? $this->relationSearch : $relationSearch;
- $search = $this->request->get("search", '');
- $filter = $this->request->get("filter", '');
- $op = $this->request->get("op", '', 'trim');
- $sort = $this->request->get("sort", !empty($this->model) && $this->model->getPk() ? $this->model->getPk() : 'id');
- $order = $this->request->get("order", "DESC");
- $offset = $this->request->get("offset/d", 0);
- $limit = $this->request->get("limit/d", 999999);
- //新增自动计算页码
- $page = $limit ? intval($offset / $limit) + 1 : 1;
- if ($this->request->has("page")) {
- $page = $this->request->get("page/d", 1);
- }
- $this->request->get([config('paginate.var_page') => $page]);
- $filter = (array)json_decode($filter, true);
- $op = (array)json_decode($op, true);
- $filter = $filter ? $filter : [];
- $where = [];
- $alias = [];
- $bind = [];
- $name = '';
- $aliasName = '';
- if (!empty($this->model) && $this->relationSearch) {
- $name = $this->model->getTable();
- $alias[$name] = Loader::parseName(basename(str_replace('\\', '/', get_class($this->model))));
- $aliasName = $alias[$name] . '.';
- }
- $sortArr = explode(',', $sort);
- foreach ($sortArr as $index => & $item) {
- $item = stripos($item, ".") === false ? $aliasName . trim($item) : $item;
- }
- unset($item);
- $sort = implode(',', $sortArr);
- $adminIds = $this->getDataLimitAdminIds();
- if (is_array($adminIds)) {
- $where[] = [$aliasName . $this->dataLimitField, 'in', $adminIds];
- }
- if ($search) {
- $searcharr = is_array($searchfields) ? $searchfields : explode(',', $searchfields);
- foreach ($searcharr as $k => &$v) {
- $v = stripos($v, ".") === false ? $aliasName . $v : $v;
- }
- unset($v);
- $where[] = [implode("|", $searcharr), "LIKE", "%{$search}%"];
- }
- $index = 0;
- foreach ($filter as $k => $v) {
- if (!preg_match('/^[a-zA-Z0-9_\-\.]+$/', $k)) {
- continue;
- }
- $sym = isset($op[$k]) ? $op[$k] : '=';
- if (stripos($k, ".") === false) {
- $k = $aliasName . $k;
- }
- $v = !is_array($v) ? trim($v) : $v;
- $sym = strtoupper(isset($op[$k]) ? $op[$k] : $sym);
- //null和空字符串特殊处理
- if (!is_array($v)) {
- if (in_array(strtoupper($v), ['NULL', 'NOT NULL'])) {
- $sym = strtoupper($v);
- }
- if (in_array($v, ['""', "''"])) {
- $v = '';
- $sym = '=';
- }
- }
- switch ($sym) {
- case '=':
- case '<>':
- $where[] = [$k, $sym, (string)$v];
- break;
- case 'LIKE':
- case 'NOT LIKE':
- case 'LIKE %...%':
- case 'NOT LIKE %...%':
- $where[] = [$k, trim(str_replace('%...%', '', $sym)), "%{$v}%"];
- break;
- case '>':
- case '>=':
- case '<':
- case '<=':
- $where[] = [$k, $sym, intval($v)];
- break;
- case 'FINDIN':
- case 'FINDINSET':
- case 'FIND_IN_SET':
- $v = is_array($v) ? $v : explode(',', str_replace(' ', ',', $v));
- $findArr = array_values($v);
- foreach ($findArr as $idx => $item) {
- $bindName = "item_" . $index . "_" . $idx;
- $bind[$bindName] = $item;
- $where[] = "FIND_IN_SET(:{$bindName}, `" . str_replace('.', '`.`', $k) . "`)";
- }
- break;
- case 'IN':
- case 'IN(...)':
- case 'NOT IN':
- case 'NOT IN(...)':
- $where[] = [$k, str_replace('(...)', '', $sym), is_array($v) ? $v : explode(',', $v)];
- break;
- case 'BETWEEN':
- case 'NOT BETWEEN':
- $arr = array_slice(explode(',', $v), 0, 2);
- if (stripos($v, ',') === false || !array_filter($arr)) {
- continue 2;
- }
- //当出现一边为空时改变操作符
- if ($arr[0] === '') {
- $sym = $sym == 'BETWEEN' ? '<=' : '>';
- $arr = $arr[1];
- } elseif ($arr[1] === '') {
- $sym = $sym == 'BETWEEN' ? '>=' : '<';
- $arr = $arr[0];
- }
- $where[] = [$k, $sym, $arr];
- break;
- case 'RANGE':
- case 'NOT RANGE':
- $v = str_replace(' - ', ',', $v);
- $arr = array_slice(explode(',', $v), 0, 2);
- if (stripos($v, ',') === false || !array_filter($arr)) {
- continue 2;
- }
- //当出现一边为空时改变操作符
- if ($arr[0] === '') {
- $sym = $sym == 'RANGE' ? '<=' : '>';
- $arr = $arr[1];
- } elseif ($arr[1] === '') {
- $sym = $sym == 'RANGE' ? '>=' : '<';
- $arr = $arr[0];
- }
- $tableArr = explode('.', $k);
- if (count($tableArr) > 1 && $tableArr[0] != $name && !in_array($tableArr[0], $alias) && !empty($this->model)) {
- //修复关联模型下时间无法搜索的BUG
- $relation = Loader::parseName($tableArr[0], 1, false);
- $alias[$this->model->$relation()->getTable()] = $tableArr[0];
- }
- $where[] = [$k, str_replace('RANGE', 'BETWEEN', $sym) . ' TIME', $arr];
- break;
- case 'NULL':
- case 'IS NULL':
- case 'NOT NULL':
- case 'IS NOT NULL':
- $where[] = [$k, strtolower(str_replace('IS ', '', $sym))];
- break;
- default:
- break;
- }
- $index++;
- }
- if (!empty($this->model)) {
- $this->model->alias($alias);
- }
- $model = $this->model;
- $where = function (Query $query) use ($where, $alias, $bind, &$model) {
- if (!empty($model)) {
- $model->alias($alias);
- $model->bind($bind);
- }
- foreach ($where as $k => $v) {
- if (is_array($v)) {
- if($v[0]=='rules.rule'){
- $query->whereExists("select * from mobile_order_rules where mobile_order_rules.mobile_order_id=mobile_order.id and rule='{$v[2]}'");
- continue;
- }elseif ($v[0]=='admin.admin_id'){
- $query->whereExists("select * from mobile_order_admin where mobile_order_admin.mobile_order_id=mobile_order.id and admin_id={$v[2]}");
- continue;
- }
- call_user_func_array([$query, 'where'], $v);
- } else {
- $query->where($v);
- }
- }
- };
- return [$where, $sort, $order, $offset, $limit, $page, $alias, $bind];
- }
- public function edit($ids = null)
- {
- $model=$this->model->find($ids);
- $originStatus=$model['status'];
- $row=$model->toArray();
- if($this->request->isGet()){
- $row['address']=$model->originData()['address'];
- $row['city']=\app\common\model\Area::getNameString($row['city'],'/');
- $this->assign('row',$row);
- return view();
- }else{
- $data=input('row/a');
- if(isset($data['city'])){
- $data['city']=\app\common\model\Area::whereIn('name|shortname',str_replace('/',',',$data['city']))->column('id');
- }
- $this->validate($data,[
- 'status'=>'in:'.implode(',',array_keys(\app\common\model\MobileOrder::$status))
- ]);
- foreach ($data as $key=>$value){
- $model[$key]=$value;
- }
- MobileOrderAdmin::add($model,$originStatus!=$model['status'],$this->auth->id);
- $model->save();
- $this->success('');
- }
- }
- public function refund($ids){
- $model=$this->model->find($ids);
- $this->assign('row',$model);
- if($this->request->isGet()){
- return view();
- }else{
- $data=input('row/a');
- $this->validate($data,RefundValidate::class);
- Db::startTrans();
- $model=$this->model->where('id',$ids)->lock(true)->findOrFail();
- $model->makeRefund('admin',Admin::find($this->auth->id),$data);
- Db::commit();
- $this->success();
- }
- }
- public function status(){
- return \app\common\model\MobileOrder::$status;
- }
- public function pay_type(){
- return \app\common\model\MobileOrder::$payTypes;
- }
- public function add_operation(){
- $id=input('ids/d');
- if($this->request->isGet()){
- return view();
- }else{
- $content=input('row.content');
- $this->validate(compact('content'),[
- 'content'=>'max:250',
- ]);
- MobileOrderOperation::create([
- 'mobile_order_id'=>$id,
- 'admin_id'=>$this->auth->id,
- 'content'=>$content,
- ]);
- $this->success();
- }
- }
- public function send(){
- $id=input('ids/d');
- if($this->request->isGet()){
- $row=$this->model->find($id);
- $this->assign('row',$row);
- return view();
- }else{
- $data=input('row/a');
- Db::startTrans();
- $row=$this->model->lock(true)->findOrFail($id);
- $row->dealSend($data['trans_no'],$data['trans_id']);
- Db::commit();
- $this->success();
- }
- }
- #展示开卡资料
- public function show_open_data($id){
- $row=$this->model->find($id);
- return view('',compact('row'));
- }
- public function see_logistics($ids){
- $order=$this->model->find($ids);
- $data=TransferCheck::instance()->setNo($order['trans_no'])->setName($order['name'])->setLogisticsCompany(LogisticsCompany::get($order['trans_id']))->get();
- $this->assign('data',$data);
- return view('mobile_order/see_logistics');
- }
- }
|