123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695 |
- <?php
- namespace app\api\controller;
- use AlibabaCloud\SDK\Vod\V20170321\Models\GetAIMediaAuditJobResponseBody\mediaAuditJob\data;
- use app\common\model\AfreshOrder;
- use app\common\model\AfreshOrderItem;
- use app\common\model\DeliveryAddress;
- use app\common\model\GoodsOrder;
- use app\common\model\StoreOrderRefund;
- use app\common\model\User;
- use app\common\model\UserCouponList;
- use app\common\model\WashOrder;
- use app\common\model\WashCate;
- use app\common\model\WashClothes;
- use app\common\model\WashOrderItem;
- use app\common\model\WashType;
- use app\common\service\OrderCallback;
- use think\Db;
- use think\Exception;
- class WashManage extends Base
- {
- protected $module = 1;
- protected $need_login = [
- 'orderConfirm',
- 'createOrder',
- 'getOrderDetail',
- 'getOrderList',
- 'payOrder',
- 'cancelOrder',
- 'applyAfreshOrder',
- 'applyAfreshOrderDetail',
- 'cancelAfreshOrder',
- 'getAfreshOrderList',
- ];
- public function initialize()
- {
- parent::initialize();
- $this->module = input('module',1);
- if($this->user_id) {
- $cancel_list = WashOrder::where('user_id','=',$this->user_id)
- ->where('pay_state',0)
- ->where('cancel_state',0)
- ->where('create_at','<=',date("Y-m-d H:i:s",time() - 900))
- ->column('id');
- if(!empty($cancel_list)) {
- WashOrder::where('id','in',$cancel_list)->update(['cancel_state'=>1,'status'=>9,'cancel_desc'=>'系统自动取消']);
- }
- }
- }
-
- public function getWashCate()
- {
- $all_cate = WashCate::getAllCateByModule($this->module);
- $this->success('ok',['list'=>$all_cate]);
- }
-
- public function getGoodsList()
- {
- $cate_id = input('get.cate_id');
- $list = WashClothes::where(['c.module'=>$this->module,'c.status'=>1,'c.is_deleted'=>0])
- ->alias('c')
- ->field('c.*,t.title wash_type_name')
- ->leftJoin('WashType t','c.wash_type = t.id')
- ->when($cate_id,function ($query)use ($cate_id){
- if($cate_id) $query->where('c.cate_id',$cate_id);
- })->limit($this->off_set,$this->page_num)
- ->order('sort desc ,id desc')
- ->select()->toArray();
- $this->success('ok',['list'=>$list]);
- }
-
- public function getGoodsDetail()
- {
- $goods_id = input('get.goods_id');
- $detail = WashClothes::where('id',$goods_id)->find()->toArray();
- if($detail['status'] == 0 || $detail['is_deleted'] == 1) $this->error('商品已下架');
- $detail['wash_type_name'] = WashType::where('id',$detail['wash_type'])->value('title');
- $this->success('获取成功',['detail'=>$detail]);
- }
-
- public function orderConfirm()
- {
- $goods_json = input('get.goods_json','');
- $goods_data = json_decode($goods_json,true);
- $goods_column = array_column($goods_data,null,'goods_id');
- $goods_ids = array_keys($goods_column);
- $list = WashClothes::field('a.id,a.price,a.name,a.cover,c.title cate_name,t.title wash_type_name,wash_type')
- ->alias('a')
- ->leftJoin('WashCate c','c.id = a.cate_id')
- ->leftJoin('WashType t','t.id = a.wash_type')
- ->where('a.id','in',$goods_ids)
- ->where('a.module','=',$this->module)
- ->select()->toArray();
- if(count($list) != count($goods_ids)) $this->error('商品信息有误');
- $price_total = 0;
- array_walk($list,function (&$v)use (&$price_total,$goods_column){
- $v['order_price'] = bcmul($v['price'],$goods_column[$v['id']]['num'],2);
- $v['buy_num'] = $goods_column[$v['id']]['num'];
- $price_total = bcadd($v['order_price'],$price_total,2);
- });
- $postage = sysconf('postage');
- $address = DeliveryAddress::field('id,pro_name,city_name,county_name,mer_name,detail,phone,name,sex')->where('user_id',$this->user_id)->where('is_mr',1)->find();
- $this->success('ok',[
- 'list'=>$list,
- 'goods_price'=>$price_total,
- 'price_total'=> bcadd($price_total,$postage,2),
- 'postage'=>$postage,
- 'total_num'=>array_sum(array_column($goods_data,'num')),
- 'address' => $address ? $address->toArray() : null,
- ]);
- }
-
- public function createOrder()
- {
- $goods_json = input('post.goods_json','');
- $address_id = input('post.address_id','');
- $remark = input('post.remark','');
- $images = input('post.images','');
- $coupon_id = input('post.coupon_id','');
- $goods_data = json_decode($goods_json,true);
- $goods_column = array_column($goods_data,null,'goods_id');
- $goods_ids = array_keys($goods_column);
- if(!$address_id) $this->error('请选择售货地址');
- $list = WashClothes::field('a.id,a.price,a.cover,a.name,c.title cate_name,t.title wash_type_name,wash_type')
- ->alias('a')
- ->leftJoin('WashCate c','c.id = a.cate_id')
- ->leftJoin('WashType t','t.id = a.wash_type')
- ->where('a.id','in',$goods_ids)
- ->where('a.module','=',$this->module)
- ->select()->toArray();
-
- if(count($list) != count($goods_ids)) $this->error('商品信息有误');
- $postage = sysconf('postage');
- $ret_data = ['pay_status'=>0,'config'=>[],'order_id'=>0];
- Db::startTrans();
- try {
- $address = DeliveryAddress::field('id,pro_name,city_name,county_name,mer_name,detail,phone,name,sex')
- ->where('user_id',$this->user_id)->where('id',$address_id)->find()->toArray();
- $price_total = 0;
- $order_info = [
- 'user_id' => $this->user_id,
- 'order_no' => get_order_sn(),
- 'pay_no' => get_order_sn(),
- 'goods_num' => array_sum(array_column($goods_data,'num')),
- 'pro_name' => $address['pro_name'],
- 'city_name' =>$address['city_name'],
- 'county_name' => $address['county_name'],
- 'add_detail' => $address['detail'],
- 'phone' => $address['phone'],
- 'sex' => $address['sex'],
- 'user_name' => $address['name'],
- 'remark' => $remark,
- 'images' => $images,
- 'coupon_id' => $coupon_id,
- 'module' => $this->module,
- ];
- $order_item = [];
- array_walk($list,function (&$v)use (&$price_total,$goods_column,&$order_item){
- $item_price = bcmul($v['price'],$goods_column[$v['id']]['num'],2);
- $price_total = bcadd($item_price,$price_total,2);
- $order_item[] = [
- 'user_id' => $this->user_id,
- 'goods_id' => $v['id'],
- 'goods_name' => $v['name'],
- 'cover' => $v['cover'],
- 'sell_price' => $v['price'],
- 'pay_price' => $item_price,
- 'wash_type' => $v['wash_type'],
- 'num' => $goods_column[$v['id']]['num'],
- ];
- });
-
- array_multisort(array_column($order_item,'sell_price'),SORT_DESC,$order_item);
-
- if($coupon_id) {
- $module = $this->module + 1;
- $coupon_info = UserCouponList::where(['l.user_id'=>$this->user_id,'l.status'=>1,'l.id'=>$coupon_id])
- ->alias('l')
- ->field('l.*,c.title,c.module,c.coupon_type,c.is_new')
- ->when($module,function ($query)use ($module){
- if($module > 1 ) $query->where('c.module','in','1,'. $module);
- })->leftJoin('CouponConfig c','c.id = l.coupon_id')
- ->find();
- if(empty($coupon_info)) $coupon_info = $this->exception('所选优惠券有误');
- $coupon_info = $coupon_info->toArray();
- if($coupon_info['coupon_type'] == 1 && $coupon_info['low_amount'] > $price_total) $this->exception('优惠券使用金额限制,最低:'.$coupon_info['low_amount'].'元');
- $coupon_cash = $coupon_info['coupon_type'] == 1 ? $coupon_info['amount'] : $order_item[0]['sell_price'];
- $price_total = bcsub($price_total , $coupon_cash,2);
- $order_info['coupon_cash'] = $coupon_cash;
- UserCouponList::where('id',$coupon_id)->update(['status'=>2,'used_time'=>date('Y-m-d H:i:s')]);
- }
- $order_info['price_total'] = bcadd($postage,$price_total,2);
- $order_info['postage'] = $postage;
- $order_info = WashOrder::create($order_info)->toArray();
- array_walk($order_item,function (&$v,$k)use ($order_info){
- $v['order_id'] = $order_info['id'];
- });
- (new WashOrderItem())->insertAll($order_item);
- $ret_data['order_id'] = $order_info['id'];
- if($price_total == 0) OrderCallback::washOrderCallBack($order_info,1);
- Db::commit();
- }catch (\Exception $e) {
- Db::rollback();
- $this->error($e->getMessage());
- }
- $this->success('ok',$ret_data);
- }
-
- public function getOrderDetail()
- {
- $order_id = input('get.id');
- $field = 'id,module,order_no,status,price_total,pay_state,images,pay_no,is_deleted,pay_at,cancel_state,goods_num,remark,pro_name,city_name,county_name,add_detail,user_name,phone,postage,create_at,finish_int,finish_time,sex,coupon_id,coupon_cash';
- $detail = WashOrder::field($field)
- ->with('orderItem')->where('id',$order_id)->find()->toArray();
- if($detail['is_deleted'] == 1) $this->error('订单已删除');
- $detail['images_arr'] = $detail['images'] ? explode('|',$detail['images']) : null;
- $afresh_order = AfreshOrder::where('wash_id',$detail['id'])->where('cancel_state',0)->value('id');
- $detail['afresh_order'] =intval($afresh_order);
- $detail['second'] = 0;
- if($detail['status'] == 0) {
- $detail['second'] = time() - strtotime($detail['create_at']) + 900;
- }
- $this->success('ok',['detail'=>$detail]);
- }
-
- public function getOrderList()
- {
- $sel_status = input('get.sel_status',0);
- $where = [];
- $where[] = ['user_id','=',$this->user_id];
- $where[] = ['is_deleted','=',0];
- $where[] = ['cancel_state','=',0];
- $where[] = ['refund_state','=',0];
- switch ($sel_status)
- {
- case 1:
- $where[] = ['pay_state','=',0];
- break;
- case 2:
- $where[] = ['pay_state','=',1];
- break;
- case 3:
- $where[] = ['status','=','2'];
- break;
- case 4:
- $where[] = ['status','=','3'];
- break;
- }
- $field = 'id,module,order_no,status,price_total,pay_state,pay_no,pay_at,cancel_state,goods_num,remark,pro_name,city_name,county_name,add_detail,user_name,phone,postage,create_at,finish_int,finish_time,sex,coupon_id,coupon_cash';
- $list = WashOrder::field($field)->with(['orderItem'])->where($where)->order('id desc ')
- ->limit($this->off_set,$this->page_num)
- ->select()->toArray();
- $this->success('ok',['list'=>$list]);
- }
-
- public function payOrder()
- {
- $order_id = input('post.id');
- $field = 'id,module,status,order_no,price_total,pay_state,pay_no,is_deleted,pay_at,cancel_state,goods_num,remark,pro_name,city_name,county_name,add_detail,user_name,phone,postage';
- $order_info = WashOrder::field($field)
- ->with('orderItem')->where('id',$order_id)->find()->toArray();
- if($order_info['is_deleted'] == 1) $this->error('订单已删除');
- if($order_info['cancel_state'] == 1) $this->error('订单已取消');
- if($order_info['status'] != 0) $this->error('订单已支付');
- $user_info = $this->userInfo();
- $ret_data = ['pay_status'=>0,'config'=>[]];
- $notify_url = $this->request->root(true) . '/api/we_chat_pay/washOrderNotify';
- $config = WeChatPay::wxPay('订单支付',$order_info['pay_no'],$order_info['price_total'],$notify_url,'JSAPI',$user_info['openid']);
- if(!$config) $this->error('支付配置错误');
- $ret_data['config']=$config['config'];
- $this->success('ok',$ret_data);
- }
-
- public function cancelOrder()
- {
- $order_id = input('post.id');
- $field = 'id,module,status,order_no,price_total,pay_state,pay_no,is_deleted,pay_at,cancel_state,goods_num,remark,pro_name,city_name,county_name,add_detail,user_name,phone,postage,coupon_id';
- $order_info = WashOrder::field($field)
- ->with('orderItem')->where('id',$order_id)->find()->toArray();
- if($order_info['is_deleted'] == 1) $this->error('订单已删除');
- if($order_info['cancel_state'] == 1) $this->error('订单已取消');
- if($order_info['status'] == 8) $this->error('订单已取消');
- if($order_info['coupon_id']) UserCouponList::where('id',$order_info['coupon_id'])->update(['status'=>1,'used_time'=>'']);
- if($order_info['status'] == 0){
- WashOrder::where('id',$order_id)->update(['status'=>9,'cancel_state'=>1,'cancel_at'=>date('Y-m-d H:i:s')]);
- }else{
- $refund_info = [
- 'user_id' => $this->user_id ,
- 'order_id' => $order_id ,
- 'num' => $order_info['goods_num'],
- 'apply_case' => '订单取消',
- 'is_deleted'=> 0,
- 'refund_type'=> 1,
- 'create_at'=> date('Y-m-d H:i:s'),
- 'refund_no'=> 'W'.$order_info['order_no'],
- 'source'=> 2,
- 'refund_money'=>$order_info['price_total'],
- ];
- $res = \library\tools\Data::save('StoreOrderRefund',$refund_info,'order_id',['order_id'=>$order_id,'source'=>2]);
- $refund_info =StoreOrderRefund::where(['order_id'=>$order_id,'source'=>2,'user_id'=>$this->user_id])->find()->toArray();
- WashOrder::where(['id'=>$order_id])->update(['refund_state'=>1]);
- OrderCallback::refundMoney($refund_info ,$order_info,$order_id);
- }
- $this->success('订单取消成功');
- }
-
- public function classIntro(){}
-
- public function applyAfreshOrder()
- {
- $order_id = input('post.order_id','');
- $goods_json = input('post.goods_json','');
- $remark = input('post.remark','');
- $images = input('post.images','');
- $goods_data = json_decode($goods_json,true);
- $wash_order = WashOrder::where('id',$order_id)->find()->toArray();
- $check = AfreshOrder::where('wash_id',$order_id)
- ->where('is_deleted',0)->where('cancel_state',0)
- ->value('id');
- if($check) $this->error('已申请重洗');
- if($wash_order['status'] !=3) $this->error('订单未完成');
- if($wash_order['finish_int'] < time() - 86400 * 2) $this->error('订单已完成48小时,无法申请重洗');
- Db::startTrans();
- try {
- $order_info = [
- 'user_id' => $this->user_id,
- 'wash_id' => $order_id,
- 'order_no' => get_order_sn(),
- 'goods_num' => array_sum(array_column($goods_data,'num')),
- 'remark' => $remark,
- 'images' => $images,
- 'module' => $wash_order['module'],
- ];
- $order_info = AfreshOrder::create($order_info);
- $item= [] ;
- foreach ($goods_data as $iv) {
- $check_item = WashOrderItem::where('id',$iv['item_id'])->value('num');
- if($iv['num'] > $check_item) $this->exception('重洗数量错误');
- $item[]= [
- 'user_id' => $this->user_id,
- 'afresh_id' => $order_info['id'],
- 'item_id' => $iv['item_id'],
- 'num' => $iv['num'],
- ];
- }
- (new AfreshOrderItem())->insertAll($item);
- Db::commit();
- }catch (\Exception $e) {
- Db::rollback();
- $this->error($e->getMessage());
- }
- $this->success('申请成功等待审核');
- }
-
- public function getAfreshOrderList()
- {
- $list = AfreshOrder::field('a.id,a.wash_id,a.status,a.images,pro_name,city_name,county_name,add_detail,user_name,phone,a.module,sh_status,w.sex')
- ->alias('a')
- ->with('orderItem')
- ->leftJoin('WashOrder w','w.id = a.wash_id')
- ->where('a.user_id',$this->user_id)
- ->where('a.cancel_state',0)
- ->order('a.id desc')
- ->limit($this->off_set,$this->page_num)
- ->select()->toArray();
- array_walk($list,function (&$v){
- $v['images_arr'] = $v['images'] ? explode("|",$v['images']) : null;
- });
- $this->success('ok',['detail'=>$list]);
- }
-
- public function applyAfreshOrderDetail()
- {
- $order_id = input('get.order_id');
- $detail = AfreshOrder::field('a.id,a.wash_id,a.status,a.sh_remark,a.images,pro_name,city_name,county_name,add_detail,user_name,phone,a.module,sh_status,w.sex')
- ->alias('a')
- ->with('orderItem')
- ->leftJoin('WashOrder w','w.id = a.wash_id')
- ->where('w.id',$order_id)
- ->where('a.cancel_state',0)
- ->find();
- if(!$detail) $this->error('订单暂未申请重洗');
- $detail= $detail->toArray();
- $detail['images_arr'] = $detail['images'] ? explode("|",$detail['images']) : null;
- $this->success('ok',['detail'=>$detail]);
- }
-
- public function cancelAfreshOrder()
- {
- AfreshOrder::where('id',input('post.id'))
- ->where('user_id',$this->user_id)
- ->update(['cancel_state'=>1]);
- $this->success('取消成功');
- }
- }
|