HappinessStoreGoodsAttr.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. namespace addons\Happiness\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 common\models\goods\GoodsCate;
  8. use common\models\goods\GoodsCateRelate;
  9. use common\models\user\User;
  10. use Yii;
  11. /**
  12. * This is the model class for table "qimall_addons_store_clerk".
  13. */
  14. class HappinessStoreGoodsAttr extends BaseActiveRecord
  15. {
  16. /**
  17. * {@inheritdoc}
  18. */
  19. public static function tableName()
  20. {
  21. return '{{%addons_happiness_store_goods_attr}}';
  22. }
  23. /**
  24. * {@inheritdoc}
  25. */
  26. public function rules()
  27. {
  28. return [
  29. [['mall_id', 'goods_id', 'store_id', 'attr_id', 'stock', 'created_at', 'updated_at', 'status', 'yun_goods_id', 'is_on_sale'], 'integer'],
  30. ];
  31. }
  32. /**
  33. * @desc:保存数据
  34. * @Author: hua
  35. * @Date: 2021/10/23
  36. * @Time: 18:07
  37. * @Copyright: copyright (c) 2021 广东七件事集团
  38. * @param array $params
  39. * @return StoreClerk|false|null
  40. */
  41. public static function setData(array $params)
  42. {
  43. try {
  44. if (!empty($params['mall_id']) && !empty($params['id']) && !empty($params['store_id'])) {
  45. $model = self::findOne(['mall_id' => $params['mall_id'], 'store_id' => $params['store_id'],'id' => $params['id'], 'status' => StatusEnum::ENABLED]);
  46. } else if(!empty($params['mall_id']) && !empty($params['goods_id'])&&!empty($params['attr_id']) && !empty
  47. ($params['store_id']))
  48. {
  49. $model = self::findOne(['mall_id' => $params['mall_id'],'attr_id' => $params['attr_id'], 'store_id' => $params['store_id'],'goods_id' => $params['goods_id'], 'status' => StatusEnum::ENABLED]);
  50. }
  51. if(empty($model)) {
  52. $model = new self();
  53. $model->loadDefaultValues();
  54. }
  55. else
  56. {
  57. $params['stock']=$params['stock']+$model->stock;
  58. }
  59. if (!$model->load($params, '')) throw new \Exception($model->getErrorMessage());
  60. if (!$model->save()) throw new \Exception($model->getErrorMessage());
  61. return $model;
  62. } catch (\Exception $e) {
  63. self::$static_error = $e->getMessage();
  64. return false;
  65. }
  66. }
  67. public static function handleStock($goods_attr_id,$num, $store_id){
  68. try{
  69. $model = self::findOne(['attr_id'=>$goods_attr_id,'status'=>StatusEnum::ENABLED, 'store_id' => $store_id, 'status' => StatusEnum::ENABLED, 'is_on_sale' => 1]);
  70. if(empty($model)) throw new \Exception('规格不存在');
  71. if(!is_int($num)) throw new \Exception('库存数量必须为整数');
  72. if($num < 0 && $model->stock < abs($num)) throw new \Exception('库存不足');
  73. $model->stock += $num;
  74. $res = $model->save();
  75. if($res === false) throw new \Exception($model->getErrorMessage());
  76. return true;
  77. }catch(\Exception $e){
  78. self::setStaticError($e->getMessage());
  79. return false;
  80. }
  81. }
  82. public function getAttr(){
  83. return $this->hasOne(GoodsAttr::class,['id'=>'attr_id']);
  84. }
  85. public function getGoods(){
  86. return $this->hasOne(Goods::class,['id'=>'goods_id']);
  87. }
  88. }