123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324 |
- <?php
- /**
- * 扣量
- * Created by: PhpStorm
- * User: lytian
- * Date: 2019/9/9
- * Time: 15:22
- */
- namespace app\main\service;
- use app\common\library\Redis;
- use app\common\model\KlRuleConfig;
- use app\common\model\KlRuleManage;
- use app\common\model\ReturnRecommand;
- use app\main\constants\CacheConstants;
- use app\main\constants\KlConstants;
- use think\db\Query;
- class KlService extends BaseService
- {
- function _construct()
- {
- }
- protected static $self = null;
- public static function instance()
- {
- if (self::$self == null) {
- self::$self = new self();
- }
- return self::$self;
- }
- /**
- * @return KlRuleConfig
- */
- public function getRuleConfigModel()
- {
- return model("KlRuleConfig");
- }
- /**
- * @return KlRuleManage
- */
- public function getRuleManageModel()
- {
- return model('KlRuleManage');
- }
- /**
- * 获取扣量规则
- * @param $channel_id
- * @return array
- */
- public function getKlRule($channel_id)
- {
- $result = [];
- $ruleId = $this->getKlRuleId($channel_id);
- if(empty($ruleId)){
- return $result;
- }
- $result = $this->getRuleConfig($ruleId);
- return $result;
- }
- /**
- * 找出渠道匹配的KL规则ID,按权重倒序排列,取一条
- * @param $channel_id
- * @return string
- */
- public function getKlRuleId($channel_id)
- {
- $cacheKey = CacheConstants::getChannelKlRuleManageCacheList();
- if ($id = Redis::instance()->hGet($cacheKey, $channel_id)) {
- $id = $id > 0 ? $id : 0;
- } else {
- $id = $this->getRuleManageModel()
- ->where('status', 'eq', '1')
- ->where(
- function (Query $query) use ($channel_id) {
- $query->where('channel_ids', '*')
- ->whereOr("FIND_IN_SET($channel_id, channel_ids)");
- }
- )
- ->order('weight', 'desc')
- ->value('id');
- if (!$id) {
- $id = -1;
- }
- Redis::instance()->hSet($cacheKey, $channel_id, $id);
- }
- return $id;
- }
- /**
- * 清除缓存
- */
- public function delKlRuleManageCache()
- {
- $cacheListKey = CacheConstants::getChannelKlRuleManageCacheList();
- Redis::instance()->del($cacheListKey);
- }
- /**
- * 拉取改规则的扣量设置
- * @param $rule_id
- * @return array
- */
- public function getRuleConfig($rule_id)
- {
- $redisKey = KlConstants::REDIS_KEY_PREFIX.$rule_id;
- if (Redis::instance()->exists($redisKey)) {
- $result = json_decode(Redis::instance()->get($redisKey), true);
- } else {
- //拉取数据
- $rule = $this->getRuleConfigModel()->where("rule_id", "EQ", $rule_id)->find();
- //筛选器
- $result[KlConstants::KL_SELECTOR] = [];
- if ($rule['s_new_user_status'] == '1') {
- $result[KlConstants::KL_SELECTOR][KlConstants::KL_SELECTOR_NEW_USER] = [];
- if (
- $rule['s_new_user_city_status'] == '1'
- && $rule['s_new_user_city']
- ) {
- $result[KlConstants::KL_SELECTOR][KlConstants::KL_SELECTOR_NEW_USER][KlConstants::KL_SELECTOR_NEW_CITY] = $rule['s_new_user_city'];
- }
- if ($rule['s_new_user_add_status'] == '1') {
- $result[KlConstants::KL_SELECTOR][KlConstants::KL_SELECTOR_NEW_USER][KlConstants::KL_SELECTOR_NEW_ADD] = $this->parseData($rule['s_new_user_add_op'], $rule['s_new_user_add']);
- }
- if ($rule['s_new_user_recharge_status'] == '1') {
- $result[KlConstants::KL_SELECTOR][KlConstants::KL_SELECTOR_NEW_USER][KlConstants::KL_SELECTOR_NEW_RECHARGE] = $this->parseData($rule['s_new_user_recharge_op'], $rule['s_new_recharge_add']);
- }
- $result[KlConstants::KL_SELECTOR][KlConstants::KL_SELECTOR_NEW_USER][KlConstants::KL_SELECTOR_NEW_RELATION] = $this->transferRelation($rule['s_new_relation']);
- }
- if ($rule['s_old_user_status'] == '1') {
- $result[KlConstants::KL_SELECTOR][KlConstants::KL_SELECTOR_OLD_USER] = [];
- if (
- $rule['s_old_user_city_status'] == '1'
- && $rule['s_old_user_city']
- ) {
- $result[KlConstants::KL_SELECTOR][KlConstants::KL_SELECTOR_OLD_USER][KlConstants::KL_SELECTOR_OLD_CITY] = $rule['s_old_user_city'];
- }
- if ($rule['s_old_user_active_status'] == '1') {
- $result[KlConstants::KL_SELECTOR][KlConstants::KL_SELECTOR_OLD_USER][KlConstants::KL_SELECTOR_OLD_ACTIVE] = $this->parseData($rule['s_old_user_active_op'], $rule['s_old_active']);
- }
- if ($rule['s_old_user_recharge_status'] == '1') {
- $result[KlConstants::KL_SELECTOR][KlConstants::KL_SELECTOR_OLD_USER][KlConstants::KL_SELECTOR_OLD_RECHARGE] = $this->parseData($rule['s_old_user_recharge_op'], $rule['s_old_recharge_add']);
- }
- $result[KlConstants::KL_SELECTOR][KlConstants::KL_SELECTOR_OLD_USER][KlConstants::KL_SELECTOR_OLD_RELATION] = $this->transferRelation($rule['s_old_relation']);
- }
- //过滤器
- $result[KlConstants::KL_FILTER] = [];
- if ($rule['f_behavior_status'] == '1') {
- $result[KlConstants::KL_FILTER][KlConstants::KL_FILTER_BEHAVOR] = [];
- if ($rule['f_one_recharge_status'] == '1') {
- $result[KlConstants::KL_FILTER][KlConstants::KL_FILTER_BEHAVOR][KlConstants::KL_FILTER_BEHAVOR_SPACE] = $rule['f_one_recharge'];
- }
- if ($rule['f_recharge_status'] == '1' && $rule['f_recharge']) {
- $result[KlConstants::KL_FILTER][KlConstants::KL_FILTER_BEHAVOR][KlConstants::KL_FILTER_BEHAVOR_RECHARGE] = $rule['f_recharge'];
- }
- if ($rule['f_read_status'] == '1') {
- $result[KlConstants::KL_FILTER][KlConstants::KL_FILTER_BEHAVOR][KlConstants::KL_FILTER_BEHAVOR_READ] = $this->parseData($rule['f_read_op'], $rule['f_read']);
- }
- if ($rule['f_kandian_status'] == '1') {
- $result[KlConstants::KL_FILTER][KlConstants::KL_FILTER_BEHAVOR][KlConstants::KL_FILTER_BEHAVOR_KANDIAN] = $this->parseData($rule['f_kandian_op'], $rule['f_kandian']);
- }
- if ($rule['f_orders_status'] == '1') {
- $result[KlConstants::KL_FILTER][KlConstants::KL_FILTER_BEHAVOR][KlConstants::KL_FILTER_BEHAVOR_ORDERS] = $this->parseData($rule['f_orders_op'], $rule['f_orders']);
- }
- $result[KlConstants::KL_FILTER][KlConstants::KL_FILTER_BEHAVOR][KlConstants::KL_FILTER_BEHAVOR_RELATION] = $this->transferRelation($rule['f_behavior_relation']);
- }
- if ($rule['f_register_status'] == '1') {
- $result[KlConstants::KL_FILTER][KlConstants::KL_FILTER_REGISTER] = [];
- $result[KlConstants::KL_FILTER][KlConstants::KL_FILTER_REGISTER][KlConstants::KL_FILTER_REGISTER_SPACE] = $this->parseData($rule['f_register_op'], $rule['f_register']);
- $result[KlConstants::KL_FILTER][KlConstants::KL_FILTER_REGISTER][KlConstants::KL_FILTER_REGISTER_RELATION] = $this->transferRelation($rule['f_register_relation']);
- }
- //扣量规则
- $result[KlConstants::KL_DEDUCT] = [];
- if ($rule['o_general_status'] == '1') {
- $result[KlConstants::KL_DEDUCT][KlConstants::KL_DEDUCT_NORMAL] = [];
- if ($rule['o_general_new_status'] == '1') {
- $result[KlConstants::KL_DEDUCT][KlConstants::KL_DEDUCT_NORMAL][KlConstants::KL_DEDUCT_NORMAL_NEW] = $rule['o_general_new'];
- }
- if ($rule['o_general_old_status'] == '1') {
- $result[KlConstants::KL_DEDUCT][KlConstants::KL_DEDUCT_NORMAL][KlConstants::KL_DEDUCT_NORMAL_OLD] = $rule['o_general_old'];
- }
- if ($rule['o_general_many_status'] == '1') {
- $result[KlConstants::KL_DEDUCT][KlConstants::KL_DEDUCT_NORMAL][KlConstants::KL_DEDUCT_NORMAL_MANY] = $rule['o_general_many'];
- }
- }
- if ($rule['o_vip_status'] == '1') {
- $result[KlConstants::KL_DEDUCT][KlConstants::KL_DEDUCT_VIP] = [];
- if ($rule['o_vip_new_status'] == '1') {
- $result[KlConstants::KL_DEDUCT][KlConstants::KL_DEDUCT_VIP][KlConstants::KL_DEDUCT_VIP_NEW] = $rule['o_vip_new'];
- }
- if ($rule['o_vip_old_status'] == '1') {
- $result[KlConstants::KL_DEDUCT][KlConstants::KL_DEDUCT_VIP][KlConstants::KL_DEDUCT_VIP_OLD] = $rule['o_vip_old'];
- }
- if ($rule['o_vip_many_status'] == '1') {
- $result[KlConstants::KL_DEDUCT][KlConstants::KL_DEDUCT_VIP][KlConstants::KL_DEDUCT_VIP_MANY] = $rule['o_vip_many'];
- }
- }
- if ($rule['o_activity_status'] == '1') {
- $result[KlConstants::KL_DEDUCT][KlConstants::KL_DEDUCT_ACTIVITY] = [];
- if ($rule['o_activity_new_status'] == '1') {
- $result[KlConstants::KL_DEDUCT][KlConstants::KL_DEDUCT_ACTIVITY][KlConstants::KL_DEDUCT_ACTIVITY_NEW] = $rule['o_activity_new'];
- }
- if ($rule['o_activity_old_status'] == '1') {
- $result[KlConstants::KL_DEDUCT][KlConstants::KL_DEDUCT_ACTIVITY][KlConstants::KL_DEDUCT_ACTIVITY_OLD] = $rule['o_activity_old'];
- }
- if ($rule['o_activity_many_status'] == '1') {
- $result[KlConstants::KL_DEDUCT][KlConstants::KL_DEDUCT_ACTIVITY][KlConstants::KL_DEDUCT_ACTIVITY_MANY] = $rule['o_activity_many'];
- }
- }
- Redis::instance()->set($redisKey, json_encode($result));
- /*$result = [
- 'KLS' => [
- 'X' => [
- 'C' => '1,2,3',
- 'N' => '>0',
- 'R' => '>0',
- 'RE' => 'and',
- ],
- 'L' => [
- 'C' => '1,2,3',
- 'N' => '<10',
- 'R' => '<10',
- 'RE' => 'or',
- ],
- ],
- 'KLG' => [
- 'Y' => [
- 'D' => 10,
- 'C' => 'H,Z',
- 'Y' => '>1',
- 'S' => '>100',
- 'O' => '>10',
- 'RE' => 'and',
- ],
- 'Z' => [
- 'T' => '>1',
- 'RE' => 'and',
- ],
- ],
- 'KLD' => [
- 'P' => [
- 'X' => 30,
- 'L' => 30,
- 'M' => 30,
- ],
- 'V' => [
- 'X' => 20,
- 'L' => 20,
- 'M' => 20,
- ],
- 'H' => [
- 'X' => 10,
- 'L' => 10,
- 'M' => 10,
- ]
- ],
- ];*/
- }
- return $result;
- }
- private function parseData($op, $val)
- {
- $relation = [
- '0' => '>',
- '1' => '<',
- '2' => '=',
- ];
- $opStr = $relation[$op] ?? '=';
- return $opStr.$val;
- }
- private function transferRelation($relation)
- {
- return $relation == 0 ? 'and' : 'or';
- }
- }
|