插入商品到本地数据库参考

<!doctype html>
<html>
        <head>
                <meta charset="utf-8">
                <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0"/>
                <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
                <title>title</title>
                <link rel="stylesheet" type="text/css" href="../css/api.css" />
                <link rel="stylesheet" type="text/css" href="../css/style.css" />
        </head>
        <body>
                <div id="main" class="flex-con" >
                        <form class="mui-input-group">
                                <!--<div class="mui-input-row">
                                <textarea name="feedback" id="feedback" cols="30" rows="10" placeholder="请输入意见建议"></textarea>
                                </div>-->
                                <div class="mui-input-row">
                                        <label>商品图片</label>
                                        <!--<input type="file" class="mui-input-clear" placeholder="请输入联系电话" id="pic">-->
                                </div>
                                <div onclick="camera();">
                                        拍照
                                </div>
                                <div onclick="photo();">
                                        手机相册
                                </div>
                                <div class="mui-input-row">
                                        <label>商品名称</label>
                                        <input type="text" class="mui-input-clear" placeholder="请输入商品名称" id="pro_name">
                                </div>
                                <div class="mui-input-row">
                                        <label>条形码</label>
                                        <input type="text" class="mui-input-clear" placeholder="请输入条形码" id="barcode" readonly="readonly">
                                </div>
                                <div class="mui-input-row">
                                        <label>数量</label>
                                        <input type="text" class="mui-input-clear" placeholder="请输入数量" id="pro_num">
                                </div>
                                <div class="mui-input-row">
                                        <label>成本价</label>
                                        <input type="text" class="mui-input-clear" placeholder="请输入成本价" id="cost_price">
                                </div>
                                <div class="mui-input-row">
                                        <label>销售价</label>
                                        <input type="text" class="mui-input-clear" placeholder="请输入销售价" id="market_price">
                                </div>
                                <div class="mui-input-row">
                                        <label>摆放位置</label>
                                        <input type="text" class="mui-input-clear" placeholder="请输入摆放位置" id="position">
                                </div>
                                <div class="mui-input-row">
                                        <label>规格</label>
                                        <input type="text" class="mui-input-clear" placeholder="请输入规格" id="pro_spec">
                                </div>
                                <div class="">
                                        <select name="product_cat" id="pro_cat_name">
                                                <option value="0">请选择商品分类</option>
                                        </select>
                                </div>
                                <div class="">
                                        <select name="supplier" id="supplier">
                                                <option value="0">请选择供应商分类</option>
                                        </select>
                                </div>
                                <div onclick="chooseCat();">
                                        确定
                                </div>
                                <div class="">
                                        <select name="supname" id="supname"></select>
                                </div>
                                <button type="button" class="mui-btn mui-btn-green" id="reg" onclick="product_submit();">
                                        提交
                                </button>
                        </form>
                </div>
        </body>
</html>
<script type="text/javascript" src="../script/api.js"></script>
<script type="text/javascript">
        //        var FNScanner;
        var db;
        var barcode;
        var pid;
        apiready = function() {
                //                $api.fixStatusBar($api.dom('header'));
                //                $api.dom('.title').innerHTML = '录入商品';
                //                FNScanner = api.require('FNScanner');
                db = api.require('db');
                barcode = $api.getStorage('barcode');
                //判断条形码是否存在
                db.selectSql({
                        name : 'ghuog_db' + $api.getStorage('mobile'),
                        sql : "SELECT * FROM T_Product where BarCode= '" + barcode + "'"
                }, function(ret, err) {
                        if (ret.data != '') {
                                //条形码存在只修改该商品信息
                                $api.byId('pro_name').value = ret.data[0].ProName;
                                $api.byId('barcode').value = ret.data[0].BarCode;
                                $api.byId('pro_num').value = ret.data[0].Num;
                                $api.byId('cost_price').value = ret.data[0].CostPrice;
                                $api.byId('market_price').value = ret.data[0].MarketPrice;
                                $api.byId('position').value = ret.data[0].ProPosition;
                                $api.byId('pro_spec').value = ret.data[0].ProSpec;
                                $api.setStorage('isdata', 1);
                                if (ret.data[0].Status == 1) {
                                        $api.setStorage('isstatus', 1);
                                } else if (ret.data[0].Status == 2) {
                                        $api.setStorage('isstatus', 2);
                                } else if (ret.data[0].Status == 5) {
                                        $api.setStorage('isstatus', 5);
                                }
                                //                                alert(ret.data[0].Status);
                                //                                alert($api.getStorage('isstatus'));
                                db.selectSql({
                                        name : 'ghuog_db' + $api.getStorage('mobile'),
                                        sql : "SELECT * FROM T_Supplier where supid= '" + ret.data[0].supId + "'"
                                }, function(ret2, err2) {
                                        if (ret2.data != '') {
                                                //
                                                //                                                alert(ret2.data[0].supid);
                                                var str = "<option value=" + ret2.data[0].supid + ">" + ret2.data[0].SupplierName + "</option>";
                                                $api.append($api.byId('supname'), str);
                                        } else {
                                        }
                                });
                                //商品分类
                                db.selectSql({
                                        name : 'ghuog_db' + $api.getStorage('mobile'),
                                        sql : "SELECT * FROM T_ProductCat"
                                }, function(ret3, err3) {
                                        if (ret3.data != '') {
                                                for (var k = 0; k <= ret3.data.length - 1; k++) {
                                                        if (ret3.data[k].catid == ret.data[0].ProCat) {
                                                                var str = "<option selected='selected' value=" + ret3.data[k].catid + ">" + ret3.data[k].ProductCatName + "</option>";
                                                        } else {
                                                                var str = "<option value=" + ret3.data[k].catid + ">" + ret3.data[k].ProductCatName + "</option>";
                                                        }
                                                        $api.append($api.byId('pro_cat_name'), str);
                                                }
                                        } else {
                                        }
                                });
                        } else {
                                //条形码不存在插入到数据库
                                $api.byId('barcode').value = barcode;
                                $api.setStorage('isdata', 0);
                                db.selectSql({
                                        name : 'ghuog_db' + $api.getStorage('mobile'),
                                        sql : "SELECT * FROM T_ProductCat"
                                }, function(ret3, err3) {
                                        if (ret3.data != '') {
                                                for (var k = 0; k <= ret3.data.length - 1; k++) {
                                                        var str = "<option value=" + ret3.data[k].catid + ">" + ret3.data[k].ProductCatName + "</option>";
                                                        $api.append($api.byId('pro_cat_name'), str);
                                                }
                                        } else {
                                        }
                                });
                        }
                });
                //
                db.selectSql({
                        name : 'ghuog_db' + $api.getStorage('mobile'),
                        sql : "SELECT * FROM T_SupplierCat"
                }, function(ret2, err2) {
                        if (ret2.data != '') {
                                //                        alert(ret2.data.length);
                                for (var i = 0; i <= ret2.data.length - 1; i++) {
                                        var str = "<option value=" + ret2.data[i].catid + ">" + ret2.data[i].SupplierCatName + "</option>";
                                        $api.append($api.byId('supplier'), str);
                                }
                        } else {
                                //
                        }
                });
        };
        function chooseCat() {
                pid = $api.byId('supplier').value;
                $api.byId('supname').innerHTML = '';
                //        alert(pid);
                db.selectSql({
                        name : 'ghuog_db' + $api.getStorage('mobile'),
                        sql : "SELECT * FROM T_Supplier where pid= '" + pid + "'"
                }, function(ret1, err1) {
                        if (ret1.data != '') {
                                //
                                for (var i = 0; i <= ret1.data.length - 1; i++) {
                                        var str = "<option value=" + ret1.data[i].supid + ">" + ret1.data[i].SupplierName + "</option>";
                                        $api.append($api.byId('supname'), str);
                                }
                        } else {
                        }
                });
        }

        function camera() {
                api.getPicture({
                        sourceType : 'camera',
                        encodingType : 'jpg',
                        mediaValue : 'pic',
                        destinationType : 'url',
                        allowEdit : true,
                        quality : 50,
                        targetWidth : 100,
                        targetHeight : 100,
                        saveToPhotoAlbum : false
                }, function(ret, err) {
                        if (ret) {
                                alert(JSON.stringify(ret));
                        } else {
                                alert(JSON.stringify(err));
                        }
                });
        }

        function photo() {
                api.getPicture({
                        sourceType : 'library',
                        encodingType : 'jpg',
                        mediaValue : 'pic',
                        destinationType : 'url',
                        allowEdit : true,
                        quality : 50,
                        targetWidth : 100,
                        targetHeight : 100,
                        saveToPhotoAlbum : false
                }, function(ret, err) {
                        if (ret) {
                                alert(JSON.stringify(ret));
                        } else {
                                alert(JSON.stringify(err));
                        }
                });
        }

        function scaner() {
                FNScanner.openScanner({
                        autorotation : false
                }, function(ret, err) {
                        if (ret.eventType = 'success') {
                                $api.byId('barcode').value = ret.content;
                                //                                alert(JSON.stringify(ret));
                        } else {
                                alert(JSON.stringify(err));
                        }
                });
        }

        //插入商品
        function product_submit() {
                //        var status;
                var supId = $api.byId('supname').value;
                var BarCode = $api.byId('barcode').value;
                var ProName = $api.byId('pro_name').value;
                var Num = $api.byId('pro_num').value;
                var CostPrice = $api.byId('cost_price').value;
                var MarketPrice = $api.byId('market_price').value;
                var ProPosition = $api.byId('position').value;
                var ProCat = $api.byId('pro_cat_name').value;
                var ProSpec = $api.byId('pro_spec').value;
                var d = new Date();
                var AddTime = d.getFullYear() + "-" + (d.getMonth() + 1) + "-" + d.getDate() + " " + d.getHours() + ":" + d.getMinutes() + ":" + d.getSeconds();
                alert(AddTime);
                //判断条形码是否存在
                if ($api.getStorage('isdata') == 1) {
                        //条形码存在只修改该商品数量
                        if ($api.getStorage('isstatus') == 1) {
                                var status = 1;
                        } else if ($api.getStorage('isstatus') == 2) {
                                var status = 2;
                        } else if ($api.getStorage('isstatus') == 5) {
                                var status = 2;
                        }
                        db.executeSql({
                                name : 'ghuog_db' + $api.getStorage('mobile'),
                                sql : "UPDATE T_Product SET ProCat='" + ProCat + "',ProSpec='" + ProSpec + "',ProName='" + ProName + "',CostPrice='" + CostPrice + "',MarketPrice='" + MarketPrice + "',Status='" + status + "',ProPosition='" + ProPosition + "',supId='" + supId + "' where Barcode= '" + BarCode + "'"
                        }, function(ret, err) {
                                if (ret.status) {
                                        //                                        api.toast({
                                        //                                                msg : '商品更新成功'
                                        //                                        });
                                        alert('商品更新成功');
                                } else {
                                        alert(err.msg);
                                        api.toast({
                                                msg : err.msg
                                        });
                                }
                                $api.setStorage('isdata', '');
                                $api.setStorage('isstatus', '');
                                api.closeWin({
                                });
                                api.execScript({
                                        name : 'add_product',
                                        frameName : 'add_product',
                                        script : 'winreload();'
                                });
                        });
                } else {
                        //条形码不存在插入到数据库
                        db.executeSql({
                                name : 'ghuog_db' + $api.getStorage('mobile'),
                                sql : "INSERT INTO T_Product(BarCode,ProName,Num,AddTime,CostPrice,MarketPrice,ProPosition,Status,supId,ProSpec,ProCat)VALUES('" + BarCode + "','" + ProName + "','" + Num + "','" + AddTime + "','" + CostPrice + "','" + MarketPrice + "','" + ProPosition + "',1,'" + supId + "','" + ProSpec + "','" + ProCat + "')"
                        }, function(ret, err) {
                                if (ret.status) {
                                        alert('商品录入成功');
                                        //                                        api.toast({
                                        //                                                msg : '商品录入成功'
                                        //                                        });
                                } else {
                                        alert(err.msg);
                                        api.toast({
                                                msg : err.msg
                                        });
                                }
                                $api.setStorage('isdata', '');
                                api.closeWin({
                                });
                                api.execScript({
                                        name : 'add_product',
                                        frameName : 'add_product',
                                        script : 'winreload();'
                                });
                        });
                }
        }
</script>

 

posted @ 2016-11-03 15:54  智昕  阅读(237)  评论(0编辑  收藏  举报