| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105 |
- <?php
- namespace app\jobs;
- use app\constants\OptionSetting;
- use app\models\Goods;
- use app\models\Option;
- use app\models\Store;
- use app\modules\admin\models\PlatformForm;
- use yii\base\BaseObject;
- use yii\queue\JobInterface;
- //风控规则
- class RiskControlModelJob extends BaseObject implements JobInterface
- {
- public $goods_id;
- //获取所有的云仓商品 使用云仓商品id去云仓中查询商品 如果查询的商品协议价大于商品售价 就执行风控规则 目前只有商品下架以及恢复成平台协议价
- public function execute($queue)
- {
- try {
- $platform_profit_strategy = Option::get('platform_profit_strategy', 0, 'store')['value'];
- $platform_profit_strategy = json_decode($platform_profit_strategy, true);
- if (empty($platform_profit_strategy)) {
- throw new \Exception('未配置风控规则');
- }
- if (!intval($platform_profit_strategy['is_risk_control']) ) {
- throw new \Exception('未开启风控规则');
- }
- //风控规则start
- $risk_control_model = intval($platform_profit_strategy['risk_control_model']);
- $goods_list = Goods::find()->alias('g')->leftJoin(['s' => Store::tableName()], 'g.store_id = s.id')
- ->where(['g.is_delete' => 0, 'g.status' => 1, 's.is_delete' => 0])->andWhere(['IS NOT', 'g.cloud_goods_id', NULL]);
- if ($this->goods_id) {
- $goods_list->andWhere(['g.id' => $this->goods_id]);
- }
- $goods_list = $goods_list->select('g.id, g.price, g.attr, g.cloud_goods_id')->asArray()->all();
- foreach ($goods_list as $item) {
- //获取云仓商品
- $cloud_url = "/goods/getGoodsInfo";
- $cloud_data = [];
- $cloud_data['goods_id'] = $item['cloud_goods_id'];
- $domain = (new OptionSetting)->getCloudDomainName();
- $cloud_info = cloud_post($domain . $cloud_url, $cloud_data);
- $cloud_info = json_decode($cloud_info, true);
- if($cloud_info['code'] != 0){
- debug_log($cloud_info, 'risk_control_result.log');
- }else{
- $goods = Goods::findOne($item['id']);
- //云仓商品协议价大于商品售价
- $cloud_goods = $cloud_info['data']['goods'];
- if ($item['price'] < $cloud_goods['platform_negotiated_price']) {
- if ($risk_control_model) {
- //原始协议价
- $goods->price = $cloud_goods['platform_negotiated_price'];
- } else {
- //商品下架
- $goods->status = 0;
- }
- }
- //规格中的云仓商品协议价大于商品售价处理
- $item['attr'] = json_decode($item['attr'], true);
- $cloud_goods['attrs'] = json_decode($cloud_goods['attrs'], true);
- foreach ($cloud_goods['attrs'] as $cloud_attr_item) {
- $cloud_no = $cloud_attr_item['no'];
- foreach ($item['attr'] as &$attr_item) {
- $_no = $attr_item['no'];
- if (!empty($cloud_no) && !empty($_no)) {
- if (strval($cloud_no) === strval($_no) && $attr_item['price'] < (!empty($cloud_attr_item['platform_negotiated_price']) ? $cloud_attr_item['platform_negotiated_price'] : $cloud_attr_item['price'])) {
- if ($risk_control_model) {
- //原始协议价
- $attr_item['price'] = (!empty($cloud_attr_item['platform_negotiated_price']) ? $cloud_attr_item['platform_negotiated_price'] : $cloud_goods['price']);
- } else {
- //商品下架
- $goods->status = 0;
- }
- }
- }
- }
- }
- $goods->attr = json_encode($item['attr'], JSON_UNESCAPED_UNICODE);
- if (!$goods->save()) {
- debug_log($goods->errors, 'risk_control_result.log');
- }
- }
- }
- } catch (\Exception $e) {
- debug_log([
- 'line' => $e->getLine(),
- 'file' => $e->getFile(),
- 'message' => $e->getMessage()
- ], 'risk_control_result.log');
- }
- }
- }
|