OrderLogic.php 16 KB

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