GoodsService.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. namespace common\models\goods;
  3. use common\enums\StatusEnum;
  4. use common\enums\WhetherEnum;
  5. use Exception;
  6. use Yii;
  7. /**
  8. * This is the model class for table "{{%goods_service}}".
  9. *
  10. *
  11. * @property int $id
  12. * @property int $mall_id 商城ID
  13. * @property int $mch_id 商户ID
  14. * @property string $name 服务名称
  15. * @property string $remark 备注、描述
  16. * @property int $sort 排序
  17. * @property int $is_default 是否为默认服务【1是 0否】
  18. * @property int $status 状态【1启用 0禁用 -1删除】
  19. * @property int $created_at 创建时间
  20. * @property int $updated_at 更新时间
  21. */
  22. class GoodsService extends \common\models\base\BaseActiveRecord
  23. {
  24. /**
  25. * {@inheritdoc}
  26. */
  27. public static function tableName()
  28. {
  29. return '{{%goods_service}}';
  30. }
  31. /**
  32. * {@inheritdoc}
  33. */
  34. public function rules()
  35. {
  36. return [
  37. [['mall_id', 'mch_id', 'sort', 'created_at', 'updated_at'], 'integer'],
  38. ['is_default', 'in', 'range' => [WhetherEnum::ENABLED,WhetherEnum::DISABLED]],
  39. ['status', 'in', 'range' => [StatusEnum::ENABLED,StatusEnum::DISABLED,StatusEnum::DELETE]],
  40. [['name'], 'string', 'max' => 65],
  41. [['remark'], 'string', 'max' => 255],
  42. ];
  43. }
  44. /**
  45. * {@inheritdoc}
  46. */
  47. public function attributeLabels()
  48. {
  49. return [
  50. 'id' => 'ID',
  51. 'mall_id' => '商城ID',
  52. 'mch_id' => '商户ID',
  53. 'name' => '服务名称',
  54. 'remark' => '备注、描述',
  55. 'sort' => '排序',
  56. 'is_default' => '是否为默认服务【1是 0否】',
  57. 'status' => '状态【1启用 0禁用 -1删除】',
  58. 'created_at' => '创建时间',
  59. 'updated_at' => '更新时间',
  60. ];
  61. }
  62. /**
  63. * 保存数据
  64. * @Author bing
  65. * @DateTime 2021-08-24 15:34:48
  66. * @copyright: Copyright (c) 2020 广东七件事集团
  67. * @param array $params
  68. * @return void
  69. */
  70. public static function setData(array $params){
  71. try{
  72. if(!empty($params['id'])){
  73. $model = self::findOne(['mall_id' => $params['mall_id'], 'id' => $params['id'],'status'=>[StatusEnum::ENABLED,StatusEnum::DISABLED]]);
  74. if(empty($model)) throw new Exception('服务不存在或已删除');
  75. }else{
  76. $model = new self();
  77. $model->loadDefaultValues();
  78. $model->mall_id = $params['mall_id'];
  79. $model->mch_id = $params['mch_id'];
  80. }
  81. if(!$model->load($params,'') || !$model->save()){
  82. throw new Exception($model->getErrorMessage());
  83. }
  84. return $model;
  85. }catch(Exception $e){
  86. self::$static_error = $e->getMessage();
  87. return false;
  88. }
  89. }
  90. /**
  91. * 获取服务列表
  92. * @Author bing
  93. * @DateTime 2021-08-26 13:00:11
  94. * @copyright: Copyright (c) 2020 广东七件事集团
  95. * @param array $cond
  96. * @param array $with
  97. * @param integer $is_array
  98. * @return array|null
  99. */
  100. public static function getServices(array $cond=[],array $with=[],bool $is_array=true,string $select='*'){
  101. $default_cond = [['<>','status',StatusEnum::DELETE]];
  102. $cond = array_merge($default_cond,$cond);
  103. $query = self::find()->select($select);
  104. self::paramPack(['where'=>$cond,'with'=>$with],$query);
  105. $cates = $query->asArray($is_array)->all();
  106. return $cates;
  107. }
  108. }