GoodsSearchAddress.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. namespace common\models\goods;
  3. use common\enums\StatusEnum;
  4. use common\models\common\Region;
  5. use Yii;
  6. /**
  7. * This is the model class for table "{{%goods_search_address}}".
  8. *
  9. * @property int $id ID
  10. * @property int $mall_id 商城ID
  11. * @property int $goods_id 商品ID
  12. * @property int $province_id 省份ID
  13. * @property int $city_id 城市ID
  14. * @property int $district_id 区ID
  15. * @property int $town_id 镇ID
  16. * @property int $is_enable 是否启用[0禁用; 1启用]
  17. * @property int $status 状态[-1删除, 0禁用; 1正常]
  18. * @property int $created_at 创建时间
  19. * @property int $updated_at 更新时间
  20. */
  21. class GoodsSearchAddress extends \common\models\base\BaseActiveRecord
  22. {
  23. public static $address_fields = ['province_id', 'city_id', 'district_id', 'town_id'];
  24. /**
  25. * {@inheritdoc}
  26. */
  27. public static function tableName()
  28. {
  29. return '{{%goods_search_address}}';
  30. }
  31. /**
  32. * {@inheritdoc}
  33. */
  34. public function rules()
  35. {
  36. return [
  37. [['mall_id', 'goods_id', 'province_id', 'city_id', 'district_id', 'town_id', 'is_enable', 'status', 'created_at', 'updated_at'], 'integer'],
  38. ];
  39. }
  40. /**
  41. * {@inheritdoc}
  42. */
  43. public function attributeLabels()
  44. {
  45. return [
  46. 'id' => 'ID',
  47. 'mall_id' => '商城ID',
  48. 'goods_id' => '商品ID',
  49. 'province_id' => '省份ID',
  50. 'city_id' => '城市ID',
  51. 'district_id' => '区ID',
  52. 'town_id' => '镇ID',
  53. 'is_enable' => '是否启用[0禁用; 1启用]',
  54. 'status' => '状态[-1删除, 0禁用; 1正常]',
  55. 'created_at' => '创建时间',
  56. 'updated_at' => '更新时间',
  57. ];
  58. }
  59. /**
  60. * @Description: 保存数据
  61. * @Author: zsan
  62. * @DateTime 2024-02-22 18:23:51
  63. * @param array $data
  64. * @return self|bool
  65. */
  66. public static function setData(array $data)
  67. {
  68. try {
  69. foreach (self::$address_fields as $value) {
  70. $data[$value] = isset($data[$value]) ? (int)$data[$value] : 0;
  71. }
  72. $model = self::findOne(['goods_id' => $data['goods_id'], 'mall_id' => $data['mall_id'], 'status' => StatusEnum::ENABLED]);
  73. if (empty($model)) {
  74. $model = new self();
  75. $model->loadDefaultValues();
  76. }
  77. if (!$model->load($data, '') || !$model->save()) throw new \Exception($model->getErrorMessage());
  78. return $model;
  79. } catch (\Exception $e) {
  80. self::setStaticError($e->getMessage());
  81. return false;
  82. }
  83. }
  84. /**
  85. * @Description: 格式化地址id
  86. * @Author: zsan
  87. * @DateTime 2024-02-23 09:41:00
  88. * @param integer $province_id
  89. * @param integer $city_id
  90. * @param integer $district_id
  91. * @return array
  92. */
  93. public static function formatAddressIds($province_id, $city_id, $district_id)
  94. {
  95. return [$province_id, $city_id, $district_id];
  96. }
  97. /**
  98. * @Description: 通过地址id获取商品id
  99. * @Author: zsan
  100. * @DateTime 2024-02-23 11:13:44
  101. * @param integer $mall_id
  102. * @param array $params
  103. * @return array|bool
  104. */
  105. public static function getGoodsIdByAddressIds($mall_id, $params)
  106. {
  107. $where = ['and', ['mall_id' => $mall_id], ['status' => StatusEnum::ENABLED], ['is_enable' => StatusEnum::ENABLED]];
  108. $is_search = false;
  109. foreach (self::$address_fields as $value) {
  110. if (!empty($params[$value])) {
  111. $where[] = [$value => $params[$value]];
  112. $is_search = true;
  113. }
  114. }
  115. if ($is_search === false) return false;
  116. return (array)self::find()->where($where)->select('goods_id')->asArray()->column();
  117. }
  118. }