using System; using System.Collections.Generic; using System.Configuration; using System.IO; using System.Linq; using System.Text; using System.Web.Mvc; using DSWeb.Areas.CommMng.DAL; using DSWeb.Areas.Dispatch.DB; using DSWeb.Areas.Dispatch.Helper; using DSWeb.Areas.Dispatch.Models; using DSWeb.Dispatch.DAL; using Newtonsoft.Json; using NPOI; using NPOI.HSSF.UserModel; using NPOI.HSSF.Util; using NPOI.SS.UserModel; using NPOI.SS.Util; using NPOI.Util; namespace DSWeb.Areas.Dispatch.Controllers { public class StockController : Controller { private StockDataContext stockDataContext = new StockDataContext(); public ActionResult StockTakeList(string openid) { var infoClient = stockDataContext.DispInfoClients.FirstOrDefault(c => c.OpenId == openid); if (infoClient == null || infoClient.IsAudit == false) { return Redirect(Url.Action("Regist", "Stock", new { openid = openid })); } ViewData["openid"] = openid; return View(); } public ActionResult StockTakeListData(string openid, string condition, int page = 1, int pageSize = 20) { var infoClient = stockDataContext.DispInfoClients.FirstOrDefault(c => c.OpenId == openid); if (infoClient == null) { return Json(new { success = false, message = "非法请求" }, JsonRequestBehavior.AllowGet); } if (!infoClient.IsAudit) { return Json(new { success = false, message = "未审核用户" }, JsonRequestBehavior.AllowGet); } var para = stockDataContext.ParamSets.First(p => p.PARAMNAME == "STOCK_TAKE_CUSTOMS_INFO_CLIENT"); var customsInfoClient = para.PARAMVALUE; //海关用户的infoclient bool isCustoms = infoClient.InfoClient == customsInfoClient; var query = from st in stockDataContext.VStockTakes.AsNoTracking() join c in stockDataContext.InfoClients.AsNoTracking() on st.CUSTOMERNAME equals c.SHORTNAME orderby c.EDICODE select new { st.GOODSNAME, st.hj_GOODSPACKSTOCK, st.WeiWanShui, KeFaHuo = st.GOODSNUM - st.hj_GOODSPACKPFSL, st.hj_GOODSPACK, st.BLNO, st.CONTRACTNO, st.STORAGENAME, st.CUSTOMERNAME }; #region 查询条件 if (!string.IsNullOrEmpty(condition)) { var objCond = JsonConvert.DeserializeAnonymousType(condition, new { blno = "", contractno = "", goodsname = "", stockstatus = "" }); if (!string.IsNullOrEmpty(objCond.blno)) { query = query.Where(st => st.BLNO.IndexOf(objCond.blno) > -1); } if (!string.IsNullOrEmpty(objCond.contractno)) { query = query.Where(st => st.CONTRACTNO.IndexOf(objCond.contractno) > -1); } if (!string.IsNullOrEmpty(objCond.goodsname)) { query = query.Where(st => st.GOODSNAME.IndexOf(objCond.goodsname) > -1); } if (objCond.stockstatus == "1") { query = query.Where(st => st.hj_GOODSPACKSTOCK != 0); } else if (objCond.stockstatus == "2") { query = query.Where(st => st.hj_GOODSPACKSTOCK == 0); } } #endregion //判断用户类型 if (isCustoms) { var canViewList = stockDataContext.SysEnumValues.Where(em => em.EnumTypeID == 92101).Select(em => em.EnumValueName).ToList(); query = query.Where(st => canViewList.Contains(st.STORAGENAME)); } else { query = query.Where(st => st.CUSTOMERNAME == infoClient.InfoClient); } var count = query.Count(); var list = query.Skip((page - 1) * pageSize).Take(pageSize).ToList(); return Json(new { success = true, total = count, page, pageSize, list }, JsonRequestBehavior.AllowGet); } public ActionResult StockTakeExport(string openid, string condition) { var infoClient = stockDataContext.DispInfoClients.FirstOrDefault(c => c.OpenId == openid); if (infoClient == null) { return Content("非法请求"); } if (!infoClient.IsAudit) { return Content("未审核用户"); } var para = stockDataContext.ParamSets.First(p => p.PARAMNAME == "STOCK_TAKE_CUSTOMS_INFO_CLIENT"); var customsInfoClient = para.PARAMVALUE; //海关用户的infoclient bool isCustoms = infoClient.InfoClient == customsInfoClient; var query = from st in stockDataContext.VStockTakes.AsNoTracking() join c in stockDataContext.InfoClients.AsNoTracking() on st.CUSTOMERNAME equals c.SHORTNAME orderby c.EDICODE select new { st.NID, st.GOODSNAME, st.hj_GOODSPACKSTOCK, st.WeiWanShui, KeFaHuo = st.GOODSNUM - st.hj_GOODSPACKPFSL, st.hj_GOODSPACK, st.BLNO, st.CONTRACTNO, st.STORAGENAME, st.CUSTOMERNAME }; #region 查询条件 if (!string.IsNullOrEmpty(condition)) { var objCond = JsonConvert.DeserializeAnonymousType(condition, new { blno = "", contractno = "", goodsname = "", stockstatus = "" }); if (!string.IsNullOrEmpty(objCond.blno)) { query = query.Where(st => st.BLNO.IndexOf(objCond.blno) > -1); } if (!string.IsNullOrEmpty(objCond.contractno)) { query = query.Where(st => st.CONTRACTNO.IndexOf(objCond.contractno) > -1); } if (!string.IsNullOrEmpty(objCond.goodsname)) { query = query.Where(st => st.GOODSNAME.IndexOf(objCond.goodsname) > -1); } if (objCond.stockstatus == "1") { query = query.Where(st => st.hj_GOODSPACKSTOCK != 0); } else if (objCond.stockstatus == "2") { query = query.Where(st => st.hj_GOODSPACKSTOCK == 0); } } #endregion //判断用户类型 if (isCustoms) { var canViewList = stockDataContext.SysEnumValues.Where(em => em.EnumTypeID == 92101).Select(em => em.EnumValueName).ToList(); query = query.Where(st => canViewList.Contains(st.STORAGENAME)); } else { query = query.Where(st => st.CUSTOMERNAME == infoClient.InfoClient); } var list = query.ToList(); var groupList1 = list.Where(x => x.STORAGENAME == "百佳仓库" || x.STORAGENAME == "外包城仓库").ToList(); var groupList2 = list.Where(x => x.STORAGENAME == "指南针仓库" || x.STORAGENAME == "高盛力诺仓库" || x.STORAGENAME == "分类监管--百佳库").ToList(); var groupList3 = list.Where(x => groupList1.Count(c => c.NID == x.NID) == 0 && groupList2.Count(c => c.NID == x.NID) == 0).ToList(); var listArr = new[] { new{ title ="未完税仓库", list =groupList1, color=HSSFColor.LightBlue.Index }, new{ title ="完税仓库", list =groupList2, color=HSSFColor.LightGreen.Index }, new{ title ="其他仓库", list =groupList3, color=HSSFColor.LightOrange.Index } }; #region 导出excel IWorkbook workbook = new HSSFWorkbook(); ISheet sheet = workbook.CreateSheet("库存数据"); IFont fontHead = workbook.CreateFont(); fontHead.FontHeightInPoints = 12; fontHead.Boldweight = (short)FontBoldWeight.Bold; #region headStyle ICellStyle headStyle = workbook.CreateCellStyle(); headStyle.SetFont(fontHead); //- 居中 headStyle.VerticalAlignment = VerticalAlignment.Center; headStyle.Alignment = HorizontalAlignment.Center; #endregion IFont fontData = workbook.CreateFont(); fontData.FontHeightInPoints = 10; fontData.Boldweight = (short)FontBoldWeight.Normal; #region dataStyle ICellStyle dataStyleLeft = workbook.CreateCellStyle(); dataStyleLeft.SetFont(fontData); dataStyleLeft.Alignment = HorizontalAlignment.Left; ICellStyle dataStyleCenter = workbook.CreateCellStyle(); dataStyleCenter.SetFont(fontData); dataStyleCenter.Alignment = HorizontalAlignment.Center; ICellStyle dataStyleRight = workbook.CreateCellStyle(); dataStyleRight.SetFont(fontData); dataStyleRight.Alignment = HorizontalAlignment.Right; #endregion IFont fontCate = workbook.CreateFont(); fontCate.FontHeightInPoints = 14; fontCate.Boldweight = (short)FontBoldWeight.Normal; #region cateStyle ICellStyle cateStyle = workbook.CreateCellStyle(); cateStyle.SetFont(fontCate); cateStyle.Alignment = HorizontalAlignment.Center; cateStyle.VerticalAlignment = VerticalAlignment.Center; cateStyle.Rotation = 255; cateStyle.FillPattern = FillPattern.SolidForeground; //cateStyle.FillForegroundColor = HSSFColor.PINK.index; #endregion dynamic[] headers = new dynamic[] { new { value = "", width = 5, dataAlign="left" }, new { value = "品名", width = 100, dataAlign="left" }, new { value = "提单号", width = 20, dataAlign="center" }, new { value = "入库件数", width = 15, dataAlign="right" }, new { value = "库存件数", width = 15, dataAlign="right" }, new { value = "未完税件数", width = 15, dataAlign="right" }, new { value = "可发货件数", width = 15, dataAlign="right" }, new { value = "合同号", width = 20, dataAlign="center" } }; IRow headerRow = sheet.CreateRow(0); headerRow.Height = 20 * 20; for (int j = 0; j < headers.Length; j++) { ICell cell = headerRow.CreateCell(j); cell.CellStyle = headStyle; cell.SetCellValue(headers[j].value); sheet.SetColumnWidth(j, headers[j].width * 256); } sheet.CreateFreezePane(0, 1); short rowIndex = 1; foreach (var obj in listArr) { var lst = obj.list; short startRow = rowIndex; var fillColor = obj.color; if (lst.Count == 0) { continue; } foreach (var item in lst) { IRow dataRow = sheet.CreateRow(rowIndex); for (int j = 0; j < headers.Length; j++) { ICell cell = dataRow.CreateCell(j); if (j == 0) { ICellStyle cateStyleTmp = workbook.CreateCellStyle(); cateStyleTmp.CloneStyleFrom(cateStyle); cateStyleTmp.FillForegroundColor = fillColor; cell.CellStyle = cateStyleTmp; } else { if (headers[j].dataAlign == "center") { cell.CellStyle = dataStyleCenter; } else if (headers[j].dataAlign == "right") { cell.CellStyle = dataStyleRight; } else { cell.CellStyle = dataStyleLeft; } } } //导出内容 dataRow.GetCell(0).SetCellValue(obj.title); dataRow.GetCell(1).SetCellValue(item.GOODSNAME); dataRow.GetCell(2).SetCellValue(item.BLNO); dataRow.GetCell(3).SetCellValue(item.hj_GOODSPACK.ToString()); dataRow.GetCell(4).SetCellValue(item.hj_GOODSPACKSTOCK.ToString()); dataRow.GetCell(5).SetCellValue(item.WeiWanShui.ToString()); dataRow.GetCell(6).SetCellValue(item.KeFaHuo.ToString()); dataRow.GetCell(7).SetCellValue(item.CONTRACTNO); rowIndex++; } if (rowIndex > startRow) { //HSSFCellRangeAddress region = new HSSFCellRangeAddress(startRow, rowIndex - 1, 0, 0); CellRangeAddress region = new CellRangeAddress(startRow, rowIndex - 1, 0, 0); sheet.AddMergedRegion(region); } } MemoryStream ms = new MemoryStream(); workbook.Write(ms); ms.Flush(); return File(ms.GetBuffer(), "application/excel", "stock.xls"); #endregion } [HttpGet] public ActionResult Regist(string openid, bool agree = false) { DataContext dataContext = new DataContext(); if (!string.IsNullOrWhiteSpace(openid)) { var infoClient = dataContext.DispInfoClients.FirstOrDefault(d => d.OpenId == openid); if (infoClient != null) { var infoClientModel = infoClient.AsDispInfoClientRegistViewModel(); ViewData["infoClient"] = infoClientModel; ViewData["isAudit"] = infoClient.IsAudit; } else if (!agree) { //保存请求的openid BasicDataRefDAL.SaveLog(openid,"","仓储公众号","用户注册"); return RedirectToAction("Declare", new { openid = openid }); } else { ViewData["infoClient"] = new DispInfoClientRegistViewModel(); ViewData["isAudit"] = false; ; } ViewData["openid"] = openid; ViewData["controllerList"] = dataContext.InfoClients.Where(c => c.ISCONTROLLER == true).AsInfoClientTruckViewModelList(); return View(); } else { return Content("非法请求!"); } } [HttpGet] public ActionResult Declare(string openid) { ViewData["openid"] = openid; return View(); } #region 红柿子-青银 接收费用信息接口 public class HSZQY_FEEInfo { public string ac { get; set; } public string uid { get; set; } public string skey { get; set; } public string optype { get; set; } public List mdata { get; set; } public List feedata { get; set; } } public class HSZQY_mdata { public string MBLNO { get; set; } public string CUSTOMERNAME { get; set; } public string OPLBNAME { get; set; } public string GID { get; set; } } public class HSZQY_feedata { public string FEEID { get; set; } public string GID { get; set; } public string ENTERDATE { get; set; } public string FEENAME { get; set; } public string CUSTOMERNAME { get; set; } public int FEETYPE { get; set; } public decimal UNITPRICE { get; set; } public decimal QUANTITY { get; set; } public decimal NOTAXAMOUNT { get; set; } public decimal TAXRATE { get; set; } public decimal TAX { get; set; } public decimal AMOUNT { get; set; } } [HttpPost] public ActionResult StockFee() { try { RespCommon resp = new RespCommon(); StreamReader sr = new StreamReader(Request.InputStream, Encoding.UTF8); var strJson = new StringBuilder(); string line = null; while (((line = sr.ReadLine())) != null) { strJson.Append(line); } var VgmStatus = JsonConvert.DeserializeObject(strJson.ToString()); BasicDataRefDAL.SaveLog(sr, "USERID", "USERNAME", "红柿子接口"); var json4 = JsonConvert.SerializeObject( new { Success = true, Message = "添加成功!" }); return new ContentResult() { Content = json4 }; } catch (Exception ex) { var json4 = JsonConvert.SerializeObject( new { Success = false, Message = ex.Message }); return new ContentResult() { Content = json4 }; } } #endregion } }