query()->insertGetId($data); // 返回结果 return $id; } /** * 添加数据 * */ public function edit($id,$data) { // 更新时间 $data['update_time'] = time(); // 写入数据表 $result = $this->query()->where(['id'=>$id])->update($data); // 返回结果 return $result; } /** * 编码转id * * @param string $code 编码 * */ public function codeToId($code){ return intval(str_ireplace('klcx','',$code)); } /** * id转编码 * * @param int $id 编码 * */ public function idToCode($id){ return 'klcx'. str_pad($id, 9, '0', STR_PAD_LEFT); } /** * 优惠券过期时间 * * @param int $expTime 过期时间 * */ public function getExpTime($expTime){ // 如果存在过期时间,且小于1000,表示这是一个领取后n天的,按天数返回 if ( $expTime && $expTime < 1000 ) return Carbon::now()->addDays($expTime)->endOfDay()->getTimestamp(); // 返回时间戳 return $expTime; } /** * 过期状态设置 * */ public function setStatusByExpire(){ // 上锁 if(RedisLock::lock('promo::set::status::by::expire',1,30)){ // 修改 $result = $this->query()->where([['status','=',0],['end_time','>',0],['end_time','<=',time()]])->update(['status'=>3,'update_time'=>time()]); // 不管成功失败,都解锁 RedisLock::unlock('promo::set::status::by::expire',1); // 返回结果 return $result; } } /** * 获取优惠券信息 * */ public function getOne($id,$field=''){ // 返回结果 $result = $this->query()->find($id); // 返回结果 $result = $result ? $result->toArray() : []; // 返回值 return empty($field) ? $result : ( isset($result[$field]) ? $result[$field] : null); } }