DiagnosisOrder.php 11 KB

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