zoukankan      html  css  js  c++  java
  • 后台

    后台

    using CommonInterface;
    using MiniSite.EF;
    using System.IO;
    using MiniSite.Web.Areas.Admin.Controllers;
    using MiniSite.Web.Areas.Pos.Models;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    using System.Drawing;
    using System.Globalization;
    
    namespace MiniSite.Web.Areas.Pos.Controllers
    {
        /// <summary>
        /// Banner图片管理
        /// </summary>
        public class BannerController : BaseController
        {
            /// <summary>
            /// 初期处理
            /// </summary>
            /// <returns></returns>
            public ActionResult Index()
            {
                ViewData["pos_city"] = GetPosCityInfos();
                ViewData["pos_brand"] = GetPosBrandInfos();
                ViewData["pos_store"] = GetPosStoreInfos();
                return View();
            }
    
            /// <summary>
            /// 取得Banner情报
            /// </summary>
            /// <param name="cityId">城市ID</param>
            /// <param name="brandId">品牌ID</param>
            /// <param name="storeId">门店ID</param>
            /// <param name="isInvalid">Banner是否失效(0:有效,1:失效,空所有)</param>
            /// <returns></returns>
            public ActionResult GetBannerInfo(string cityId, string brandId, string storeId, string isInvalid)
            {
                // 取得Banner图片信息
                var results = from ban in DB.pos_banner
                              where ban.status == 1 
                              orderby ban.sort ascending
                              select new
                              {
                                  ban.id,
                                  ban.brand_explain,
                                  ban.img_x,
                                  ban.img_s,
                                  ban.remark1,
                                  ban.remark2,
                                  ban.sort,
                                  validity_begin_dt = ban.validity_begin,
                                  validity_end_dt = ban.validity_end,
                                  validity_begin = ((DateTime?)ban.validity_begin).ToString().Substring(0, 10),
                                  validity_end = ((DateTime?)ban.validity_end).ToString().Substring(0, 10)
                              };
                if (string.IsNullOrEmpty(isInvalid) == false)
                {
                    var systemNow = DateTime.Now;
                    if ("0".Equals(isInvalid))
                    {
                        // 有效
                        results = results.Where(p => p.validity_begin_dt <= systemNow && systemNow <= p.validity_end_dt);
                    }
                    else
                    {
                        // 失效
                        results = results.Where(p => systemNow < p.validity_begin_dt || p.validity_end_dt < systemNow);
                    }
                }
    
                var returnInfo = new List<object>();
                foreach (var item in results)
                {
                    var tmpResult = from storeban in DB.pos_store_banner
                              join store in DB.pos_store on storeban.store_id equals store.id
                              join city in DB.pos_city on store.city_id equals city.id
                              join brand in DB.pos_brand on store.brand_id equals brand.id
                              where storeban.status == 1 && storeban.banner_id == item.id && store.status == 1
                              select new {
                                  storeban.store_id,
                                  store.city_id,
                                  store.brand_id,
                                  brand_name = brand.name,
                                  cityname = city.city_name,
                                  store_name = store.store_name
                              };
    
                    if (string.IsNullOrEmpty(cityId) == false)
                    {
                        var iCityId = Convert.ToInt32(cityId);
                        tmpResult = tmpResult.Where(p => p.city_id == iCityId);
                    }
                    if (string.IsNullOrEmpty(brandId) == false)
                    {
                        var iBrandId = Convert.ToInt32(brandId);
                        tmpResult = tmpResult.Where(p => p.brand_id == iBrandId);
                    }
                    if (string.IsNullOrEmpty(storeId) == false)
                    {
                        var iStoreId = Convert.ToInt32(storeId);
                        tmpResult = tmpResult.Where(p => p.store_id == iStoreId);
                    }
                    var tmpStoreList = tmpResult.ToArray();
                    if (tmpStoreList.Count() > 0)
                    {
                        var tmpStoreName = tmpStoreList.Select(p => p.store_name).Distinct().ToArray();
    
                        returnInfo.Add(new {
                            item.id,
                            item.brand_explain,
                            item.img_x,
                            item.img_s,
                            item.remark1,
                            item.remark2,
                            item.sort,
                            item.validity_begin,
                            item.validity_end,
                            store_info = tmpStoreList
                            //store_name = String.Join(",", tmpStoreName)
                        });
                    }
                }
    
                return Json(returnInfo, JsonRequestBehavior.AllowGet);
            }
    
            /// <summary>
            /// 根据城市ID和品牌ID取得门店列表
            /// </summary>
            /// <param name="cityId"></param>
            /// <param name="brandId"></param>
            /// <returns></returns>
            public ActionResult GetStoreInfo(string cityId, string brandId) {
                var storeInfo = DB.pos_store.Where(p => p.status == 1);
                if (string.IsNullOrEmpty(cityId) == false)
                {
                    var iCityId = Convert.ToInt32(cityId);
                    storeInfo = storeInfo.Where(p => p.city_id == iCityId);
                }
                if (string.IsNullOrEmpty(brandId) == false)
                {
                    var iBrandId = Convert.ToInt32(brandId);
                    storeInfo = storeInfo.Where(p => p.brand_id == iBrandId);
                }
                var result = storeInfo.Select(p => new SelectListItem { Value = p.id.ToString(), Text = p.store_code + ":" + p.store_name }).ToArray();
                return Json(result, JsonRequestBehavior.AllowGet);
            }
    
            /// <summary>
            /// 保存Banner信息
            /// </summary>
            /// <param name="bannerInfo"></param>
            /// <returns></returns>
            public ActionResult saveBannerInfo(BannerInfoModel bannerInfo)
            {
                var returnInfo = new { retCode = 0, retMsg = "Banner信息更新成功" };
                using (var tran = DB.Database.BeginTransaction())
                {
                    try
                    {
                        // 添加更新Banner情报
                        var posBannerInfo = new pos_banner();
                        if (string.IsNullOrEmpty(bannerInfo.banner_id) == false)
                        {
                            // 更新的时候
                            var iBannerId = Convert.ToInt32(bannerInfo.banner_id);
                            posBannerInfo = DB.pos_banner.Where(p => p.id == iBannerId).FirstOrDefault();
                        }
                        posBannerInfo.brand_explain = bannerInfo.brand_explain;
                        posBannerInfo.img_x = bannerInfo.img_x;
                        posBannerInfo.img_s = bannerInfo.img_s;
                        posBannerInfo.validity_begin = Convert.ToDateTime(bannerInfo.validity_begin);
                        posBannerInfo.validity_end = Convert.ToDateTime(bannerInfo.validity_end);
                        posBannerInfo.status = 1;
                        posBannerInfo.update_date = DateTime.Now;
    
                        if (string.IsNullOrEmpty(bannerInfo.banner_id))
                        {
                            // 新规添加
                            // 最大排序
                            posBannerInfo.sort = DB.pos_banner.Max(p => p.sort) + 1;
                            posBannerInfo.create_date = DateTime.Now;
                            DB.pos_banner.Add(posBannerInfo);
                        }
                        DB.SaveChanges();
    
                        // 添加门店所属的Banner情报
                        var stroreBannerInfos = DB.pos_store_banner.Where(p => p.banner_id == posBannerInfo.id).ToList();
                        DB.pos_store_banner.RemoveRange(stroreBannerInfos);
    
                        foreach (var item in bannerInfo.store_id_list)
                        {
                            var storeId = Convert.ToInt32(item);
                            var storeBanner = new pos_store_banner();
                            storeBanner.banner_id = posBannerInfo.id;
                            storeBanner.store_id = storeId;
                            storeBanner.status = 1;
                            storeBanner.create_date = DateTime.Now;
                            storeBanner.update_date = DateTime.Now;
                            DB.pos_store_banner.Add(storeBanner);
                        }
                        DB.SaveChanges();
                        tran.Commit();
                    }
                    catch (Exception ex)
                    {
                        returnInfo = new { retCode = 1, retMsg = "Banner信息更新失败" };
                        Logger.LogException("保存出错", "saveBannerInfo", "BannerController", ex);
                        tran.Rollback();
                    }
                }
                return Json(returnInfo);
            }
    
            /// <summary>
            /// 根据ID取得Banner信息
            /// </summary>
            /// <param name="bannerId"></param>
            /// <returns></returns>
            [HttpGet]
            public ActionResult GetBannerInfoById(string bannerId) {
                var returnInfo = new { retCode = 0, data = new object(), retMsg = "Banner信息取得成功" };
                if (string.IsNullOrEmpty(bannerId))
                {
                    returnInfo = new { retCode = -1, data = new object(), retMsg = "Banner信息取得失败" };
                }
                else
                {
                    var iBannerId = Convert.ToInt32(bannerId);
                    var bannerInfo = DB.pos_banner.Where(p => p.id == iBannerId).FirstOrDefault();
                    if (bannerInfo == null)
                    {
                        returnInfo = new { retCode = -1, data = new object(), retMsg = "指定的Banner ID的信息不存在" };
                    }
                    else
                    {
                        var bannerStore = from storeBanner in DB.pos_store_banner
                                          join store in DB.pos_store on new { storeBanner.store_id, status = 1 } equals new { store_id = store.id, status = store.status }
                                          where storeBanner.status == 1 && storeBanner.banner_id == bannerInfo.id
                                          select new
                                          {
                                              storeBanner.store_id,
                                              store_name = store.store_code + ":" + store.store_name
                                          };
                        var returnData = new
                        {
                            banner_id = bannerInfo.id.ToString(),
                            banner_explain = bannerInfo.brand_explain,
                            img_x = bannerInfo.img_x,
                            img_s = bannerInfo.img_s,
                            validity_begin = ((DateTime?)bannerInfo.validity_begin).ToString().Substring(0, 10),
                            validity_end = ((DateTime?)bannerInfo.validity_end).ToString().Substring(0, 10),
                            store_id_list = bannerStore.ToList()
                        };
                        return Json(new
                        {
                            retCode = 0,
                            retMsg = "Banner信息取得成功",
                            data = returnData
                        }, JsonRequestBehavior.AllowGet);
                    }
                }
    
                return Json(returnInfo, JsonRequestBehavior.AllowGet);
            }
    
            /// <summary>
            /// 删除Banner图片
            /// </summary>
            /// <param name="bannerIds"></param>
            /// <returns></returns>
            public ActionResult DeleteBannerInfo(List<string> bannerIds) {
                var returnInfo = new { retCode = 0, retMsg = "Banner图片删除成功" };
                using (var tran = DB.Database.BeginTransaction())
                {
                    try
                    {
                        foreach (var bannerid in bannerIds)
                        {
                            var id = Convert.ToInt32(bannerid);
                            var posbanner = DB.pos_banner.Where(m => m.id == id).FirstOrDefault();
                            if (posbanner == null)
                            {
                                throw new Exception("没找到指定的ID数据");
                            }
                            else
                            {
                                DB.pos_banner.Remove(posbanner);
                            }
                        }
                        //保存数据
                        DB.SaveChanges();
                        tran.Commit();
                    }
                    catch (Exception ex)
                    {
                        returnInfo = new { retCode = 99, retMsg = "Banner图片删除失败" };
                        Logger.LogException("删除Banner图片出错", "", "", ex);
                        tran.Rollback();
                    }
                }
                return Json(returnInfo);
            }
    
            #region 上传图片功能
    
            /// <summary>
            /// 上传图片的路径
            /// </summary>
            private string UploadImgPath = "/assets/files/banner/";
    
            /// <summary>
            /// 上传图片处理
            /// </summary>
            /// <returns></returns>
            [HttpPost]
            public ActionResult UploadPic()
            {
                var resultInfo = new UploadImgResultModel();
                try
                {
                    resultInfo.files = new List<UpFileInfo>();
                    resultInfo.initialPreview = new List<string>();
                    Response.ContentType = "text/plain";
                    // 相对路径
                    // FileRelativePath = System.Configuration.ConfigurationManager.AppSettings["UploadProductImgPath"];
                    //FileRelativePath = "../../assets/files/banner/";
                    // 绝对路径
                    var uploadFilePath = System.Web.HttpContext.Current.Server.MapPath(UploadImgPath);
                    if (!Directory.Exists(uploadFilePath))
                    {
                        Directory.CreateDirectory(uploadFilePath);
                    }
    
                    HttpFileCollectionBase files = (HttpFileCollectionBase)Request.Files;
                    for (int i = 0; i < files.Count; i++)
                    {
                        HttpPostedFileBase file = files[i];
                        // 检查是否是图片
                        string checkResult = CheckImage(file);
                        // 检查结果判断
                        if (string.IsNullOrEmpty(checkResult))
                        {
                            // 上传图片
                            var imgInfo = UploadImage(file, uploadFilePath);
                            resultInfo.files.Add(imgInfo);
                            resultInfo.initialPreview.Add(imgInfo.fileurl);
                        }
                        else
                        {
                            throw new Exception("上传的图片格式不正确");
                        }
                    }
                }
                catch (Exception ex)
                {
                    resultInfo.code = 2;
                    resultInfo.msg = ex.Message;
                }
                return Json(resultInfo, JsonRequestBehavior.AllowGet);
            }
    
            /// <summary>
            /// 检查是否为合法的上传图片
            /// </summary>
            /// <param name="_fileExt"></param>
            /// <returns></returns>
            private string CheckImage(HttpPostedFileBase imgfile)
            {
                string allowExt = ".gif.jpg.png";
                string fileName = imgfile.FileName;
                FileInfo file = new FileInfo(fileName);
                string imgExt = file.Extension;
                Image img = IsImage(imgfile);
                string errorMsg = fileName + "";
                if (img == null)
                {
                    errorMsg = "文件格式错误,请上传gif、jpg、png格式的图片";
                    return errorMsg;
                }
                if (allowExt.IndexOf(imgExt.ToLower()) == -1)
                {
                    errorMsg = "请上传gif、jpg、png格式的图片;";
                }
                //if (imgfile.ContentLength > 512 * 1024)
                //{
                //    errorMsg += "图片最大限制为0.5Mb;";
                //}
                //if (img.Width < 20 || img.Width > 480 || img.Height < 20 || img.Height > 854)
                //{
                //    errorMsg += "请上传正确尺寸的图片,图片最小为20x20,最大为480*854。";
                //}
                if (errorMsg == fileName + "")
                {
                    return "";
                }
                return errorMsg;
    
            }
    
            /// <summary>
            /// 验证是否为真正的图片
            /// </summary>
            /// <param name="file"></param>
            /// <returns></returns>
            private Image IsImage(HttpPostedFileBase file)
            {
                try
                {
                    Image img = Image.FromStream(file.InputStream);
                    return img;
                }
                catch
                {
                    return null;
                }
            }
    
            /// <summary>
            /// 上传图片
            /// </summary>
            /// <param name="file"></param>
            /// <returns></returns>
            private UpFileInfo UploadImage(HttpPostedFileBase file, string uploadFilePath)
            {
                string fileNameExt = (new FileInfo(file.FileName)).Extension;
    
                // 获得要保存的文件路径
                string newFileName = NewFileName(fileNameExt);
                // 设置保存路径
                string ymd = DateTime.Now.ToString("yyyyMMdd", DateTimeFormatInfo.InvariantInfo);
                uploadFilePath += ymd + "/";
    
                // 保存图片
                string fileFullPath = uploadFilePath + newFileName;
                // 保存图片路径不存在时,新建路径
                if (!Directory.Exists(uploadFilePath))
                    Directory.CreateDirectory(uploadFilePath);
                file.SaveAs(fileFullPath);
    
                UpFileInfo upFileInfo = new UpFileInfo();
                upFileInfo.filename = newFileName;
                //upFileInfo.filepath = FilePhysicalPath + newFileName;
                upFileInfo.fileurl = Request.Url.Scheme + "://" + Request.Url.Authority + UploadImgPath + ymd + "/" + newFileName;
    
                return upFileInfo;
            }
    
            /// <summary>
            /// 从新取得图片名称
            /// </summary>
            /// <param name="fileNameExt"></param>
            /// <returns></returns>
            private string NewFileName(string fileNameExt)
            {
                //return DateTime.Now.ToString("yyyyMMddHHmmss_ffff", DateTimeFormatInfo.InvariantInfo) + fileNameExt;
                return Guid.NewGuid().ToString() + fileNameExt;
            }
            #endregion
    
    
        }
    }
  • 相关阅读:
    私有程序集的探测过程
    程序集版本控制
    浅谈对对象clone的理解
    [导入]WCF后传系列(3):深入WCF寻址Part 3—消息过滤引擎
    [导入]WCF后传系列(5):深入WCF寻址Part 5—逻辑地址和物理地址
    强名称程序集
    [导入]WCF后传系列(4):深入WCF寻址Part 4—自定义消息筛选器
    绑定过程小结
    概述
    UpdatePanel 的更新与触发环境
  • 原文地址:https://www.cnblogs.com/qiqiqiqiqia/p/11814406.html
Copyright © 2011-2022 走看看