Level.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. <?php
  2. namespace addons\OperationCenter\common\models;
  3. use Yii;
  4. use yii\helpers\Json;
  5. /**
  6. * This is the model class for table "{{%addons_operation_center_level}}".
  7. *
  8. * @property int $id
  9. * @property int $mall_id Mall Id
  10. * @property string $name 等级名称
  11. * @property int $level 等级权重
  12. * @property int $commission1_type 1级佣金奖励类型
  13. * @property float $commission1_value 1级佣金数量
  14. * @property int $commission2_type 2级佣金奖励类型
  15. * @property float $commission2_value 2级佣金数量
  16. * @property int $is_auto_upgrade 是否自动升级
  17. * @property int $upgrade_type_condition 是否开启条件升级
  18. * @property string|null $checked_condition_values 升级条件
  19. * @property string|null $checked_condition_keys 升级条件选中的key集合
  20. * @property int $upgrade_type_goods 购买升级商品,0关闭,1开启
  21. * @property int $buy_goods_type 购买升级商品触发升级:0订单完成 1下单完成
  22. * @property int|null $goods_type 商品升级类型,1- 任意商品 2- 指定商品
  23. * @property string|null $goods_ids 商品id集合
  24. * @property string|null $goods_list 商品列表json
  25. * @property string|null $explain 等级说明
  26. * @property int $status 等级开启状态
  27. * @property int|null $deleted_at
  28. * @property int|null $updated_at
  29. * @property int|null $created_at
  30. */
  31. class Level extends BaseActiveRecord
  32. {
  33. /**
  34. * 比例
  35. */
  36. const RATIO = 1;
  37. /**
  38. * 固定
  39. */
  40. const FIXED = 2;
  41. /**
  42. * @var array
  43. */
  44. protected $json_keys = [
  45. 'checked_condition_values', 'checked_condition_keys', 'goods_ids', 'goods_list'
  46. ];
  47. /**
  48. * {@inheritdoc}
  49. */
  50. public static function tableName()
  51. {
  52. return '{{%addons_operation_center_level}}';
  53. }
  54. /**
  55. * {@inheritdoc}
  56. */
  57. public function rules()
  58. {
  59. return [
  60. [['mall_id', 'name', 'level'], 'required'],
  61. [['mall_id', 'level', 'commission1_type', 'commission2_type', 'is_auto_upgrade', 'upgrade_type_condition', 'upgrade_type_goods', 'buy_goods_type', 'goods_type', 'status', 'deleted_at', 'updated_at', 'created_at'], 'integer'],
  62. [['commission1_value', 'commission2_value'], 'number'],
  63. [['checked_condition_values', 'checked_condition_keys', 'goods_ids', 'goods_list'], 'string'],
  64. [['name'], 'string', 'max' => 64],
  65. [['explain'], 'string', 'max' => 255],
  66. ];
  67. }
  68. /**
  69. * {@inheritdoc}
  70. */
  71. public function attributeLabels()
  72. {
  73. return [
  74. 'id' => 'ID',
  75. 'mall_id' => 'Mall ID',
  76. 'name' => 'Name',
  77. 'level' => 'Level',
  78. 'commission1_type' => 'Commission1 Type',
  79. 'commission1_value' => 'Commission1 Value',
  80. 'commission2_type' => 'Commission2 Type',
  81. 'commission2_value' => 'Commission2 Value',
  82. 'is_auto_upgrade' => 'Is Auto Upgrade',
  83. 'upgrade_type_condition' => 'Upgrade Type Condition',
  84. 'checked_condition_values' => 'Checked Condition Values',
  85. 'checked_condition_keys' => 'Checked Condition Keys',
  86. 'upgrade_type_goods' => 'Upgrade Type Goods',
  87. 'buy_goods_type' => 'Buy Goods Type',
  88. 'goods_type' => 'Goods Type',
  89. 'goods_ids' => 'Goods Ids',
  90. 'goods_list' => 'Goods List',
  91. 'explain' => 'Explain',
  92. 'status' => 'Status',
  93. 'deleted_at' => 'Deleted At',
  94. 'updated_at' => 'Updated At',
  95. 'created_at' => 'Created At',
  96. ];
  97. }
  98. /**
  99. * 获取开启的等级列表
  100. *
  101. * @param integer $mall_id
  102. * @return array
  103. */
  104. public static function getOpenLevelList(int $mall_id)
  105. {
  106. return static::find()
  107. ->andWhere(['mall_id' => $mall_id, 'status' => 1])
  108. ->select(['id', 'name', 'level'])
  109. ->asArray()
  110. ->all();
  111. }
  112. /**
  113. * 获取权重
  114. *
  115. * @param integer $mall_id
  116. * @return array
  117. */
  118. public static function getLevelWeights(int $mall_id)
  119. {
  120. $list = self::find()->select('level')->andWhere(['mall_id' => $mall_id])->column();
  121. $newList = [];
  122. for ($i = 1; $i <= 10; $i++) {
  123. $newList[] = [
  124. 'name' => '等级' . $i,
  125. 'level' => $i,
  126. 'disabled' => in_array($i, $list),
  127. ];
  128. }
  129. return $newList;
  130. }
  131. /**
  132. * 设置等级数据
  133. *
  134. * @param array $data
  135. * @return true|static
  136. */
  137. public function setLevelData(array $data)
  138. {
  139. $data = $this->jsonEncode($data);
  140. $this->load($data, '');
  141. return $this->save() ? true : $this;
  142. }
  143. /**
  144. * 获取等级详情
  145. *
  146. * @param integer $id
  147. * @return static|null
  148. */
  149. public static function getLevelDetail(int $mall_id, int $id)
  150. {
  151. return static::find()
  152. ->andWhere(['mall_id' => $mall_id, 'id' => $id])
  153. ->one();
  154. }
  155. /**
  156. * 等级删除
  157. *
  158. * @return mixed
  159. */
  160. public function delLevel()
  161. {
  162. return $this->delete();
  163. }
  164. /**
  165. * 通过分佣等级(直推 间推)获取分佣类型
  166. *
  167. * @param int $level
  168. * @return int
  169. */
  170. public function getCommissionTypeByLevel($level)
  171. {
  172. $key = sprintf('commission%s_type', $level);
  173. return $this->{$key};
  174. }
  175. /**
  176. * 通过分佣等级(直推 间推)获取分佣数量
  177. *
  178. * @param int $level
  179. * @return float
  180. */
  181. public function getCommissionValueByLevel($level)
  182. {
  183. $key = sprintf('commission%s_value', $level);
  184. return $this->{$key};
  185. }
  186. /**
  187. * @return boolean
  188. */
  189. public function hasUpgrade()
  190. {
  191. return (boolean) $this->is_auto_upgrade;
  192. }
  193. /**
  194. * @return boolean
  195. */
  196. public function openGoodsUpgrade()
  197. {
  198. return (boolean) $this->upgrade_type_goods;
  199. }
  200. /**
  201. * @param integer $is_finish
  202. * @return boolean
  203. */
  204. public function buyGoodsType(int $is_finish)
  205. {
  206. if ($is_finish == 0 && $this->buy_goods_type == 1) {
  207. return true;
  208. }
  209. if ($is_finish == 1 && $this->buy_goods_type == 0) {
  210. return true;
  211. }
  212. return false;
  213. // return $this->buy_goods_type == $is_finish;
  214. }
  215. /**
  216. * 是否升级商品
  217. *
  218. * @param integer $goods_id
  219. * @return boolean
  220. */
  221. public function isUpgradeGoods(int $goods_id)
  222. {
  223. // 任意商品
  224. if ($this->goods_type == 1) {
  225. return true;
  226. }
  227. // 指定商品
  228. if (in_array($goods_id, $this->getGoodsIds())) {
  229. return true;
  230. }
  231. return false;
  232. }
  233. /**
  234. * @return array
  235. */
  236. public function getGoodsIds()
  237. {
  238. if ($this->goods_ids) {
  239. return Json::decode($this->goods_ids);
  240. }
  241. return [];
  242. }
  243. /**
  244. * 查询大于等级的等级
  245. *
  246. * @param integer $mall_id
  247. * @param integer $level
  248. * @return array
  249. */
  250. public static function getLevelByGtLevel(int $mall_id, int $level)
  251. {
  252. return static::find()
  253. ->andWhere(
  254. ['=', 'mall_id', $mall_id],
  255. )
  256. ->andWhere(
  257. ['>', 'level', $level]
  258. )
  259. ->andWhere(['status' => 1])
  260. ->orderBy('level desc')
  261. ->all();
  262. }
  263. }