TableExampleService.cs 18 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431
using NCC.Common.Core.Manager;
using NCC.Common.Enum;
using NCC.Common.Extension;
using NCC.Common.Filter;
using NCC.Common.Helper;
using NCC.Common.Util;
using NCC.Dependency;
using NCC.DynamicApiController;
using NCC.Extend.Entitys.common_extend;
using NCC.Extend.Entitys.Dto.TableExample;
using NCC.Extend.Entitys.Model;
using NCC.FriendlyException;
using NCC.LinqBuilder;
using NCC.System.Entitys.Permission;
using NCC.System.Interfaces.Permission;
using NCC.System.Interfaces.System;
using Mapster;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using Yitter.IdGenerator;

namespace NCC.Extend
{
    /// <summary>
    /// 表格示例数据
    /// 版 本:V1.20.15
    /// 版 权:Wesley(https://www.NCCsoft.com)
    /// 作 者:NCC开发平台组
    /// 日 期:2022-03-16 
    /// </summary>
    [ApiDescriptionSettings(Tag = "Extend", Name = "TableExample", Order = 600)]
    [Route("api/extend/[controller]")]
    public class TableExampleService : IDynamicApiController, ITransient
    {
        private readonly ISqlSugarRepository<TableExampleEntity> _tableExampleRepository;
        private readonly SqlSugarScope db;
        private readonly IUsersService _usersService;
        private readonly IProvinceService _provinceService;
        private readonly IDictionaryDataService _dictionaryDataService;
        private readonly IUserManager _userManager;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="tableExampleRepository"></param>
        /// <param name="usersService"></param>
        /// <param name="provinceService"></param>
        /// <param name="dictionaryDataService"></param>
        /// <param name="userManager"></param>
        public TableExampleService(ISqlSugarRepository<TableExampleEntity> tableExampleRepository, IUsersService usersService, IProvinceService provinceService, IDictionaryDataService dictionaryDataService,IUserManager userManager)
        {
            _tableExampleRepository = tableExampleRepository;
            db = tableExampleRepository.Context;
            _usersService = usersService;
            _provinceService = provinceService;
            _dictionaryDataService = dictionaryDataService;
            _userManager = userManager;
        }

        #region GET
        /// <summary>
        /// 获取表格数据列表
        /// </summary>
        /// <param name="input">请求参数</param>
        /// <returns></returns>
        [HttpGet("")]
        public async Task<dynamic> GetList([FromQuery] PageInputBase input)
        {
            return  await GetPageList("",input);
        }

        /// <summary>
        /// 列表(树形表格)
        /// </summary>
        /// <param name="typeId"></param>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpGet("ControlSample/{typeId}")]
        public async Task<dynamic> GetList(string typeId, [FromQuery] PageInputBase input)
        {
            return await GetPageList(typeId, input);
        }

        /// <summary>
        /// 列表
        /// </summary>
        /// <returns></returns>
        [HttpGet("All")]
        public async Task<dynamic> GetListAll([FromQuery] KeywordInput input)
        {
            var list = await db.Queryable<TableExampleEntity, UserEntity>((a, b) => new JoinQueryInfos(JoinType.Left, a.Registrant == b.Id)).Select((a, b) => new TableExampleEntity()
            {
                Id = a.Id,
                InteractionDate = a.InteractionDate,
                ProjectCode = a.ProjectCode,
                ProjectName = a.ProjectName,
                Principal = a.Principal,
                JackStands = a.JackStands,
                ProjectType = a.ProjectType,
                ProjectPhase = a.ProjectPhase,
                CustomerName = a.CustomerName,
                CostAmount = a.CostAmount,
                TunesAmount = a.TunesAmount,
                ProjectedIncome = a.ProjectedIncome,
                RegisterDate = a.RegisterDate,
                Registrant = SqlFunc.MergeString(b.RealName, "/", b.Account),
                Description = a.Description,
                Sign = a.Sign,
                PostilJson = a.Sign,
                PostilCount = a.PostilCount,
                EnabledMark = a.EnabledMark,
                SortCode = a.SortCode,
                LastModifyTime = a.LastModifyTime,
                LastModifyUserId = a.LastModifyUserId
            }).WhereIF(input.keyword.IsNotEmptyOrNull(), a => a.ProjectCode.Contains(input.keyword) || a.ProjectName.Contains(input.keyword) || a.CustomerName.Contains(input.keyword)).MergeTable().ToListAsync();
            var output = list.Adapt<List<TableExampleAllOutput>>().OrderByDescending(x=>x.registerDate);
            return new { list = output };
        }

        /// <summary>
        /// 获取延伸扩展列表(行政区划)
        /// </summary>
        /// <returns></returns>
        [HttpGet("IndustryList")]
        public async Task<dynamic> GetIndustryList([FromQuery] KeywordInput input)
        {
            var data = (await _provinceService.GetList("-1")).Adapt<List<TableExampleIndustryListOutput>>();
            if (input.keyword.IsNotEmptyOrNull())
            {
                data = data.FindAll(x => x.enCode.Contains(input.keyword) || x.fullName.Contains(input.keyword));
            }
            return new { list = data };
        }

        /// <summary>
        /// 获取城市信息列表(获取延伸扩展列表(行政区划))
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("CityList/{id}")]
        public async Task<dynamic> GetCityList(string id)
        {
            var data = (await _provinceService.GetList(id)).Adapt<List<TableExampleCityListOutput>>();
            return new { list = data };
        }

        /// <summary>
        /// 列表(表格树形)
        /// </summary>
        /// <returns></returns>
        [HttpGet("ControlSample/TreeList")]
        public async Task<dynamic> GetTreeList(string isTree)
        {
            var data = (await _dictionaryDataService.GetList("IndustryType")).FindAll(x => x.EnabledMark == 1);
            var treeList = data.Select(x => new TableExampleTreeListOutput()
            {
                id = x.Id,
                parentId = x.ParentId,
                text = x.FullName,
                loaded = true,
                expanded = true,
                ht = x.Adapt<Dictionary<string, object>>()
            }).ToList();
            var output = isTree.IsNotEmptyOrNull() && "1".Equals(isTree) ? treeList.ToTree() : treeList;
            return new { list = output };
        }

        /// <summary>
        /// 信息
        /// </summary>
        /// <param name="id">主键值</param>
        /// <returns></returns>
        [HttpGet, Route("{id}")]
        public async Task<dynamic> GetInfo(string id)
        {
            var data = (await _tableExampleRepository.FirstOrDefaultAsync(x=>x.Id==id)).Adapt<TableExampleInfoOutput>();
            return data;
        }

        /// <summary>
        /// 获取批注
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("{id}/Actions/Postil")]
        public async Task<dynamic> GetPostil(string id)
        {
            var tableExampleEntity = await _tableExampleRepository.FirstOrDefaultAsync(x => x.Id == id);
            if (tableExampleEntity == null)
                throw NCCException.Oh(ErrorCode.COM1007);
            return new { postilJson = tableExampleEntity.PostilJson };
        }
        #endregion

        #region POST

   



        /// <summary>
        /// 删除
         /// </summary>
        /// <param name="id">主键值</param>
         /// <returns></returns>
        [HttpDelete("{id}")]
        public async Task Delete(string id)
        {
            var entity = await _tableExampleRepository.FirstOrDefaultAsync(x => x.Id == id);
            if (entity != null)
            {
                await _tableExampleRepository.DeleteAsync(entity);
            }
        }

        /// <summary>
        /// 创建
        /// </summary>
        /// <param name="input">实体对象</param>
        /// <returns></returns>
        [HttpPost("")]
        public async Task Create([FromBody] TableExampleCrInput input)
        {
            var entity = input.Adapt<TableExampleEntity>();
            entity.Id = YitIdHelper.NextId().ToString();
            entity.RegisterDate = DateTime.Now;
            entity.Registrant = _userManager.UserId;
            entity.CostAmount = entity.CostAmount == null ? 0 : entity.CostAmount;
            entity.TunesAmount = entity.TunesAmount == null ? 0 : entity.TunesAmount;
            entity.ProjectedIncome = entity.ProjectedIncome == null ? 0 : entity.ProjectedIncome;
            entity.Sign = "0000000";
            await _tableExampleRepository.InsertAsync(entity);
        }

        /// <summary>
        /// 编辑
        /// </summary>
        /// <param name="id">主键</param>
        /// <param name="input">实体对象</param>
        /// <returns></returns>
        [HttpPut("{id}")]
        public async Task Update(string id, [FromBody] TableExampleUpInput input)
        {
            var entity = input.Adapt<TableExampleEntity>();
            entity.Id = id;
            entity.LastModifyTime = DateTime.Now;
            entity.LastModifyUserId = _userManager.UserId;
            entity.CostAmount = entity.CostAmount == null ? 0 : entity.CostAmount;
            entity.TunesAmount = entity.TunesAmount == null ? 0 : entity.TunesAmount;
            entity.ProjectedIncome = entity.ProjectedIncome == null ? 0 : entity.ProjectedIncome;
            await _tableExampleRepository.Context.Updateable(entity).IgnoreColumns(ignoreAllNullColumns:true).ExecuteCommandAsync();
        }

        /// <summary>
        /// 更新标签
        /// </summary>
        /// <param name="id">主键</param>
        /// <param name="input">实体对象</param>
        /// <returns></returns>
        [HttpPut("UpdateSign/{id}")]
        public async Task UpdateSign(string id, [FromBody] TableExampleSignUpInput input)
        {
            var tableExampleEntity = await _tableExampleRepository.FirstOrDefaultAsync(x => x.Id == id);
            tableExampleEntity.Sign = input.sign;
            tableExampleEntity.Id = id;
            tableExampleEntity.LastModifyTime = DateTime.Now;
            tableExampleEntity.LastModifyUserId = _userManager.UserId;
            await _tableExampleRepository.Context.Updateable(tableExampleEntity).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommandAsync();
        }

        /// <summary>
        /// 行编辑
        /// </summary>
        /// <param name="id"></param>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPut("{id}/Actions/RowsEdit")]
        public async Task RowEditing(string id, [FromBody] TableExampleRowUpInput input)
        {
            var entity = input.Adapt<TableExampleEntity>();
            entity.Id = id;
            entity.CostAmount = entity.CostAmount == null ? 0 : entity.CostAmount;
            entity.TunesAmount = entity.TunesAmount == null ? 0 : entity.TunesAmount;
            entity.ProjectedIncome = entity.ProjectedIncome == null ? 0 : entity.ProjectedIncome;
            var tableExampleEntity = await _tableExampleRepository.FirstOrDefaultAsync(x => x.Id == id);
            var exampleEntity = BindModelValue(tableExampleEntity, entity);
            exampleEntity.LastModifyTime = DateTime.Now;
            exampleEntity.LastModifyUserId = _userManager.UserId;
            await _tableExampleRepository.Context.Updateable(tableExampleEntity).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommandAsync();
        }


        /// <summary>
        /// 发送批注
        /// </summary>
        /// <param name="id"></param>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost("{id}/Postil")]
        public async Task SendPostil(string id, [FromBody] TableExamplePostilSendInput input)
        {
            var tableExampleEntity = await _tableExampleRepository.FirstOrDefaultAsync(x => x.Id == id);
            if (tableExampleEntity == null)
                throw NCCException.Oh(ErrorCode.COM1005);
            var model = new PostilModel()
            {
                userId =await _usersService.GetUserName(_userManager.UserId),
                text = input.text,
                creatorTime = DateTime.Now
            };
            var list = new List<PostilModel>();
            list.Add(model);
            if (tableExampleEntity.PostilJson.IsNotEmptyOrNull())
            {
                list = list.Concat(tableExampleEntity.PostilJson.ToList<PostilModel>()).ToList();
            }
            var postilJson = list.ToJson();
            tableExampleEntity.PostilJson = postilJson;
            tableExampleEntity.PostilCount = list.Count;
            tableExampleEntity.Id = id;
            tableExampleEntity.LastModifyTime = DateTime.Now;
            tableExampleEntity.LastModifyUserId = _userManager.UserId;
            await _tableExampleRepository.Context.Updateable(tableExampleEntity).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommandAsync();
        }

        /// <summary>
        /// 删除批注
        /// </summary>
        /// <param name="id">主键值</param>
        /// <param name="index">请求参数</param>
        /// <returns></returns>
        [HttpDelete("{id}/Postil/{index}")]
        public async Task DeletePostil(string id, int index)
        {
            var tableExampleEntity = await _tableExampleRepository.FirstOrDefaultAsync(x => x.Id == id);
            if (tableExampleEntity == null)
                throw NCCException.Oh(ErrorCode.COM1005);
            var list = tableExampleEntity.PostilJson.ToList<PostilModel>();
            list.Remove(list[index]);
            tableExampleEntity.PostilJson = list.ToJson();
            tableExampleEntity.PostilCount = list.Count;
            tableExampleEntity.Id = id;
            tableExampleEntity.LastModifyTime = DateTime.Now;
            tableExampleEntity.LastModifyUserId = _userManager.UserId;
            await _tableExampleRepository.Context.Updateable(tableExampleEntity).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommandAsync();
        }
        #endregion

        #region PrivateMethod
        /// <summary>
        /// 分页列表
        /// </summary>
        /// <param name="typeId"></param>
        /// <param name="input"></param>
        /// <returns></returns>
        private async Task<dynamic> GetPageList(string typeId, PageInputBase input)
        {
            var queryWhere = LinqExpression.And<TableExampleListOutput>();
            var queryParam = input.queryJson.ToObject();
            if (typeId.IsNotEmptyOrNull())
            {
                queryWhere = queryWhere.And(a => a.projectType == typeId);
            }
            //关键字(项目编码、项目名称、客户名称)
            if (input.keyword.IsNotEmptyOrNull())
            {
                var keyword = input.keyword;
                queryWhere = queryWhere.And(a => a.projectCode.Contains(input.keyword) || a.projectName.Contains(input.keyword));
            }
            //标签查询
            if (queryParam["sign"].IsNotEmptyOrNull())
            {
                var index = 0;
                var arraySign = queryParam["sign"].ToString().Split(',');
                foreach (var item in arraySign)
                {
                    queryWhere = index == 0 ? queryWhere.And(a => a.sign.Contains(item)) : queryWhere.Or(a => a.sign.Contains(item));
                    index++;
                }
            }
            var list = await db.Queryable<TableExampleEntity, UserEntity>((a, b) => new JoinQueryInfos(JoinType.Left, a.Registrant == b.Id)).Select((a, b) => new TableExampleListOutput()
            {
                id=a.Id,
                interactionDate=a.InteractionDate,
                projectCode=a.ProjectCode,
                projectName=a.ProjectName,
                principal=a.Principal,
                jackStands=a.JackStands,
                projectType=a.ProjectType,
                projectPhase=a.ProjectPhase,
                customerName=a.CustomerName,
                costAmount=a.CostAmount,
                tunesAmount=a.TunesAmount,
                projectedIncome=a.ProjectedIncome,
                registerDate=a.RegisterDate,
                registrant= SqlFunc.MergeString(b.RealName,"/",b.Account),
                description = a.Description,
                sign=a.Sign,
                postilJson=a.Sign,
                postilCount=a.PostilCount.ToString(),
            }).MergeTable().Where(queryWhere).OrderBy(a => a.registerDate,OrderByType.Desc).ToPagedListAsync(input.currentPage, input.pageSize);
            return PageResult<TableExampleListOutput>.SqlSugarPageResult(list);
        }

        /// <summary>
        /// 将实体2的值动态赋值给实体1(名称一样的属性进行赋值)
        /// </summary>
        /// <param name="entity1">实体1</param>
        /// <param name="entity2">实体2</param>
        /// <returns>赋值后的model1</returns>
        private T1 BindModelValue<T1, T2>(T1 entity1, T2 entity2) where T1 : class where T2 : class
        {
            Type t1 = entity1.GetType();
            Type t2 = entity2.GetType();
            PropertyInfo[] property2 = t2.GetProperties();
            //排除字段
            List<string> exclude = new List<string>() { "F_Id", "F_Registrant", "F_RegisterDate", "F_SortCode", "F_Sign", "F_PostilJson", "F_PostilCount" };
            foreach (PropertyInfo p in property2)
            {
                if (exclude.Contains(p.Name)) { continue; }
                t1.GetProperty(p.Name)?.SetValue(entity1, p.GetValue(entity2, null));
            }
            return entity1;
        }
        #endregion
    }
}