def product_add():
    if session.get('admin', None) is None:
        return redirect(url_for('admin_login'))
    else:
        db = Mysql_helper("10.0.0.12", "root", "123456", "mar", 3306)
        #得到页面下拉列表所需数据
        yongtus = db.query_sql(
            "select id,categoryname from category1 WHERE explanation = '用途'",
            one=False)
        donglis = db.query_sql(
            "select id,categoryname from category1 WHERE explanation = '动力能源'",
            one=False)
        cailiaos = db.query_sql(
            "select id,categoryname from category1 WHERE explanation = '光源材料'",
            one=False)
        fengges = db.query_sql(
            "select id,categoryname from category1 WHERE explanation = '外形风格'",
            one=False)
        zhutis = db.query_sql(
            "select id,categoryname from category1 WHERE explanation = '主题'",
            one=False)
        pinpais = db.query_sql(
            "select id,categoryname from category1 WHERE explanation = '品牌'",
            one=False)
        db.close_connection()
        return render_template('product_add.html',
                               yongtus=yongtus,
                               donglis=donglis,
                               cailiaos=cailiaos,
                               fengges=fengges,
                               zhutis=zhutis,
                               pinpais=pinpais)
def admin_login():
    if request.method == 'POST':
        username = request.form.get('username')
        pwd = request.form.get('pwd')
        s1 = sha1()
        s1.update(pwd.encode("utf8"))
        pwd_hash = s1.hexdigest()
        sql = 'select count(*) as Count from admin WHERE NAME =%s and pwd =%s'
        db = Mysql_helper("10.0.0.12", "root", "123456", "mar", 3306)
        result = db.query_sql(sql, (username, pwd_hash), one=True)
        if int(result.get('Count')) > 0:
            session['admin'] = username
            return redirect(url_for('p'))
        else:
            flash('用户名或密码错误')
    return render_template('admin_login.html')
def save_edit_product():
    if request.method == 'POST':
        xinghao = request.form.get('xinghao', '')
        yongtu = request.form.get('yongtu', 1)
        dongli = request.form.get('dongli', 20)
        cailiao = request.form.get('cailiao', 30)
        fengge = request.form.get('fengge', 70)
        zhuti = request.form.get('zhuti', 90)
        pinpai = request.form.get('pinpai', 119)
        danjia = request.form.get('danjia', '')
        gonglv = request.form.get('gonglv', '')
        gtl = request.form.get('gtl', '')
        sewen = request.form.get('sewen', '')
        fgjd = request.form.get('fgjd', '')
        xszs = request.form.get('xszs', '')
        zbq = request.form.get('zbq', '')
        shuoming = request.form.get('shuoming', '')
        #
        # a = [xinghao,yongtu,dongli,cailiao,fengge,zhuti,pinpai,danjia,gonglv,gtl,sewen,fgjd,xszs,zbq,shuoming]
        # print(a)

        db = Mysql_helper("10.0.0.12", "root", "123456", "mar", 3306)
        sql = """
        update productinfo1 set
        yongtu=%s,dongli=%s,cailiao=%s,fengge=%s,zhuti=%s,pinpai=%s,danjia=%s,gonglv=%s,gtl=%s,sewen=%s,fgjd=%s,xszs=%s, zbq=%s,shuoming=%s
         where xinghao = %s
        """
        db.execute_sql(
            sql, (yongtu, dongli, cailiao, fengge, zhuti, pinpai, danjia,
                  gonglv, gtl, sewen, fgjd, xszs, zbq, shuoming, xinghao))
        db.close_connection()
        flash('用户更新成功!')

    return redirect(url_for('p'))
def product_del(xinghao=None):
    if xinghao:
        db = Mysql_helper("10.0.0.12", "root", "123456", "mar", 3306)
        sql = 'delete from productinfo1 WHERE xinghao = %s'
        db.execute_sql(sql, params=(xinghao, ))
        db.close_connection()
    return redirect(url_for('p'))
def product_info(xinghao=None):
    if session.get('admin', None) is None:
        return redirect(url_for('admin_login'))
    else:
        db = Mysql_helper("10.0.0.12", "root", "123456", "mar", 3306)
        sql = """
    select p.*,
    a.categoryname AS yongtu,
    b.categoryname AS dongli,
    c.categoryname AS cailiao,
    d.categoryname AS fengge,
    e.categoryname AS zhuti,
    f.categoryname AS pinpai from productinfo1 p
    LEFT JOIN category1 a ON a.id = p.yongtu
    LEFT JOIN category1 b ON b.id = p.dongli
    LEFT JOIN category1 c ON c.id = p.cailiao
    LEFT JOIN category1 d ON d.id = p.fengge
    LEFT JOIN category1 e ON e.id = p.zhuti
    LEFT JOIN category1 f ON f.id = p.pinpai
    WHERE p.xinghao = %s"""
        params = xinghao
        product = db.query_sql(sql, params, one=True)
        db.close_connection()
        return render_template('product_info.html', product=product)
def product_list(id=1):
    if session.get('admin', None) is None:
        return redirect(url_for('admin_login'))
    else:
        db = Mysql_helper("10.0.0.12", "root", "123456", "mar", 3306)

        sql = """
    select p.xinghao,
    a.categoryname AS yongtu,
    b.categoryname AS dongli,
    c.categoryname AS cailiao,
    d.categoryname AS fengge,
    e.categoryname AS zhuti,
    f.categoryname AS pinpai,
    p.danjia from productinfo1 p
    LEFT JOIN category1 a ON a.id = p.yongtu
    LEFT JOIN category1 b ON b.id = p.dongli
    LEFT JOIN category1 c ON c.id = p.cailiao
    LEFT JOIN category1 d ON d.id = p.fengge
    LEFT JOIN category1 e ON e.id = p.zhuti
    LEFT JOIN category1 f ON f.id = p.pinpai
    """
        products = db.query_sql(sql, one=False)
        #计算总数,以用于分页
        sql2 = "select count(*) from productinfo1"
        pro_number = db.query_sql(sql2)
        db.close_connection()
        unit_products = []
        #i是用来显示序号的,mysql内不存在rowid,id不连续,显示出来会比较难看。使用如下代码自定义id能显得好看
        for i, product in enumerate(products[10 * (int(id) - 1):(10 *
                                                                 int(id))]):
            unit_products.append(((i + 10 * int(id) - 9), product))
        #总页数
        pages = ((pro_number['count(*)']) // 10 + 1)
        return render_template('product_list.html',
                               products=unit_products,
                               pages=pages,
                               id=int(id))
def post_add_product():
    if request.method == 'POST':
        #从request的form中获取
        xinghao = request.form.get('xinghao', '')
        yongtu = request.form.get('yongtu', 1)
        dongli = request.form.get('dongli', 20)
        cailiao = request.form.get('cailiao', 30)
        fengge = request.form.get('fengge', 70)
        zhuti = request.form.get('zhuti', 90)
        pinpai = request.form.get('pinpai', 119)
        danjia = request.form.get('danjia', '')
        gonglv = request.form.get('gonglv', '')
        gtl = request.form.get('gtl', '')
        sewen = request.form.get('sewen', '')
        fgjd = request.form.get('fgjd', '')
        xszs = request.form.get('xszs', '')
        zbq = request.form.get('zbq', '')
        shuoming = request.form.get('shuoming', '')
        tupian = None
        #判断图片是否存在,是否符合格式要求(注意一定是判断文件的filename而非文件本身),ok的话,设置文件新名并以此名保存上传
        if request.files.get('tupian', None):
            pic = request.files.get('tupian')
            if check_pic_ext(pic.filename):
                img_path1 = datetime.now().strftime(
                    "%Y%m%d%H%M%f") + os.path.splitext(pic.filename)[1]
                pic.save(os.path.join(UPLOAD_FOLODER, img_path1))

        db = Mysql_helper("10.0.0.12", "root", "123456", "mar", 3306)

        sql = "insert into productinfo1( xinghao, tupian, yongtu, dongli, cailiao, fengge, zhuti, pinpai, danjia, gonglv, gtl, sewen, fgjd, xszs, zbq, shuoming) VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)"
        params = (xinghao, img_path1, yongtu, dongli, cailiao, fengge, zhuti,
                  pinpai, danjia, gonglv, gtl, sewen, fgjd, xszs, zbq,
                  shuoming)

        db.execute_sql(sql, params)
        db.close_connection()
        return redirect(url_for('product_add'))
def search_product(id=1):
    db = Mysql_helper("10.0.0.12", "root", "123456", "mar", 3306)
    # 给用途、品牌的下拉列表以数据库内选项
    yongtus = db.query_sql(
        "select id,categoryname from category1 WHERE explanation = '用途'",
        one=False)
    pinpais = db.query_sql(
        "select id,categoryname from category1 WHERE explanation = '品牌'",
        one=False)

    if request.args.get('yongtu', None) is None:
        sql = r"""
            select p.xinghao,
            a.categoryname AS yongtu,
            b.categoryname AS dongli,
            c.categoryname AS cailiao,
            d.categoryname AS fengge,
            e.categoryname AS zhuti,
            f.categoryname AS pinpai,
            p.danjia from productinfo1 p
            LEFT JOIN category1 a ON a.id = p.yongtu
            LEFT JOIN category1 b ON b.id = p.dongli
            LEFT JOIN category1 c ON c.id = p.cailiao
            LEFT JOIN category1 d ON d.id = p.fengge
            LEFT JOIN category1 e ON e.id = p.zhuti
            LEFT JOIN category1 f ON f.id = p.pinpai
            """
        products = db.query_sql(sql, one=False)
    else:
        sql = r"""
                    select p.xinghao,
                    a.categoryname AS yongtu,
                    b.categoryname AS dongli,
                    c.categoryname AS cailiao,
                    d.categoryname AS fengge,
                    e.categoryname AS zhuti,
                    f.categoryname AS pinpai,
                    p.danjia from productinfo1 p
                    LEFT JOIN category1 a ON a.id = p.yongtu
                    LEFT JOIN category1 b ON b.id = p.dongli
                    LEFT JOIN category1 c ON c.id = p.cailiao
                    LEFT JOIN category1 d ON d.id = p.fengge
                    LEFT JOIN category1 e ON e.id = p.zhuti
                    LEFT JOIN category1 f ON f.id = p.pinpai
                    WHERE p.id> 0 """
        yongtu = request.args.get('yongtu', None)
        pinpai = request.args.get('pinpai', None)
        xinghao = request.args.get('xinghao', '')
        print('品牌是--{}'.format(pinpai))
        jiagequjian = request.args.get('danjia', None)
        dict1 = {
            'a': 'and p.danjia <= 20 ',
            'b': 'and p.danjia > 20 & p.danjia <= 100 ',
            'c': 'and p.danjia > 100 & p.danjia <= 1000 ',
            'd': 'and p.danjia >1000 ',
            'e': ''
        }
        yongtu_sql = "and p.yongtu = {} ".format(
            yongtu) if yongtu != '---' else ''
        pinpai_sql = "and p.pinpai = {} ".format(
            pinpai) if pinpai != '---' else ''
        #双%是转义需要
        xinghao_sql = "and p.xinghao like '%%{}%%'".format(
            xinghao) if xinghao != '' else ''
        sql1 = sql + yongtu_sql + pinpai_sql + dict1.get(
            jiagequjian) + xinghao_sql
        products = db.query_sql(sql1, one=False)
    db.close_connection()
    unit_products = []
    #i是用来显示产品序号的,mysql内不存在rowid,自定义id不连续,显示出来比较难看。
    if products:
        for i, product in enumerate(products[10 * (int(id) - 1):(10 *
                                                                 int(id))]):
            unit_products.append(((i + 10 * int(id) - 9), product))
    else:
        unit_products = []
    #总页数
    pages = (len(products) // 10 + 1)
    return render_template('search_product.html',
                           products=unit_products,
                           pages=pages,
                           id=int(id),
                           yongtus=yongtus,
                           pinpais=pinpais)
def product_edit(xinghao=None):
    if session.get('admin', None) is None:
        return redirect(url_for('admin_login'))
    else:
        db = Mysql_helper("10.0.0.12", "root", "123456", "mar", 3306)
        yongtus = db.query_sql(
            "select id,categoryname from category1 WHERE explanation = '用途'",
            one=False)
        donglis = db.query_sql(
            "select id,categoryname from category1 WHERE explanation = '动力能源'",
            one=False)
        cailiaos = db.query_sql(
            "select id,categoryname from category1 WHERE explanation = '光源材料'",
            one=False)
        fengges = db.query_sql(
            "select id,categoryname from category1 WHERE explanation = '外形风格'",
            one=False)
        zhutis = db.query_sql(
            "select id,categoryname from category1 WHERE explanation = '主题'",
            one=False)
        pinpais = db.query_sql(
            "select id,categoryname from category1 WHERE explanation = '品牌'",
            one=False)

        sql = """
    select p.xinghao,p.tupian,p.danjia,p.gonglv,p.gtl,p.sewen,p.fgjd,p.xszs,p.zbq,p.shuoming,
    p.yongtu AS yongtuid,p.dongli AS dongliid,p.cailiao AS cailiaoid,p.fengge AS fenggeid,
    p.pinpai AS pinpaiid,p.zhuti AS zhutiid,
    a.categoryname AS yongtu,
    b.categoryname AS dongli,
    c.categoryname AS cailiao,
    d.categoryname AS fengge,
    e.categoryname AS zhuti,
    f.categoryname AS pinpai from productinfo1 p
    LEFT JOIN category1 a ON a.id = p.yongtu
    LEFT JOIN category1 b ON b.id = p.dongli
    LEFT JOIN category1 c ON c.id = p.cailiao
    LEFT JOIN category1 d ON d.id = p.fengge
    LEFT JOIN category1 e ON e.id = p.zhuti
    LEFT JOIN category1 f ON f.id = p.pinpai
    WHERE p.xinghao = %s
    """
        params = xinghao
        product = db.query_sql(sql, params, one=True)
        db.close_connection()

        return render_template('product_edit.html',
                               product=product,
                               yongtus=yongtus,
                               donglis=donglis,
                               cailiaos=cailiaos,
                               fengges=fengges,
                               zhutis=zhutis,
                               pinpais=pinpais)