StoreBossAgentLevel.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. <?php
  2. namespace addons\Store\common\models;
  3. use common\enums\StatusEnum;
  4. use Yii;
  5. /**
  6. * This is the model class for table "{{%addons_store_boss_agent_level}}".
  7. *
  8. * @property int $id
  9. * @property int $mall_id
  10. * @property int $store_id
  11. * @property int $level
  12. * @property string $name
  13. * @property int $is_auto_upgrade
  14. * @property string $detail
  15. * @property float $price 永久分红
  16. * @property int $created_at
  17. * @property int $updated_at
  18. * @property int $status
  19. * @property string|null $checked_condition_values 升级条件
  20. * @property int $is_enable 是否启用
  21. * @property string|null $checked_condition_keys 升级条件选中的key集合
  22. * @property int $condition_type 0 未选择、1满足其一 2、满足所有
  23. * @property int $upgrade_type_goods
  24. * @property int $upgrade_type_condition
  25. * @property string|null $goods_warehouse_ids 商品仓库的ID
  26. * @property string|null $goods_list 商品列表
  27. * @property int $goods_type 商品升级类型
  28. * @property int $buy_goods_type 0订单完成 1下单 完成
  29. */
  30. class StoreBossAgentLevel extends \common\models\base\BaseActiveRecord
  31. {
  32. /**
  33. * {@inheritdoc}
  34. */
  35. public static function tableName()
  36. {
  37. return '{{%addons_store_boss_agent_level}}';
  38. }
  39. /**
  40. * {@inheritdoc}
  41. */
  42. public function rules()
  43. {
  44. return [
  45. [['mall_id', 'store_id', 'level', 'is_auto_upgrade', 'created_at', 'updated_at', 'status', 'is_enable', 'condition_type', 'upgrade_type_goods', 'upgrade_type_condition', 'goods_type', 'buy_goods_type'], 'integer'],
  46. [['name'], 'required'],
  47. [['price'], 'number'],
  48. [['checked_condition_values', 'checked_condition_keys', 'goods_warehouse_ids', 'goods_list'], 'string'],
  49. [['name'], 'string', 'max' => 45],
  50. [['detail'], 'string', 'max' => 255],
  51. ];
  52. }
  53. /**
  54. * {@inheritdoc}
  55. */
  56. public function attributeLabels()
  57. {
  58. return [
  59. 'id' => 'ID',
  60. 'mall_id' => 'Mall ID',
  61. 'store_id' => 'Store ID',
  62. 'level' => 'Level',
  63. 'name' => 'Name',
  64. 'is_auto_upgrade' => 'Is Auto Upgrade',
  65. 'detail' => 'Detail',
  66. 'price' => 'Price',
  67. 'created_at' => 'Created At',
  68. 'updated_at' => 'Updated At',
  69. 'status' => 'Status',
  70. 'checked_condition_values' => 'Checked Condition Values',
  71. 'is_enable' => 'Is Enable',
  72. 'checked_condition_keys' => 'Checked Condition Keys',
  73. 'condition_type' => 'Condition Type',
  74. 'upgrade_type_goods' => 'Upgrade Type Goods',
  75. 'upgrade_type_condition' => 'Upgrade Type Condition',
  76. 'goods_warehouse_ids' => 'Goods Warehouse Ids',
  77. 'goods_list' => 'Goods List',
  78. 'goods_type' => 'Goods Type',
  79. 'buy_goods_type' => 'Buy Goods Type',
  80. ];
  81. }
  82. /**
  83. * 获取可升级的等级列表
  84. * @Author bing
  85. * @DateTime 2020-11-07 09:59:33
  86. * @copyright: Copyright (c) 2020 广东七件事集团
  87. * @param integer $level
  88. * @param integer $mall_id
  89. * @param integer $store_id
  90. * @param string $order
  91. * @return void|mixed
  92. */
  93. public static function getCanLevelList($level,$mall_id,$store_id,$order = 'desc'){
  94. return self::find()
  95. ->where(['mall_id' => $mall_id,'store_id' => $store_id, 'is_enable' => 1, 'is_delete' => 0])
  96. ->andWhere(['>', 'level', $level])
  97. ->orderBy("level $order")
  98. ->all();
  99. }
  100. public static function getList($mall_id,$store_id)
  101. {
  102. $levelList = self::find()->where([
  103. 'mall_id' => $mall_id,'store_id' => $store_id, 'status' => [StatusEnum::ENABLED,StatusEnum::DISABLED], 'is_enable' => 1,
  104. ])->select(['id', 'level', 'name'])->orderBy(['level' => SORT_ASC])->asArray()->all();
  105. return $levelList;
  106. }
  107. /**
  108. * @desc:保存数据
  109. * @Author: hua
  110. * @Date: 2021/10/20
  111. * @Time: 15:46
  112. * @Copyright: copyright (c) 2021 广东七件事集团
  113. * @param array $params
  114. * @return AgentLevel|false|null
  115. * @throws \Exception
  116. */
  117. public static function setData(array $params)
  118. {
  119. $t = \Yii::$app->db->beginTransaction();
  120. try {
  121. if(empty($params['id']) && !empty($params['level'])){
  122. $level_exist = self::findOne(['mall_id' => $params['mall_id'], 'level' => $params['level'], 'store_id'=>$params['store_id'],'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]);
  123. if($level_exist) throw new \Exception('该等级已存在数据');
  124. }
  125. if(!empty($params['id'])){
  126. $model = self::findOne(['mall_id' => $params['mall_id'], 'id' => $params['id'], 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]);
  127. if (empty($model)) throw new \Exception('服务不存在或已删除');
  128. }else{
  129. $model = new self();
  130. $model->loadDefaultValues();
  131. }
  132. if (!$model->load($params, '')) throw new \Exception($model->getErrorMessage());
  133. $model->checked_condition_keys = json_encode($params['checked_condition_keys']);
  134. $model->checked_condition_values = json_encode($params['checked_condition_values']);
  135. $model->goods_list = json_encode($params['goods_list']);
  136. $model->goods_warehouse_ids = json_encode($params['goods_warehouse_ids']);
  137. if (!$model->save()) throw new \Exception($model->getErrorMessage());
  138. $t->commit();
  139. return $model;
  140. } catch (\Exception $e) {
  141. $t->rollBack();
  142. self::$static_error = $e->getMessage();
  143. return false;
  144. }
  145. }
  146. public static function getDetail($params)
  147. {
  148. $weights = self::getLevelWeights();
  149. $level = self::findOne(['id' => $params['id'], 'status' => [StatusEnum::ENABLED,StatusEnum::DISABLED]]);
  150. if ($level) {
  151. if ($level->checked_condition_values) {
  152. $level->checked_condition_values = json_decode($level->checked_condition_values,true);
  153. }
  154. if ($level->checked_condition_keys) {
  155. $level->checked_condition_keys = json_decode($level->checked_condition_keys,true);
  156. }
  157. if ($level->goods_warehouse_ids) {
  158. $level->goods_warehouse_ids = json_decode($level->goods_warehouse_ids,true);
  159. } else {
  160. $level->goods_warehouse_ids = [];
  161. }
  162. if ($level->goods_list) {
  163. $level->goods_list = json_decode($level->goods_list,true);
  164. } else {
  165. $level->goods_list = [];
  166. }
  167. }
  168. return [
  169. 'code' => 0,
  170. 'msg' => '',
  171. 'data' => [
  172. 'detail' => $level,
  173. 'weights' => $weights['newList']
  174. ]
  175. ];
  176. }
  177. public static function getLevelWeights()
  178. {
  179. $store = Yii::$app->session->get('o2o');
  180. // if(empty($store)){
  181. $list = self::find()->select('level,name')->where([
  182. 'mall_id' =>$store ['mall_id'],
  183. 'store_id' => $store['id'],
  184. 'status' => [StatusEnum::ENABLED,StatusEnum::DISABLED]
  185. ])->orderBy(['level' => SORT_ASC])->all();
  186. $level_list = array_column($list,'level');
  187. $newList = [];
  188. $matchList = [];
  189. for ($i = 1; $i <= 10; $i++) {
  190. $newList[] = [
  191. 'name' => '权重' . $i,
  192. 'level' => $i,
  193. 'disabled' => in_array($i, $level_list),
  194. ];
  195. if(isset($list[$i-1])){
  196. $matchList[] = [
  197. 'name' => $list[$i-1]['name'],
  198. 'level' => "$i",
  199. 'disabled' => in_array($i, $level_list),
  200. ];
  201. }
  202. }
  203. return ['newList'=>$newList,'matchList'=>$matchList];
  204. }
  205. public static function del($params){
  206. try{
  207. $model = self::findOne(['id'=>$params['id']]);
  208. if(!empty($model)){
  209. $res = StoreBossAgent::findOne(['level'=>$model->level,'mall_id'=>$params['mall_id'],'status'=>[StatusEnum::ENABLED,StatusEnum::DISABLED],'store_id'=>$params['store_id']]);
  210. if(!empty($res)) throw new \Exception('该等级关联了股东');
  211. $model->status = StatusEnum::DELETE;
  212. $res = $model->save();
  213. if($res==false) throw new \Exception($model->getErrorMessage());
  214. }
  215. return true;
  216. }catch (\Exception $e){
  217. self::$static_error = $e->getMessage();
  218. return false;
  219. }
  220. }
  221. }