//將excel的資料寫入資料庫 private string InsertGoods(DataTable dt) { string result = ""; BLL.article_category categoryBll = new BLL.article_category(); BLL.article articleBll = new BLL.article(); BLL.goods goodsBll = new BLL.goods(); Model.article articleModel = null; Model.goods goodsModel = null; //判斷列名是否規範 string[] strColumn = { "類別*", "品牌", "商品型號*", "商品名稱", "隊伍", "顏色*", "尺寸*", "數量量*", "市價", "售價", "商品描述", "商品說明", "注意事項", "商品分類1", "商品分類2", "商品分類3", "關鍵字", "上架日期", "下架日期", "每人限購數量*", "前臺排序", "重量", "長", "寬", "高", "紅利" }; int num = 0; for (int i = 0; i < dt.Columns.Count; i++) { foreach (string str in strColumn) { if (str == dt.Columns[i].ColumnName) { num++; } } } if (num == strColumn.Length) { //遍歷主件 DataRow[] drArr = dt.Select("[類別*]='商品主件'"); foreach (DataRow dr in drArr) { if (dr[0].ToString().Trim() != "") { bool updatea = true; DataSet ds = Tea.DBUtility.DbHelperSQL.Query("select * from shop_article where goods_no='" + dr[2].ToString().Trim() + "'"); if (ds.Tables[0].Rows.Count > 0) { articleModel = articleBll.GetModel(int.Parse(ds.Tables[0].Rows[0]["id"].ToString())); } if (articleModel == null) { articleModel = new Model.article(); updatea = false; } articleModel.channel_id = 2; if (string.IsNullOrEmpty(dr[13].ToString())) { articleModel.category_id = 0; } else { articleModel.category_id = int.Parse(dr[13].ToString());// categoryBll.GetID(dr[13].ToString().Split(',')[0]); } //articleModel.more_type = dr[15].ToString(); articleModel.call_index = ""; articleModel.title = dr[3].ToString(); articleModel.link_url = ""; if (string.IsNullOrEmpty(dr[1].ToString())) { //articleModel.brand_id = 0; } else { //articleModel.brand_id = brandBll.GetID(dr[1].ToString()); } if (string.IsNullOrEmpty(dr[4].ToString())) { // articleModel.team_id = 0; } else { //articleModel.team_id = teamBll.GetID(dr[4].ToString()); } articleModel.img_url = ""; articleModel.seo_title = ""; articleModel.seo_keywords = dr[16].ToString(); articleModel.seo_description = ""; articleModel.zhaiyao = ""; articleModel.content = "";// dr[10].ToString(); articleModel.sort_id = Utils.ObjToInt(dr[20], 999); articleModel.click = 0; articleModel.status = 1; articleModel.is_msg = 0; articleModel.is_tui = 0; articleModel.is_can = 0; articleModel.is_zhe = 0; articleModel.is_slide = 0; articleModel.is_sys = 1; //管理員發佈 articleModel.user_name = "admin"; //獲得當前登入用戶名 articleModel.add_time = DateTime.Now; //articleModel.sales_id = 0; int pid = 0; if (updatea) { articleBll.Update(articleModel); pid = articleModel.id; } else { pid = articleBll.Add(articleModel); } //匯入子件 DataRow[] drArr1 = dt.Select("[類別*]='子件' and [商品型號*] like '" + dr[2].ToString() + "%'"); foreach (DataRow dr1 in drArr1) { bool updateg = true; DataSet dsg = Tea.DBUtility.DbHelperSQL.Query("select * from shop_goods where goods_no='" + dr1[2].ToString().Trim() + "'"); if (dsg.Tables[0].Rows.Count > 0) { goodsModel = goodsBll.GetModel(int.Parse(dsg.Tables[0].Rows[0]["id"].ToString())); } if (goodsModel == null) { goodsModel = new Model.goods(); updateg = false; } goodsModel.parent_id = pid; goodsModel.color = dr1[5].ToString(); goodsModel.size = dr1[6].ToString(); goodsModel.goods_no = dr1[2].ToString(); if (dr1[8].ToString().Trim() != "") { goodsModel.market_price = Utils.ObjToDecimal(dr1[8], 0M); } else { goodsModel.market_price = Utils.ObjToDecimal(dr[8], 0M); } if (dr1[9].ToString().Trim() != "") { goodsModel.sell_price = Utils.ObjToDecimal(dr1[9], 0M); } else { goodsModel.sell_price = Utils.ObjToDecimal(dr[9], 0M); } if (dr1[7].ToString().Trim() != "") { goodsModel.stock_quantity = Utils.ObjToInt(dr1[7], 0); } else { goodsModel.stock_quantity = Utils.ObjToInt(dr[7], 0); } goodsModel.alert_quantity = 0; goodsModel.img_url = ""; if (goodsModel.goods_no.Trim().Length > 4) { if (updateg) { goodsBll.Update(goodsModel); } else { goodsBll.Add(goodsModel); } } } } } } else { result = "請檢查excel檔案格式!" + num + strColumn.Length; } return(result); }
private bool DoAdd() { bool result = true; Model.goods model = new Model.goods(); BLL.goods bll = new BLL.goods(); model.channel_id = this.channel_id; model.title = txtTitle.Text.Trim(); model.category_id = int.Parse(ddlBusiness.SelectedValue); model.link_url = ""; //检查是否有自定义图片 if (txtImgUrl.Text.Trim() != "") { model.img_url = txtImgUrl.Text; } else { model.img_url = @"/admin/images/default.png"; } model.content = txtContent.Text.Trim(); model.sell_time = String.Format("{0}:{1}-{2}:{3}", ddlTime_l1.SelectedValue, ddlTime_l2.SelectedValue, ddlTime_r1.SelectedValue, ddlTime_r2.SelectedValue); model.phone = txtPhone.Text.Trim(); model.mobilephone = txtMobile.Text.Trim(); model.city_id = int.Parse(Request.Form["ddlArea"]); model.address = txtLocation.Text.Trim(); model.lat = txtlat.Text.Trim(); model.lng = txtlng.Text.Trim() ; model.type_id = string.IsNullOrEmpty(Request.Form["ddlType"]) ? 0 : int.Parse(Request.Form["ddlType"]); //Request.Form["selectA"] model.rtype_id = 0; model.discount = string.IsNullOrEmpty(Request.Form["ddlDiscount"]) ? 0 : int.Parse(Request.Form["ddlDiscount"]); model.avg_price = decimal.Parse(txtAvgPrice.Text.Trim()); //判断 当为门票类型时,market_price为成人票价格 sell_price 为儿童票价格 if (int.Parse(ddlBusiness.SelectedValue) == (int)DTEnums.CatergoryID.season) { model.market_price = decimal.Parse(txtAdultPrice.Text); model.sell_price = decimal.Parse(txtChildPrice.Text); model.old_price = decimal.Parse(txtOldPrice.Text); } else { model.market_price = decimal.Parse(txtMarketPrice.Text); model.sell_price = 0; model.old_price = 0; } model.status = 1; //0申请 1正常 2过期 model.sort_id = int.Parse(txtSortId.Text.Trim()); model.begin_date = txtStartDate.Text.Trim(); model.end_date = txtEndDate.Text.Trim(); model.add_time = DateTime.Now; model.good_limit = 0; model.is_red = 0; model.is_slide = 0; model.is_top = 0; model.remark1 = Request.Form["ddlProvince"] + "," + Request.Form["ddlCity"] + "," + Request.Form["ddlArea"]; model.remark2 = 0; model.contact = txtContact.Text.Trim(); if (cblItem.Items[0].Selected == true) { model.is_top = 1; } if (cblItem.Items[1].Selected == true) { model.is_red = 1; } //查看权限 model.good_limit = int.Parse(rdoItem.SelectedValue); ////会员组价格 //List<Model.goods_group_price> priceList = new List<Model.goods_group_price>(); //for (int i = 0; i < rptPrice.Items.Count; i++) //{ // int _groupid = Convert.ToInt32(((HiddenField)rptPrice.Items[i].FindControl("hideGroupId")).Value); // decimal _price = Convert.ToDecimal(((TextBox)rptPrice.Items[i].FindControl("txtGroupPrice")).Text.Trim()); // priceList.Add(new Model.goods_group_price { group_id = _groupid, price = _price }); //} //model.goods_group_prices = priceList; //保存相册 string[] albumArr = Request.Form.GetValues("hide_photo_name"); string[] remarkArr = Request.Form.GetValues("hide_photo_remark"); if (albumArr != null && albumArr.Length > 0) { List<Model.goods_albums> ls = new List<Model.goods_albums>(); for (int i = 0; i < albumArr.Length; i++) { string[] imgArr = albumArr[i].Split('|'); if (imgArr.Length == 3) { if (!string.IsNullOrEmpty(remarkArr[i])) { ls.Add(new Model.goods_albums { big_img = imgArr[1], small_img = imgArr[2], remark = remarkArr[i] }); } else { ls.Add(new Model.goods_albums { big_img = imgArr[1], small_img = imgArr[2] }); } } } model.albums = ls; } if (bll.Add(model) < 1) { result = false; } return result; }
private bool DoEdit(int _id) { bool result = false; BLL.article bll = new BLL.article(); Model.article model = bll.GetModel(_id); model.channel_id = this.channel_id; model.category_id = Utils.StrToInt(ddlCategoryId.SelectedValue, 0); model.brand_id = Utils.StrToInt(ddlType.SelectedValue, 0); model.call_index = txtCallIndex.Text.Trim(); model.title = txtTitle.Text.Trim(); model.link_url = txtLinkUrl.Text.Trim(); model.img_url = txtImgUrl.Text; model.seo_title = txtSeoTitle.Text.Trim(); model.seo_keywords = txtSeoKeywords.Text.Trim(); model.seo_description = txtSeoDescription.Text.Trim(); //內容摘要提取內容前255個字元 if (string.IsNullOrEmpty(txtZhaiyao.Text.Trim())) { model.zhaiyao = Utils.DropHTML(txtContent.Value, 255); } else { model.zhaiyao = Utils.DropHTML(txtZhaiyao.Text, 255); } model.content = txtContent.Value; model.sort_id = Utils.StrToInt(txtSortId.Text.Trim(), 99); model.click = int.Parse(txtClick.Text.Trim()); model.status = Utils.StrToInt(rblStatus.SelectedValue, 0); model.is_tui = 0; model.is_zhe = 0; if (cblItem.Items[0].Selected == true) { model.is_tui = 1; } if (cblItem.Items[1].Selected == true) { model.is_zhe = 1; } model.add_time = Utils.StrToDateTime(txtAddTime.Text.Trim()); model.begin_time = Utils.StrToDateTime(txtBeginTime.Text.Trim()); model.end_time = Utils.StrToDateTime(txtEndTime.Text.Trim()); DateTime _end_time; if (DateTime.TryParse(txtUpdate.Text.Trim(), out _end_time)) { model.update_time = _end_time; } DateTime __end_time; if (DateTime.TryParse(txt_Xia_Date.Text.Trim(), out __end_time)) { model.xia_date = __end_time; } if (cbIsLock.Checked) { model.is_msg = 1; } else { model.is_msg = 0; } //分表 model.sell_price = Utils.StrToDecimal(txtSell_price.Text, 0); model.market_price = Utils.StrToDecimal(txtMarket_price.Text, 0); model.point = Utils.StrToInt(txtPoint.Text, 0); model.goods_no = txtGoods_no.Text; model.guige = txtGuige.Text; model.sub_title = txtSub_title.Text; model.guigemore = txtGuigeMore.Value; model.shuoming = txtShuo.Value; model.zhuyi = txtZhuyi.Text.Replace("/s", "|"); System.Text.StringBuilder sb = new StringBuilder(); sb.Append(','); foreach (ListItem li in cbMore.Items) { if (li.Selected) { sb.AppendFormat("{0},", li.Value); } } model.more_type = sb.ToString(); model.tags = txtTag.Text + "$" + txtTag1.Text + "$" + txtTag2.Text; #region 儲存相冊==================== //檢查是否有自訂圖片 if (txtImgUrl.Text.Trim() == "") { model.img_url = hidFocusPhoto.Value; } if (model.albums != null) { model.albums.Clear(); } string[] albumArr = Request.Form.GetValues("hid_photo_name"); string[] remarkArr = Request.Form.GetValues("hid_photo_remark"); if (albumArr != null) { List <Model.article_albums> ls = new List <Model.article_albums>(); for (int i = 0; i < albumArr.Length; i++) { string[] imgArr = albumArr[i].Split('|'); int img_id = Utils.StrToInt(imgArr[0], 0); if (imgArr.Length == 3) { if (!string.IsNullOrEmpty(remarkArr[i])) { ls.Add(new Model.article_albums { id = img_id, article_id = _id, original_path = imgArr[1], thumb_path = imgArr[2], remark = remarkArr[i] }); } else { ls.Add(new Model.article_albums { id = img_id, article_id = _id, original_path = imgArr[1], thumb_path = imgArr[2] }); } } } model.albums = ls; } #endregion //#region 儲存會員組價格============== //List<Model.user_group_price> priceList = new List<Model.user_group_price>(); //for (int i = 0; i < rptPrice.Items.Count; i++) //{ // int hidPriceId = 0; // if (!string.IsNullOrEmpty(((HiddenField)rptPrice.Items[i].FindControl("hidePriceId")).Value)) // { // hidPriceId = Convert.ToInt32(((HiddenField)rptPrice.Items[i].FindControl("hidePriceId")).Value); // } // int hidGroupId = Convert.ToInt32(((HiddenField)rptPrice.Items[i].FindControl("hideGroupId")).Value); // decimal _price = Convert.ToDecimal(((TextBox)rptPrice.Items[i].FindControl("txtGroupPrice")).Text.Trim()); // priceList.Add(new Model.user_group_price { id = hidPriceId, article_id = _id, group_id = hidGroupId, price = _price }); //} //model.group_price = priceList; //#endregion if (Request.Form.GetValues("item_goods_no") == null || Request.Form.GetValues("item_goods_no").Length < 1) { JscriptMsg("請設定商品規格!", string.Empty); return(false); } #region 儲存子件============== BLL.goods bll_goods = new BLL.goods(); StringBuilder idList = new StringBuilder(); string[] itemid = Request.Form.GetValues("item_id"); string[] goods_no = Request.Form.GetValues("item_goods_no"); string[] color = Request.Form.GetValues("item_color"); string[] imgurl = Request.Form.GetValues("item_imgurl"); string[] market_price = Request.Form.GetValues("item_market_price"); string[] sell_price = Request.Form.GetValues("item_sell_price"); string[] stock_quantity = Request.Form.GetValues("item_stock_quantity"); string[] chang = Request.Form.GetValues("item_chang"); string[] kuan = Request.Form.GetValues("item_kuan"); string[] gao = Request.Form.GetValues("item_gao"); string[] zhong = Request.Form.GetValues("item_zhong"); if (goods_no != null && goods_no.Length > 0) { for (int i = 0; i < goods_no.Length; i++) { Model.goods model_goods = null; int id = int.Parse(itemid[i].ToString()); bool update = true; if (id == 0) { model_goods = new Model.goods(); update = false; } else { model_goods = bll_goods.GetModel(id); } model_goods.parent_id = model.id; model_goods.img_url = imgurl[i].ToString(); model_goods.market_price = Utils.StrToDecimal(market_price[i].ToString(), 0); model_goods.goods_no = goods_no[i].ToString(); model_goods.sell_price = Utils.StrToDecimal(sell_price[i].ToString(), 0); model_goods.stock_quantity = Utils.StrToInt(stock_quantity[i].ToString(), 0); model_goods.color = color[i].ToString(); model_goods.guige = color[i].ToString(); model_goods.chang = Utils.StrToDecimal(chang[i].ToString(), 0); model_goods.kuan = Utils.StrToDecimal(kuan[i].ToString(), 0); model_goods.gao = Utils.StrToDecimal(gao[i].ToString(), 0); model_goods.zhong = Utils.StrToDecimal(zhong[i].ToString(), 0); if (update) { bll_goods.Update(model_goods); idList.Append(model_goods.id + ","); } else { int a = bll_goods.Add(model_goods); idList.Append(a + ","); } } } string id_list = Utils.DelLastChar(idList.ToString(), ","); if (!string.IsNullOrEmpty(id_list)) { Tea.DBUtility.DbHelperSQL.ExecuteSql("delete from shop_goods where parent_id=" + model.id + " and id not in(select id from shop_goods where id in(" + id_list + "))"); } #endregion if (bll.Update(model)) { AddAdminLog(TWEnums.ActionEnum.Edit.ToString(), "修改" + this.channel_name + "頻道內容:" + model.title); //記錄日誌 result = true; } return(result); }