updategoodstype(1); } $pagenum = 10;//每页显示多少条 if ((int)I('pagenum/s') > 0) { $pagenum = I('pagenum/s'); } $condition = array(); $condition['store_id'] = getAdmStoId(); $condition['is_end'] = 0; $keywords = trim(urldecode(urldecode(I('keywords')))); $condition['redname'] = array('like', '%' . $keywords . '%'); $model = M('red_config'); $count = $model->where($condition)->count(); $Page = new Page($count, $pagenum); $show = $Page->show(); $redmoneylist = $model->where($condition)->order("start_time asc,id asc")->limit($Page->firstRow . ',' . $Page->listRows)->select(); $this->assign('redmoneylist', $redmoneylist); $this->assign('keywords', $keywords); $this->assign('page', $show);// 赋值分页输出 $this->assign('pager', $Page); $this->assign('pagenum', $pagenum); return $this->fetch('', getAdmStoId()); } //添加活动 public function addredmoney() { //预存款项目 $getstoid = getAdmStoId(); if (getERPId()) { $tk['api_token'] = tpCache('shop_info.api_token', $getstoid); $yucun = getApiData("wxd.vip.advanceitem.list.get", $tk['api_token']); $yucun = json_decode($yucun, true); if ($yucun['code'] && $yucun['code'] == "1") { $yucunlist = $yucun['data']; $this->assign('yucunlist', $yucunlist); } } if (IS_POST) { ClearALLCache(); delFile(TEMP_PATH . "/" . $getstoid); $data = I('post.'); $data['end_time'] = strtotime($data['end_time']); $data['start_time'] = strtotime($data['start_time']); $where = " NOT ((end_time < " . $data['start_time'] . ") OR (start_time > " . $data['end_time'] . "))"; if ($data['id']) { $where .= " and id<>" . $data['id']; } $red_count = M('red_config')->where(array('store_id' => getAdmStoId(), 'is_end' => 0)) ->where($where) ->count(); if ($red_count) { $this->error('当前设置活动时间有重叠,无法添加!'); } if ($data['end_time'] <= $data['start_time']) { $this->error('结束时间必须大于结束时间'); } if ($data['is_show'] == 1 && $data['id']) { $this->error('当前状态为启用,无法修改'); } $data['store_id'] = $getstoid; $admid = getAdminId(); $amdinfo = getAdminInfo($admid); $data['edit_man'] = $amdinfo['ERPName']; if (empty($data['id'])) { $data['money'] = $data['addmoney']; $data['add_time'] = time(); $data['add_ip'] = getIP(); $r = M('red_config')->add($data); adminLog("管理员添加抢红包活动 " . $data['name']); } else { $rededit = M('red_config')->where(array('store_id' => getAdmStoId(), 'id' => $data['id']))->find(); if ($rededit) { $new_money = (float)$data['addmoney'] - (float)$rededit['addmoney'] + (float)$rededit['money']; if ($data['redstate'] == 0) { $this->creat_redredis(getAdmStoId(), $data['id'], $new_money, $data['rednum'] - $rededit['rednum_rob'], $data['emptynum'] - $rededit['emptynum_rob'], $data['minmoney'], $data['maxmoney'], $data['redstate']); } else { $this->creat_redredis(getAdmStoId(), $data['id'], $new_money, $data['rednum'] - $rededit['rednum_rob'], $data['emptynum'] - $rededit['emptynum_rob'], $data['redmoney'], $data['maxmoney'], $data['redstate']); } $data['money'] = $new_money; //缓存重新设置 $data['edit_time'] = time(); $data['edit_mark'] = "编辑抢红包活动"; $data['edit_ip'] = getIP(); $getstate = I('is_show'); if ($getstate == "0") { $data['liststoptime'] = time(); $data['liststopip'] = getIP(); } else { $data['liststarttime'] = time(); $data['liststartip'] = getIP(); } $r = M('red_config')->where("id=" . $data['id'])->save($data); adminLog("管理员编辑抢红包活动 " . $data['name']); } else { $this->error('编辑抢红包活动失败', U('Redmoney/index')); } } if ($r) { if (empty($data['id'])) { $this->success('添加抢红包活动成功', U('Redmoney/index')); } else { $this->success('编辑抢红包活动成功', $_SERVER['HTTP_REFERER']); } exit; } else { $this->error('编辑抢红包活动失败', U('Redmoney/index')); } } $id = I('id'); $info['start_time'] = date('Y-m-d H:i:s'); $info['end_time'] = date('Y-m-d 23:59:59', time() + 3600 * 24 * 60); if ($id > 0) { $info = M('red_config')->where("id=$id")->find(); $info['start_time'] = date('Y-m-d H:i:s', $info['start_time']); $info['end_time'] = date('Y-m-d H:i:s', $info['end_time']); } $this->assign('info', $info); return $this->fetch('', getAdmStoId()); } //生成队列 public function creat_redredis($stoid, $redid, $summoney, $rednum, $emptynum, $minmoney, $maxmoney, $type) { //1-50 空红包 51-100有钱红包 //$redis = new \Redis(); //$redis->connect(redisip, 6379); $redis = get_redis_handle(); $redkey = "redmoney_" . $stoid . "_" . $redid; $redis->del($redkey);//删除本来 if ($rednum > 0 || $emptynum > 0)//有个数 { $getnum = $rednum + $emptynum; $getrednum = 0; $getemptynum = 0; $rednum_job = $rednum; for ($i = 1; $i <= $getnum; $i++) { $rndid = rand(1, 100); if ($getrednum >= $rednum) { $rndid = rand(1, 50); } else if ($getemptynum >= $emptynum) { $rndid = rand(51, 100); } if ($rndid <= 50)//抢到空红包 { $getemptynum = $getemptynum + 1; $get_usermoney = 0; } else { $getrednum = $getrednum + 1; if ($type == 1) { $get_usermoney = $minmoney;//固定金额 mlog($get_usermoney, "creat_redredis/" . $stoid); } else { if ($i == $getnum)//即最后一个红包,金额为剩余全部金额 { if ($summoney > $maxmoney) { $get_usermoney = rand($minmoney * 100, $maxmoney * 100) * 0.01; } else { $get_usermoney = $summoney; } } else { //剩余的总金额 取剩余总金额 -最小红包*剩余人数 $rndmoney = $summoney - $minmoney * $rednum_job; if ($rndmoney > 0) { $getrnd_advmoney = 0; if ($rndmoney >= ($maxmoney - $minmoney)) { $getrnd_advmoney = rand(0, ($maxmoney - $minmoney) * 100) * 0.01; } else { $getrnd_advmoney = rand(0, $rndmoney * 100) * 0.01; } $get_usermoney = $minmoney + $getrnd_advmoney; } else { $get_usermoney = $minmoney; } $rednum_job = $rednum_job - 1; $summoney = $summoney - $get_usermoney; } } } $get_usermoney = number_format($get_usermoney, 2); $redis->lPush($redkey, $get_usermoney); } } $name = "redmoney_c_" . $stoid . "_" . $redid; Cache::set($name, "1"); } //更新状态 public function updatestate() { $id_value = I('id_value'); // 表主键id值 $getstate = I('state'); if ($id_value && $getstate != "") { $data['is_show'] = $getstate; if ($getstate == "0") { $data['liststoptime'] = time(); $data['liststopip'] = getIP(); } else { $data['liststarttime'] = time(); $data['liststartip'] = getIP(); } $res = M("red_config")->where("id", $id_value)->save($data); // 根据条件保存修改的数据 } } //活动到期商品状态还原 public function updategoodstype($state) { switch ($state) { case '1': $table = "red_config"; break; } $time = time(); M($table)->where(array('store_id' => getAdmStoId(), 'is_end' => 0, 'end_time' => ['<', $time]))->save(array('is_end' => 1)); } //删除活动 public function del_redmoney() { ClearALLCache(); delFile(TEMP_PATH . "/" . getAdmStoId()); $id = I('del_id'); if ($id) { $admid = getAdminId(); $amdinfo = getAdminInfo($admid); $data['edit_time'] = time(); $data['edit_ip'] = getIP(); $data['edit_mark'] = "删除抢红包活动"; $data["is_end"] = 1; $res = M('red_config')->where("id=" . $id)->save($data); if ($res) { $name = "redmoney_c_" . getAdmStoId() . "_" . $id; Cache::rm($name); } exit(json_encode(1)); } else { exit(json_encode(0)); } } //抽中记录 public function redmoneylist() { $pagenum = 20;//每页显示多少条 if ((int)I('pagenum/s') > 0) { $pagenum = I('pagenum/s'); } $p = I('p', 1); $timegap = I('timegap'); $timegap = urldecode(urldecode($timegap)); if ($timegap) { $gap = explode(' - ', $timegap); $begin = $gap[0]; $end = $gap[1]; } else { $lastweek = date('Y-m-d', strtotime("-1 year")) . ' 00:00:00';//30天前 $begin = I('begin', $lastweek); $end = I('end', date('Y-m-d')) . ' 23:59:59'; } $this->assign('start_time', $begin); $this->assign('end_time', $end); $this->begin = strtotime($begin); $this->end = strtotime($end);/*+86399*/ $keywords = I('keywords');//手机号码 $redid = I('redid');// $where = " a.store_id=" . getAdmStoId(); $where .= " and a.redid=" . $redid; $where .= " and a.addtime>$this->begin and a.addtime<$this->end "; if ($keywords) { $where .= " and (a.mobile='" . $keywords . "' or a.vipname='" . $keywords . "') "; } $qdrecord = M('red_record'); $count = $qdrecord->alias('a') ->where($where)->count(); $Page = $pager = new Page($count, $pagenum);// 实例化分页类 传入总记录数和每页显示的记录数 $redmoneylist = $qdrecord->alias('a') ->where($where) ->limit($Page->firstRow . ',' . $Page->listRows) ->order('a.addtime desc') ->select();// 查询满足要求的总记录数 $show = $Page->show(); $this->assign('page', $show); $this->assign('total', $count); $this->assign('redid', $redid); $this->assign('keywords', $keywords); $this->assign('store_id', getAdmStoId()); $this->assign('redmoneylist', $redmoneylist); $this->assign('pagenum', $pagenum); $this->assign('timegap', date('Y-m-d h:m:s', $this->begin) . ' - ' . date('Y-m-d h:m:s', $this->end)); return $this->fetch('', getAdmStoId()); } //再次预存 public function reset_yucun() { $geterpid = getERPId(); $getstoid = getAdmStoId(); $getid = I('id/d', 0); if (empty($geterpid) || empty($getid)) { $return_arr = array('code' => -1, 'msg' => '生成失败,非法参数'); return json($return_arr); } $red_res = M('red_record')->alias('a') ->join('red_config b', 'a.redid=b.id', 'left') ->field('a.*,b.yucunid') ->where(array('a.store_id' => $getstoid, 'a.id' => $getid))->find(); if (empty($red_res)) { $return_arr = array('code' => -1, 'msg' => '生成失败,找不到相关记录'); return json($return_arr); } $user['api_token'] = tpCache('shop_info.api_token', $getstoid); $erpdata = array( 'VIPId' => $red_res['erpvipid'],//会员ID 'AdvanceItemId' => $red_res['yucunid'],//预存款项目id 'AddSum' => $red_res['redmoney'],//预存金额 'Remark' => "抢红包预存款",//摘要 'FromBillId' => $red_res['frombillid'],//来源ID 'BillDate' => date('Y-m-d h:m:s', $red_res['addtime']),//时间 'kind' => '',//空:自定义充值 非空:促销充值ID ); $erp_rs = getApiData("wxd.vip.addvancelist", $user['api_token'], $erpdata); if (empty($erp_rs)) { $return_arr = array('code' => -1, 'msg' => '生成失败'); return json($return_arr); } $isupdate = 0; $erp_rs1 = json_decode($erp_rs, true); if ($erp_rs1['code'] != 1) { $savedata['transdate'] = $red_res['addtime']; $savedata['state'] = 1; $saveres = M('red_record')->where(array('store_id' => $getstoid, 'id' => $getid))->save($savedata); $return_arr = array('code' => -1, 'msg' => $erp_rs1['msg']); return json($return_arr); } $savedata['transdate'] = $red_res['addtime']; $savedata['state'] = 1; $saveres = M('red_record')->where(array('store_id' => $getstoid, 'id' => $getid))->save($savedata); $return_arr = array('code' => 1, 'msg' => '生成成功'); return json($return_arr); } public function redmoney_history() { $pagenum = 10;//每页显示多少条 if ((int)I('pagenum/s') > 0) { $pagenum = I('pagenum/s'); } $condition = array(); $condition['store_id'] = getAdmStoId(); $condition['is_end'] = 1; $keywords = trim(urldecode(urldecode(I('keywords')))); $condition['redname'] = array('like', '%' . $keywords . '%'); $model = M('red_config'); $count = $model->where($condition)->count(); $Page = new Page($count, $pagenum); $show = $Page->show(); $redmoneylist = $model->where($condition)->order("start_time asc,id asc") ->limit($Page->firstRow . ',' . $Page->listRows) ->order('add_time desc') ->select(); $this->assign('redmoneylist', $redmoneylist); $this->assign('page', $show);// 赋值分页输出 $this->assign('pager', $Page); $this->assign('keywords', $keywords); $this->assign('pagenum', $pagenum); // upload_ylp_log('历史记录'); return $this->fetch('', getAdmStoId()); } }