StoreCashierOrderDetail.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. namespace addons\Store\common\models;
  3. use common\enums\OrderStatusEnum;
  4. use common\enums\RedisKeyEnum;
  5. use common\enums\StatusEnum;
  6. use common\helpers\LockHelper;
  7. /**
  8. * This is the model class for table "{{%addons_store_cashier_order_detail}}".
  9. *
  10. * @property int $id 订单项ID
  11. * @property int $cashier_order_id 收银台订单ID
  12. * @property int $user_id 用户id
  13. * @property int $mall_id 商城ID
  14. * @property int $goods_id 商品ID
  15. * @property string $goods_name 商品名称
  16. * @property int $goods_attr_id 规格ID
  17. * @property string $goods_attr_name 规格名称
  18. * @property string $sign_id 规格ID标识
  19. * @property string|null $attr_groups_format 格式化规格组
  20. * @property float $price 商品价格
  21. * @property float $cost_price 商品成本价
  22. * @property float $original_goods_price 商品原本总价
  23. * @property float $goods_price 商品优惠后总价
  24. * @property int $num 购买数量
  25. * @property string $pic_url 商品图片
  26. * @property int $order_status 订单状态【0待付款 1待发货 2 已发货 3已收货 4已完成 -1申请售后 -2售后中 -3售后完成 -4 已关闭 -5撤销售后】
  27. * @property int $is_virtual 是否包含 虚拟商品 0 不包含 1 包含
  28. * @property int $status 状态[-1:删除;0:禁用;1启用]
  29. * @property string|null $extra_info 额外信息
  30. * @property int $created_at
  31. * @property int $updated_at
  32. * @property int $store_id 门店id
  33. */
  34. class StoreCashierOrderDetail extends \common\models\base\BaseActiveRecord
  35. {
  36. /**
  37. * {@inheritdoc}
  38. */
  39. public static function tableName()
  40. {
  41. return '{{%addons_store_cashier_order_detail}}';
  42. }
  43. /**
  44. * {@inheritdoc}
  45. */
  46. public function rules()
  47. {
  48. return [
  49. [['cashier_order_id', 'user_id', 'mall_id', 'goods_id', 'goods_attr_id', 'num', 'order_status', 'is_virtual', 'status', 'created_at', 'updated_at', 'store_id'], 'integer'],
  50. [['attr_groups_format', 'extra_info'], 'string'],
  51. [['price', 'cost_price', 'original_goods_price', 'goods_price'], 'number'],
  52. [['goods_name'], 'string', 'max' => 200],
  53. [['goods_attr_name', 'sign_id', 'pic_url'], 'string', 'max' => 255],
  54. ];
  55. }
  56. /**
  57. * {@inheritdoc}
  58. */
  59. public function attributeLabels()
  60. {
  61. return [
  62. 'id' => '订单项ID',
  63. 'cashier_order_id' => '收银台订单ID',
  64. 'user_id' => '用户id',
  65. 'mall_id' => '商城ID',
  66. 'goods_id' => '商品ID',
  67. 'goods_name' => '商品名称',
  68. 'goods_attr_id' => '规格ID',
  69. 'goods_attr_name' => '规格名称',
  70. 'sign_id' => '规格ID标识',
  71. 'attr_groups_format' => '格式化规格组',
  72. 'price' => '商品价格',
  73. 'cost_price' => '商品成本价',
  74. 'original_goods_price' => '商品原本总价',
  75. 'goods_price' => '商品优惠后总价',
  76. 'num' => '购买数量',
  77. 'pic_url' => '商品图片',
  78. 'order_status' => '订单状态【0待付款 1待发货 2 已发货 3已收货 4已完成 -1申请售后 -2售后中 -3售后完成 -4 已关闭 -5撤销售后】',
  79. 'is_virtual' => '是否包含 虚拟商品 0 不包含 1 包含',
  80. 'status' => '状态[-1:删除;0:禁用;1启用]',
  81. 'extra_info' => '额外信息',
  82. 'created_at' => 'Created At',
  83. 'updated_at' => 'Updated At',
  84. 'store_id' => '门店id',
  85. ];
  86. }
  87. /**
  88. * @Description: 批量保存数据
  89. * @Author: zsan
  90. * @DateTime 2023-08-10 18:27:53
  91. * @param array $attrs
  92. * @param array $info
  93. * @return boolean
  94. * @throws \Exception
  95. */
  96. public static function batchDataSave($attrs, $info)
  97. {
  98. $cashier_order_id = $info['cashier_order_id'];
  99. $user_id = $info['user_id'];
  100. $mall_id = $info['mall_id'];
  101. $store_id = $info['store_id'];
  102. $goods_attrs = StoreGoodsAttr::lists([
  103. 'where' => [['id' => array_column($attrs, 'attr_id')], ['status' => StatusEnum::ENABLED]],
  104. 'with' => ['goods' => function ($q) {
  105. return $q->select('id, goods_name, goods_type, cover_pic');
  106. }]
  107. ]);
  108. if (empty($goods_attrs)) throw new \Exception('商品属性ID错误');
  109. $nums = array_column($attrs, 'num', 'attr_id');
  110. $goods_type = [];
  111. $goods_stock_ids = [];
  112. foreach ($goods_attrs as $v) {
  113. $result = StoreGoodsAttr::handleStock($v['id'], $nums[$v['id']] * -1);
  114. if ($result === false) {
  115. throw new \Exception(StoreGoodsAttr::getStaticError());
  116. }
  117. $goods_stock_ids[] = $v['goods_id'];
  118. $goods_type[$v['goods_id']] = $v['goods']['goods_type'];
  119. }
  120. $stock_locks = [];
  121. foreach($goods_stock_ids as $id){
  122. $lock_key = RedisKeyEnum::suffix(RedisKeyEnum::LOCK_GOODS , $id);
  123. $identification = uniqid();
  124. if (!LockHelper::lock($lock_key, $identification, 30, 60)) throw new \Exception('当前访问人数过多');
  125. $stock_locks[$lock_key] = $identification;
  126. $stock = StoreGoodsAttr::find()->where(['goods_id'=>$id])->sum('stock');
  127. $stock=$stock??0;
  128. $res = StoreGoods::updateAll(['stock'=>$stock],['id'=>$id]);
  129. if(!$res) throw new \Exception(StoreGoods::getStaticError());
  130. }
  131. LockHelper::batchUlock($stock_locks);
  132. $rows = [];
  133. foreach ($goods_attrs as $goods_attr) {
  134. $rows[] = [
  135. $cashier_order_id, $user_id, $mall_id, $store_id, $goods_attr['goods_id'],
  136. $goods_attr['goods']['goods_name'], $goods_attr['id'], $goods_attr['name'], $goods_attr['sign_id'], $goods_attr['price'],
  137. $goods_attr['cost_price'], bcmul($goods_attrs['original_goods_price'], $nums[$goods_attr['id']], 2),
  138. $info['pay_money'], $nums[$goods_attr['id']],$goods_type[$goods_attr['goods_id']],($goods_attr['pic_url'] ? $goods_attr['pic_url'] : $goods_attr['goods']['cover_pic']),
  139. OrderStatusEnum::NOT_PAY, StatusEnum::ENABLED, time(), time()
  140. ];
  141. }
  142. $columns = [
  143. 'cashier_order_id', 'user_id', 'mall_id', 'store_id', 'goods_id', 'goods_name',
  144. 'goods_attr_id', 'goods_attr_name', 'sign_id', 'price', 'cost_price', 'original_goods_price',
  145. 'goods_price', 'num', 'is_virtual', 'pic_url', 'order_status', 'status', 'created_at', 'updated_at'
  146. ];
  147. $res = \Yii::$app->db->createCommand()->batchInsert(self::tableName(), $columns, $rows)->execute();
  148. if ($res == 0) {
  149. throw new \Exception('订单详情数据存储失败');
  150. }
  151. return true;
  152. }
  153. }