OrderLogic.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?php
  2. namespace app\mobile\logic;
  3. use app\model\AddrModel;
  4. use app\model\CommonModel;
  5. use app\model\CompanyGoodModel;
  6. use app\model\GoodModel;
  7. use app\model\InventoryExchangeModel;
  8. use app\model\OrderModel;
  9. use think\facade\Db;
  10. use think\response\Json;
  11. class OrderLogic extends BaseLogic
  12. {
  13. //兑换商品下单
  14. public static function add(array $data = []): Json
  15. {
  16. Db::startTrans();
  17. try {
  18. //判断收货地址
  19. $rs = AddrModel::field('id')
  20. ->where(['is_del' => CommonModel::$del_normal, 'id' => $data['addr_id'], 'uid' => self::$aid])
  21. ->findOrEmpty()
  22. ->isEmpty();
  23. if ($rs) throw new Exception('该地址不存在');
  24. //判断商品
  25. $rs = CompanyGoodModel::alias('a')
  26. ->field('a.id,a.good_id,b.good_code,b.good_name,b.status,b.moq,c.id inventory_id,c.inventory')
  27. ->leftJoin('good b', 'b.id=a.good_id AND b.is_del=' . CommonModel::$del_normal)
  28. ->leftJoin('inventory_exchange c', 'c.account_id=' . self::$aid . ' AND c.good_id=a.good_id')
  29. ->where(['a.is_del' => CommonModel::$del_normal, 'a.group_id' => self::$group_id, 'a.good_id' => $data['good_id']])
  30. ->findOrEmpty();
  31. if ($rs->isEmpty()) throw new Exception('该商品不存在或不允许兑换');
  32. if ($rs->status != CommonModel::$status_normal) throw new Exception('该商品已被禁用');
  33. if ($rs->moq > $data['num']) throw new Exception('不满足该商品的起订量,' . $rs->moq);
  34. if ($rs->inventory < $data['num']) throw new Exception('库存数不足');
  35. $date = date('Y-m-d H:i:s');
  36. $after_inventory = bcsub($rs->inventory, $data['num']);
  37. //添加库存变动记录
  38. Db::name('inventory_exchange_log')
  39. ->insert([
  40. 'inventory_exchange_id' => $rs->inventory_id,
  41. 'before_inventory' => $rs->inventory,
  42. 'after_inventory' => $after_inventory,
  43. 'flag' => '-1',
  44. 'good_id' => $rs->good_id,
  45. 'good_code' => $rs->good_code,
  46. 'good_name' => $rs->good_name,
  47. 'source' => CommonModel::$source_account,
  48. 'createrid' => self::$aid,
  49. 'creater' => self::$aname,
  50. 'addtime' => $date
  51. ]);
  52. //扣减库存
  53. InventoryExchangeModel::where('id', $rs->inventory_id)
  54. ->save(['inventory' => $after_inventory, 'updaterid' => self::$aid, 'updater' => self::$aname, 'updatetime' => $date]);
  55. //生成兑换订单
  56. OrderModel::create([
  57. 'orderCode' => make_no('QR'),
  58. 'company_id' => self::$company_id,
  59. 'card_id' => self::$card_id,
  60. 'uid' => self::$aid,
  61. 'good_id' => $rs->good_id,
  62. 'num' => $data['num'],
  63. 'price' => 0,
  64. 'total_price' => 0,
  65. 'addr_id' => $data['addr_id'],
  66. 'type' => GoodModel::$type_exchange,
  67. 'remark' => $data['remark'],
  68. 'status' => CommonModel::$order_status_not_deliver,
  69. 'is_del' => CommonModel::$del_normal,
  70. 'creater' => self::$aname,
  71. 'createrid' => self::$aid,
  72. 'addtime' => $date,
  73. 'updater' => self::$aname,
  74. 'updaterid' => self::$aid,
  75. 'updatetime' => $date,
  76. ])->save();
  77. Db::commit();
  78. return json_show(CommonModel::$success, '兑换商品成功');
  79. } catch (Exception $exception) {
  80. Db::rollback();
  81. return json_show(CommonModel::$error_param, '兑换失败,' . $exception->getMessage());
  82. }
  83. }
  84. //订单列表
  85. public static function list(array $data = []): Json
  86. {
  87. $db = OrderModel::alias('a')
  88. ->leftJoin('good b', 'b.id=a.good_id AND b.is_del=' . CommonModel::$del_normal)
  89. ->where(['a.is_del'=>CommonModel::$del_normal, 'a.uid' => self::$aid]);
  90. if ($data['status'] != '') $db->where('a.status', $data['status']);
  91. $count = $db->count('a.id');
  92. $list = $db
  93. ->field('a.id,a.orderCode,b.good_name,a.num,a.status,a.addtime,a.type,a.price,a.total_price')
  94. ->page($data['page'], $data['size'])
  95. ->order('a.id', 'desc')
  96. ->select()
  97. ->toArray();
  98. return json_show(CommonModel::$success, '获取订单列表成功', ['count' => $count, 'list' => $list]);
  99. }
  100. //订单详情
  101. public static function info(int $id = 0): Json
  102. {
  103. $rs = OrderModel::alias('a')
  104. ->field('a.id,a.orderCode,a.status,a.addtime,a.type,b.good_name,a.num,c.contactor,c.mobile,c.addr,c.addr_code,a.price,a.total_price,a.post_name,a.post_code,a.remark,"支付流水号" p')
  105. ->leftJoin('good b', 'b.id=a.good_id AND b.is_del=' . CommonModel::$del_normal)
  106. ->leftJoin('addr c', 'c.id=a.addr_id AND c.is_del=' . CommonModel::$del_normal)
  107. ->leftJoin('pay_info d', 'd.id=a.pay_info_id')
  108. ->where(['a.is_del' => CommonModel::$del_normal, 'a.id' => $id, 'a.uid' => self::$aid])
  109. ->withAttr('addr', function ($val, $da) {
  110. return $da['addr_code'] ? get_addr_name($da['addr_code']) . $val : $val;
  111. })
  112. ->findOrEmpty()
  113. ->toArray();
  114. return $rs ? json_show(CommonModel::$success, '获取订单详情成功', $rs) : json_show(CommonModel::$error_param, '该订单不存在');
  115. }
  116. }