| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161 |
- <?php
- namespace addons\Store\common\models;
- use common\enums\OrderStatusEnum;
- use common\enums\RedisKeyEnum;
- use common\enums\StatusEnum;
- use common\helpers\LockHelper;
- /**
- * This is the model class for table "{{%addons_store_cashier_order_detail}}".
- *
- * @property int $id 订单项ID
- * @property int $cashier_order_id 收银台订单ID
- * @property int $user_id 用户id
- * @property int $mall_id 商城ID
- * @property int $goods_id 商品ID
- * @property string $goods_name 商品名称
- * @property int $goods_attr_id 规格ID
- * @property string $goods_attr_name 规格名称
- * @property string $sign_id 规格ID标识
- * @property string|null $attr_groups_format 格式化规格组
- * @property float $price 商品价格
- * @property float $cost_price 商品成本价
- * @property float $original_goods_price 商品原本总价
- * @property float $goods_price 商品优惠后总价
- * @property int $num 购买数量
- * @property string $pic_url 商品图片
- * @property int $order_status 订单状态【0待付款 1待发货 2 已发货 3已收货 4已完成 -1申请售后 -2售后中 -3售后完成 -4 已关闭 -5撤销售后】
- * @property int $is_virtual 是否包含 虚拟商品 0 不包含 1 包含
- * @property int $status 状态[-1:删除;0:禁用;1启用]
- * @property string|null $extra_info 额外信息
- * @property int $created_at
- * @property int $updated_at
- * @property int $store_id 门店id
- */
- class StoreCashierOrderDetail extends \common\models\base\BaseActiveRecord
- {
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return '{{%addons_store_cashier_order_detail}}';
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['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'],
- [['attr_groups_format', 'extra_info'], 'string'],
- [['price', 'cost_price', 'original_goods_price', 'goods_price'], 'number'],
- [['goods_name'], 'string', 'max' => 200],
- [['goods_attr_name', 'sign_id', 'pic_url'], 'string', 'max' => 255],
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function attributeLabels()
- {
- return [
- 'id' => '订单项ID',
- 'cashier_order_id' => '收银台订单ID',
- 'user_id' => '用户id',
- 'mall_id' => '商城ID',
- 'goods_id' => '商品ID',
- 'goods_name' => '商品名称',
- 'goods_attr_id' => '规格ID',
- 'goods_attr_name' => '规格名称',
- 'sign_id' => '规格ID标识',
- 'attr_groups_format' => '格式化规格组',
- 'price' => '商品价格',
- 'cost_price' => '商品成本价',
- 'original_goods_price' => '商品原本总价',
- 'goods_price' => '商品优惠后总价',
- 'num' => '购买数量',
- 'pic_url' => '商品图片',
- 'order_status' => '订单状态【0待付款 1待发货 2 已发货 3已收货 4已完成 -1申请售后 -2售后中 -3售后完成 -4 已关闭 -5撤销售后】',
- 'is_virtual' => '是否包含 虚拟商品 0 不包含 1 包含',
- 'status' => '状态[-1:删除;0:禁用;1启用]',
- 'extra_info' => '额外信息',
- 'created_at' => 'Created At',
- 'updated_at' => 'Updated At',
- 'store_id' => '门店id',
- ];
- }
- /**
- * @Description: 批量保存数据
- * @Author: zsan
- * @DateTime 2023-08-10 18:27:53
- * @param array $attrs
- * @param array $info
- * @return boolean
- * @throws \Exception
- */
- public static function batchDataSave($attrs, $info)
- {
- $cashier_order_id = $info['cashier_order_id'];
- $user_id = $info['user_id'];
- $mall_id = $info['mall_id'];
- $store_id = $info['store_id'];
- $goods_attrs = StoreGoodsAttr::lists([
- 'where' => [['id' => array_column($attrs, 'attr_id')], ['status' => StatusEnum::ENABLED]],
- 'with' => ['goods' => function ($q) {
- return $q->select('id, goods_name, goods_type, cover_pic');
- }]
- ]);
- if (empty($goods_attrs)) throw new \Exception('商品属性ID错误');
- $nums = array_column($attrs, 'num', 'attr_id');
- $goods_type = [];
- $goods_stock_ids = [];
- foreach ($goods_attrs as $v) {
- $result = StoreGoodsAttr::handleStock($v['id'], $nums[$v['id']] * -1);
- if ($result === false) {
- throw new \Exception(StoreGoodsAttr::getStaticError());
- }
- $goods_stock_ids[] = $v['goods_id'];
- $goods_type[$v['goods_id']] = $v['goods']['goods_type'];
- }
- $stock_locks = [];
- foreach($goods_stock_ids as $id){
- $lock_key = RedisKeyEnum::suffix(RedisKeyEnum::LOCK_GOODS , $id);
- $identification = uniqid();
- if (!LockHelper::lock($lock_key, $identification, 30, 60)) throw new \Exception('当前访问人数过多');
- $stock_locks[$lock_key] = $identification;
- $stock = StoreGoodsAttr::find()->where(['goods_id'=>$id])->sum('stock');
- $stock=$stock??0;
- $res = StoreGoods::updateAll(['stock'=>$stock],['id'=>$id]);
- if(!$res) throw new \Exception(StoreGoods::getStaticError());
- }
- LockHelper::batchUlock($stock_locks);
- $rows = [];
- foreach ($goods_attrs as $goods_attr) {
- $rows[] = [
- $cashier_order_id, $user_id, $mall_id, $store_id, $goods_attr['goods_id'],
- $goods_attr['goods']['goods_name'], $goods_attr['id'], $goods_attr['name'], $goods_attr['sign_id'], $goods_attr['price'],
- $goods_attr['cost_price'], bcmul($goods_attrs['original_goods_price'], $nums[$goods_attr['id']], 2),
- $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']),
- OrderStatusEnum::NOT_PAY, StatusEnum::ENABLED, time(), time()
- ];
- }
- $columns = [
- 'cashier_order_id', 'user_id', 'mall_id', 'store_id', 'goods_id', 'goods_name',
- 'goods_attr_id', 'goods_attr_name', 'sign_id', 'price', 'cost_price', 'original_goods_price',
- 'goods_price', 'num', 'is_virtual', 'pic_url', 'order_status', 'status', 'created_at', 'updated_at'
- ];
- $res = \Yii::$app->db->createCommand()->batchInsert(self::tableName(), $columns, $rows)->execute();
- if ($res == 0) {
- throw new \Exception('订单详情数据存储失败');
- }
- return true;
- }
- }
|