* @day: 2017/11/11 */ namespace app\admin\controller\diagnosis; use app\admin\controller\AuthController; use app\admin\model\diagnosis\DiagnosisOrderAttr; use app\admin\model\user\User; use app\admin\model\user\UserBill; use app\admin\model\user\UserCard; use crmeb\basic\BaseModel; use crmeb\services\{ExpressService, JsonService, JsonService as Json, MiniProgramService, WechatService, FormBuilder as Form, CacheService, UtilService as Util}; use think\facade\Route as Url; use think\facade\Validate; Use app\admin\model\diagnosis\DiagnosisOrder as model; /** * 订单管理控制器 同一个订单表放在一个控制器 * Class StoreOrder * @package app\admin\controller\store */ class DiagnosisOrder extends AuthController { /** * @return mixed */ public function index() { $count['djd'] = \app\admin\model\diagnosis\DiagnosisOrder::where('status', 0)->where('paid', 1)->count(); $count['yjd'] = \app\admin\model\diagnosis\DiagnosisOrder::where('status', 1)->count(); $count['dqd'] = \app\admin\model\diagnosis\DiagnosisOrder::where('status', 2)->count(); $count['ywc'] = \app\admin\model\diagnosis\DiagnosisOrder::where('status', 3)->count(); $count['sh'] = \app\admin\model\diagnosis\DiagnosisOrder::where('after_sales', 1)->count(); $count['qx'] = \app\admin\model\diagnosis\DiagnosisOrder::where('status', -1)->count(); $count['gq'] = \app\admin\model\diagnosis\DiagnosisOrder::where('status', -2)->count(); $this->assign([ 'year' => get_month(), 'count' => $count ]); return $this->fetch(); } public function list() { $where = Util::getMore([ ['page', 1], ['limit', 20], ['name', ''], ['order_id', ''], ['type', ''], ['pay_type', ''], ['rece', ''], ['data', ''], ['status', ''], ]); return Json::successlayui(model::list($where)); } /** * 显示创建资源表单页. * * @return \think\Response */ public function create($id = 0) { $f = []; $f[] = Form::input('name', '名称')->required(); $f[] = Form::select('type', '选择类型', '')->options([ ['value' => 1, 'label' => '陪诊'], ['value' => 2, 'label' => '代办'], ])->filterable(true)->required(); $f[] = Form::input('price', '金额')->required(); $f[] = Form::input('reward', '接单奖励')->required(); $f[] = Form::radio('status', '状态', 1)->options([['value' => 0, 'label' => '禁用'], ['value' => 1, 'label' => '正常']]); $form = Form::make_post_form('添加', $f, Url::buildUrl('save')); $this->assign(compact('form')); return $this->fetch('public/form-builder'); } public function save() { $model = new model; $data = Util::postMore([ 'name', 'type', 'price', 'status', 'reward', ]); $res = $model->save($data); if ($res) return Json::successful('添加成功'); return Json::fail('添加失败'); } /** * 显示创建资源表单页. * * @return \think\Response */ public function edit($id = 0) { $data = model::find($id); $f = []; $f[] = Form::input('name', '分类名称', $data['name'])->required(); $f[] = Form::select('type', '选择分类', (string)$data['type'])->options([ ['value' => 1, 'label' => '陪诊'], ['value' => 2, 'label' => '代办'], ])->filterable(true)->required(); $f[] = Form::input('price', '金额', $data['price'])->required(); $f[] = Form::input('reward', '接单奖励', $data['reward'])->required(); $f[] = Form::radio('status', '状态', (string)$data['status'])->options([['value' => 0, 'label' => '禁用'], ['value' => 1, 'label' => '正常']]); $f[] = Form::hidden('id', $id); $form = Form::make_post_form('修改', $f, Url::buildUrl('update')); $this->assign(compact('form')); return $this->fetch('public/form-builder'); } /** * 修改 * @return void * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ public function update() { $model = new model; $data = Util::postMore([ 'name', 'type', 'price', 'status', 'reward', 'id' ]); $details = $model->find($data['id']); $details['name'] = $data['name']; $details['type'] = $data['type']; $details['price'] = $data['price']; $details['status'] = $data['status']; $details['reward'] = $data['reward']; $res = $details->save(); if ($res) return Json::successful('修改成功'); return Json::fail('修改失败'); } /** * 删除 * @param $id * @return void * @throws \Exception */ public function delete($id) { if (!$id) return Json::fail('删除失败'); $model = new model; $res = model::destroy($id); if ($res){ return Json::success('删除成功!'); }else{ return Json::fail($model->getErrorInfo()); } } /** * 完成 * @param $id * @return void * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ public function wc($id) { $order = model::find($id); $order['status'] = 2; $res = $order->save(); if ($res){ return Json::success('成功!'); }else{ return Json::fail('失败'); } } /** * 完成 * @param $id * @return void * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ public function sales($id) { $order = model::find($id); $order['after_sales'] = 2; $order['status'] = 3; $res = $order->save(); if ($res){ return Json::success('成功!'); }else{ return Json::fail('失败'); } } /** * 设置单个产品上架|下架 * * @return json */ public function set_show($is_show = '', $id = '') { ($is_show == '' || $id == '') && Json::fail('缺少参数'); $res = model::where(['id' => $id])->update(['status' => (int)$is_show]); if ($res) { return JsonService::successful($is_show == 1 ? '显示成功' : '隐藏成功'); } else { return JsonService::fail($is_show == 1 ? '显示失败' : '隐藏失败'); } } /** * 订单详情 * @param $id * @return string * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ public function info($id) { $order = model::find($id); if ($order['images']){ $order['images'] = json_decode($order['images']); $order['after_images'] = json_decode($order['after_images']); } $user = User::where('uid', $order['uid'])->find();//订单用户 $re = User::where('uid', $order['order_receiving'])->find();//接单员 if ($re){ $apply = \app\admin\model\diagnosis\DiagnosisApply::where('uid', $re['uid'])->find(); $re['bd_phone'] = $apply['phone']; $re['bd_name'] = $apply['name']; $re['sex'] = $apply['sex'] == 1? '男' : '女'; } $attr = DiagnosisOrderAttr::where('oid', $id)->find(); $cate = implode(',',\app\admin\model\diagnosis\DiagnosisCate::where('id', 'in', $order['cate_id'])->column('name')); $service = \app\admin\model\diagnosis\DiagnosisService::where('id', 'in', $attr['service_id'])->select(); $card = UserCard::alias('a') ->field('b.name') ->leftJoin('card b', 'a.card_id = b.id') ->where('a.id', 'in', $order['user_card_id']) ->select(); $attr['time'] = date('Y-m-d H:i:s', $attr['time']); $this->assign('order', $order); $this->assign('user', $user); $this->assign('re', $re); $this->assign('attr', $attr); $this->assign('cate', $cate); $this->assign('service', $service); $this->assign('card', $card); return $this->fetch(); } public function refund($id) { $order = model::find($id); if (!$order) return JsonService::fail('数据不存在'); $f = []; $f[] = Form::input('order_id', '退款单号', $order['order_id'])->disabled(1); $f[] = Form::number('refund_price', '退款金额', $order['pay_price'])->precision(2)->min(0.01); $f[] = Form::radio('type', '状态', 1)->options([['label' => '直接退款', 'value' => 1], ['label' => '退款后,返回原状态', 'value' => 2]]); $form = Form::make_post_form('退款处理', $f, Url::buildUrl('updateRefundY', array('id' => $id)), 7); $this->assign(compact('form')); return $this->fetch('public/form-builder'); } /** * 退款处理 * @param $id */ public function updateRefundY($id) { $data = Util::postMore([ 'refund_price', ['type', 1], ]); if (!$id) return $this->failed('数据不存在'); $product = model::get($id); if (!$product) return Json::fail('数据不存在!'); if ($product['pay_price'] == $product['refund_price']) return Json::fail('已退完支付金额!不能再退款了'); if (!$data['refund_price']) return Json::fail('请输入退款金额'); $refund_price = $data['refund_price']; $data['refund_price'] = bcadd($data['refund_price'], $product['refund_price'], 2); $bj = bccomp((float)$product['pay_price'], (float)$data['refund_price'], 2); if ($bj < 0) return Json::fail('退款金额大于支付金额,请修改退款金额'); $data['refund_status'] = 1; $type = $data['type']; unset($data['type']); $refund_data['pay_price'] = $product['pay_price']; $refund_data['refund_price'] = $refund_price; if ($product['pay_type'] == 2) { try { WechatService::payOrderRefund($product['order_id'], $refund_data); } catch (\Exception $e) { return Json::fail($e->getMessage()); } } else if ($product['pay_type'] == 1) { BaseModel::beginTrans(); $usermoney = User::where('uid', $product['uid'])->value('now_money'); $res1 = User::bcInc($product['uid'], 'now_money', $refund_price, 'uid'); $res2 = UserBill::income('退款', $product['uid'], 'now_money', 'pay_product_refund', $refund_price, $product['id'], bcadd($usermoney, $refund_price, 2), '订单退款到余额' . floatval($refund_price) . '元'); $res = $res1 && $res2; BaseModel::checkTrans($res); if (!$res) return Json::fail('余额退款失败!'); } $resEdit = model::edit($data, $id); $res = true; if ($resEdit) { BaseModel::commitTrans(); return Json::success('修改成功!'); } else { return Json::fail('修改失败!'); } } }