Goods.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. <?php
  2. namespace addons\StarChain\common\models;
  3. use Exception;
  4. use Yii;
  5. use yii\helpers\Json;
  6. /**
  7. * This is the model class for table "{{%addons_star_chain_goods}}".
  8. *
  9. * @property int $id
  10. * @property string|null $spu_id SPU
  11. * @property string|null $name
  12. * @property string|null $carousel_img_list
  13. * @property string|null $category_id_1 一级分类ID
  14. * @property string|null $category_id_2 二级分类ID
  15. * @property string|null $category_id_3 三级分类ID
  16. * @property int|null $limit_area_type 限制类型 (0 : 可销售地区,1: 不可销售地区)
  17. * @property int $is_limit_area 限制销售地区 ( 1: 是, 0:否)
  18. * @property string|null $limit_area 可下单地址
  19. * @property string|null $propertys 类目属性/属性值
  20. * @property int|null $status 0、已上架1、已下架
  21. * @property int|null $updated_at
  22. * @property int|null $created_at
  23. */
  24. class Goods extends BaseModel
  25. {
  26. /**
  27. * {@inheritdoc}
  28. */
  29. public static function tableName()
  30. {
  31. return '{{%addons_star_chain_goods}}';
  32. }
  33. /**
  34. * {@inheritdoc}
  35. */
  36. public function rules()
  37. {
  38. return [
  39. [['carousel_img_list', 'limit_area', 'propertys'], 'string'],
  40. [['limit_area_type', 'is_limit_area', 'status', 'updated_at', 'created_at'], 'integer'],
  41. [['spu_id', 'category_id_1', 'category_id_2', 'category_id_3'], 'string', 'max' => 64],
  42. [['name'], 'string', 'max' => 255],
  43. ];
  44. }
  45. /**
  46. * {@inheritdoc}
  47. */
  48. public function attributeLabels()
  49. {
  50. return [
  51. 'id' => 'ID',
  52. 'spu_id' => 'Spu ID',
  53. 'name' => 'Name',
  54. 'carousel_img_list' => 'Carousel Img List',
  55. 'category_id_1' => 'Category Id 1',
  56. 'category_id_2' => 'Category Id 2',
  57. 'category_id_3' => 'Category Id 3',
  58. 'limit_area_type' => 'Limit Area Type',
  59. 'is_limit_area' => 'Is Limit Area',
  60. 'limit_area' => 'Limit Area',
  61. 'propertys' => 'Propertys',
  62. 'status' => 'Status',
  63. 'updated_at' => 'Updated At',
  64. 'created_at' => 'Created At',
  65. ];
  66. }
  67. /**
  68. * 一级分类
  69. *
  70. * @return ActiveQueryInterface
  71. */
  72. public function getCategory1()
  73. {
  74. return $this->hasOne(Category::class, ['category_id' => 'category_id_1'])->select('id, category_id, name');
  75. }
  76. /**
  77. * 二级分类
  78. *
  79. * @return ActiveQueryInterface
  80. */
  81. public function getCategory2()
  82. {
  83. return $this->hasOne(Category::class, ['category_id' => 'category_id_2'])->select('id, category_id, name');
  84. }
  85. /**
  86. * 三级分类
  87. *
  88. * @return ActiveQueryInterface
  89. */
  90. public function getCategory3()
  91. {
  92. return $this->hasOne(Category::class, ['category_id' => 'category_id_3'])->select('id, category_id, name');
  93. }
  94. /**
  95. * 批量更新商品
  96. *
  97. * @param array $list
  98. * @return void
  99. */
  100. public static function saveList($list, $insert = true)
  101. {
  102. $list = self::getGoodsData($list);
  103. if (empty($list)) return false;
  104. $spu_ids = array_column($list, 'spu_id');
  105. // 查询已存储
  106. $goods_list = self::find()->where(['spu_id' => $spu_ids])->indexBy('spu_id')->all();
  107. $has_spu_ids = array_column($goods_list, 'spu_id');
  108. // 更新已存在
  109. foreach ($list as $k => $v) {
  110. if (in_array($v['spu_id'], $has_spu_ids)) {
  111. if (!(
  112. $goods_list[$v['spu_id']]->load($v, '') &&
  113. $goods_list[$v['spu_id']]->save()
  114. )) {
  115. // $goods_list[$v['spu_id']]->getErrors();
  116. throw new Exception('SPUID:' . $v['spu_id'] . ',更新失败');
  117. }
  118. unset($list[$k]);
  119. }
  120. }
  121. // 剩余使用批量插入
  122. if ($list && $insert) {
  123. self::batchInsert($list);
  124. }
  125. }
  126. /**
  127. * 获取goods数据
  128. *
  129. * @param array $spu_list
  130. * @return array
  131. */
  132. protected static function getGoodsData($spu_list)
  133. {
  134. $data = [];
  135. foreach ($spu_list as $v) {
  136. $data[] = [
  137. 'spu_id' => strval($v['spuId']),
  138. 'name' => $v['name'],
  139. 'carousel_img_list' => Json::encode($v['carouselImgList']),
  140. 'category_id_1' => strval($v['categoryId1']),
  141. 'category_id_2' => strval($v['categoryId2']),
  142. 'category_id_3' => strval($v['categoryId3']),
  143. 'propertys' => Json::encode($v['propertys']),
  144. 'limit_area_type' => $v['limitAreaType'],
  145. 'is_limit_area' => $v['isLimitArea'] == 'Y' ? 1 : 0,
  146. 'limit_area' => Json::encode($v['limitArea']),
  147. 'status' => $v['status'],
  148. ];
  149. }
  150. return $data;
  151. }
  152. /**
  153. * 查询商品是否可售
  154. *
  155. * @param string $spu_id 商品SPUID
  156. * @param integer $region_code 地址CODE
  157. * @return boolean
  158. */
  159. public static function checkGoodsLimit($spu_id, $region_code)
  160. {
  161. $goods = self::find()
  162. ->where(['spu_id' => $spu_id])
  163. ->select('is_limit_area, limit_area_type, limit_area')
  164. ->cache(5)
  165. ->one();
  166. $limit_area = Json::decode($goods->limit_area);
  167. // 未开启限制
  168. if ($goods->is_limit_area == 0) {
  169. return true;
  170. }
  171. // 限制类型
  172. if ($goods->limit_area_type == 0) {
  173. return in_array($region_code, $limit_area);
  174. }
  175. return !in_array($region_code, $limit_area);
  176. }
  177. /**
  178. * 通过SPU_ID获取商品信息
  179. *
  180. * @param array $spu_ids
  181. * @return array
  182. */
  183. public static function getInfoBySpuIds(array $spu_ids, string $field = '')
  184. {
  185. $model = static::find();
  186. $model->where(['spu_id' => $spu_ids,]);
  187. $field && $model->select($field);
  188. $model->cache(5);
  189. return $model->all();
  190. }
  191. /**
  192. * @return void
  193. */
  194. public function goodsPulled()
  195. {
  196. $this->status = 1;
  197. $this->save();
  198. }
  199. }