| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129 |
- <?php
- namespace addons\CardSecret\common\models;
- use common\enums\StatusEnum;
- use Yii;
- use yii\db\Exception;
- /**
- * This is the model class for table "{{%addons_CardSecret_cate}}".
- *
- * @property int $id
- * @property int|null $mall_id 商城ID
- * @property string|null $name
- * @property int|null $sort 排序,越小越前面
- * @property int|null $status 状态[-1:删除;0:禁用;1启用]
- * @property int|null $no_use_num 未发卡数量
- * @property int $use_num 已发卡数量
- * @property int|null $created_at
- * @property int|null $updated_at
- */
- class Cate extends \common\models\base\BaseActiveRecord
- {
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return '{{%addons_card_secret_cate}}';
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['mall_id', 'sort', 'status', 'no_use_num', 'use_num', 'created_at', 'updated_at'], 'integer'],
- [['name'], 'string', 'max' => 255],
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function attributeLabels()
- {
- return [
- 'id' => 'ID',
- 'mall_id' => '商城ID',
- 'name' => 'Name',
- 'sort' => '排序,越小越前面',
- 'status' => '状态[-1:删除;0:禁用;1启用]',
- 'no_use_num' => '未发卡数量',
- 'use_num' => '已发卡数量',
- 'created_at' => 'Created At',
- 'updated_at' => 'Updated At',
- ];
- }
- /**
- * 保存数据
- * @Author: ltm
- * @DateTime: 2021/12/13 0022 17:13
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @param array $params
- * @return AddonsMaterial|bool
- */
- public static function setData(array $params){
- try{
- $params = self::exceptNullVal($params);
- $where[] = ['mall_id' => $params['mall_id']];
- if($params['name']) $where[] = ['name' => $params['name']];
- $find_data = self::getOne($where);
- if($find_data&&!$params['id']){
- self::$static_error = "分类名称已经存在";
- return false;
- }
- if($params['id']) {
- $cont = ['id' => $params['id']];
- $model = self::findOne($cont);
- }
- //var_dump($params);die;
- //判断分类是否已经被使用
- if($params['status']=='-1'){
- $findMaterial = CardSecret::getOne([['cate_id' => $params['id']]],1);
- if($findMaterial){
- self::$static_error = "分类名称已经使用,请先移除该分类的下卡号";
- return false;
- }
- }
- if(empty($model)) {
- $model = new self();
- $model->loadDefaultValues();
- $model->mall_id = $params['mall_id'];
- }
- if(!$model->load($params,'') || !$model->save()){
- throw new Exception($model->getErrorMessage());
- }
- return $model;
- }catch(\Exception $e){
- self::$static_error = $e->getMessage();
- return false;
- }
- }
- /**
- * 获取分类信息
- * @Author: ltm
- * @DateTime: 2022/5/6 0022 17:13
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @param array $params
- * @return AddonsMaterial|bool
- */
- public static function getCateMap($type = 1,$mall_id)
- {
- $list = self::find()->select('id,name,no_use_num,use_num')->where(['status' => StatusEnum::ENABLED,'mall_id' =>$mall_id])->orderBy('id asc')->asArray()->all();
- if ($type == 1) {
- if ($list) {
- foreach ($list as $item) {
- $cate[$item['id']] = $item['name'];
- }
- }
- } else {
- $cate = $list;
- }
- return $cate;
- }
- }
|