120], [['symptom_list', 'description_list', 'member_alone_buy_limit_setting'], 'safe'] ]; } /** * {@inheritdoc} */ public function attributeLabels() { return [ 'id' => 'ID', 'goods_id' => '商品ID', 'full_reduce_money' => '单品满额减免金额', 'full_reduce_price_limit' => '单品满额金额', 'score_give_setting' => '积分赠送设置', 'balance_give_setting' => '余额赠送设置', 'score_deduct_setting' => '积分抵扣设置', 'currency_deduct_setting' => '货币营销设置', 'is_level' => '是否享受会员功能 [0=否,1=是]', 'is_member_price' => '是否单独设置会员价 [0=否,1=是]', 'is_show_price' => '商品是否显示会员价[0=否, 1是]', 'member_price_switch' => '商品是否单独显示会员价[0=否, 1是]', 'member_price_setting' => '会员折扣设置', 'member_buy_limit_setting' => '会员限购设置', 'is_repurchase' => '是否开启复购价[0=否, 1=是]', 'repurchase_setting' => '复购价设置', 'status' => '状态[1正常 -1删除]', 'created_at' => '创建时间', 'updated_at' => '修改时间', 'symptom_list' => '疾病列表,药品商品类型使用', 'description_template_id' => '商品说明模板id', 'description_list' => '商品说明列表', 'drug_generic_name' => '药品通用名称', ]; } /** * 保存数据 * @param $params * @return bool */ public static function setData($goods_id, $params) { try { // 判断参数中是否存在数组,存在则转为json字符串 foreach ($params as $key => &$item) { $item = is_array($item) ? json_encode($item, JSON_UNESCAPED_UNICODE) : $item; } // 根据商品id获取商品信息 $model = self::findOne(['goods_id' => $goods_id, 'status' => StatusEnum::ENABLED]); if (empty($model)) { // 初始化商品额外参数 $model = new self(); $model->loadDefaultValues(); } // 维护商品说明模板使用数量 $beforeDescriptionTemplateId = $model['description_template_id']; $model->goods_id = $goods_id; if (!$model->load($params, '') || !$model->save(false)) { throw new \Exception($model->getErrorMessage()); } if ($beforeDescriptionTemplateId != $model['description_template_id']) { $now = time(); if ($beforeDescriptionTemplateId) { GoodsDescriptionTemplate::updateAll( [ 'use_num' => new Expression("`use_num` - 1"), 'updated_at' => $now, ], [ 'id' => $beforeDescriptionTemplateId, 'status' => StatusEnum::ENABLED, 'mall_id' => $params['mall_id'], ] ); } if ($model['description_template_id']) { GoodsDescriptionTemplate::updateAll( [ 'use_num' => new Expression("`use_num` + 1"), 'updated_at' => $now, ], [ 'id' => $model['description_template_id'], 'status' => StatusEnum::ENABLED, 'mall_id' => $params['mall_id'], ] ); } } return true; } catch (\Exception $e) { self::$static_error = $e->getMessage(); return false; } } /** * 购买条件验证 * @Author bing * @DateTime 2021-09-27 14:20:53 * @copyright: Copyright (c) 2020 广东七件事集团 * @param [type] $user * @param [type] $limit_setting * @return void */ public static function buyLimitValidate($user, &$limit_setting) { try { if (!empty($limit_setting)) { if (!in_array($user->level, $limit_setting['member_level'])) { throw new Exception('你的等级不支持购买该商品'); } foreach ($limit_setting as $key => $val) { if ($val == 0) continue; } } return true; } catch (\Exception $e) { self::$static_error = $e->getMessage(); return false; } } public static function getFieldsDecoration($from, $mall_id) { $name = \Yii::$app->services->setting->mall->get($mall_id, 'score.score_name'); $home_page = [ ['label' => DecorationFieldsLabelEnum::SCORE_LABEL, 'name' => $name, 'color' => '', 'showAlpha' => 0, 'field' => 'system_score'] ]; $goods_detail = [ ['name' => $name, 'type' => 'system_score', 'prefix_text' => ''] ]; $permession_fields = [ ['key' => 'system_score', 'label' => DecorationFieldsLabelEnum::SCORE_LABEL, 'name' => $name, 'color' => '', 'prefix_text' => '赠送'] ]; $data = compact('home_page', 'goods_detail', 'permession_fields'); return $data[$from] ?? []; } public static function getMarketingScore($mall_id, $source, $data) { if (!in_array($source, ['detail', 'list'])) return $data; //订单满额赠送 $order_config = Yii::$app->services->setting->mall->get($mall_id, 'marketing'); if (!empty($order_config['score_give_setting'])) $order_config['score_give_setting'] = json_decode($order_config['score_give_setting'], true); $name = \Yii::$app->services->setting->mall->get($mall_id, 'score.score_name'); if ($source == 'list') { $goods_ids = array_column($data, 'id'); $settings = self::lists([ 'where' => [['goods_id' => $goods_ids], ['status' => StatusEnum::ENABLED]], 'select' => 'goods_id, score_give_setting', 'index' => 'goods_id' ], true); foreach ($data as &$val) { $val['score_name'] = $name; $val['system_score'] = 0; if (isset($settings[$val['id']])) { $give_setting = json_decode($settings[$val['id']]['score_give_setting'], true); if ($give_setting['enable_give_score'] == 1) { $give_num = $give_setting['give_num'] ?? 0; $give_type = $give_setting['give_type'] ?? 1; if ($give_num > 0) { if ($give_type == 2) { if ($give_setting['calculation_value'] == 1) { //商品利润 $give_num = bcmul($val['price']-$val['cost_price'], bcdiv($give_num, 100, 2), 2); } else { //商品售价 $give_num = bcmul($val['price'], bcdiv($give_num, 100, 2), 2); } } } $give_num > 0 && $val['system_score'] = $give_num; } else { //设置-订单设置-营销设置,开启营销设置开关并且开启积分赠送开关,商品金额大于等于设置的订单消费金额 if ($order_config['enable_marketing'] == 1 && $order_config['enable_give_score'] == 1 && $val['price'] >= $order_config['total_price']) { $give_num = $order_config['score_give_setting']['give_num'] ?? 0; $give_type = $order_config['score_give_setting']['give_type'] ?? 1; if ($give_type == 2) { if ($order_config['score_give_setting']['score_giving_calculation'] == 1) { $give_num = bcmul($val['price']-$val['cost_price'], bcdiv($give_num, 100, 2), 2); } else { $give_num = bcmul($val['price'], bcdiv($give_num, 100, 2), 2); } } $give_num > 0 && $val['system_score'] = $give_num; } } } } unset($val); return $data; } if ($source == 'detail') { $goods_ids = $data['id']; $settings = self::getOne([['goods_id' => $goods_ids], ['status' => StatusEnum::ENABLED]], true, [], false, 'goods_id, score_give_setting'); $data['score_name'] = $name; $data['system_score'] = 0; $give_setting = json_decode($settings['score_give_setting'], true); //商品营销设置积分赠送开关开启 if ($give_setting['enable_give_score'] == 1) { $give_num = $give_setting['give_num'] ?? 0; $give_type = $give_setting['give_type'] ?? 1; if ($give_num > 0) { if ($give_type == 2) { if ($give_setting['calculation_value'] == 1) { //商品利润 $give_num = bcmul($data['price']-$data['cost_price'], bcdiv($give_num, 100, 2), 2); } else { //商品售价 $give_num = bcmul($data['price'], bcdiv($give_num, 100, 2), 2); } } } $give_num > 0 && $data['system_score'] = $give_num; } else { //score_giving_calculation //设置-订单设置-营销设置,开启营销设置开关并且开启积分赠送开关,商品金额大于等于设置的订单消费金额 if ($order_config['enable_marketing'] == 1 && $order_config['enable_give_score'] == 1 && $data['price'] >= $order_config['total_price']) { $give_num = $order_config['score_give_setting']['give_num'] ?? 0; $give_type = $order_config['score_give_setting']['give_type'] ?? 1; if ($give_type == 2) { if ($order_config['score_give_setting']['score_giving_calculation'] == 1) { $give_num = bcmul($data['price']-$data['cost_price'], bcdiv($give_num, 100, 2), 2); } else { $give_num = bcmul($data['price'], bcdiv($give_num, 100, 2), 2); } } $give_num > 0 && $data['system_score'] = $give_num; } } return $data; } } public static function getExtraFields($mall_id, $from) { $list = self::getFieldsDecoration($from, $mall_id); // 公共调用 $fields = GlobalInvoke::exec(GlobalInvoke::ADDONS_FIELDS_DECORATION, $mall_id, [ 'type' => $from, 'mall_id' => $mall_id, 'list' => $list ]); return $fields ? $fields : $list; } public static function fixGoodsExtraSetting() { $model = self::find(); $i = 1; $page_size = 1000; while ($list = $model->asArray()->offset(($i - 1) * $page_size)->limit($page_size)->all()) { foreach ($list as $key => $value) { $data = []; $score_give_setting = []; $balance_give_setting = []; if (!empty($value['score_give_setting'])) { $score_give_setting = json_decode($value['score_give_setting'],true); if (!isset($score_give_setting['enable_give_score'])) { $score_give_setting['enable_give_score'] = 1; $data['score_give_setting'] = json_encode($score_give_setting); } } if (!empty($value['balance_give_setting'])) { $balance_give_setting = json_decode($value['balance_give_setting'],true); if (!isset($balance_give_setting['enable_give_balance'])) { $balance_give_setting['enable_give_balance'] = 1; $data['balance_give_setting'] = json_encode($balance_give_setting); } } if (!empty($data)) { $res = self::updateAll($data,['id' => $value['id']]); // if ($res) { // echo 'id:' .$value['id'] .' update success'; // } } } $i++; } } }