WechatVideoShopLevel.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. <?php
  2. namespace addons\WechatVideoShop\common\models;
  3. use common\enums\StatusEnum;
  4. use common\models\base\BaseActiveRecord;
  5. use PHPUnit\Util\Exception;
  6. /**
  7. * This is the model class for table "qimall_addons_wechat_video_shop_level".
  8. *
  9. * @property int $id
  10. * @property int $mall_id 商城ID
  11. * @property int $level
  12. * @property string $name 等级名称
  13. * @property int $is_auto_upgrade 是否开启自动升级
  14. * @property int $upgrade_type_condition 是否开启条件升级
  15. * @property string $checked_condition_values 升级条件
  16. * @property string $checked_condition_keys 升级条件选中的key集合
  17. * @property int $condition_type 0 未选择、1满足其一 2、满足所有
  18. * @property int $upgrade_type_goods 购买升级商品,0关闭,1开启
  19. * @property int $buy_goods_type 购买升级商品触发升级:0订单完成 1下单完成
  20. * @property int $goods_type 商品升级类型,1- 任意商品 2- 指定商品
  21. * @property string $goods_ids 商品id集合
  22. * @property string $goods_list 商品列表json
  23. * @property string $explain 等级说明
  24. * @property int $status 状态[-1:删除;0:禁用;1启用]
  25. * @property int $created_at
  26. * @property int $updated_at
  27. * @property int $reward_commission_type
  28. * @property int $reward_score_type
  29. * @property string $reward_commission
  30. * @property string $reward_score
  31. */
  32. class WechatVideoShopLevel extends BaseActiveRecord
  33. {
  34. /**
  35. * {@inheritdoc}
  36. */
  37. public static function tableName()
  38. {
  39. return '{{%addons_wechat_video_shop_level}}';
  40. }
  41. /**
  42. * {@inheritdoc}
  43. */
  44. public function rules()
  45. {
  46. return [
  47. [['mall_id', 'checked_condition_values'], 'required'],
  48. [['mall_id', 'level', 'is_auto_upgrade', 'upgrade_type_condition', 'condition_type', 'upgrade_type_goods',
  49. 'buy_goods_type', 'goods_type', 'status', 'created_at', 'updated_at','reward_commission_type', 'reward_score_type'], 'integer'],
  50. [['checked_condition_values'], 'string'],
  51. [['name'], 'string', 'max' => 45],
  52. [['checked_condition_keys'], 'string', 'max' => 1000],
  53. [['goods_ids'], 'string', 'max' => 3000],
  54. [['goods_list', 'explain'], 'string'],
  55. [['reward_commission', 'reward_score'], 'number'],
  56. ];
  57. }
  58. /**
  59. * {@inheritdoc}
  60. */
  61. public function attributeLabels()
  62. {
  63. return [
  64. 'id' => 'ID',
  65. 'mall_id' => '商城ID',
  66. 'level' => 'Level',
  67. 'name' => '等级名称',
  68. 'is_auto_upgrade' => '是否开启自动升级',
  69. 'upgrade_type_condition' => '是否开启条件升级',
  70. 'checked_condition_values' => '升级条件',
  71. 'checked_condition_keys' => '升级条件选中的key集合',
  72. 'condition_type' => '0 未选择、1满足其一 2、满足所有',
  73. 'upgrade_type_goods' => '购买升级商品,0关闭,1开启',
  74. 'buy_goods_type' => '购买升级商品触发升级:0订单完成 1下单完成',
  75. 'goods_type' => '商品升级类型,1- 任意商品 2- 指定商品',
  76. 'goods_ids' => '商品id集合',
  77. 'goods_list' => '商品列表json',
  78. 'explain' => '等级说明',
  79. 'status' => '状态[-1:删除;0:禁用;1启用]',
  80. 'created_at' => 'Created At',
  81. 'updated_at' => 'Updated At',
  82. ];
  83. }
  84. /**
  85. * @desc:保存数据
  86. * @Author: hua
  87. * @Date: 2021/10/20
  88. * @Time: 15:46
  89. * @Copyright: copyright (c) 2021 广东七件事集团
  90. * @param array $params
  91. * @return WechatVideoShopLevel|false|null
  92. * @throws \Exception
  93. */
  94. public static function setData(array $params)
  95. {
  96. $t = \Yii::$app->db->beginTransaction();
  97. try {
  98. if(!empty($params['id'])){
  99. $model = self::findOne(['mall_id' => $params['mall_id'], 'id' => $params['id'], 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]);
  100. if (empty($model)) throw new \Exception('服务不存在或已删除');
  101. }else{
  102. $model = new self();
  103. $model->loadDefaultValues();
  104. }
  105. if (!$model->load($params, '')) throw new \Exception($model->getErrorMessage());
  106. if (empty($params['checked_condition_keys'])) $params['checked_condition_keys'] = [];
  107. if (empty($params['checked_condition_values'])) $params['checked_condition_values'] = [];
  108. if (empty($params['goods_ids'])) $params['goods_ids'] = [];
  109. if (empty($params['goods_list'])) $params['goods_list'] = [];
  110. $model->checked_condition_keys = json_encode($params['checked_condition_keys'], JSON_NUMERIC_CHECK);
  111. $model->checked_condition_values = json_encode($params['checked_condition_values'], JSON_NUMERIC_CHECK);
  112. $model->goods_ids = json_encode($params['goods_ids'], JSON_NUMERIC_CHECK);
  113. $model->goods_list = json_encode($params['goods_list'], JSON_NUMERIC_CHECK);
  114. if (!$model->save()) throw new Exception($model->getErrorMessage());
  115. $t->commit();
  116. return $model;
  117. } catch (\Exception $e) {
  118. $t->rollBack();
  119. self::$static_error = $e->getMessage();
  120. return false;
  121. }
  122. }
  123. /**
  124. * @desc:条件升级数据源使用
  125. * @Author: hua
  126. * @Date: 2021/10/20
  127. * @Time: 9:54
  128. * @Copyright: copyright (c) 2021 广东七件事集团
  129. * @param $params
  130. * @return array
  131. */
  132. public static function getAgentListByUpgrade($params)
  133. {
  134. $rows = [];
  135. if (!empty($params['mall_id'])) {
  136. $where = ['mall_id' => $params['mall_id'], 'status' => StatusEnum::ENABLED];
  137. $list = self::findAll($where);
  138. foreach ($list as $v) {
  139. $rows[] = ['level' => $v['level'], 'name' => $v['name']];
  140. }
  141. }
  142. return $rows;
  143. }
  144. /**
  145. * @desc:等级和权重
  146. * @Author: hua
  147. * @Date: 2021/10/20
  148. * @Time: 16:08
  149. * @Copyright: copyright (c) 2021 广东七件事集团
  150. * @param $mall_id
  151. * @return array
  152. */
  153. public static function getLevelWeights($mall_id)
  154. {
  155. $list = self::find()->select('level')->where(['status' => [StatusEnum::DISABLED, StatusEnum::ENABLED], 'mall_id' => $mall_id])->column();
  156. $newList = [];
  157. for ($i = 1; $i <= 10; $i++) {
  158. $newList[] = [
  159. 'name' => '等级' . $i,
  160. 'level' => $i,
  161. 'disabled' => in_array($i, $list),
  162. ];
  163. }
  164. // array_unshift($newList, [
  165. // 'name' => "默认等级",
  166. // 'level' => 0,
  167. // 'disabled' => true,
  168. // ]);
  169. return $newList;
  170. }
  171. /**
  172. * @desc:获取经销商等级列表
  173. * @Author: hua
  174. * @Date: 2021/10/26
  175. * @Time: 14:52
  176. * @Copyright: copyright (c) 2021 广东七件事集团
  177. * @param array $cond
  178. * @param array $with
  179. * @param bool $is_array
  180. * @param string $select
  181. * @return array|\yii\db\ActiveRecord[]
  182. */
  183. public static function getLevelList(array $cond = [], array $with = [], bool $is_array = true, string $select = '*')
  184. {
  185. $default_cond = [['<>', 'status', StatusEnum::DELETE]];
  186. $cond = array_merge($default_cond, $cond);
  187. $query = self::find()->select($select);
  188. self::paramPack(['where' => $cond, 'with' => $with], $query);
  189. return $query->asArray($is_array)->all();
  190. }
  191. public static function del($params){
  192. try{
  193. $model = self::findOne(['id'=>$params['id']]);
  194. if(!empty($model)){
  195. $res = WechatVideoShopSharer::findOne(['level'=>$model->level,
  196. 'mall_id'=>$params['mall_id'],'status'=>[StatusEnum::ENABLED,StatusEnum::DISABLED]]);
  197. if(!empty($res)) throw new \Exception('该等级关联了经销商');
  198. $model->status = StatusEnum::DELETE;
  199. $res = $model->save();
  200. if(!$res) throw new \Exception($model->getErrorMessage());
  201. }
  202. return true;
  203. }catch (\Exception $e){
  204. self::$static_error = $e->getMessage();
  205. return false;
  206. }
  207. }
  208. /**
  209. * 获取等级
  210. * @Author: lun
  211. * @DateTime: 2021/12/16 0016 9:17
  212. * @Copyright: copyright (c) 2021 广东七件事集团
  213. * @param $mall_id
  214. * @return array
  215. */
  216. public static function getLevelByColumn($mall_id){
  217. return self::find()
  218. ->select('name')
  219. ->where(array('mall_id'=>$mall_id,'status'=>StatusEnum::ENABLED))
  220. ->asArray()
  221. ->indexBy('level')
  222. ->orderBy('level desc')
  223. ->column();
  224. }
  225. /**
  226. * @desc:条件升级数据源使用
  227. * @Author: hua
  228. * @Date: 2021/10/23
  229. * @Time: 15:30
  230. * @Copyright: copyright (c) 2021 广东七件事集团
  231. * @param $params
  232. * @return array
  233. */
  234. public static function getListByUpgrade($params)
  235. {
  236. $rows = [];
  237. if (!empty($params['mall_id'])) {
  238. $where = ['mall_id' => $params['mall_id'],'status'=>StatusEnum::ENABLED];
  239. $list = self::findAll($where);
  240. foreach ($list as $v) {
  241. $rows[] = ['level' => $v['level'], 'name' => $v['name']];
  242. }
  243. }
  244. return $rows;
  245. }
  246. }