using NCC.Common.Core.Manager; using NCC.Common.Enum; using NCC.Common.Extension; using NCC.Common.Filter; using NCC.Dependency; using NCC.DynamicApiController; using NCC.FriendlyException; using NCC.Extend.Interfaces.LqXmzl; using Mapster; using Microsoft.AspNetCore.Mvc; using SqlSugar; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using NCC.Extend.Entitys.lq_xmzl; using NCC.Extend.Entitys.Dto.LqXmzl; using Yitter.IdGenerator; using NCC.Common.Helper; using NCC.JsonSerialization; using NCC.Common.Model.NPOI; using NCC.Common.Configuration; using NCC.DataEncryption; using NCC.ClayObject; namespace NCC.Extend.LqXmzl { /// /// 项目资料服务 /// [ApiDescriptionSettings(Tag = "绿纤项目资料服务", Name = "LqXmzl", Order = 200)] [Route("api/Extend/[controller]")] public class LqXmzlService : ILqXmzlService, IDynamicApiController, ITransient { private readonly ISqlSugarRepository _lqXmzlRepository; private readonly SqlSugarScope _db; private readonly IUserManager _userManager; /// /// 初始化一个类型的新实例 /// public LqXmzlService(ISqlSugarRepository lqXmzlRepository, IUserManager userManager) { _lqXmzlRepository = lqXmzlRepository; _db = _lqXmzlRepository.Context; _userManager = userManager; } #region 获取项目资料 /// /// 获取项目资料 /// /// 参数 /// [HttpGet("{id}")] public async Task GetInfo(string id) { var entity = await _db.Queryable().FirstAsync(p => p.Id == id); var output = entity.Adapt(); return output; } #endregion #region 获取项目资料列表 /// /// 获取项目资料列表 /// /// 请求参数 /// [HttpGet("")] public async Task GetList([FromQuery] LqXmzlListQueryInput input) { var sidx = input.sidx == null ? "id" : input.sidx; var data = await _db.Queryable() .WhereIF(!string.IsNullOrEmpty(input.id), p => p.Id.Contains(input.id)) .WhereIF(!string.IsNullOrEmpty(input.xmbh), p => p.Xmbh.Contains(input.xmbh)) .WhereIF(!string.IsNullOrEmpty(input.xmmc), p => p.Xmmc.Contains(input.xmmc)) .WhereIF(!string.IsNullOrEmpty(input.bzjg), p => p.Bzjg.Equals(input.bzjg)) .WhereIF(!string.IsNullOrEmpty(input.xmsc), p => p.Xmsc.Equals(input.xmsc)) .WhereIF(!string.IsNullOrEmpty(input.jcfwtc), p => p.Jcfwtc.Equals(input.jcfwtc)) .WhereIF(!string.IsNullOrEmpty(input.fl1), p => p.Fl1.Contains(input.fl1)) .WhereIF(!string.IsNullOrEmpty(input.fl2), p => p.Fl2.Contains(input.fl2)) .WhereIF(!string.IsNullOrEmpty(input.fl3), p => p.Fl3.Contains(input.fl3)) .WhereIF(!string.IsNullOrEmpty(input.fl4), p => p.Fl4.Contains(input.fl4)) .WhereIF(!string.IsNullOrEmpty(input.fl), p => p.Fl.Contains(input.fl)) .WhereIF(!string.IsNullOrEmpty(input.qt1), p => p.Qt1.Contains(input.qt1)) .WhereIF(!string.IsNullOrEmpty(input.qt2), p => p.Qt2.Contains(input.qt2)) .WhereIF(!string.IsNullOrEmpty(input.sgf), p => p.Sgf.Equals(input.sgf)) .WhereIF(!string.IsNullOrEmpty(input.beautyType), p => p.BeautyType.Contains(input.beautyType)) .WhereIF(input.isEffective != 0, p => p.IsEffective.Equals(input.isEffective)) .WhereIF(!string.IsNullOrEmpty(input.sourceType), p => p.SourceType.Contains(input.sourceType)) .Select(it => new LqXmzlListOutput { id = it.Id, xmbh = it.Xmbh, xmmc = it.Xmmc, bzjg = it.Bzjg, xmsc = it.Xmsc, jcfwtc = it.Jcfwtc, fl1 = it.Fl1, fl2 = it.Fl2, fl3 = it.Fl3, fl4 = it.Fl4, fl = it.Fl, qt1 = it.Qt1, qt2 = it.Qt2, sgf = it.Sgf, beautyType = it.BeautyType, sourceType = it.SourceType, isEffective = it.IsEffective, }).MergeTable().OrderBy(sidx + " " + input.sort).ToPagedListAsync(input.currentPage, input.pageSize); return PageResult.SqlSugarPageResult(data); } #endregion #region 新建项目资料 /// /// 新建项目资料 /// /// 参数 /// [HttpPost("")] public async Task Create([FromBody] LqXmzlCrInput input) { var userInfo = await _userManager.GetUserInfo(); var entity = input.Adapt(); entity.Id = YitIdHelper.NextId().ToString(); var isOk = await _db.Insertable(entity).IgnoreColumns(ignoreNullColumn: true).ExecuteCommandAsync(); if (!(isOk > 0)) throw NCCException.Oh(ErrorCode.COM1000); } #endregion #region 获取项目资料无分页列表 /// /// 获取项目资料无分页列表 /// /// 请求参数 /// [NonAction] public async Task GetNoPagingList([FromQuery] LqXmzlListQueryInput input) { var sidx = input.sidx == null ? "id" : input.sidx; var data = await _db.Queryable() .WhereIF(!string.IsNullOrEmpty(input.id), p => p.Id.Contains(input.id)) .WhereIF(!string.IsNullOrEmpty(input.xmbh), p => p.Xmbh.Contains(input.xmbh)) .WhereIF(!string.IsNullOrEmpty(input.xmmc), p => p.Xmmc.Contains(input.xmmc)) .WhereIF(!string.IsNullOrEmpty(input.bzjg), p => p.Bzjg.Equals(input.bzjg)) .WhereIF(!string.IsNullOrEmpty(input.xmsc), p => p.Xmsc.Equals(input.xmsc)) .WhereIF(!string.IsNullOrEmpty(input.jcfwtc), p => p.Jcfwtc.Equals(input.jcfwtc)) .WhereIF(!string.IsNullOrEmpty(input.fl1), p => p.Fl1.Contains(input.fl1)) .WhereIF(!string.IsNullOrEmpty(input.fl2), p => p.Fl2.Contains(input.fl2)) .WhereIF(!string.IsNullOrEmpty(input.fl3), p => p.Fl3.Contains(input.fl3)) .WhereIF(!string.IsNullOrEmpty(input.fl4), p => p.Fl4.Contains(input.fl4)) .WhereIF(!string.IsNullOrEmpty(input.fl), p => p.Fl.Contains(input.fl)) .WhereIF(!string.IsNullOrEmpty(input.qt1), p => p.Qt1.Contains(input.qt1)) .WhereIF(!string.IsNullOrEmpty(input.qt2), p => p.Qt2.Contains(input.qt2)) .WhereIF(!string.IsNullOrEmpty(input.sgf), p => p.Sgf.Equals(input.sgf)) .Select(it => new LqXmzlListOutput { id = it.Id, xmbh = it.Xmbh, xmmc = it.Xmmc, bzjg = it.Bzjg, xmsc = it.Xmsc, jcfwtc = it.Jcfwtc, fl1 = it.Fl1, fl2 = it.Fl2, fl3 = it.Fl3, fl4 = it.Fl4, fl = it.Fl, qt1 = it.Qt1, qt2 = it.Qt2, sgf = it.Sgf, }).MergeTable().OrderBy(sidx + " " + input.sort).ToListAsync(); return data; } #endregion #region 导出项目资料 /// /// 导出项目资料 /// /// 请求参数 /// [HttpGet("Actions/Export")] public async Task Export([FromQuery] LqXmzlListQueryInput input) { var userInfo = await _userManager.GetUserInfo(); var exportData = new List(); if (input.dataType == 0) { var data = Clay.Object(await this.GetList(input)); exportData = data.Solidify>().list; } else { exportData = await this.GetNoPagingList(input); } List paramList = "[{\"value\":\"主键\",\"field\":\"id\"},{\"value\":\"项目编号\",\"field\":\"xmbh\"},{\"value\":\"项目名称\",\"field\":\"xmmc\"},{\"value\":\"标准价格\",\"field\":\"bzjg\"},{\"value\":\"时长(分)\",\"field\":\"xmsc\"},{\"value\":\"基础服务提成\",\"field\":\"jcfwtc\"},{\"value\":\"分类1汇总表\",\"field\":\"fl1\"},{\"value\":\"分类2汇总表\",\"field\":\"fl2\"},{\"value\":\"分类3工资用\",\"field\":\"fl3\"},{\"value\":\"分类4品项用\",\"field\":\"fl4\"},{\"value\":\"统计类别\",\"field\":\"tjlb\"},{\"value\":\"折扣类别\",\"field\":\"zklb\"},{\"value\":\"分类\",\"field\":\"fl\"},{\"value\":\"其它1\",\"field\":\"qt1\"},{\"value\":\"其它2\",\"field\":\"qt2\"},{\"value\":\"溯源金额\",\"field\":\"syje\"},{\"value\":\"Cell金额\",\"field\":\"cellje\"},{\"value\":\"手工费\",\"field\":\"sgf\"},]".ToList(); ExcelConfig excelconfig = new ExcelConfig(); excelconfig.FileName = "项目资料.xls"; excelconfig.HeadFont = "微软雅黑"; excelconfig.HeadPoint = 10; excelconfig.IsAllSizeColumn = true; excelconfig.ColumnModel = new List(); List selectKeyList = input.selectKey.Split(',').ToList(); foreach (var item in selectKeyList) { var isExist = paramList.Find(p => p.field == item); if (isExist != null) { excelconfig.ColumnModel.Add(new ExcelColumnModel() { Column = isExist.field, ExcelColumn = isExist.value }); } } var addPath = FileVariable.TemporaryFilePath + excelconfig.FileName; ExcelExportHelper.Export(exportData, excelconfig, addPath); var fileName = _userManager.UserId + "|" + addPath + "|xls"; var output = new { name = excelconfig.FileName, url = "/api/File/Download?encryption=" + DESCEncryption.Encrypt(fileName, "NCC") }; return output; } #endregion #region 批量删除项目资料 /// /// 批量删除项目资料 /// /// 主键数组 /// [HttpPost("batchRemove")] public async Task BatchRemove([FromBody] List ids) { throw NCCException.Oh("不允许删除项目资料"); var entitys = await _db.Queryable().In(it => it.Id, ids).ToListAsync(); if (entitys.Count > 0) { try { //开启事务 _db.BeginTran(); //批量删除项目资料 await _db.Deleteable().In(d => d.Id, ids).ExecuteCommandAsync(); //关闭事务 _db.CommitTran(); } catch (Exception) { //回滚事务 _db.RollbackTran(); throw NCCException.Oh(ErrorCode.COM1002); } } } #endregion #region 更新项目资料 /// /// 更新项目资料 /// /// 主键 /// 参数 /// [HttpPut("{id}")] public async Task Update(string id, [FromBody] LqXmzlUpInput input) { var entity = input.Adapt(); var isOk = await _db.Updateable(entity).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommandAsync(); if (!(isOk > 0)) throw NCCException.Oh(ErrorCode.COM1001); } #endregion #region 删除项目资料 /// /// 删除项目资料 /// /// 主键 /// [HttpDelete("{id}")] public async Task Delete(string id) { throw NCCException.Oh("不允许删除项目资料"); } #endregion #region 获取字段去重数据 /// /// 获取指定字段的去重数据 /// /// 字段名称,支持:fl1、fl2、fl3、fl4、fl、qt1、beautyType、sourceType /// 去重后的字段数据 [HttpGet("GetDistinctFieldData")] public async Task GetDistinctFieldData([FromQuery] string fieldName) { try { // 验证字段名称 var validFields = new[] { "fl1", "fl2", "fl3", "fl4", "fl", "qt1", "qt2", "beautyType", "sourceType" }; if (!validFields.Contains(fieldName)) { throw NCCException.Oh($"无效的字段名称: {fieldName}。支持的字段: {string.Join(", ", validFields)}"); } List distinctValues = new List(); switch (fieldName) { case "fl1": distinctValues = await _db.Queryable() .Where(p => !string.IsNullOrEmpty(p.Fl1)) .Select(p => p.Fl1) .Distinct() .ToListAsync(); break; case "fl2": distinctValues = await _db.Queryable() .Where(p => !string.IsNullOrEmpty(p.Fl2)) .Select(p => p.Fl2) .Distinct() .ToListAsync(); break; case "fl3": distinctValues = await _db.Queryable() .Where(p => !string.IsNullOrEmpty(p.Fl3)) .Select(p => p.Fl3) .Distinct() .ToListAsync(); break; case "fl4": distinctValues = await _db.Queryable() .Where(p => !string.IsNullOrEmpty(p.Fl4)) .Select(p => p.Fl4) .Distinct() .ToListAsync(); break; case "fl": distinctValues = await _db.Queryable() .Where(p => !string.IsNullOrEmpty(p.Fl)) .Select(p => p.Fl) .Distinct() .ToListAsync(); break; case "qt1": distinctValues = await _db.Queryable() .Where(p => !string.IsNullOrEmpty(p.Qt1)) .Select(p => p.Qt1) .Distinct() .ToListAsync(); break; case "qt2": distinctValues = await _db.Queryable() .Where(p => !string.IsNullOrEmpty(p.Qt2)) .Select(p => p.Qt2) .Distinct() .ToListAsync(); break; case "beautyType": distinctValues = await _db.Queryable() .Where(p => !string.IsNullOrEmpty(p.BeautyType)) .Select(p => p.BeautyType) .Distinct() .ToListAsync(); break; case "sourceType": distinctValues = await _db.Queryable() .Where(p => !string.IsNullOrEmpty(p.SourceType)) .Select(p => p.SourceType) .Distinct() .ToListAsync(); break; } return new { fieldName = fieldName, values = distinctValues.OrderBy(x => x).ToList() }; } catch (Exception ex) { throw NCCException.Oh($"获取字段去重数据失败:{ex.Message}"); } } #endregion } }