using Infrastructure; using Infrastructure.Attribute; using Infrastructure.Enums; using Infrastructure.Model; using Mapster; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using ARW.Admin.WebApi.Extensions; using ARW.Admin.WebApi.Filters; using ARW.Common; using ARW.Model.Dto.Business.LogisticsManage.Deliverys; using ARW.Service.Business.IBusinessService.LogisticsManage.Deliverys; using ARW.Admin.WebApi.Controllers; using ARW.Model.Models.Business.LogisticsManage.Deliverys; using ARW.Model.Vo.Business.LogisticsManage.Deliverys; using Microsoft.AspNetCore.Authorization; using ARW.Admin.WebApi.Framework; using ARW.Service.Business.IBusinessService.LogisticsManage.DeliveryRules; using ARW.Service.Business.IBusinessService.ShopManager.Shops; namespace ARW.WebApi.Controllers.Business.LogisticsManage.Deliverys { /// /// 配送模板控制器 /// /// @author lwh /// @date 2023-06-16 /// [Verify] [Route("business/[controller]")] public class DeliveryController : BaseController { private readonly IDeliveryService _DeliveryService; private readonly IDeliveryRuleService _DeliveryRuleService; private readonly IShopService _ShopService; /// /// 依赖注入 /// /// 配送模板服务 public DeliveryController(IDeliveryService DeliveryService, IDeliveryRuleService deliveryRuleService, IShopService shopService) { _DeliveryService = DeliveryService; _DeliveryRuleService = deliveryRuleService; _ShopService = shopService; } /// /// 获取配送模板列表 /// /// 查询参数 /// [HttpGet("getDeliveryList")] [ActionPermissionFilter(Permission = "business:delivery:list")] public async Task GetDeliveryList([FromQuery] DeliveryQueryDto parm) { var user = JwtUtil.GetLoginUser(App.HttpContext); if (user.UserId != 1) { var shop = await _ShopService.GetFirstAsync(s => s.ShopUserId == user.UserId); if (shop == null) throw new Exception("当前用户没有店铺"); parm.ShopGuid = shop.ShopGuid; } var res = await _DeliveryService.GetDeliveryList(parm); return SUCCESS(res); } /// /// 获取配送模板的配送区域和运费列表 /// /// 查询参数 /// [HttpGet("getDeliveryRuleList")] public async Task GetDeliveryRuleList([FromQuery] DeliveryRuleEditDto parm) { var res = await _DeliveryRuleService.GetDeliveryRuleList(parm.DeliveryGuid); return SUCCESS(res); } /// /// 添加或修改配送模板 /// /// /// [HttpPost("addOrUpdateDelivery")] [ActionPermissionFilter(Permission = "business:delivery:addOrUpdate")] [Log(Title = "添加或修改配送模板", BusinessType = BusinessType.ADDORUPDATE)] public async Task AddOrUpdateDelivery([FromBody] DeliveryDto parm) { if (parm == null) { throw new CustomException("请求参数错误"); } var modal = new Delivery(); if (parm.DeliveryId != 0) modal = parm.Adapt().ToUpdate(HttpContext); else modal = parm.Adapt().ToCreate(HttpContext); var user = JwtUtil.GetLoginUser(App.HttpContext); if (user.UserId != 1) { var shop = await _ShopService.GetFirstAsync(s => s.ShopUserId == user.UserId); if (shop == null) throw new Exception("当前用户没有店铺"); modal.ShopGuid = shop.ShopGuid; } var res = await _DeliveryService.AddOrUpdateDelivery(modal); return SUCCESS(res); } /// /// 删除配送模板 /// /// [HttpDelete("{ids}")] [ActionPermissionFilter(Permission = "business:delivery:delete")] [Log(Title = "配送模板删除", BusinessType = BusinessType.DELETE)] public IActionResult Delete(string ids) { long[] idsArr = Tools.SpitLongArrary(ids); if (idsArr.Length <= 0) { return ToResponse(ApiResult.Error($"删除失败Id 不能为空")); } var response = _DeliveryService.Delete(idsArr); return SUCCESS("删除成功!"); } /// /// 导出配送模板 /// /// [Log(Title = "配送模板导出", BusinessType = BusinessType.EXPORT, IsSaveResponseData = false)] [HttpGet("exportDelivery")] [ActionPermissionFilter(Permission = "business:delivery:export")] public async Task ExportExcel([FromQuery] DeliveryQueryDto parm) { parm.PageSize = 10000; var list = await _DeliveryService.GetDeliveryList(parm); var data = list.Result; // 选中数据 if (!string.IsNullOrEmpty(parm.ids)) { int[] idsArr = Tools.SpitIntArrary(parm.ids); var selectDataList = new List(); foreach (var item in idsArr) { var select_data = data.Where(s => s.DeliveryId == item).First(); selectDataList.Add(select_data); } data = selectDataList; } // 导出数据处理 var handleData = await _DeliveryService.HandleExportData(data); string sFileName = ExportExcel(handleData, "Delivery", "配送模板列表"); return SUCCESS(new { path = "/export/" + sFileName, fileName = sFileName }); } } }