DiagnosisOrder.php 10.0 KB

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