DiagnosisOrder.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. <?php
  2. /**
  3. *
  4. * @author: xaboy<365615158@qq.com>
  5. * @day: 2017/11/11
  6. */
  7. namespace app\admin\controller\diagnosis;
  8. use app\admin\controller\AuthController;
  9. use app\admin\model\diagnosis\DiagnosisOrderAttr;
  10. use app\admin\model\user\User;
  11. use app\admin\model\user\UserBill;
  12. use app\admin\model\user\UserCard;
  13. use crmeb\basic\BaseModel;
  14. use crmeb\services\{ExpressService,
  15. JsonService,
  16. JsonService as Json,
  17. MiniProgramService,
  18. WechatService,
  19. FormBuilder as Form,
  20. CacheService,
  21. UtilService as Util};
  22. use think\facade\Route as Url;
  23. use think\facade\Validate;
  24. Use app\admin\model\diagnosis\DiagnosisOrder as model;
  25. /**
  26. * 订单管理控制器 同一个订单表放在一个控制器
  27. * Class StoreOrder
  28. * @package app\admin\controller\store
  29. */
  30. class DiagnosisOrder extends AuthController
  31. {
  32. /**
  33. * @return mixed
  34. */
  35. public function index()
  36. {
  37. $admin = $this->adminInfo;
  38. $where = [];
  39. if ($admin['roles'] > 1){
  40. if ($admin['roles'] == 2){
  41. $where[] = ['d.province', 'like', '%'.$admin['province'].'%'];
  42. }
  43. if ($admin['roles'] == 3){
  44. $city = explode(',', $admin['city']);
  45. $where[] = ['d.city', 'like', '%'.$city[1].'%'];
  46. }
  47. if ($admin['roles'] == 4){
  48. $where[] = ['d.hospital_id', '=', $admin['hospital_id']];
  49. }
  50. }
  51. $count['djd'] = \app\admin\model\diagnosis\DiagnosisOrder::alias('a')
  52. ->leftJoin('diagnosis_order_attr d', 'd.oid = a.id')->where('a.status', 0)->where($where)->where('a.paid', 1)->count();
  53. $count['yjd'] = \app\admin\model\diagnosis\DiagnosisOrder::alias('a')
  54. ->leftJoin('diagnosis_order_attr d', 'd.oid = a.id')->where('a.status', 1)->count();
  55. $count['dqd'] = \app\admin\model\diagnosis\DiagnosisOrder::alias('a')
  56. ->leftJoin('diagnosis_order_attr d', 'd.oid = a.id')->where('a.status', 2)->count();
  57. $count['ywc'] = \app\admin\model\diagnosis\DiagnosisOrder::alias('a')
  58. ->leftJoin('diagnosis_order_attr d', 'd.oid = a.id')->where('a.status', 3)->count();
  59. $count['sh'] = \app\admin\model\diagnosis\DiagnosisOrder::alias('a')
  60. ->leftJoin('diagnosis_order_attr d', 'd.oid = a.id')->where('a.after_sales', 1)->count();
  61. $count['qx'] = \app\admin\model\diagnosis\DiagnosisOrder::alias('a')
  62. ->leftJoin('diagnosis_order_attr d', 'd.oid = a.id')->where('a.status', -1)->where('refund_status',0)->count();
  63. $count['gq'] = \app\admin\model\diagnosis\DiagnosisOrder::alias('a')
  64. ->leftJoin('diagnosis_order_attr d', 'd.oid = a.id')->where('a.status', -2)->where('refund_status',0)->count();
  65. $this->assign([
  66. 'year' => get_month(),
  67. 'count' => $count
  68. ]);
  69. return $this->fetch();
  70. }
  71. public function list()
  72. {
  73. $where = Util::getMore([
  74. ['page', 1],
  75. ['limit', 20],
  76. ['name', ''],
  77. ['order_id', ''],
  78. ['type', ''],
  79. ['pay_type', ''],
  80. ['rece', ''],
  81. ['data', ''],
  82. ['status', ''],
  83. ]);
  84. $admin = $this->adminInfo;
  85. return Json::successlayui(model::list($where, $admin));
  86. }
  87. /**
  88. * 显示创建资源表单页.
  89. *
  90. * @return \think\Response
  91. */
  92. public function create($id = 0)
  93. {
  94. $f = [];
  95. $f[] = Form::input('name', '名称')->required();
  96. $f[] = Form::select('type', '选择类型', '')->options([
  97. ['value' => 1, 'label' => '陪诊'],
  98. ['value' => 2, 'label' => '代办'],
  99. ])->filterable(true)->required();
  100. $f[] = Form::input('price', '金额')->required();
  101. $f[] = Form::input('reward', '接单奖励')->required();
  102. $f[] = Form::radio('status', '状态', 1)->options([['value' => 0, 'label' => '禁用'], ['value' => 1, 'label' => '正常']]);
  103. $form = Form::make_post_form('添加', $f, Url::buildUrl('save'));
  104. $this->assign(compact('form'));
  105. return $this->fetch('public/form-builder');
  106. }
  107. public function save()
  108. {
  109. $model = new model;
  110. $data = Util::postMore([
  111. 'name',
  112. 'type',
  113. 'price',
  114. 'status',
  115. 'reward',
  116. ]);
  117. $res = $model->save($data);
  118. if ($res) return Json::successful('添加成功');
  119. return Json::fail('添加失败');
  120. }
  121. /**
  122. * 显示创建资源表单页.
  123. *
  124. * @return \think\Response
  125. */
  126. public function edit($id = 0)
  127. {
  128. $data = model::find($id);
  129. $f = [];
  130. $f[] = Form::input('name', '分类名称', $data['name'])->required();
  131. $f[] = Form::select('type', '选择分类', (string)$data['type'])->options([
  132. ['value' => 1, 'label' => '陪诊'],
  133. ['value' => 2, 'label' => '代办'],
  134. ])->filterable(true)->required();
  135. $f[] = Form::input('price', '金额', $data['price'])->required();
  136. $f[] = Form::input('reward', '接单奖励', $data['reward'])->required();
  137. $f[] = Form::radio('status', '状态', (string)$data['status'])->options([['value' => 0, 'label' => '禁用'], ['value' => 1, 'label' => '正常']]);
  138. $f[] = Form::hidden('id', $id);
  139. $form = Form::make_post_form('修改', $f, Url::buildUrl('update'));
  140. $this->assign(compact('form'));
  141. return $this->fetch('public/form-builder');
  142. }
  143. /**
  144. * 修改
  145. * @return void
  146. * @throws \think\db\exception\DataNotFoundException
  147. * @throws \think\db\exception\DbException
  148. * @throws \think\db\exception\ModelNotFoundException
  149. */
  150. public function update()
  151. {
  152. $model = new model;
  153. $data = Util::postMore([
  154. 'name',
  155. 'type',
  156. 'price',
  157. 'status',
  158. 'reward',
  159. 'id'
  160. ]);
  161. $details = $model->find($data['id']);
  162. $details['name'] = $data['name'];
  163. $details['type'] = $data['type'];
  164. $details['price'] = $data['price'];
  165. $details['status'] = $data['status'];
  166. $details['reward'] = $data['reward'];
  167. $res = $details->save();
  168. if ($res) return Json::successful('修改成功');
  169. return Json::fail('修改失败');
  170. }
  171. /**
  172. * 删除
  173. * @param $id
  174. * @return void
  175. * @throws \Exception
  176. */
  177. public function delete($id)
  178. {
  179. if (!$id) return Json::fail('删除失败');
  180. $model = new model;
  181. $res = model::destroy($id);
  182. if ($res){
  183. return Json::success('删除成功!');
  184. }else{
  185. return Json::fail($model->getErrorInfo());
  186. }
  187. }
  188. /**
  189. * 完成
  190. * @param $id
  191. * @return void
  192. * @throws \think\db\exception\DataNotFoundException
  193. * @throws \think\db\exception\DbException
  194. * @throws \think\db\exception\ModelNotFoundException
  195. */
  196. public function wc($id)
  197. {
  198. $order = model::find($id);
  199. $order['status'] = 2;
  200. $res = $order->save();
  201. if ($res){
  202. return Json::success('成功!');
  203. }else{
  204. return Json::fail('失败');
  205. }
  206. }
  207. /**
  208. * 完成
  209. * @param $id
  210. * @return void
  211. * @throws \think\db\exception\DataNotFoundException
  212. * @throws \think\db\exception\DbException
  213. * @throws \think\db\exception\ModelNotFoundException
  214. */
  215. public function sales($id)
  216. {
  217. $order = model::find($id);
  218. $order['after_sales'] = 2;
  219. $order['status'] = 3;
  220. $res = $order->save();
  221. if ($res){
  222. return Json::success('成功!');
  223. }else{
  224. return Json::fail('失败');
  225. }
  226. }
  227. /**
  228. * 设置单个产品上架|下架
  229. *
  230. * @return json
  231. */
  232. public function set_show($is_show = '', $id = '')
  233. {
  234. ($is_show == '' || $id == '') && Json::fail('缺少参数');
  235. $res = model::where(['id' => $id])->update(['status' => (int)$is_show]);
  236. if ($res) {
  237. return JsonService::successful($is_show == 1 ? '显示成功' : '隐藏成功');
  238. } else {
  239. return JsonService::fail($is_show == 1 ? '显示失败' : '隐藏失败');
  240. }
  241. }
  242. /**
  243. * 订单详情
  244. * @param $id
  245. * @return string
  246. * @throws \think\db\exception\DataNotFoundException
  247. * @throws \think\db\exception\DbException
  248. * @throws \think\db\exception\ModelNotFoundException
  249. */
  250. public function info($id)
  251. {
  252. $order = model::find($id);
  253. if ($order['images']){
  254. $order['images'] = json_decode($order['images']);
  255. $order['after_images'] = json_decode($order['after_images']);
  256. }
  257. $user = User::where('uid', $order['uid'])->find();//订单用户
  258. $re = User::where('uid', $order['order_receiving'])->find();//接单员
  259. if ($re){
  260. $apply = \app\admin\model\diagnosis\DiagnosisApply::where('uid', $re['uid'])->find();
  261. $re['bd_phone'] = $apply['phone'];
  262. $re['bd_name'] = $apply['name'];
  263. $re['sex'] = $apply['sex'] == 1? '男' : '女';
  264. }
  265. $attr = DiagnosisOrderAttr::where('oid', $id)->find();
  266. $cate = implode(',',\app\admin\model\diagnosis\DiagnosisCate::where('id', 'in', $order['cate_id'])->column('name'));
  267. $service = \app\admin\model\diagnosis\DiagnosisService::where('id', 'in', $attr['service_id'])->select();
  268. $card = UserCard::alias('a')
  269. ->field('b.name')
  270. ->leftJoin('card b', 'a.card_id = b.id')
  271. ->where('a.id', 'in', $order['user_card_id'])
  272. ->select();
  273. $attr['time'] = date('Y-m-d H:i:s', $attr['time']);
  274. $this->assign('order', $order);
  275. $this->assign('user', $user);
  276. $this->assign('re', $re);
  277. $this->assign('attr', $attr);
  278. $this->assign('cate', $cate);
  279. $this->assign('service', $service);
  280. $this->assign('card', $card);
  281. return $this->fetch();
  282. }
  283. public function refund($id)
  284. {
  285. $order = model::find($id);
  286. if (!$order) return JsonService::fail('数据不存在');
  287. $f = [];
  288. $f[] = Form::input('order_id', '退款单号', $order['order_id'])->disabled(1);
  289. $f[] = Form::number('refund_price', '退款金额', $order['pay_price'])->precision(2)->min(0.01);
  290. $f[] = Form::radio('type', '状态', 1)->options([['label' => '直接退款', 'value' => 1], ['label' => '退款后,返回原状态', 'value' => 2]]);
  291. $form = Form::make_post_form('退款处理', $f, Url::buildUrl('updateRefundY', array('id' => $id)), 7);
  292. $this->assign(compact('form'));
  293. return $this->fetch('public/form-builder');
  294. }
  295. /**
  296. * 退款处理
  297. * @param $id
  298. */
  299. public function updateRefundY($id)
  300. {
  301. $data = Util::postMore([
  302. 'refund_price',
  303. ['type', 1],
  304. ]);
  305. if (!$id) return $this->failed('数据不存在');
  306. $product = model::get($id);
  307. if (!$product) return Json::fail('数据不存在!');
  308. if ($product['pay_price'] == $product['refund_price']) return Json::fail('已退完支付金额!不能再退款了');
  309. if (!$data['refund_price']) return Json::fail('请输入退款金额');
  310. $refund_price = $data['refund_price'];
  311. $data['refund_price'] = bcadd($data['refund_price'], $product['refund_price'], 2);
  312. $bj = bccomp((float)$product['pay_price'], (float)$data['refund_price'], 2);
  313. if ($bj < 0) return Json::fail('退款金额大于支付金额,请修改退款金额');
  314. $data['refund_status'] = 1;
  315. $type = $data['type'];
  316. unset($data['type']);
  317. $refund_data['pay_price'] = $product['pay_price'];
  318. $refund_data['refund_price'] = $refund_price;
  319. if ($product['pay_type'] == 2) {
  320. try {
  321. WechatService::payOrderRefund($product['order_id'], $refund_data);
  322. } catch (\Exception $e) {
  323. return Json::fail($e->getMessage());
  324. }
  325. } else if ($product['pay_type'] == 1) {
  326. BaseModel::beginTrans();
  327. $usermoney = User::where('uid', $product['uid'])->value('now_money');
  328. $res1 = User::bcInc($product['uid'], 'now_money', $refund_price, 'uid');
  329. $res2 = UserBill::income('退款', $product['uid'], 'now_money', 'pay_product_refund', $refund_price, $product['id'], bcadd($usermoney, $refund_price, 2), '订单退款到余额' . floatval($refund_price) . '元');
  330. $res = $res1 && $res2;
  331. BaseModel::checkTrans($res);
  332. if (!$res) return Json::fail('余额退款失败!');
  333. }
  334. $resEdit = model::edit($data, $id);
  335. $res = true;
  336. if ($resEdit) {
  337. BaseModel::commitTrans();
  338. return Json::success('修改成功!');
  339. } else {
  340. return Json::fail('修改失败!');
  341. }
  342. }
  343. }