DistributionLevel.php 13 KB

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