| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118 |
- <?php
- namespace addons\SignIn\common\models;
- use common\models\base\BaseActiveRecord;
- use common\enums\StatusEnum;
- use Exception;
- use Yii;
- /**
- * This is the model class for table "{{%addons_sign_in_award_config}}".
- * @property int $id
- * @property int $mall_id
- * @property int $day 签到天数
- * @property int $num 赠送数量
- * @property int $coupon_id 赠送优惠券ID
- * @property int $type 1:积分,2:优惠券
- * @property int $status 1:正常,0:删除
- * @property int $created_at 创建时间
- * @property int $updated_at 编辑时间
- */
- class SignInAwardConfig extends BaseActiveRecord
- {
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return '{{%addons_sign_in_award_config}}';
- }
- /**
- * {@inheritdoc}
- */
- public function rules(){
- return [
- [['mall_id'], 'required'],
- [['mall_id', 'day', 'num', 'coupon_id', 'type', 'status', 'created_at', 'updated_at'], 'integer'],
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function attributeLabels(){
- return [
- 'id' => 'ID',
- 'mall_id' => 'Mall ID',
- 'day' => '签到天数',
- 'num' => '赠送数量',
- 'coupon_id' => '赠送优惠券ID',
- 'type' => '赠送类型',
- 'status' => '状态',
- 'created_at' => '创建时间',
- 'updated_at' => '修改时间'
- ];
- }
- /**
- * @param array $get 参数
- * @author hpei
- * @return array [couponArr=>优惠券签到奖励,scoreArr=>积分签到奖励]
- */
- public static function getSignInConfig($mall_id, $coupon_list) {
- $list = self::find()
- ->where(['mall_id' => $mall_id, 'status' => StatusEnum::ENABLED])
- ->select(['day', 'type', 'coupon_id', 'num'])
- ->orderBy('day asc')
- ->asArray()
- ->all();
- $couponArr = [];
- $scoreArr = [];
- if (!empty($list)) {
- foreach ($list as $val) {
- if ($val['type'] == 1) $scoreArr[] = $val;
- if ($val['type'] == 2) {
- //优惠券不可用券ID设置为0,前端必须选择券才能提交
- if (isset($coupon_list[$val['coupon_id']])) {
- $val['coupon_name'] = $coupon_list[$val['coupon_id']];
- } else {
- $val['coupon_name'] = '请选择优惠券';
- $val['coupon_id'] = 0;
- }
- $couponArr[] = $val;
- }
- }
- }
- return ['coupon_arr' => $couponArr, 'score_arr' => $scoreArr];
- }
- public static function setSignInAwardConfig($mall_id, $installData) {
- try {
- $t = Yii::$app->db->beginTransaction();
- $field = ['num', 'coupon_id', 'day', 'type', 'mall_id', 'updated_at', 'created_at'];
- $cont = self::find()->where(['mall_id' => $mall_id, 'status' => StatusEnum::ENABLED])->count();
- if ($cont == 0) {
- $res1 = true;
- } else {
- $res1 = self::updateAll(['status' => StatusEnum::DISABLED, 'updated_at' => time()], ['mall_id' => $mall_id, 'status' => StatusEnum::ENABLED]);
- }
- $res2 = Yii::$app->db->createCommand()->batchInsert(self::tableName(), $field, $installData)->execute();
- if (!$res1 || !$res2) {
- $t->rollBack();
- self::$static_error = (new self())->getErrorMessage();
- return false;
- }
- $t->commit();
- return array_values($installData);
- } catch (\Exception $e) {
- $t->rollBack();
- self::$static_error = '设置失败,' . $e->getMessage() . ',行数:' . $e->getLine();
- return false;
- }
- }
- }
|