OrderLogic.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. <?php
  2. namespace app\mobile\logic;
  3. use app\model\AccountModel;
  4. use app\model\AddrModel;
  5. use app\model\CommonModel;
  6. use app\model\GoodModel;
  7. use app\model\InventoryExchangeModel;
  8. use app\model\MasterOrderModel;
  9. use app\model\OrderModel;
  10. use app\model\ServiceModel;
  11. use think\facade\Db;
  12. use think\Exception;
  13. use think\response\Json;
  14. class OrderLogic extends BaseLogic
  15. {
  16. //下单
  17. public static function add(array $data = []): json
  18. {
  19. Db::startTrans();
  20. try {
  21. $rs = AddrModel::field('id')
  22. ->where(['is_del' => CommonModel::$del_normal, 'id' => $data['addr_id'], 'uid' => self::$aid])
  23. ->findOrEmpty()
  24. ->isEmpty();
  25. if ($rs) throw new Exception('该地址不存在');
  26. $amount = '0';//支付金额
  27. $num = array_column($data['list'], 'num', 'id');//
  28. $date = date('Y-m-d H:i:s');
  29. $masterOrderCode = make_no('MS');
  30. //主订单数据
  31. $insert_master_order = [
  32. 'masterOrderCode' => $masterOrderCode,
  33. 'uid' => self::$aid,
  34. 'type' => $data['type'],
  35. 'createrid' => self::$aid,
  36. 'creater' => self::$aname,
  37. 'addtime' => $date,
  38. 'updaterid' => self::$aid,
  39. 'updater' => self::$aname,
  40. 'updatetime' => $date,
  41. 'remark' => $data['remark'],
  42. 'addr_id' => $data['addr_id'],
  43. ];
  44. //子订单数据
  45. $insert_order = [];
  46. //库存变动记录数据
  47. $insert_inventory_exchange_log = [];
  48. //兑换商品
  49. if ($data['type'] == MasterOrderModel::$type_exchange_good) {
  50. $rs = GoodModel::alias('a')
  51. ->leftJoin('inventory_exchange c', 'c.account_id=' . self::$aid . ' AND c.good_id=a.id')
  52. ->whereIn('a.id', array_column($data['list'], 'id'))
  53. ->where(['a.is_del' => CommonModel::$del_normal, 'a.status' => CommonModel::$status_normal, 'type' => GoodModel::$type_exchange])
  54. ->column('a.id,a.good_code,a.good_name,a.moq,c.inventory,c.id inventory_id', 'a.id');
  55. if (empty($rs)) throw new Exception('购买的商品不存在或已禁用');
  56. $i = 0;
  57. $orderCode = make_no('QR');
  58. $orderCode = substr($orderCode, 0, -2) . str_pad($i, 2, '0', STR_PAD_LEFT);
  59. foreach ($rs as $item) {
  60. if ($item['moq'] > $num[$item['id']]) throw new Exception($item['good_name'] . '不满足起订量,' . $item['moq']);
  61. if ($item['inventory'] < $num[$item['id']]) throw new Exception($item['good_name'] . '的库存数不足');
  62. $insert_order[] = [
  63. 'masterOrderCode' => $masterOrderCode,
  64. 'orderCode' => $orderCode,
  65. 'company_id' => self::$company_id,
  66. 'card_id' => self::$card_id,
  67. 'uid' => self::$aid,
  68. 'child_id' => $item['id'],
  69. 'child_title' => $item['good_name'],
  70. 'num' => $num[$item['id']],
  71. 'price' => 0,
  72. 'total_price' => 0,
  73. 'addr_id' => $data['addr_id'],
  74. 'type' => $data['type'],
  75. 'status' => MasterOrderModel::$order_status_not_deliver,
  76. 'is_del' => CommonModel::$del_normal,
  77. 'createrid' => self::$aid,
  78. 'creater' => self::$aname,
  79. 'addtime' => $date,
  80. 'updaterid' => self::$aid,
  81. 'updater' => self::$aname,
  82. 'updatetime' => $date,
  83. ];
  84. //扣减库存
  85. $after_inventory = bcsub($item['inventory'], $num[$item['id']]);
  86. //添加库存变动记录
  87. $insert_inventory_exchange_log[] = [
  88. 'inventory_exchange_id' => $item['inventory_id'],
  89. 'before_inventory' => $item['inventory'],
  90. 'after_inventory' => $after_inventory,
  91. 'flag' => '-1',
  92. 'good_id' => $item['id'],
  93. 'good_code' => $item['good_code'],
  94. 'good_name' => $item['good_name'],
  95. 'source' => CommonModel::$source_account,
  96. 'createrid' => self::$aid,
  97. 'creater' => self::$aname,
  98. 'addtime' => $date
  99. ];
  100. //扣减库存
  101. InventoryExchangeModel::where('id', $item['inventory_id'])
  102. ->save(['inventory' => $after_inventory, 'updaterid' => self::$aid, 'updater' => self::$aname, 'updatetime' => $date]);
  103. $i++;
  104. }
  105. //更新主订单表数据
  106. $insert_master_order = array_merge($insert_master_order, [
  107. 'wx_openId' => '',
  108. 'prepay_info' => '',
  109. 'amount' => $amount,
  110. 'transaction_id' => '',
  111. 'trade_type' => '',
  112. 'notify_result' => '',
  113. 'expiretime' => $date,
  114. 'status' => MasterOrderModel::$order_status_not_deliver,
  115. ]);
  116. $res = ['is_need_pay' => 0, 'result' => ''];
  117. } else {
  118. $openId = AccountModel::where(['id' => self::$aid, 'is_del' => CommonModel::$del_normal])
  119. ->value('wx_openId', '');
  120. if ($openId == '') return json_show(CommonModel::$error_token, '获取账户openId失败,请重新登录');
  121. switch ($data['type']) {
  122. //服务订单
  123. case MasterOrderModel::$type_service:
  124. $rs = ServiceModel::field('id,original_price,activity_price,title,activity_status')
  125. ->whereIn('id', array_column($data['list'], 'id'))
  126. ->where(['is_del' => CommonModel::$del_normal, 'status' => CommonModel::$status_normal])
  127. ->select()
  128. ->toArray();
  129. if (empty($rs)) throw new Exception('购买的服务不存在或已下架');
  130. $i = 0;
  131. $orderCode = make_no('QR');
  132. $orderCode = substr($orderCode, 0, -2) . str_pad($i, 2, '0', STR_PAD_LEFT);
  133. foreach ($rs as $item) {
  134. //单价
  135. if ($item['activity_status'] == ServiceModel::$activity_status_ing) $price = $rs['activity_price'];
  136. else $price = $rs['original_price'];
  137. $total_price = bcmul($price, $num[$item['id']] ?? 0, 2);//总价
  138. $amount = bcadd($amount, $total_price, 2);//总金额
  139. $insert_order[] = [
  140. 'masterOrderCode' => $masterOrderCode,
  141. 'orderCode' => $orderCode,
  142. 'company_id' => self::$company_id,
  143. 'card_id' => self::$card_id,
  144. 'uid' => self::$aid,
  145. 'child_id' => $item['id'],
  146. 'child_title' => $item['title'],
  147. 'num' => $num[$item['id']] ?? 0,
  148. 'price' => $price,
  149. 'total_price' => $total_price,
  150. 'addr_id' => $data['addr_id'],
  151. 'type' => $data['type'],
  152. 'status' => MasterOrderModel::$order_status_wait_pay,
  153. 'is_del' => CommonModel::$del_normal,
  154. 'createrid' => self::$aid,
  155. 'creater' => self::$aname,
  156. 'addtime' => $date,
  157. 'updaterid' => self::$aid,
  158. 'updater' => self::$aname,
  159. 'updatetime' => $date,
  160. ];
  161. $i++;
  162. }
  163. break;
  164. //商城商品订单
  165. case MasterOrderModel::$type_shopping_good:
  166. $rs = GoodModel::alias('a')
  167. ->leftJoin('inventory_exchange c', 'c.account_id=' . self::$aid . ' AND c.good_id=a.id')
  168. ->whereIn('a.id', array_column($data['list'], 'id'))
  169. ->where(['a.is_del' => CommonModel::$del_normal, 'a.status' => CommonModel::$status_normal, 'type' => GoodModel::$type_shopping])
  170. ->column('a.id,a.good_code,a.good_name,a.moq,a.price,c.inventory,c.id inventory_id', 'a.id');
  171. if (empty($rs)) throw new Exception('购买的商品不存在或已禁用');
  172. $i = 0;
  173. $orderCode = make_no('QR');
  174. $orderCode = substr($orderCode, 0, -2) . str_pad($i, 2, '0', STR_PAD_LEFT);
  175. foreach ($rs as $item) {
  176. if ($item['moq'] > $num[$item['id']]) throw new Exception('不满足该商品的起订量,' . $item['moq']);
  177. if ($item['inventory'] < $num[$item['id']]) throw new Exception('库存数不足');
  178. $total_price = bcmul($rs['price'], $num[$item['id']] ?? 0, 2);//总价
  179. $amount = bcadd($amount, $total_price, 2);//总金额
  180. $insert_order[] = [
  181. 'masterOrderCode' => $masterOrderCode,
  182. 'orderCode' => $orderCode,
  183. 'company_id' => self::$company_id,
  184. 'card_id' => self::$card_id,
  185. 'uid' => self::$aid,
  186. 'child_id' => $item['id'],
  187. 'child_title' => $item['good_name'],
  188. 'num' => $num[$item['id']],
  189. 'price' => $rs['price'],
  190. 'total_price' => $total_price,
  191. 'addr_id' => $data['addr_id'],
  192. 'type' => $data['type'],
  193. 'status' => MasterOrderModel::$order_status_wait_pay,
  194. 'is_del' => CommonModel::$del_normal,
  195. 'createrid' => self::$aid,
  196. 'creater' => self::$aname,
  197. 'addtime' => $date,
  198. 'updaterid' => self::$aid,
  199. 'updater' => self::$aname,
  200. 'updatetime' => $date,
  201. ];
  202. //扣减库存
  203. $after_inventory = bcsub($item['inventory'], $num[$item['id']]);
  204. //添加库存变动记录
  205. $insert_inventory_exchange_log[] = [
  206. 'inventory_exchange_id' => $item['inventory_id'],
  207. 'before_inventory' => $item['inventory'],
  208. 'after_inventory' => $after_inventory,
  209. 'flag' => '-1',
  210. 'good_id' => $item['id'],
  211. 'good_code' => $item['good_code'],
  212. 'good_name' => $item['good_name'],
  213. 'source' => CommonModel::$source_account,
  214. 'createrid' => self::$aid,
  215. 'creater' => self::$aname,
  216. 'addtime' => $date
  217. ];
  218. //扣减库存
  219. InventoryExchangeModel::where('id', $item['inventory_id'])
  220. ->save(['inventory' => $after_inventory, 'updaterid' => self::$aid, 'updater' => self::$aname, 'updatetime' => $date]);
  221. $i++;
  222. }
  223. break;
  224. }
  225. //获取预支付信息,
  226. //@todo body要写什么????????
  227. // $result = WechatLogic::getPrepayId($openId, '', $masterOrderCode, $amount);
  228. //更新主订单表数据
  229. $insert_master_order = array_merge($insert_master_order, [
  230. 'wx_openId' => $openId,
  231. 'prepay_info' => '',//预支付信息
  232. 'amount' => $amount,
  233. 'transaction_id' => '',
  234. 'trade_type' => '',
  235. 'notify_result' => '',
  236. 'expiretime' => $date,
  237. 'status' => MasterOrderModel::$order_status_wait_pay,
  238. ]);
  239. $res = ['is_need_pay' => 1, 'result' => ''];
  240. }
  241. //处理数据
  242. MasterOrderModel::create($insert_master_order)->save();
  243. if ($insert_inventory_exchange_log) Db::name('inventory_exchange_log')->insertAll($insert_inventory_exchange_log);
  244. if ($insert_order) Db::name('order')->insertAll($insert_order);
  245. Db::commit();
  246. return json_show(CommonModel::$success, '下单成功', $res);
  247. } catch (Exception $exception) {
  248. Db::rollback();
  249. return json_show(CommonModel::$error_param, '下单失败,' . $exception->getMessage());
  250. }
  251. }
  252. //订单列表
  253. public static function list(array $data = []): Json
  254. {
  255. $db = MasterOrderModel::where(['uid' => self::$aid]);
  256. if ($data['status'] != '') $db->where('status', $data['status']);
  257. $count = $db->count('id');
  258. $list = $db
  259. ->field('id,masterOrderCode,amount,status,addtime,type')
  260. ->append(['order_list'])
  261. ->withAttr('order_list', function ($val, $da) {
  262. return OrderModel::field('id,orderCode,child_title,price,num,total_price')
  263. ->where(['is_del' => CommonModel::$del_normal, 'masterOrderCode' => $da['masterOrderCode'], 'uid' => self::$aid])
  264. ->order('id')
  265. ->select()
  266. ->toArray();
  267. })
  268. ->page($data['page'], $data['size'])
  269. ->order('id', 'desc')
  270. ->select()
  271. ->toArray();
  272. return json_show(CommonModel::$success, '获取订单列表成功', ['count' => $count, 'list' => $list]);
  273. }
  274. //订单详情
  275. public static function info(int $id = 0): Json
  276. {
  277. $rs = MasterOrderModel::alias('a')
  278. ->field('a.id,a.masterOrderCode,a.amount,a.status,a.addtime,c.contactor,c.mobile,c.addr,c.addr_code,a.transaction_id,a.remark,a.type')
  279. ->leftJoin('addr c', 'c.id=a.addr_id')
  280. ->append(['order_list', 'express_data'])
  281. ->withAttr('order_list', function ($val, $da) {
  282. return OrderModel::field('id,orderCode,child_title,child_cover,price,num,total_price,post_code')
  283. ->where(['is_del' => CommonModel::$del_normal, 'masterOrderCode' => $da['masterOrderCode'], 'uid' => self::$aid])
  284. ->order('id')
  285. ->select()
  286. ->toArray();
  287. })
  288. ->withAttr('addr', function ($val, $da) {
  289. return $da['addr_code'] ? get_addr_name($da['addr_code']) . $val : $val;
  290. })->where('a.id', $id)
  291. ->findOrEmpty()
  292. ->toArray();
  293. return $rs ? json_show(CommonModel::$success, '获取订单详情成功', $rs) : json_show(CommonModel::$error_param, '该订单不存在');
  294. }
  295. //物流信息
  296. public static function express(string $post_code = ''): Json
  297. {
  298. $res = Db::name('express_data')
  299. ->where('post_code', $post_code)
  300. ->field(true)
  301. ->withAttr('post_data', function ($val) {
  302. return json_decode($val, true);
  303. })
  304. ->findOrEmpty();
  305. return json_show(CommonModel::$success, '获取快递单号的物流信息成功', $res);
  306. }
  307. }