// +---------------------------------------------------------------------- declare (strict_types=1); namespace app\store\model; use app\common\library\helper; use app\common\model\GoodsCoupon as GoodsCouponModel; use cores\exception\BaseException; /** * 商品优惠券关系模型 * Class GoodsCoupon * @package app\store\model */ class GoodsCoupon extends GoodsCouponModel { /** * 验证优惠券是否合法 * @param array $couponList * @throws BaseException */ public static function checkCouponData(array $couponList) { $values = helper::getArrayColumn($couponList, 'coupon_id'); if (count($couponList) != count(array_unique($values))) { throwError('很抱歉,不能存在重复的优惠券 请检查您的选择'); } foreach ($couponList as $item) { if(empty($item['send_num'])){ throwError('很抱歉,优惠券数量不能为空 请检查您的输入'); }elseif($item['send_num'] < 0 || $item['send_num'] > 100){ throwError('很抱歉,优惠券数量只能在1和100之间 请检查您的输入'); } } } /** * 批量写入商品与优惠券关系记录 * @param int $goodsId * @param array $couponList * @return array|false */ public static function increased(int $goodsId, array $couponList) { $dataset = []; foreach ($couponList as $item) { $dataset[] = [ 'goods_id' => $goodsId, 'coupon_id' => $item['coupon_id'], 'send_num' => $item['send_num'], 'store_id' => self::$storeId ]; } return (new static)->addAll($dataset); } /** * 批量更新商品与优惠券关系记录 * @param int $goodsId * @param array $couponList * @return array|false */ public static function updates(int $goodsId, array $couponList) { // 删除所有的记录 static::deleteAll(['goods_id' => $goodsId]); // 批量新增记录 return static::increased($goodsId, $couponList); } }