<?php
/**
 *  Guojiz网址导航系统
 *	www.guojiz.com
 *  作者：梦雨
 *  @ QQ50361804
 */
namespace app\admin\controller;

use think\Controller;
use think\Db;
class Kami extends Common
{
    public function index()
    {
        $dingdan = db('dingdan');
        $html = Db::name('kami')->order('id desc')->paginate(15);
        $this->assign('html', $html);
        returnhtml();
        return view();
    }
    //添加
    public function add()
    {
        $data = $this->request->post();
        if (!Db::name('kamicate')->data($data)->insert()) {
            $this->error('添加失败');
        } else {
            $this->success('添加成功');
        }
    }
    // 修改
    public function editcate()
    {
        if (config(pa('NP6ZgJozVhyuF')) != pa('K7Q03OgSv')) {
            $this->error(config('web.sq'));
        } else {
            $data['id'] = input('id');
            $data['name'] = input('name');
            $data['par'] = input('par');
            $data['rmb'] = input('rmb');
            $data['url'] = input('url');
            $cid = Db::name('kamicate')->update($data);
            if ($cid > 0) {
                exit('{"code":200,"msg":"修改成功"}');
            } else {
                exit('{"code":-1,"msg":"修改失败"}');
            }
        }
    }
    //删除
    public function del()
    {
        if (config(pa('NP6ZgJozVhyuF')) != pa('K7Q03OgSv')) {
            $this->error(config('web.sq'));
        } else {
            $id = input('id');
            if (Db::name('kamicate')->where('id', $id)->delete()) {
                Db::name('kami')->where('cid', $id)->delete();
                $this->success('删除成功');
            } else {
                $this->error('删除失败');
            }
        }
    }
    //生成卡密
    public function lists()
    {
        if (config(pa('NP6ZgJozVhyuF')) != pa('K7Q03OgSv')) {
            $this->error(config('web.sq'));
        } else {
            $addlist = Db::name('kami')->alias('d')->join('kamicate c', 'd.cid=c.id')->field('d.create_time,count(d.create_time) as count,sum(d.status) as status1,sum(d.sales) as sales1,c.par')->group('d.create_time,c.par')->order('d.create_time desc')->select();
            $this->assign('addlist', $addlist);
            $cate = Db::name('kamicate')->select();
            $this->assign('cate', $cate);
            returnhtml();
            return view();
        }
    }
    //生成
    public function addsc()
    {
        if (config(pa('NP6ZgJozVhyuF')) != pa('K7Q03OgSv')) {
            $this->error(config('web.sq'));
        } else {
            $data = $this->request->param();
            $length = $data['passslength'];
            $cid = $data['cid'];
            $par = Db::name('kamicate')->where('id', $cid)->value('par');
            //$rmb=$data['rmb'];
            $amount = $data['amount'];
            $insertdata = [];
            $res2 = [];
            for ($i = 0; $i < $amount; $i++) {
                $key = generate_password($length);
                $insertdata[] = ['passwdstr' => $key, 'cid' => $cid, 'create_time' => time()];
                $res2[] = ['passwdstr' => $key, 'par' => $par];
            }
            $res = Db::name('kami')->insertAll($insertdata);
            Db::name('kamicate')->where('id', $cid)->setInc('total', $amount);
            $this->success('生成成功');
        }
    }
    //查看
    public function viewkeybytime()
    {
        $data = $this->request->param();
        $ctime = $data['ctime'];
        $res = Db::name('kami')->alias('d')->join('kamicate c', 'd.cid=c.id')->field('passwdstr,par')->where('create_time', $ctime)->select();
        if ($res) {
            exit('{"code":200,"msg":' . json_encode($res) . '}');
        } else {
            exit('{"code":200,"msg":"无数据"}');
        }
    }
    //   删除
    public function delkeybytime()
    {
        if (config(pa('NP6ZgJozVhyuF')) != pa('K7Q03OgSv')) {
            $this->error(config('web.sq'));
        } else {
            $data = $this->request->param();
            $ctime = $data['ctime'];
            if (Db::name('kami')->where(array('create_time' => $ctime))->delete()) {
                $this->success('删除成功');
            } else {
                $this->assign('删除失败');
            }
        }
    }
}