123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349 |
- <?php
- /**
- *
- * @author: xaboy<365615158@qq.com>
- * @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)->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();
- $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;
- $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('修改失败!');
- }
- }
- }
|