using Microsoft.AspNetCore.Mvc; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Ys.Core.Common; using djy.Paas.IService; using djy.Paas.Model; using Microsoft.AspNetCore.Authorization; using NPOI.HSSF.UserModel; using NPOI.SS.UserModel; using Hangfire; using System.IO; using Microsoft.AspNetCore.Hosting; using NPOI.SS.Util; namespace djyweb_djyPaasApi.Controllers { /// ///外部结合跳转控制 /// public class BaoguanController : ApiBase { IBaoguanService _server = IOC.AddServer(); //货主端 IBaoguanOrderClientService _cservice = IOC.AddServer(); private string _moduleKey = "baoguanorder"; #region 货主 /// /// 货主报关单查询 /// /// /// /// [HttpGet("client")] public object ClientList([FromQuery] BaoguanOrderClientDto Dto, [FromQuery] ApiFromDto apiFrom) { var aut = GetUserAuthorityToFormDto(_moduleKey); if (!aut.IsPower) { return new ReturnResult() { Message = "没有权限" }; } Dto.CompGId = aut.CompayId; Dto.UserGid = aut.UserId; return _cservice.GetList(Dto, apiFrom); } /// /// 货主端创建 /// /// /// [HttpPost("client")] public object Clientorderadd([FromBody] tb_BaoguanOrderClient Dto) { var rs = new ReturnResult(); Dto.UserGid = GetLoginId; Dto.CompGId = Guid.Parse(GetUserInfo().CompId); if (Dto.ShipOrderNo.IsNull() || Dto.IEFlag.IsNull() || Dto.BaoguanAgent.IsNull()) { rs.Not("进出口类型 提单号 报关行不允许为空!"); return rs; } if (Dto.FileUrl.IsNull()) { rs.Not("请上传资料文件"); return rs; } var list = _cservice.GetList(new BaoguanOrderClientDto { ShipOrderNo = Dto.ShipOrderNo, CompGId = Dto.CompGId }, new ApiFromDto()); if (list.Count > 0) { rs.Not("此提单号已经提交过了"); return rs; } return _cservice.Add(Dto); } /// /// 更新 货主端海关单 /// /// /// [HttpPost("client/up")] public object ClientOrderUp([FromBody] tb_BaoguanOrderClient Dto) { if (Dto == null) { return new ReturnResult() { Status = false, Message = "非法请求!", Code = 400 }; } if (!_GetData_authority(new Guid?[] { Dto.Gid })) { return new ReturnResult() { Status = false, Message = "没有操作权限!", Code = 400 }; } return _cservice.Up(Dto); } /// ///删除 货主订单 /// /// /// [HttpPost("client/del")] public object ClientOrderDel([FromBody] Guid?[] gidlist) { if (gidlist == null || gidlist.Length == 0) { return new ReturnResult() { Status = false, Message = "没有要删除的数据!", Code = 400 }; } if (!_GetData_authority(gidlist)) { return new ReturnResult() { Status = false, Message = "没有操作权限!", Code = 400 }; } return _cservice.Del(gidlist); } /// /// 执行接收阿里报关数据并解析报关数据 /// /// [HttpPost("baoguanjob")] public async Task BaoguanJob() { return await _server.AutoAllibonBaoguanJob(new ApiFromDto { UserId = GetLoginId.ToString(), Name = GetLoginName }); } /// /// 判断数是否有操作更新权限 /// /// /// private bool _GetData_authority(Guid?[] gidlist) { //权限方式查询可删除数据 var Dto = new BaoguanOrderClientDto(); var aut = GetUserAuthorityToFormDto(_moduleKey, 1); Dto.UserGid = aut.UserId; Dto.CompGId = aut.CompayId; Dto.Gidlist = gidlist.ToList(); var getdata = _cservice.GetList(Dto, new ApiFromDto()); if (getdata.Data == null || getdata.Data.Count == 0) { return false; } else { return true; } } #endregion /// /// 绑定客户关系 /// /// /// [HttpPost("bindclient")] public async Task BindClient([FromBody] ClientLinkInfoDto Dto) => await _server.BindClient(Dto); /// /// 指定order 文件Email发送 Gid filename /// /// /// [HttpPost("sendmail")] public object SendMail([FromBody] ApiFromDto Dto) => _server.EmailSend(Dto); /// /// 报关单查询 /// /// /// /// [HttpGet] public async Task GetBaoguanOrderList([FromQuery] tb_BaoGuanOrderList Dto, [FromQuery] ApiFromDto apiFrom) { var aut = GetUserAuthorityToFormDto(_moduleKey); if (!aut.IsPower) { return new ReturnResult() { Message = "没有权限" }; } Dto.CompId = aut.CompayId; return await _server.GetBaoguanOrderList(Dto, apiFrom); } /// /// 更新指定订单数据 /// /// /// [HttpPost] public object BaoguanOrderUp([FromBody] tb_BaoGuanOrderList Dto) => _server.Up(Dto, Notcolumns: new List { nameof(tb_BaoGuanOrderList.FileInfo), nameof(tb_BaoGuanOrderList.FileList) }); /// /// 报关校验审核保存 /// /// /// [AllowAnonymous] [HttpPost("ordercheck")] public async Task OrderCheck([FromBody] tb_BaoGuanOrderList Dto) => await _server.OrderCheck(Dto); /// ///获取单条报关单数据 /// /// /// [AllowAnonymous] [HttpGet("{Gid}")] public async Task GetOrderDto(Guid? Gid) { var Rs = new ReturnResult(); var Dto = new tb_BaoGuanOrderList(); Dto.Gid = Gid; var getlist = await _server.GetBaoguanOrderList(Dto, new ApiFromDto()); if (getlist.Status && getlist.Data.Count > 0) { Rs.Data = getlist.Data[0]; Rs.OK(); } return Rs; } ///// /////获取阿里云报关跳转token ///// ///// //[HttpGet("gettoken")] //public async Task GetBaoguanToken() //{ // var rs = await _server.AliyunBaoguanToken(new djy.Model.User { GID = GetLoginId.ToString() }); // if (rs.Status) // {//直接跳转到报关预录 // rs.MemoData = @"https://onetouch-partner.alibaba.com/ptnProvider/customs/thirdParty.htm?token={" + rs.Data + "}&platform=DaJianYun&targetUrl=%2F%2Fonetouch.alibaba.com%2FsaasCustomer%2Flist.htm%3Fplatform%3DDaJianYun"; // } // return rs; //} ///// ///// 接收阿里推送的绑定反馈 ///// ///// ///// //[AllowAnonymous] //[HttpPost("aliback")] //public async Task Aliback([FromBody] ApiFromDto Dto) => await _server.AliBack(Dto); ///// ///// 报关用户离职 [key=usergid base64位加密字符串] ///// ///// ///// //[AllowAnonymous] //[HttpPost("userquit")] //public async Task UserQuit([FromBody] ApiFromDto Dto) //{ // //2022-4-1,配合大简云主程序员工离职流程调整,特修改:开发测试环境不调用阿里离职 // if (sysOptionConfig.YsWebconfig.IsDev) // { // var rs = new ReturnResult(); // rs.OK(); // return rs; // } // _LogsAdd("userquit", "api", Dto); // var userid = Dto.Key; // if (userid.IsNotNull()) // { // return await _server.AliUserQuit(userid); // } // else // { // return new ReturnResult() { Message = "错误的请求" }; // } //} /// /// 根据箱子编号获取箱内详细报关数据 Code 多个,间隔 /// /// /// [AllowAnonymous] [HttpGet("open/container")] public async Task GetContainerList([FromQuery] ApiFromDto Dto) { return await _server.GetContainerList(Dto.Code.Split(',').ToList()); } //导出EXCEL [HttpGet("export")] public object Export([FromQuery] tb_BaoGuanOrderList Dto, [FromQuery] ApiFromDto apiFrom) { var aut = GetUserAuthorityToFormDto(_moduleKey); if (!aut.IsPower) { return new ReturnResult() { Message = "没有权限" }; } _LogsAdd("export", "api", "执行导出"); apiFrom.Limit = int.MaxValue; Dto.CompId = aut.CompayId; var bgOrdList = _server.GetBaoguanOrderList(Dto, apiFrom).Result; _LogsAdd("export", "api", $"获得结果{bgOrdList.Count}条"); var rs = new ReturnResult(); try { var excelwork = new HSSFWorkbook(); var sheet = excelwork.CreateSheet("报关订单"); sheet.ForceFormulaRecalculation = true; //单元格样式 var cellStyle = NpoiExcelExportHelper._.CreateStyle(excelwork, HorizontalAlignment.Center, VerticalAlignment.Center, 10, true, 0); //行高 var cellheigth = 20; //表头名称 var headerName = new[] { new{title="客户名称",width=20 }, new{title="联系人",width=15 }, new{title="EMAIL",width=40 }, new{title="报关行名称",width=30 }, new{title="进口/出口",width=10 }, new{title="报关订单号",width=23 }, new{title="报关单号",width=20 }, new{title="申报地海关",width=10 }, new{title="境内收发",width=40 }, new{title="提单号",width=20 }, new{title="查验时间",width=22 }, new{title="主单号",width=22 }, new{title="申报状态",width=15 }, new{title="创建时间",width=22 } }; var row = NpoiExcelExportHelper._.CreateRow(sheet, 0, cellheigth); for (int i = 0; i < headerName.Length; i++) { var cell = NpoiExcelExportHelper._.CreateCells(row, cellStyle, i, headerName[i].title); sheet.SetColumnWidth(cell.ColumnIndex, headerName[i].width * 256); } var columnwidth = 20 * 256; for (int i = 0; i < bgOrdList.Data.Count; i++) { row = NpoiExcelExportHelper._.CreateRow(sheet, i + 1, cellheigth); var item = bgOrdList.Data[i]; var cell = NpoiExcelExportHelper._.CreateCells(row, cellStyle, 0, item.ClientName); cell = NpoiExcelExportHelper._.CreateCells(row, cellStyle, 1, item.ClientMain); cell = NpoiExcelExportHelper._.CreateCells(row, cellStyle, 2, item.ClientEmail); cell = NpoiExcelExportHelper._.CreateCells(row, cellStyle, 3, item.CompName); cell = NpoiExcelExportHelper._.CreateCells(row, cellStyle, 4, item.IEFlag == "I" ? "进口" : item.IEFlag == "E" ? "出口" : ""); cell = NpoiExcelExportHelper._.CreateCells(row, cellStyle, 5, item.UniqueNumber); cell = NpoiExcelExportHelper._.CreateCells(row, cellStyle, 6, item.CustomsNo); cell = NpoiExcelExportHelper._.CreateCells(row, cellStyle, 7, item.DeclareCustoms); cell = NpoiExcelExportHelper._.CreateCells(row, cellStyle, 8, item.TradeName); cell = NpoiExcelExportHelper._.CreateCells(row, cellStyle, 9, item.ShipOrderNo); cell = NpoiExcelExportHelper._.CreateCells(row, cellStyle, 10, item.CheckTime.ToDataTime().ToString("yyyy-MM-dd HH:mm:ss")); cell = NpoiExcelExportHelper._.CreateCells(row, cellStyle, 11, item.ShipOrderNoMain); cell = NpoiExcelExportHelper._.CreateCells(row, cellStyle, 12, item.stateName); cell = NpoiExcelExportHelper._.CreateCells(row, cellStyle, 13, item.AddTime.ToDataTime().ToString("yyyy-MM-dd HH:mm:ss")); } var _environment = IOC.AddServer(); var filepath = _environment.WebRootPath.Trim() + "/excel/"; if (!Directory.Exists(filepath)) { Directory.CreateDirectory(filepath); } var filename = string.Format("{0}.xls", YsTools.GetNewNo()); var filestream = new FileStream(filepath + filename, FileMode.OpenOrCreate, FileAccess.ReadWrite); excelwork.Write(filestream); filestream.Close(); filestream.Dispose(); _LogsAdd("export", "api", $"excel写入完成:{filename}"); rs.OK("生成成功!", filename); //过期删除 BackgroundJob.Schedule(() => System.IO.File.Delete(filepath + filename), TimeSpan.FromMinutes(5)); //直接输出数据流 //var exceldata = excelwork.GetBytes(); //rs.OK(); // return File(exceldata, "application/ms-excel", filename); return rs; } catch (Exception ex) { rs.Not("下载失败"); rs.MemoData = ex.Message; _LogsAdd("error", "api", ex.Message); _LogsAdd("error", "api", ex.StackTrace); } return rs; } } }