SettingService.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. <?php
  2. namespace addons\QiJuhe\common\service;
  3. use addons\QiJuhe\common\api\Api;
  4. use addons\QiJuhe\common\models\GoodsModel;
  5. use addons\QiJuhe\common\models\SettingLogModel;
  6. use addons\QiJuhe\common\models\SettingModel;
  7. use Exception;
  8. use Yii;
  9. use yii\base\Model;
  10. use yii\helpers\Json;
  11. class SettingService extends Model
  12. {
  13. /**
  14. * mall—id
  15. *
  16. * @var integer
  17. */
  18. public $mall_id;
  19. /**
  20. * 设置
  21. *
  22. * @var array
  23. */
  24. public $setting;
  25. /**
  26. * 错误信息
  27. *
  28. * @var string
  29. */
  30. public $error;
  31. public function rules()
  32. {
  33. return [
  34. [['setting'], 'string'],
  35. ];
  36. }
  37. /**
  38. * 回调链接
  39. *
  40. * @var string
  41. */
  42. protected $callback_url = '/qi-juhe/v1/notice/handler';
  43. /**
  44. * 回调链接
  45. *
  46. * @return void
  47. */
  48. public function getCallbackUrl()
  49. {
  50. return Yii::$app->services->setting->platform->get('system.api_url') . $this->callback_url; // . $this->mall_id;
  51. }
  52. /**
  53. * 获取配置(包含默认值)
  54. *
  55. * @param [type] $mall_id
  56. * @return void
  57. */
  58. public static function getSet($mall_id)
  59. {
  60. $model = SettingModel::find()->where(['mall_id' => $mall_id, 'is_delete' => 0])->cache(5)->one();
  61. if (empty($model)) throw new Exception('还未添加基础设置');
  62. $set = Json::decode($model->setting, true);
  63. return $set;
  64. }
  65. /**
  66. * 返回APPKEY配置
  67. *
  68. * @param int $mall_id
  69. * @return array
  70. */
  71. public static function getKey($mall_id)
  72. {
  73. $model = SettingModel::findOne(['mall_id' => $mall_id, 'is_delete' => 0]);
  74. if(empty($model)){
  75. return [];
  76. }
  77. $setting = Json::decode($model->setting, true);
  78. return [
  79. 'app_key' => $setting['app_key'],
  80. 'app_secret' => $setting['app_secret'],
  81. ];
  82. }
  83. /**
  84. * 获取APItoken
  85. *
  86. * @param integer $mall_id
  87. * @return string
  88. */
  89. public static function getToken($mall_id)
  90. {
  91. $model = SettingModel::findOne(['mall_id' => $mall_id, 'is_delete' => 0]);
  92. if(empty($model->token) || $model->expire_time < time()){
  93. // 过期或者不存在,重新获取
  94. $api = new Api([
  95. 'mall_id'=>$mall_id,
  96. 'app_key'=>$model->getConfig('app_key'),
  97. 'app_secret'=>$model->getConfig('app_secret'),
  98. 'app_host'=>$model->getConfig('app_host'),
  99. ]);
  100. try {
  101. $data = $api->getToken();
  102. $model->token = $data['token'] ?? '';
  103. $model->expire_time = ($data['expiresAt'] / 100) - 3600;
  104. $model->save();
  105. return $data['token'];
  106. }catch (Exception $e){
  107. Yii::error($e);
  108. return false;
  109. }
  110. }
  111. return $model->token;
  112. }
  113. /**
  114. * 获取配置
  115. *
  116. * @return array ['callback_url' => ...]
  117. */
  118. public function getSetting()
  119. {
  120. $model = SettingModel::findOne(['mall_id' => $this->mall_id, 'is_delete' => 0]);
  121. $setting['callback_url'] = $this->getCallbackUrl();
  122. if (empty($model)) return $setting;
  123. $setting = Json::decode($model->setting, true);
  124. $setting['callback_url'] = $this->getCallbackUrl();
  125. // $setting['jd_original_strategy'] = isset($setting['jd_original_strategy']) ? intval($setting['jd_original_strategy']) : 0;
  126. // $setting['jd_sell_strategy'] = isset($setting['jd_sell_strategy']) ? intval($setting['jd_sell_strategy']) : 0;
  127. // $setting['jd_cost_strategy'] = isset($setting['jd_cost_strategy']) ? intval($setting['jd_cost_strategy']) : 0;
  128. $setting['management_tactics'] = isset($setting['management_tactics']) ? intval($setting['management_tactics']) : 0;
  129. $setting['danger_type'] = isset($setting['danger_type']) ? intval($setting['danger_type']) : 0;
  130. $setting['auto_display'] = isset($setting['auto_display']) ? intval($setting['auto_display']) : 1; // 默认开启自动更新价格
  131. return $setting;
  132. }
  133. /**
  134. * 更新设置
  135. *
  136. * @return void
  137. */
  138. public function setSetting()
  139. {
  140. $model = SettingModel::findOne(['mall_id' => $this->mall_id, 'is_delete' => 0]);
  141. $log_model = new SettingLogModel();
  142. if (empty($model)) {
  143. $model = new SettingModel();
  144. $model->mall_id = $this->mall_id;
  145. }
  146. $db = Yii::$app->db->beginTransaction();
  147. try {
  148. // 日志
  149. $log_model->mall_id = $this->mall_id;
  150. $log_model->before_setting = $model->setting ?? '';
  151. $log_model->save();
  152. // 保存
  153. $model->setting = Json::encode($this->setting);
  154. $model->save();
  155. $db->commit();
  156. return true;
  157. } catch (\Exception $e) {
  158. $db->rollback();
  159. return false;
  160. }
  161. }
  162. /**
  163. * time:2021-10-20-22:36
  164. * user: 林深时见鹿
  165. * title:检测风控
  166. * desc:
  167. * 1、产品售价 < (售价策略)
  168. * 1、利润率 = (售价-成本价)/成本价 小于 多少
  169. */
  170. public function checkDanger($total_price, $sku_id, $num, $shop_goods_id, $supply_id)
  171. {
  172. $setting = self::getSet($this->mall_id);
  173. $danger_status = $setting['danger_status'] ?? 0;
  174. $danger_type = $setting['danger_type'] ?? 0;
  175. $danger_rate = $setting['danger_rate'] ?? 0;
  176. if(!$danger_status){
  177. return true;
  178. }
  179. $juhe_goods = GoodsModel::find()->where(['goods_id' => $shop_goods_id])->asArray()->one();
  180. // sku 列表
  181. if (!empty($juhe_goods['skus'])) { // 多规格
  182. $sku_list = Json::decode( $juhe_goods['skus'] );
  183. foreach ($sku_list as $item) {
  184. if($sku_id == $item['id']) {
  185. // 查找sku价格
  186. $juhe_goods['guide_price'] = $item['guide_price'];
  187. $juhe_goods['activity_price'] = $item['activity_price'];
  188. $juhe_goods['agreement_price'] = $item['agreement_price'];
  189. break;
  190. }
  191. }
  192. }
  193. $qi_service = new QiGoodsService(['mall_id' => $this->mall_id]);
  194. $qi_service->setSettings($supply_id);
  195. $qi_service->setSupplyId($supply_id);
  196. // 获取销售价格
  197. $sell_price = $qi_service->getPrice($juhe_goods, 'sell');
  198. // 成本价
  199. $cost = $qi_service->getPrice($juhe_goods, 'cost') * $num;
  200. if($danger_type == 1 && $total_price < $cost){
  201. // 产品售价 < (售价策略) 这个错误信息不能随便改
  202. $this->error = '下单失败,该商品信息已更新,请刷新重试1';
  203. // ...看是否需要下架
  204. return false;
  205. }
  206. if($danger_type == 2 ){
  207. //利润率 = (售价-成本价)/成本价 小于 多少
  208. $profit_rate = ($total_price - $cost) / $cost * 100;
  209. $profit_rate = number_format($profit_rate, 2, '.', '');
  210. if($profit_rate < $danger_rate){
  211. // 产品售价 < (售价策略) 这个错误信息不能随便改
  212. $this->error = '下单失败,该商品信息已更新,请刷新重试2';
  213. // ...看是否需要下架
  214. return false;
  215. }
  216. }
  217. return true;
  218. }
  219. }