HappinessCashierOrderDetail.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <?php
  2. namespace addons\Happiness\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_happiness_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 HappinessCashierOrderDetail extends \common\models\base\BaseActiveRecord
  35. {
  36. /**
  37. * {@inheritdoc}
  38. */
  39. public static function tableName()
  40. {
  41. return '{{%addons_happiness_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 = HappinessStoreGoodsAttr::lists([
  103. 'where' => [['attr_id' => array_column($attrs, 'attr_id')], ['status' => StatusEnum::ENABLED],
  104. ['store_id'=>$store_id],['status'=>StatusEnum::ENABLED]],
  105. 'with' => ['goods' => function ($q) {
  106. return $q->select('id, goods_name, goods_type, cover_pic');
  107. },'attr']
  108. ]);
  109. if (empty($goods_attrs)) throw new \Exception('商品属性ID错误');
  110. $nums = array_column($attrs, 'num', 'attr_id');
  111. $goods_type = [];
  112. $goods_stock_ids = [];
  113. foreach ($goods_attrs as $v) {
  114. //$result = HappinessStoreGoodsAttr::handleStock($v['attr_id'], $nums[$v['attr_id']] * -1,$store_id);
  115. //if ($result === false) {
  116. // throw new \Exception(HappinessStoreGoodsAttr::getStaticError());
  117. //}
  118. $goods_stock_ids[] = $v['goods_id'];
  119. $goods_type[$v['goods_id']] = $v['goods']['goods_type'];
  120. }
  121. $stock_locks = [];
  122. //foreach($goods_stock_ids as $id){
  123. // $lock_key = RedisKeyEnum::suffix(RedisKeyEnum::LOCK_GOODS , 'happiness_'.$store_id.'_'.$id);
  124. // $identification = uniqid();
  125. // if (!LockHelper::lock($lock_key, $identification, 30, 60)) throw new \Exception('当前访问人数过多');
  126. // $stock_locks[$lock_key] = $identification;
  127. // $stock = HappinessStoreGoodsAttr::find()->where(['goods_id'=>$id,'store_id'=>$store_id,
  128. // 'status'=>StatusEnum::ENABLED])->sum('stock');
  129. // $stock=$stock??0;
  130. // $res = HappinessStoreGoods::updateAll(['stock'=>$stock],['goods_id'=>$id,'status'=>StatusEnum::ENABLED,
  131. //'store_id'=>$store_id]);
  132. // if(!$res) throw new \Exception(HappinessStoreGoods::getStaticError());
  133. //}
  134. //LockHelper::batchUlock($stock_locks);
  135. $rows = [];
  136. foreach ($goods_attrs as $goods_att) {
  137. $goods_attr=$goods_att['attr'];
  138. $goods_attr['goods']=$goods_att['goods'];
  139. $rows[] = [
  140. $cashier_order_id, $user_id, $mall_id, $store_id, $goods_attr['goods_id'],
  141. $goods_attr['goods']['goods_name'], $goods_attr['id'], $goods_attr['name'], $goods_attr['sign_id'], $goods_attr['price'],
  142. $goods_attr['cost_price'], bcmul($goods_attr['price'], $nums[$goods_attr['id']], 2),
  143. $info['pay_money'], $nums[$goods_attr['id']],$goods_type[$goods_attr['goods_id']],
  144. ($goods_attr['pic_url'] ?
  145. $goods_attr['pic_url'] : $goods_attr['goods']['cover_pic']),
  146. OrderStatusEnum::NOT_PAY, StatusEnum::ENABLED, time(), time()
  147. ];
  148. }
  149. $columns = [
  150. 'cashier_order_id', 'user_id', 'mall_id', 'store_id', 'goods_id', 'goods_name',
  151. 'goods_attr_id', 'goods_attr_name', 'sign_id', 'price', 'cost_price', 'original_goods_price',
  152. 'goods_price', 'num', 'is_virtual', 'pic_url', 'order_status', 'status', 'created_at', 'updated_at'
  153. ];
  154. $res = \Yii::$app->db->createCommand()->batchInsert(self::tableName(), $columns, $rows)->execute();
  155. if ($res == 0) {
  156. throw new \Exception('订单详情数据存储失败');
  157. }
  158. return true;
  159. }
  160. }