AgentLevel.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. <?php
  2. namespace addons\Agent\common\models;
  3. use addons\Agent\common\enums\AgentEnum;
  4. use common\enums\StatusEnum;
  5. use common\models\base\BaseActiveRecord;
  6. use Yii;
  7. /**
  8. * This is the model class for table "qimall_addons_agent_level".
  9. *
  10. * @property int $id
  11. * @property int $mall_id 商城ID
  12. * @property int $level
  13. * @property string $name 等级名称
  14. * @property int $is_auto_upgrade 是否开启自动升级
  15. * @property int $upgrade_type_condition 是否开启条件升级
  16. * @property string $checked_condition_values 升级条件
  17. * @property string $checked_condition_keys 升级条件选中的key集合
  18. * @property int $condition_type 0 未选择、1满足其一 2、满足所有
  19. * @property int $upgrade_type_goods 购买升级商品,0关闭,1开启
  20. * @property int $buy_goods_type 购买升级商品触发升级:0订单完成 1下单完成
  21. * @property int $goods_type 商品升级类型,1- 任意商品 2- 指定商品
  22. * @property string $goods_ids 商品id集合
  23. * @property string $goods_list 商品列表json
  24. * @property string $explain 等级说明
  25. * @property int $status 状态[-1:删除;0:禁用;1启用]
  26. * @property int $created_at
  27. * @property int $updated_at
  28. * @property int $upgrade_type_store_goods 购买门店商品升级,0关闭,1开启
  29. * @property int $buy_store_goods_type 购买门店商品触发升级:0订单完成 1下单完成
  30. * @property int $store_goods_type 门店商品升级类型,1- 任意商品 2- 指定商品
  31. * @property int $store_goods_ids 门店商品id集合
  32. * @property int $store_goods_list 门店商品集合
  33. * @property int $is_custom_condition_num 是否自定义升级商品条件数量
  34. */
  35. class AgentLevel extends BaseActiveRecord
  36. {
  37. /**
  38. * {@inheritdoc}
  39. */
  40. public static function tableName()
  41. {
  42. return '{{%addons_agent_level}}';
  43. }
  44. /**
  45. * {@inheritdoc}
  46. */
  47. public function rules()
  48. {
  49. return [
  50. [['mall_id', 'checked_condition_values'], 'required'],
  51. [['mall_id', 'level', 'is_auto_upgrade', 'upgrade_type_condition', 'condition_type', 'upgrade_type_goods',
  52. 'buy_goods_type', 'goods_type', 'status', 'created_at', 'updated_at','is_open_equal_settle_type','equal_settle_type', 'is_custom_condition_num'], 'integer'],
  53. [['checked_condition_values'], 'string'],
  54. [['name'], 'string', 'max' => 45],
  55. [['checked_condition_keys'], 'string', 'max' => 1000],
  56. [['goods_ids'], 'string', 'max' => 3000],
  57. [['goods_list', 'explain'], 'string'],
  58. [['upgrade_type_store_goods','buy_store_goods_type','store_goods_type'],'integer'],
  59. [['store_goods_ids','store_goods_list'],'string']
  60. ];
  61. }
  62. /**
  63. * {@inheritdoc}
  64. */
  65. public function attributeLabels()
  66. {
  67. return [
  68. 'id' => 'ID',
  69. 'mall_id' => '商城ID',
  70. 'level' => 'Level',
  71. 'name' => '等级名称',
  72. 'is_auto_upgrade' => '是否开启自动升级',
  73. 'upgrade_type_condition' => '是否开启条件升级',
  74. 'checked_condition_values' => '升级条件',
  75. 'checked_condition_keys' => '升级条件选中的key集合',
  76. 'condition_type' => '0 未选择、1满足其一 2、满足所有',
  77. 'upgrade_type_goods' => '购买升级商品,0关闭,1开启',
  78. 'buy_goods_type' => '购买升级商品触发升级:0订单完成 1下单完成',
  79. 'goods_type' => '商品升级类型,1- 任意商品 2- 指定商品',
  80. 'goods_ids' => '商品id集合',
  81. 'goods_list' => '商品列表json',
  82. 'explain' => '等级说明',
  83. 'status' => '状态[-1:删除;0:禁用;1启用]',
  84. 'created_at' => 'Created At',
  85. 'updated_at' => 'Updated At',
  86. 'is_custom_condition_num' => '是否自定义升级商品条件数量',
  87. ];
  88. }
  89. /**关联模型 */
  90. // 分类关系
  91. public function getCommissionLevelSetting()
  92. {
  93. return $this->hasOne(AgentCommissionLevelSetting::class, ['level' => 'level']);
  94. }
  95. public function getCommissionLevelSettingList()
  96. {
  97. return $this->hasMany(AgentCommissionLevelSetting::class, ['level' => 'level']);
  98. }
  99. /**
  100. * @desc:保存数据
  101. * @Author: hua
  102. * @Date: 2021/10/20
  103. * @Time: 15:46
  104. * @Copyright: copyright (c) 2021 广东七件事集团
  105. * @param array $params
  106. * @return AgentLevel|false|null
  107. * @throws \Exception
  108. */
  109. public static function setData(array $params)
  110. {
  111. $t = \Yii::$app->db->beginTransaction();
  112. try {
  113. if(!empty($params['id'])){
  114. $model = self::findOne(['mall_id' => $params['mall_id'], 'id' => $params['id'], 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]);
  115. if (empty($model)) throw new \Exception('服务不存在或已删除');
  116. }else{
  117. $model = new self();
  118. $model->loadDefaultValues();
  119. }
  120. if (!$model->load($params, '')) throw new \Exception($model->getErrorMessage());
  121. if (empty($params['checked_condition_keys'])) $params['checked_condition_keys'] = [];
  122. if (empty($params['checked_condition_values'])) $params['checked_condition_values'] = [];
  123. if (empty($params['goods_ids'])) $params['goods_ids'] = [];
  124. if (empty($params['goods_list'])) $params['goods_list'] = [];
  125. if (empty($params['store_goods_ids'])) $params['store_goods_ids'] = [];
  126. if (empty($params['store_goods_list'])) $params['store_goods_list'] = [];
  127. $model->checked_condition_keys = json_encode($params['checked_condition_keys'], JSON_NUMERIC_CHECK);
  128. $model->checked_condition_values = json_encode($params['checked_condition_values'], JSON_NUMERIC_CHECK);
  129. $model->goods_ids = json_encode($params['goods_ids'], JSON_NUMERIC_CHECK);
  130. $model->goods_list = json_encode($params['goods_list'], JSON_NUMERIC_CHECK);
  131. $model->store_goods_ids = json_encode($params['store_goods_ids'], JSON_NUMERIC_CHECK);
  132. $model->store_goods_list = json_encode($params['store_goods_list'], JSON_NUMERIC_CHECK);
  133. $status = 1;
  134. if (!$model->save()) throw new \Exception($model->getErrorMessage());
  135. if (isset($params['score']) && !empty($params['commission'])) {
  136. $params['from_type'] = AgentEnum::ADDONS_NAME;
  137. // 积分
  138. $params['commission_type'] = 'score';
  139. unset($params['score']['level']);
  140. $params = array_merge($params,$params['score']);
  141. $params['status'] = $status;
  142. if(!empty($params['alone_agent_equal_prize_info'])){
  143. $params['alone_agent_equal_prize_info'] = json_encode($params['alone_agent_equal_prize_info']);
  144. }else{
  145. $params['alone_agent_equal_prize_info'] = "";
  146. }
  147. if(!empty($params['repurchase_subsidy_condition'])){
  148. $params['repurchase_subsidy_condition'] = json_encode($params['repurchase_subsidy_condition']);
  149. }else{
  150. $params['repurchase_subsidy_condition'] = "";
  151. }
  152. $res = AgentCommissionLevelSetting::setData($params);
  153. if (!$res) throw new \Exception(AgentCommissionLevelSetting::getStaticError());
  154. // 佣金
  155. $params['commission_type'] = 'commission';
  156. unset($params['commission']['level']);
  157. $params = array_merge($params,$params['commission']);
  158. $params['status'] = $status;
  159. if(!empty($params['alone_agent_equal_prize_info'])){
  160. $params['alone_agent_equal_prize_info'] = json_encode($params['alone_agent_equal_prize_info']);
  161. }else{
  162. $params['alone_agent_equal_prize_info'] = "";
  163. }
  164. if(!empty($params['repurchase_subsidy_condition'])){
  165. $params['repurchase_subsidy_condition'] = json_encode($params['repurchase_subsidy_condition']);
  166. }else{
  167. $params['repurchase_subsidy_condition'] = "";
  168. }
  169. $res = AgentCommissionLevelSetting::setData($params);
  170. if (!$res) throw new \Exception(AgentCommissionLevelSetting::getStaticError());
  171. // 金币
  172. $params['commission_type'] = 'digital';
  173. unset($params['digital']['level']);
  174. $params = array_merge($params,$params['digital']);
  175. $params['status'] = $status;
  176. if(!empty($params['alone_agent_equal_prize_info'])){
  177. $params['alone_agent_equal_prize_info'] = json_encode($params['alone_agent_equal_prize_info']);
  178. }else{
  179. $params['alone_agent_equal_prize_info'] = "";
  180. }
  181. if(!empty($params['repurchase_subsidy_condition'])){
  182. $params['repurchase_subsidy_condition'] = json_encode($params['repurchase_subsidy_condition']);
  183. }else{
  184. $params['repurchase_subsidy_condition'] = "";
  185. }
  186. $res = AgentCommissionLevelSetting::setData($params);
  187. if (!$res) throw new \Exception(AgentCommissionLevelSetting::getStaticError());
  188. // 额外积分
  189. if (!empty($params['additional_score'])) {
  190. $params['commission_type'] = 'additional_score';
  191. unset($params['additional_score']['level']);
  192. $params = array_merge($params,$params['additional_score']);
  193. $params['status'] = $status;
  194. if(!empty($params['alone_agent_equal_prize_info'])){
  195. $params['alone_agent_equal_prize_info'] = json_encode($params['alone_agent_equal_prize_info']);
  196. }else{
  197. $params['alone_agent_equal_prize_info'] = "";
  198. }
  199. if(!empty($params['repurchase_subsidy_condition'])){
  200. $params['repurchase_subsidy_condition'] = json_encode($params['repurchase_subsidy_condition']);
  201. }else{
  202. $params['repurchase_subsidy_condition'] = "";
  203. }
  204. $res = AgentCommissionLevelSetting::setData($params);
  205. if (!$res) throw new \Exception(AgentCommissionLevelSetting::getStaticError());
  206. }
  207. }
  208. $t->commit();
  209. return $model;
  210. } catch (\Exception $e) {
  211. $t->rollBack();
  212. self::$static_error = $e->getMessage();
  213. return false;
  214. }
  215. }
  216. /**
  217. * @desc:条件升级数据源使用
  218. * @Author: hua
  219. * @Date: 2021/10/20
  220. * @Time: 9:54
  221. * @Copyright: copyright (c) 2021 广东七件事集团
  222. * @param $params
  223. * @return array
  224. */
  225. public static function getAgentListByUpgrade($params)
  226. {
  227. $rows = [];
  228. if (!empty($params['mall_id'])) {
  229. $where = ['mall_id' => $params['mall_id'], 'status' => StatusEnum::ENABLED];
  230. $list = self::findAll($where);
  231. foreach ($list as $v) {
  232. $rows[] = ['level' => $v['level'], 'name' => $v['name']];
  233. }
  234. }
  235. return $rows;
  236. }
  237. /**
  238. * @desc:等级和权重
  239. * @Author: hua
  240. * @Date: 2021/10/20
  241. * @Time: 16:08
  242. * @Copyright: copyright (c) 2021 广东七件事集团
  243. * @param $mall_id
  244. * @return array
  245. */
  246. public static function getLevelWeights($mall_id)
  247. {
  248. $list = self::find()->select('level')->where(['status' => [StatusEnum::DISABLED, StatusEnum::ENABLED], 'mall_id' => $mall_id])->column();
  249. $newList = [];
  250. for ($i = 1; $i <= 10; $i++) {
  251. $newList[] = [
  252. 'name' => '等级' . $i,
  253. 'level' => $i,
  254. 'disabled' => in_array($i, $list),
  255. ];
  256. }
  257. // array_unshift($newList, [
  258. // 'name' => "默认等级",
  259. // 'level' => 0,
  260. // 'disabled' => true,
  261. // ]);
  262. return $newList;
  263. }
  264. /**
  265. * @desc:获取经销商等级列表
  266. * @Author: hua
  267. * @Date: 2021/10/26
  268. * @Time: 14:52
  269. * @Copyright: copyright (c) 2021 广东七件事集团
  270. * @param array $cond
  271. * @param array $with
  272. * @param bool $is_array
  273. * @param string $select
  274. * @return array|\yii\db\ActiveRecord[]
  275. */
  276. public static function getLevelList(array $cond = [], array $with = [], bool $is_array = true, string $select = '*')
  277. {
  278. $default_cond = [['<>', 'status', StatusEnum::DELETE]];
  279. $cond = array_merge($default_cond, $cond);
  280. $query = self::find()->select($select);
  281. self::paramPack(['where' => $cond, 'with' => $with], $query);
  282. return $query->asArray($is_array)->all();
  283. }
  284. public static function del($params){
  285. try{
  286. $model = self::findOne(['id'=>$params['id']]);
  287. if(!empty($model)){
  288. $res = Agent::findOne(['level'=>$model->level,'mall_id'=>$params['mall_id'],'status'=>[StatusEnum::ENABLED,StatusEnum::DISABLED]]);
  289. if(!empty($res)) throw new \Exception('该等级关联了经销商');
  290. $model->status = StatusEnum::DELETE;
  291. $res = $model->save();
  292. if($res==false) throw new \Exception($model->getErrorMessage());
  293. $params['level'] = $model->level;
  294. $res = AgentCommissionLevelSetting::del($params);
  295. if($res==false) throw new \Exception(AgentCommissionLevelSetting::getStaticError());
  296. }
  297. return true;
  298. }catch (\Exception $e){
  299. self::$static_error = $e->getMessage();
  300. return false;
  301. }
  302. }
  303. /**
  304. * 获取等级
  305. * @Author: lun
  306. * @DateTime: 2021/12/16 0016 9:17
  307. * @Copyright: copyright (c) 2021 广东七件事集团
  308. * @param $mall_id
  309. * @return array
  310. */
  311. public static function getLevelByColumn($mall_id){
  312. return self::find()
  313. ->select('name')
  314. ->where(array('mall_id'=>$mall_id,'status'=>StatusEnum::ENABLED))
  315. ->asArray()
  316. ->indexBy('level')
  317. ->orderBy('level desc')
  318. ->column();
  319. }
  320. public static function getLevelCheckbox($params)
  321. {
  322. $rows = [];
  323. if (!empty($params['mall_id'])) {
  324. $rows = self::find()->where(['mall_id' => $params['mall_id']])
  325. ->andWhere(['status' => StatusEnum::ENABLED])
  326. ->select('level as value, name as label')->asArray()->all();
  327. }
  328. return $rows;
  329. }
  330. }