DiagnosisOrder.php 12 KB

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