DiagnosisOrder.php 11 KB

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