| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250 |
- <?php
- namespace addons\QiJuhe\common\service;
- use addons\QiJuhe\common\api\Api;
- use addons\QiJuhe\common\models\GoodsModel;
- use addons\QiJuhe\common\models\SettingLogModel;
- use addons\QiJuhe\common\models\SettingModel;
- use Exception;
- use Yii;
- use yii\base\Model;
- use yii\helpers\Json;
- class SettingService extends Model
- {
- /**
- * mall—id
- *
- * @var integer
- */
- public $mall_id;
- /**
- * 设置
- *
- * @var array
- */
- public $setting;
- /**
- * 错误信息
- *
- * @var string
- */
- public $error;
- public function rules()
- {
- return [
- [['setting'], 'string'],
- ];
- }
- /**
- * 回调链接
- *
- * @var string
- */
- protected $callback_url = '/qi-juhe/v1/notice/handler';
- /**
- * 回调链接
- *
- * @return void
- */
- public function getCallbackUrl()
- {
- return Yii::$app->services->setting->platform->get('system.api_url') . $this->callback_url; // . $this->mall_id;
- }
- /**
- * 获取配置(包含默认值)
- *
- * @param [type] $mall_id
- * @return void
- */
- public static function getSet($mall_id)
- {
- $model = SettingModel::find()->where(['mall_id' => $mall_id, 'is_delete' => 0])->cache(5)->one();
- if (empty($model)) throw new Exception('还未添加基础设置');
- $set = Json::decode($model->setting, true);
- return $set;
- }
- /**
- * 返回APPKEY配置
- *
- * @param int $mall_id
- * @return array
- */
- public static function getKey($mall_id)
- {
- $model = SettingModel::findOne(['mall_id' => $mall_id, 'is_delete' => 0]);
- if(empty($model)){
- return [];
- }
- $setting = Json::decode($model->setting, true);
- return [
- 'app_key' => $setting['app_key'],
- 'app_secret' => $setting['app_secret'],
- ];
- }
- /**
- * 获取APItoken
- *
- * @param integer $mall_id
- * @return string
- */
- public static function getToken($mall_id)
- {
- $model = SettingModel::findOne(['mall_id' => $mall_id, 'is_delete' => 0]);
- if(empty($model->token) || $model->expire_time < time()){
- // 过期或者不存在,重新获取
- $api = new Api([
- 'mall_id'=>$mall_id,
- 'app_key'=>$model->getConfig('app_key'),
- 'app_secret'=>$model->getConfig('app_secret'),
- 'app_host'=>$model->getConfig('app_host'),
- ]);
- try {
- $data = $api->getToken();
- $model->token = $data['token'] ?? '';
- $model->expire_time = ($data['expiresAt'] / 100) - 3600;
- $model->save();
- return $data['token'];
- }catch (Exception $e){
- Yii::error($e);
- return false;
- }
- }
- return $model->token;
- }
- /**
- * 获取配置
- *
- * @return array ['callback_url' => ...]
- */
- public function getSetting()
- {
- $model = SettingModel::findOne(['mall_id' => $this->mall_id, 'is_delete' => 0]);
- $setting['callback_url'] = $this->getCallbackUrl();
- if (empty($model)) return $setting;
- $setting = Json::decode($model->setting, true);
- $setting['callback_url'] = $this->getCallbackUrl();
- // $setting['jd_original_strategy'] = isset($setting['jd_original_strategy']) ? intval($setting['jd_original_strategy']) : 0;
- // $setting['jd_sell_strategy'] = isset($setting['jd_sell_strategy']) ? intval($setting['jd_sell_strategy']) : 0;
- // $setting['jd_cost_strategy'] = isset($setting['jd_cost_strategy']) ? intval($setting['jd_cost_strategy']) : 0;
- $setting['management_tactics'] = isset($setting['management_tactics']) ? intval($setting['management_tactics']) : 0;
- $setting['danger_type'] = isset($setting['danger_type']) ? intval($setting['danger_type']) : 0;
- $setting['auto_display'] = isset($setting['auto_display']) ? intval($setting['auto_display']) : 1; // 默认开启自动更新价格
- return $setting;
- }
- /**
- * 更新设置
- *
- * @return void
- */
- public function setSetting()
- {
- $model = SettingModel::findOne(['mall_id' => $this->mall_id, 'is_delete' => 0]);
- $log_model = new SettingLogModel();
- if (empty($model)) {
- $model = new SettingModel();
- $model->mall_id = $this->mall_id;
- }
- $db = Yii::$app->db->beginTransaction();
- try {
- // 日志
- $log_model->mall_id = $this->mall_id;
- $log_model->before_setting = $model->setting ?? '';
- $log_model->save();
- // 保存
- $model->setting = Json::encode($this->setting);
- $model->save();
- $db->commit();
- return true;
- } catch (\Exception $e) {
- $db->rollback();
- return false;
- }
- }
- /**
- * time:2021-10-20-22:36
- * user: 林深时见鹿
- * title:检测风控
- * desc:
- * 1、产品售价 < (售价策略)
- * 1、利润率 = (售价-成本价)/成本价 小于 多少
- */
- public function checkDanger($total_price, $sku_id, $num, $shop_goods_id, $supply_id)
- {
- $setting = self::getSet($this->mall_id);
- $danger_status = $setting['danger_status'] ?? 0;
- $danger_type = $setting['danger_type'] ?? 0;
- $danger_rate = $setting['danger_rate'] ?? 0;
- if(!$danger_status){
- return true;
- }
- $juhe_goods = GoodsModel::find()->where(['goods_id' => $shop_goods_id])->asArray()->one();
- // sku 列表
- if (!empty($juhe_goods['skus'])) { // 多规格
- $sku_list = Json::decode( $juhe_goods['skus'] );
- foreach ($sku_list as $item) {
- if($sku_id == $item['id']) {
- // 查找sku价格
- $juhe_goods['guide_price'] = $item['guide_price'];
- $juhe_goods['activity_price'] = $item['activity_price'];
- $juhe_goods['agreement_price'] = $item['agreement_price'];
- break;
- }
- }
- }
- $qi_service = new QiGoodsService(['mall_id' => $this->mall_id]);
- $qi_service->setSettings($supply_id);
- $qi_service->setSupplyId($supply_id);
- // 获取销售价格
- $sell_price = $qi_service->getPrice($juhe_goods, 'sell');
- // 成本价
- $cost = $qi_service->getPrice($juhe_goods, 'cost') * $num;
- if($danger_type == 1 && $total_price < $cost){
- // 产品售价 < (售价策略) 这个错误信息不能随便改
- $this->error = '下单失败,该商品信息已更新,请刷新重试1';
- // ...看是否需要下架
- return false;
- }
- if($danger_type == 2 ){
- //利润率 = (售价-成本价)/成本价 小于 多少
- $profit_rate = ($total_price - $cost) / $cost * 100;
- $profit_rate = number_format($profit_rate, 2, '.', '');
- if($profit_rate < $danger_rate){
- // 产品售价 < (售价策略) 这个错误信息不能随便改
- $this->error = '下单失败,该商品信息已更新,请刷新重试2';
- // ...看是否需要下架
- return false;
- }
- }
- return true;
- }
- }
|