MchGoodsModel.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. namespace addons\Mch\common\models;
  3. use common\enums\StatusEnum;
  4. use common\models\base\BaseActiveRecord;
  5. use Yii;
  6. /**
  7. * This is the model class for table "qimall_addons_mch_goods_model".
  8. *
  9. * @property int $id
  10. * @property int $mall_id
  11. * @property int $mch_id
  12. * @property int $goods_id 商品id
  13. * @property string|null $content 内容
  14. * @property int $status 状态[0禁用 1启用 -1删除]
  15. * @property int $created_at 创建时间
  16. * @property int $updated_at 修改时间
  17. */
  18. class MchGoodsModel extends BaseActiveRecord
  19. {
  20. /**
  21. * {@inheritdoc}
  22. */
  23. public static function tableName()
  24. {
  25. return '{{%addons_mch_goods_model}}';
  26. }
  27. /**
  28. * {@inheritdoc}
  29. */
  30. public function rules()
  31. {
  32. return [
  33. [['mall_id', 'mch_id', 'goods_id', 'status', 'created_at', 'updated_at'], 'integer'],
  34. [['content'], 'string'],
  35. ];
  36. }
  37. /**
  38. * {@inheritdoc}
  39. */
  40. public function attributeLabels()
  41. {
  42. return [
  43. 'id' => 'ID',
  44. 'mall_id' => 'Mall ID',
  45. 'mch_id' => 'Mch ID',
  46. 'goods_id' => '商品id',
  47. 'content' => '内容',
  48. 'status' => '状态[0禁用 1启用 -1删除]',
  49. 'created_at' => '创建时间',
  50. 'updated_at' => '修改时间',
  51. ];
  52. }
  53. public static function setData(array $params)
  54. {
  55. try {
  56. if (!empty($params['mall_id'])&&!empty($params['mch_id'])&&!empty($params['goods_id'])) {
  57. $model = self::findOne(['mall_id' => $params['mall_id'], 'mch_id' => $params['mch_id'], 'goods_id' => $params['goods_id'], 'status' => [StatusEnum::ENABLED]]);
  58. if (empty($model)) {
  59. $model = new self();
  60. $model->loadDefaultValues();
  61. }
  62. } else {
  63. $model = new self();
  64. $model->loadDefaultValues();
  65. }
  66. if (!$model->load($params, '') || !$model->save()) {
  67. throw new \Exception($model->getErrorMessage());
  68. }
  69. return $model;
  70. } catch (\Exception $e) {
  71. self::$static_error = $e->getMessage() . $e->getLine() . $e->getFile();
  72. return false;
  73. }
  74. }
  75. /**
  76. * 商品是否参与渠道分红
  77. *
  78. * @param int $mallId
  79. * @param int $mchId
  80. * @param int $goodsId
  81. *
  82. * @return bool
  83. */
  84. public static function isParticipateAgent(int $mallId, int $mchId, int $goodsId)
  85. {
  86. $content = MchGoodsModel::find()
  87. ->where([
  88. 'mch_id' => $mchId,
  89. 'mall_id' => $mallId,
  90. 'goods_id' => $goodsId,
  91. 'status' => StatusEnum::ENABLED
  92. ])
  93. ->select('content')
  94. ->scalar();
  95. $content = json_decode($content, true);
  96. $agent = json_decode($content['Agent'], true);
  97. return !isset($agent['distribution_setting']['is_participate']) || !empty($agent['distribution_setting']['is_participate']);
  98. }
  99. }