Redmoney.php 16.7 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474
<?php
/**
 * tpshop
 * ============================================================================
 * 版权所有 2015-2027 深圳搜豹网络科技有限公司,并保留所有权利。
 * 网站地址: http://www.tp-shop.cn
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件!您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
 * 不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * Author: 当燃
 * 专题管理
 * Date: 2016-03-09
 */

namespace app\admin\controller;

use think\AjaxPage;
use think\Page;
use app\admin\logic\GoodsLogic;
use think\db;
use think\Cache;
use think\Cache\driver\Redis;
class Redmoney extends Base
{

    //列表
    public function index()
    {

        $getp = I('p/d', 1);
        if ($getp <= 1) {
            $this->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());
    }


}