1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889 |
- <?php
- // +----------------------------------------------------------------------
- // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
- // +----------------------------------------------------------------------
- // | Copyright (c) 2017~2021 https://www.yiovo.com All rights reserved.
- // +----------------------------------------------------------------------
- // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
- // +----------------------------------------------------------------------
- // | Author: 萤火科技 <admin@yiovo.com>
- // +----------------------------------------------------------------------
- declare (strict_types = 1);
- namespace app\store\model;
- use app\store\model\Goods;
- use app\store\model\GoodsSku;
- use app\common\model\CouponGood as CouponGoodModel;
- use app\common\library\helper;
- /**
- * 优惠券商品模型
- * Class Coupon
- * @package app\store\model
- */
- class CouponGood extends CouponGoodModel
- {
-
- public function add($coupon_id, $isExcept, $data)
- {
- // 先删除全部
- static::deleteAll(['coupon_id' => $coupon_id, 'is_except' => $isExcept]);
- if (!empty($data)) {
- // 添加
- $dataset = [];
- foreach ($data as $item) {
- // 商品信息
- $goodsInfo = Goods::detail($item['goods_id'], ['images' => ['file'], 'skuList' => ['image']]);
- if (empty($goodsInfo)) {
- $this->error = "商品信息错误";
- return false;
- }
- $goodsInfo['goods_images'] = helper::getArrayColumn($goodsInfo['images'], 'file');
- // 商品主图
- $goodsInfo['goods_image'] = current($goodsInfo['goods_images'])['preview_url'];
- $goodsInfo['image_id'] = current($goodsInfo['goods_images'])['file_id'];
- // $goodsSku = GoodsSku::detail($item['goods_id'], $item['goods_sku_id']);
- // if (empty($goodsSku)) {
- // $this->error = "商品信息错误";
- // return false;
- // }
- $dataset[] = [
- 'goods_no'=>$goodsInfo['goods_no'],
- 'coupon_id' => $coupon_id,
- 'goods_id' => $item['goods_id'],
- // 'goods_sku_id' => $item['goods_sku_id'],
- 'goods_sku_id' => 0,
- 'goods_num' => $item['goods_num'] ?? 1,
- 'is_except' => $isExcept,
- // 'goods_sku_no' => $goodsSku['goods_sku_no'],
- 'image_id' => $goodsInfo['image_id'],
- 'goods_name' => $goodsInfo['goods_name'],
- // 'goods_props' => $goodsSku['goods_props']??[],
- 'goods_price' => $goodsInfo['goods_price_min'] ?? '0.00',
- // 'total_price' => helper::bcsub($goodsSku['goods_price'] * ($item['goods_num'] ?? 1), 0, 2),
- 'total_price' => $goodsInfo['goods_price_min'] ?? '0.00',
- ];
- }
- (new static)->addAll($dataset);
- }
- }
- /**
- * 获取优惠券排外商品ID
- * @param $couponId
- * @param int $isExpect
- * @return array
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public static function getExceptGoodsId($couponId,$isExpect = 1){
- return self::where('coupon_id',$couponId)->where('is_except',$isExpect)
- ->select()
- ->column('goods_id');
- }
- }
|