* @day: 2017/11/11 */ namespace app\admin\model\ump; use app\admin\model\wechat\WechatUser as UserModel; use crmeb\traits\ModelTrait; use crmeb\basic\BaseModel; /** * Class StoreCategory * @package app\admin\model\store */ class StoreCouponUser extends BaseModel { /** * 数据表主键 * @var string */ protected $pk = 'id'; /** * 模型名称 * @var string */ protected $name = 'store_coupon_user'; use ModelTrait; /** * @param $where * @return array */ public static function systemPage($where) { $model = new self; $model = $model->alias('a') ->field('a.*,b.nickname,c.name') ->leftJoin('system_store c', 'a.store_id = c.id') ->leftJoin('user b', 'a.uid=b.uid'); if ($where['status'] != '') $model = $model->where('a.status', $where['status']); if ($where['is_fail'] != '') $model = $model->where('a.status', $where['is_fail']); if ($where['coupon_title'] != '') $model = $model->where('a.coupon_title', 'LIKE', "%$where[coupon_title]%"); if ($where['nickname'] != '') $model = $model->where('b.nickname|b.uid', 'LIKE', "%".$where['nickname']."%"); // $model = $model->where('is_del',0); $model = $model->order('id desc'); return $model->paginate(['list_rows'=>20,'query' => request()->param()]); } /** * 给用户发放优惠券 * @param $coupon * @param $user * @return int|string */ public static function setCoupon($coupon, $user) { $data = array(); foreach ($user as $k => $v) { $data[$k]['cid'] = $coupon['id']; $data[$k]['uid'] = $v; $data[$k]['coupon_title'] = $coupon['title']; $data[$k]['coupon_price'] = $coupon['coupon_price']; $data[$k]['use_min_price'] = $coupon['use_min_price']; $data[$k]['add_time'] = $coupon['start_time']; $data[$k]['end_time'] = $coupon['end_time']; $data[$k]['code'] = generate_promotion_code(1); $data[$k]['store_id'] = $coupon['store_id']; } $data_num = array_chunk($data, 30); self::beginTrans(); $res = true; foreach ($data_num as $k => $v) { $res = $res && self::insertAll($v); } self::checkTrans($res); return $res; } /** * TODO 恢复优惠券 * @param $id * @return StoreCouponUser|bool */ public static function recoverCoupon($id) { $status = self::where('id', $id)->value('status'); if ($status) return self::where('id', $id)->update(['status' => 0, 'use_time' => '']); else return true; } }