SignInAwardConfig.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. namespace addons\SignIn\common\models;
  3. use common\models\base\BaseActiveRecord;
  4. use common\enums\StatusEnum;
  5. use Exception;
  6. use Yii;
  7. /**
  8. * This is the model class for table "{{%addons_sign_in_award_config}}".
  9. * @property int $id
  10. * @property int $mall_id
  11. * @property int $day 签到天数
  12. * @property int $num 赠送数量
  13. * @property int $coupon_id 赠送优惠券ID
  14. * @property int $type 1:积分,2:优惠券
  15. * @property int $status 1:正常,0:删除
  16. * @property int $created_at 创建时间
  17. * @property int $updated_at 编辑时间
  18. */
  19. class SignInAwardConfig extends BaseActiveRecord
  20. {
  21. /**
  22. * {@inheritdoc}
  23. */
  24. public static function tableName()
  25. {
  26. return '{{%addons_sign_in_award_config}}';
  27. }
  28. /**
  29. * {@inheritdoc}
  30. */
  31. public function rules(){
  32. return [
  33. [['mall_id'], 'required'],
  34. [['mall_id', 'day', 'num', 'coupon_id', 'type', 'status', 'created_at', 'updated_at'], 'integer'],
  35. ];
  36. }
  37. /**
  38. * {@inheritdoc}
  39. */
  40. public function attributeLabels(){
  41. return [
  42. 'id' => 'ID',
  43. 'mall_id' => 'Mall ID',
  44. 'day' => '签到天数',
  45. 'num' => '赠送数量',
  46. 'coupon_id' => '赠送优惠券ID',
  47. 'type' => '赠送类型',
  48. 'status' => '状态',
  49. 'created_at' => '创建时间',
  50. 'updated_at' => '修改时间'
  51. ];
  52. }
  53. /**
  54. * @param array $get 参数
  55. * @author hpei
  56. * @return array [couponArr=>优惠券签到奖励,scoreArr=>积分签到奖励]
  57. */
  58. public static function getSignInConfig($mall_id, $coupon_list) {
  59. $list = self::find()
  60. ->where(['mall_id' => $mall_id, 'status' => StatusEnum::ENABLED])
  61. ->select(['day', 'type', 'coupon_id', 'num'])
  62. ->orderBy('day asc')
  63. ->asArray()
  64. ->all();
  65. $couponArr = [];
  66. $scoreArr = [];
  67. if (!empty($list)) {
  68. foreach ($list as $val) {
  69. if ($val['type'] == 1) $scoreArr[] = $val;
  70. if ($val['type'] == 2) {
  71. //优惠券不可用券ID设置为0,前端必须选择券才能提交
  72. if (isset($coupon_list[$val['coupon_id']])) {
  73. $val['coupon_name'] = $coupon_list[$val['coupon_id']];
  74. } else {
  75. $val['coupon_name'] = '请选择优惠券';
  76. $val['coupon_id'] = 0;
  77. }
  78. $couponArr[] = $val;
  79. }
  80. }
  81. }
  82. return ['coupon_arr' => $couponArr, 'score_arr' => $scoreArr];
  83. }
  84. public static function setSignInAwardConfig($mall_id, $installData) {
  85. try {
  86. $t = Yii::$app->db->beginTransaction();
  87. $field = ['num', 'coupon_id', 'day', 'type', 'mall_id', 'updated_at', 'created_at'];
  88. $cont = self::find()->where(['mall_id' => $mall_id, 'status' => StatusEnum::ENABLED])->count();
  89. if ($cont == 0) {
  90. $res1 = true;
  91. } else {
  92. $res1 = self::updateAll(['status' => StatusEnum::DISABLED, 'updated_at' => time()], ['mall_id' => $mall_id, 'status' => StatusEnum::ENABLED]);
  93. }
  94. $res2 = Yii::$app->db->createCommand()->batchInsert(self::tableName(), $field, $installData)->execute();
  95. if (!$res1 || !$res2) {
  96. $t->rollBack();
  97. self::$static_error = (new self())->getErrorMessage();
  98. return false;
  99. }
  100. $t->commit();
  101. return array_values($installData);
  102. } catch (\Exception $e) {
  103. $t->rollBack();
  104. self::$static_error = '设置失败,' . $e->getMessage() . ',行数:' . $e->getLine();
  105. return false;
  106. }
  107. }
  108. }