CouponGood.php 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2021 https://www.yiovo.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
  8. // +----------------------------------------------------------------------
  9. // | Author: 萤火科技 <admin@yiovo.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types = 1);
  12. namespace app\store\model;
  13. use app\store\model\Goods;
  14. use app\store\model\GoodsSku;
  15. use app\common\model\CouponGood as CouponGoodModel;
  16. use app\common\library\helper;
  17. /**
  18. * 优惠券商品模型
  19. * Class Coupon
  20. * @package app\store\model
  21. */
  22. class CouponGood extends CouponGoodModel
  23. {
  24. public function add($coupon_id, $isExcept, $data)
  25. {
  26. // 先删除全部
  27. static::deleteAll(['coupon_id' => $coupon_id, 'is_except' => $isExcept]);
  28. if (!empty($data)) {
  29. // 添加
  30. $dataset = [];
  31. foreach ($data as $item) {
  32. // 商品信息
  33. $goodsInfo = Goods::detail($item['goods_id'], ['images' => ['file'], 'skuList' => ['image']]);
  34. if (empty($goodsInfo)) {
  35. $this->error = "商品信息错误";
  36. return false;
  37. }
  38. $goodsInfo['goods_images'] = helper::getArrayColumn($goodsInfo['images'], 'file');
  39. // 商品主图
  40. $goodsInfo['goods_image'] = current($goodsInfo['goods_images'])['preview_url'];
  41. $goodsInfo['image_id'] = current($goodsInfo['goods_images'])['file_id'];
  42. // $goodsSku = GoodsSku::detail($item['goods_id'], $item['goods_sku_id']);
  43. // if (empty($goodsSku)) {
  44. // $this->error = "商品信息错误";
  45. // return false;
  46. // }
  47. $dataset[] = [
  48. 'goods_no'=>$goodsInfo['goods_no'],
  49. 'coupon_id' => $coupon_id,
  50. 'goods_id' => $item['goods_id'],
  51. // 'goods_sku_id' => $item['goods_sku_id'],
  52. 'goods_sku_id' => 0,
  53. 'goods_num' => $item['goods_num'] ?? 1,
  54. 'is_except' => $isExcept,
  55. // 'goods_sku_no' => $goodsSku['goods_sku_no'],
  56. 'image_id' => $goodsInfo['image_id'],
  57. 'goods_name' => $goodsInfo['goods_name'],
  58. // 'goods_props' => $goodsSku['goods_props']??[],
  59. 'goods_price' => $goodsInfo['goods_price_min'] ?? '0.00',
  60. // 'total_price' => helper::bcsub($goodsSku['goods_price'] * ($item['goods_num'] ?? 1), 0, 2),
  61. 'total_price' => $goodsInfo['goods_price_min'] ?? '0.00',
  62. ];
  63. }
  64. (new static)->addAll($dataset);
  65. }
  66. }
  67. /**
  68. * 获取优惠券排外商品ID
  69. * @param $couponId
  70. * @param int $isExpect
  71. * @return array
  72. * @throws \think\db\exception\DataNotFoundException
  73. * @throws \think\db\exception\DbException
  74. * @throws \think\db\exception\ModelNotFoundException
  75. */
  76. public static function getExceptGoodsId($couponId,$isExpect = 1){
  77. return self::where('coupon_id',$couponId)->where('is_except',$isExpect)
  78. ->select()
  79. ->column('goods_id');
  80. }
  81. }