| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128 |
- <?php
- namespace common\models\goods;
- use common\enums\StatusEnum;
- use common\models\common\Region;
- use Yii;
- /**
- * This is the model class for table "{{%goods_search_address}}".
- *
- * @property int $id ID
- * @property int $mall_id 商城ID
- * @property int $goods_id 商品ID
- * @property int $province_id 省份ID
- * @property int $city_id 城市ID
- * @property int $district_id 区ID
- * @property int $town_id 镇ID
- * @property int $is_enable 是否启用[0禁用; 1启用]
- * @property int $status 状态[-1删除, 0禁用; 1正常]
- * @property int $created_at 创建时间
- * @property int $updated_at 更新时间
- */
- class GoodsSearchAddress extends \common\models\base\BaseActiveRecord
- {
- public static $address_fields = ['province_id', 'city_id', 'district_id', 'town_id'];
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return '{{%goods_search_address}}';
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['mall_id', 'goods_id', 'province_id', 'city_id', 'district_id', 'town_id', 'is_enable', 'status', 'created_at', 'updated_at'], 'integer'],
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function attributeLabels()
- {
- return [
- 'id' => 'ID',
- 'mall_id' => '商城ID',
- 'goods_id' => '商品ID',
- 'province_id' => '省份ID',
- 'city_id' => '城市ID',
- 'district_id' => '区ID',
- 'town_id' => '镇ID',
- 'is_enable' => '是否启用[0禁用; 1启用]',
- 'status' => '状态[-1删除, 0禁用; 1正常]',
- 'created_at' => '创建时间',
- 'updated_at' => '更新时间',
- ];
- }
- /**
- * @Description: 保存数据
- * @Author: zsan
- * @DateTime 2024-02-22 18:23:51
- * @param array $data
- * @return self|bool
- */
- public static function setData(array $data)
- {
- try {
- foreach (self::$address_fields as $value) {
- $data[$value] = isset($data[$value]) ? (int)$data[$value] : 0;
- }
- $model = self::findOne(['goods_id' => $data['goods_id'], 'mall_id' => $data['mall_id'], 'status' => StatusEnum::ENABLED]);
- if (empty($model)) {
- $model = new self();
- $model->loadDefaultValues();
- }
- if (!$model->load($data, '') || !$model->save()) throw new \Exception($model->getErrorMessage());
- return $model;
- } catch (\Exception $e) {
- self::setStaticError($e->getMessage());
- return false;
- }
- }
- /**
- * @Description: 格式化地址id
- * @Author: zsan
- * @DateTime 2024-02-23 09:41:00
- * @param integer $province_id
- * @param integer $city_id
- * @param integer $district_id
- * @return array
- */
- public static function formatAddressIds($province_id, $city_id, $district_id)
- {
- return [$province_id, $city_id, $district_id];
- }
- /**
- * @Description: 通过地址id获取商品id
- * @Author: zsan
- * @DateTime 2024-02-23 11:13:44
- * @param integer $mall_id
- * @param array $params
- * @return array|bool
- */
- public static function getGoodsIdByAddressIds($mall_id, $params)
- {
- $where = ['and', ['mall_id' => $mall_id], ['status' => StatusEnum::ENABLED], ['is_enable' => StatusEnum::ENABLED]];
- $is_search = false;
- foreach (self::$address_fields as $value) {
- if (!empty($params[$value])) {
- $where[] = [$value => $params[$value]];
- $is_search = true;
- }
- }
- if ($is_search === false) return false;
- return (array)self::find()->where($where)->select('goods_id')->asArray()->column();
- }
- }
|