Cate.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. namespace addons\CardSecret\common\models;
  3. use common\enums\StatusEnum;
  4. use Yii;
  5. use yii\db\Exception;
  6. /**
  7. * This is the model class for table "{{%addons_CardSecret_cate}}".
  8. *
  9. * @property int $id
  10. * @property int|null $mall_id 商城ID
  11. * @property string|null $name
  12. * @property int|null $sort 排序,越小越前面
  13. * @property int|null $status 状态[-1:删除;0:禁用;1启用]
  14. * @property int|null $no_use_num 未发卡数量
  15. * @property int $use_num 已发卡数量
  16. * @property int|null $created_at
  17. * @property int|null $updated_at
  18. */
  19. class Cate extends \common\models\base\BaseActiveRecord
  20. {
  21. /**
  22. * {@inheritdoc}
  23. */
  24. public static function tableName()
  25. {
  26. return '{{%addons_card_secret_cate}}';
  27. }
  28. /**
  29. * {@inheritdoc}
  30. */
  31. public function rules()
  32. {
  33. return [
  34. [['mall_id', 'sort', 'status', 'no_use_num', 'use_num', 'created_at', 'updated_at'], 'integer'],
  35. [['name'], 'string', 'max' => 255],
  36. ];
  37. }
  38. /**
  39. * {@inheritdoc}
  40. */
  41. public function attributeLabels()
  42. {
  43. return [
  44. 'id' => 'ID',
  45. 'mall_id' => '商城ID',
  46. 'name' => 'Name',
  47. 'sort' => '排序,越小越前面',
  48. 'status' => '状态[-1:删除;0:禁用;1启用]',
  49. 'no_use_num' => '未发卡数量',
  50. 'use_num' => '已发卡数量',
  51. 'created_at' => 'Created At',
  52. 'updated_at' => 'Updated At',
  53. ];
  54. }
  55. /**
  56. * 保存数据
  57. * @Author: ltm
  58. * @DateTime: 2021/12/13 0022 17:13
  59. * @Copyright: copyright (c) 2021 广东七件事集团
  60. * @param array $params
  61. * @return AddonsMaterial|bool
  62. */
  63. public static function setData(array $params){
  64. try{
  65. $params = self::exceptNullVal($params);
  66. $where[] = ['mall_id' => $params['mall_id']];
  67. if($params['name']) $where[] = ['name' => $params['name']];
  68. $find_data = self::getOne($where);
  69. if($find_data&&!$params['id']){
  70. self::$static_error = "分类名称已经存在";
  71. return false;
  72. }
  73. if($params['id']) {
  74. $cont = ['id' => $params['id']];
  75. $model = self::findOne($cont);
  76. }
  77. //var_dump($params);die;
  78. //判断分类是否已经被使用
  79. if($params['status']=='-1'){
  80. $findMaterial = CardSecret::getOne([['cate_id' => $params['id']]],1);
  81. if($findMaterial){
  82. self::$static_error = "分类名称已经使用,请先移除该分类的下卡号";
  83. return false;
  84. }
  85. }
  86. if(empty($model)) {
  87. $model = new self();
  88. $model->loadDefaultValues();
  89. $model->mall_id = $params['mall_id'];
  90. }
  91. if(!$model->load($params,'') || !$model->save()){
  92. throw new Exception($model->getErrorMessage());
  93. }
  94. return $model;
  95. }catch(\Exception $e){
  96. self::$static_error = $e->getMessage();
  97. return false;
  98. }
  99. }
  100. /**
  101. * 获取分类信息
  102. * @Author: ltm
  103. * @DateTime: 2022/5/6 0022 17:13
  104. * @Copyright: copyright (c) 2021 广东七件事集团
  105. * @param array $params
  106. * @return AddonsMaterial|bool
  107. */
  108. public static function getCateMap($type = 1,$mall_id)
  109. {
  110. $list = self::find()->select('id,name,no_use_num,use_num')->where(['status' => StatusEnum::ENABLED,'mall_id' =>$mall_id])->orderBy('id asc')->asArray()->all();
  111. if ($type == 1) {
  112. if ($list) {
  113. foreach ($list as $item) {
  114. $cate[$item['id']] = $item['name'];
  115. }
  116. }
  117. } else {
  118. $cate = $list;
  119. }
  120. return $cate;
  121. }
  122. }