StoreGoodsAttr.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. <?php
  2. namespace addons\Store\common\models;
  3. use common\enums\StatusEnum;
  4. use common\models\base\BaseActiveRecord;
  5. use common\models\goods\Goods;
  6. use common\models\goods\GoodsAttr;
  7. use Yii;
  8. /**
  9. * This is the model class for table "qimall_addons_store_goods_attr".
  10. *
  11. * @property int $id
  12. * @property int $goods_id 商品ID
  13. * @property string $name 规格名称
  14. * @property string $sign_id 规格ID标识
  15. * @property int $stock 库存
  16. * @property float $price 价格
  17. * @property float $original_price 原价
  18. * @property float $cost_price 成本价
  19. * @property string $goods_no 货号
  20. * @property int $weight 重量(克)
  21. * @property string $pic_url 规格图片
  22. * @property int $status 状态[1正常 -1删除]
  23. * @property int $version 版本
  24. * @property int $sort 排序
  25. * @property int $created_at 创建时间
  26. * @property int $updated_at 修改时间
  27. * @property string $attr_detail 规格详情
  28. * @property int $mall_attr_id 商城规格ID
  29. */
  30. class StoreGoodsAttr extends BaseActiveRecord
  31. {
  32. /**
  33. * {@inheritdoc}
  34. */
  35. public static function tableName()
  36. {
  37. return '{{%addons_store_goods_attr}}';
  38. }
  39. /**
  40. * {@inheritdoc}
  41. */
  42. public function rules()
  43. {
  44. return [
  45. [['goods_id', 'stock', 'status', 'version', 'sort', 'created_at', 'updated_at', 'mall_attr_id'], 'integer'],
  46. [['price', 'original_price', 'cost_price', 'weight'], 'number'],
  47. [['name'], 'string', 'max' => 500],
  48. [['sign_id', 'goods_no', 'pic_url'], 'string', 'max' => 255],
  49. [['attr_detail'], 'string'],
  50. ];
  51. }
  52. /**
  53. * {@inheritdoc}
  54. */
  55. public function attributeLabels()
  56. {
  57. return [
  58. 'id' => 'ID',
  59. 'goods_id' => '商品ID',
  60. 'name' => '规格名称',
  61. 'sign_id' => '规格ID标识',
  62. 'stock' => '库存',
  63. 'price' => '价格',
  64. 'original_price' => '原价',
  65. 'cost_price' => '成本价',
  66. 'goods_no' => '货号',
  67. 'weight' => '重量(克)',
  68. 'pic_url' => '规格图片',
  69. 'status' => '状态[1正常 -1删除]',
  70. 'version' => '版本',
  71. 'sort' => '排序',
  72. 'created_at' => '创建时间',
  73. 'updated_at' => '修改时间',
  74. 'attr_detail' => '规格详情',
  75. 'mall_attr_id' => '商城规格ID',
  76. ];
  77. }
  78. // 关联模型
  79. public function getGoods()
  80. {
  81. return $this->hasOne(StoreGoods::class, ['id' => 'goods_id']);
  82. }
  83. public function getMainGoodsAttr()
  84. {
  85. return $this->hasOne(GoodsAttr::class, ['id' => 'mall_attr_id']);
  86. }
  87. /**
  88. * 保存规格数据
  89. * @Author bing
  90. * @DateTime 2021-09-02 19:37:52
  91. * @copyright: Copyright (c) 2020 广东七件事集团
  92. * @param [type] $goods_id
  93. * @param [type] $attrs
  94. * @param [type] $attr_groups
  95. * @param string $addons 来自插件
  96. * @param callback $call_back 回调函数
  97. * @return
  98. */
  99. public static function setData($goods_id, $attrs, $attr_groups,$mall_id, &$wait_del_attr_id_arr=[])
  100. {
  101. try{
  102. if($goods_id && $attrs && $attr_groups){
  103. $goods_attr_list = self::lists(['where'=>[['goods_id'=>$goods_id],['status'=>StatusEnum::ENABLED]],'select'=>'id']);
  104. $wait_del_attr_id_arr = array_column($goods_attr_list,'id');//等待删除的规格id
  105. self::updateAll(['status'=>StatusEnum::DELETE],['goods_id'=>$goods_id]);
  106. $total_stock = 0; // 总库存
  107. $attr_pic_list = array_column($attr_groups[0]['attr_list'], 'pic_url', 'attr_id');
  108. foreach($attrs as $attr){
  109. $attr['goods_no'] = (string)$attr['goods_no'];
  110. StoreGoods::priceLimit($mall_id,$attr['price']);
  111. $attr_ids = array_column($attr['attr_list'],'attr_id');
  112. asort($attr_ids);
  113. $attr['pic_url'] = $attr_pic_list[$attr_ids[0]] ?? '';
  114. $attr['sign_id'] = implode(':',$attr_ids);
  115. $attr_group_name_list = array_column($attr['attr_list'], 'attr_group_name');
  116. $attr_name_list = array_column($attr['attr_list'], 'attr_name');
  117. $attr_name_arr = [];
  118. foreach ($attr_group_name_list as $index => $attr_group_name) {
  119. $attr_name_arr[] = $attr_group_name . ':' . $attr_name_list[$index];
  120. }
  121. $attr['name'] = implode(',',$attr_name_arr);
  122. $attr['weight'] = empty($attr['weight']) ? 0: $attr['weight'];
  123. $total_stock += $attr['stock'];
  124. if(!empty($attr['id'])){
  125. $model = self::findOne(['id' => $attr['id'],'goods_id' => $goods_id]);
  126. $model->status = StatusEnum::ENABLED;
  127. }else{
  128. $model = new self();
  129. $model->loadDefaultValues();
  130. $model->goods_id = $goods_id;
  131. }
  132. if(!$model->load($attr,'') || !$model->save()){
  133. throw new \Exception($model->getErrorMessage());
  134. }
  135. }
  136. // 更新商品库存
  137. $goods = StoreGoods::findOne($goods_id);
  138. $goods->stock = $total_stock;
  139. if(!$goods->save()) throw new \Exception($goods->getErrorMessage());
  140. }
  141. return true;
  142. }catch(\Exception $e){
  143. self::$static_error = $e->getMessage();
  144. return false;
  145. }
  146. }
  147. /**
  148. * 无规格商品添加货品数据
  149. * @Author bing
  150. * @DateTime 2021-09-02 19:37:52
  151. * @copyright: Copyright (c) 2020 广东七件事集团
  152. * @param [type] $goods_id
  153. * @param [type] $attrs
  154. * @param [type] $attr_groups
  155. * @return
  156. */
  157. public static function setAttr($goods_id,$attr){
  158. try{
  159. self::updateAll(['status'=>StatusEnum::DELETE],['goods_id'=>$goods_id]);
  160. $model = self::findOne(['goods_id' => $goods_id]);
  161. if(empty($model)){
  162. $model = new self();
  163. $model->loadDefaultValues();
  164. $model->goods_id = $goods_id;
  165. }else{
  166. $model->status = StatusEnum::ENABLED;
  167. }
  168. if(!$model->load($attr,'') || !$model->save()){
  169. throw new \Exception($model->getErrorMessage());
  170. }
  171. return true;
  172. }catch(\Exception $e){
  173. self::$static_error = $e->getMessage();
  174. return false;
  175. }
  176. }
  177. /**
  178. * 格式化表单需要的规格
  179. * @Author bing
  180. * @DateTime 2021-09-03 15:52:03
  181. * @copyright: Copyright (c) 2020 广东七件事集团
  182. * @param array $attrs
  183. * @param array $format_attr_group
  184. * @return array
  185. */
  186. public static function formatFormAttr(array $attrs,array $format_attr_group){
  187. if(empty($attrs)) return $attrs;
  188. foreach ($attrs as $key => $attr) {
  189. $attrs[$key]['attr_list'] = $format_attr_group[$attr['sign_id']] ?? [];
  190. }
  191. return $attrs;
  192. }
  193. public static function handleStock($goods_attr_id,$num){
  194. try{
  195. $model = self::findOne(['id'=>$goods_attr_id,'status'=>StatusEnum::ENABLED]);
  196. if(empty($model)) throw new \Exception('规格不存在');
  197. if(!is_int($num)) throw new \Exception('库存数量必须为整数');
  198. if($num < 0 && $model->stock < abs($num)) throw new \Exception('库存不足');
  199. $model->stock += $num;
  200. $res = $model->save();
  201. if($res === false) throw new \Exception($model->getErrorMessage());
  202. return true;
  203. }catch(\Exception $e){
  204. self::setStaticError($e->getMessage());
  205. return false;
  206. }
  207. }
  208. }