Order.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. namespace app\controller\api;
  3. use app\extra\basic\Base;
  4. use app\extra\tools\CodeExtend;
  5. use app\middleware\WxMiddleware;
  6. use app\model\saas\SaasCart;
  7. use app\model\saas\SaasDiscount;
  8. use app\model\saas\SaasOrder;
  9. use app\model\saas\SaasPrintClient;
  10. use app\model\saas\SaasUser;
  11. use LinFly\Annotation\Route\Controller;
  12. use LinFly\Annotation\Route\Middleware;
  13. use LinFly\Annotation\Route\Route;
  14. use support\Request;
  15. use support\Response;
  16. #[Controller(prefix: "/wx_api/order"),Middleware(WxMiddleware::class)]
  17. class Order extends Base
  18. {
  19. protected array $noNeedLogin = [];
  20. protected array $types = [
  21. '1_1_1' => ['name' => '彩色-单面', 'amount' => 0, 'quantity' => 0,'discount' => 0],
  22. '1_2_1' => ['name' => '彩色-双面', 'amount' => 0, 'quantity' => 0,'discount' => 0],
  23. '2_1_1' => ['name' => '黑白-单面', 'amount' => 0, 'quantity' => 0,'discount' => 0],
  24. '2_2_1' => ['name' => '黑白-双面', 'amount' => 0, 'quantity' => 0,'discount' => 0],
  25. ];
  26. /**
  27. * @param Request $request
  28. * @return Response
  29. */
  30. #[Route(path: "create",methods: "post")]
  31. public function createOrder(Request $request): Response
  32. {
  33. try {
  34. $param = $this->_valid([
  35. "shop.require" => trans("empty.require"),
  36. "print.require" => trans("empty.require"),
  37. "pay.require" => trans("empty.require"),
  38. "printName.default" => "",
  39. "express.require" => trans("empty.require"),
  40. "card.default" => [],
  41. "gift.default" => 0
  42. ],$request->method());
  43. if (!is_array($param)) return error($param);
  44. $cart = (new SaasCart)->where("shop_id",$param['shop'])->order("create_at desc")->select();
  45. if ($cart->isEmpty()) return success('ok',['cart' => []]);
  46. $totalAmount = $totalDiscount = 0;
  47. foreach ($cart as $k=>$v){
  48. $key = $v['color'] . '_' . $v['duplex'] . '_' . $v['source'];
  49. if (isset($this->types[$key])) {
  50. $this->types[$key]['quantity'] += $v['page'];
  51. $this->types[$key]['amount'] += $v['money'];
  52. }
  53. $cart[$k] = $v;
  54. $cart[$k]['money'] = format_money($v['money'] / 100,2);
  55. $cart[$k]['name'] = msubstr($v['name'],0,12);
  56. }
  57. $printData = (new SaasPrintClient)->where("shop_id",$param['shop'])->where("code",$param['print'])->select();
  58. if ($printData->isEmpty()) return error('无可用打印机');
  59. // 计算折扣
  60. foreach ($this->types as $k=>$v) {
  61. $discount = (new SaasDiscount)->where("shop_id",$param['shop'])->where("keys",$k)->where("number",'<',$v['quantity'])->findOrEmpty();
  62. if (!$discount->isEmpty()) {
  63. $v['discount'] = round($v['amount'] * $discount['rate']);
  64. $this->types[$k]['discount'] = $v['discount'];
  65. }
  66. $totalAmount += $v['amount']; // 实际金额
  67. $totalDiscount += $v['discount']; // 折扣后金额
  68. }
  69. $orderSn = CodeExtend::uniqidDate(16).date("is").rand(1,9);
  70. $totalDay = (new SaasOrder)->where("shop_id",$param['shop'])->whereDay("create_at")->count();
  71. $orderData = [
  72. "shop_id" => $param['shop'], // 所属店铺
  73. "parent_id" => $param['shop'], // 消费店铺
  74. "order_sn" => $orderSn,
  75. "money" => $totalAmount,
  76. "discount" => $totalDiscount,
  77. "uuid" => $request->user['id'],
  78. "print_name" => $param['printName'],
  79. "package" => $param['express'],
  80. "package_sn" => date('md')."-".sprintf("%02d",($totalDay+1)),
  81. "extra_money" => "",
  82. "remark" => $param['remark']??''
  83. ];
  84. if ($param['pay'] == 2) { // 会员卡支付或开通会员卡并充值
  85. $card = (new SaasUser)->where("openid",$request->user['openid'])->where("shop_id",$param['shop'])->field("balance")->findOrEmpty();
  86. if ($card->isEmpty()) // 创建会员并发起支付
  87. {
  88. }
  89. if ($totalDiscount > 0 && $totalDiscount > $card['balance']) {
  90. return error("卡内余额不足~");
  91. }
  92. if ($totalDiscount == 0 && $totalAmount > $card['balance']) {
  93. return error("卡内余额不足");
  94. }
  95. }
  96. $orderDetail = [];
  97. foreach ($cart as $key=>$val)
  98. {
  99. unset($val['id']);
  100. $orderDetail[$key] = $val;
  101. $orderDetail[$key]['money'] = $val['money']*100;
  102. $orderDetail[$key]['order_sn'] = $orderSn;
  103. $orderDetail[$key]['print_name'] = $param['printName'];
  104. }
  105. print_r($orderDetail);
  106. } catch (\Throwable $throwable) {
  107. echo $throwable->getLine()."\n";
  108. return error($throwable->getMessage());
  109. }
  110. }
  111. }