文件内容 server.v1.txt 14 KB 14270 字符
using System.Collections.Generic;
using System;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Domain.Repositories;
using PasteForm.Helper;
using {{ Namespace }}.Application.Contracts;

namespace {{ Namespace }}.{{ DirName }}
{
    /// <summary>
    ///
    ///</summary>
    [TypeFilter(typeof(RoleAttribute), Arguments = new object[] { "data", "view" })]
    public class {{ Name }}AppService : {{ Namespace }}AppService
    {

        /// <summary>
        ///
        ///</summary>
        public {{ Name }}AppService():base()
        {

        }

        /// <summary>
        /// 读取AddDto的数据模型
        /// </summary>
        /// <returns></returns>
        [HttpGet]
		[TypeFilter(typeof(RoleAttribute), Arguments = new object[] { "data", "add" })]
        public VoloModelInfo ReadAddModel()
        {
            var _model = BuilderHelper.DynamicReadModelProperty<{{ Name }}AddDto>(new {{ Name }}AddDto());
            return _model;
        }

        /// <summary>
        /// 读取UpdateDto的数据模型
        /// </summary>
        /// <returns></returns>
        [HttpGet]
		[TypeFilter(typeof(RoleAttribute), Arguments = new object[] { "data", "edit" })]
        public async Task<VoloModelInfo> ReadUpdateModel({{ EntityType | replace : '"',"" }} id)
        {

            var _query = from a in _dbContext.{{ Name }}
            {%- assign my_index = 0 -%}
            {%- for classProperty in UpdatePropertys -%}
                {%- if classProperty.class_attributes != null -%}
                    {%- for classAttribute in classProperty.class_attributes -%}
                        {%- if classAttribute.name == "PasteShort" -%}
                            {%- assign my_index = my_index | plus: 1 -%}
                            join b{{my_index}}1 in _dbContext.{{ classAttribute.Args1 }} on a.{{ classProperty.name }} equals b{{my_index}}1.Id into b{{my_index}}2
                            from b{{my_index}}3 in b{{my_index}}2.DefaultIfEmpty()
                        {%- endif -%}
                    {%- endfor -%}
                {%- endif -%}
            {%- endfor -%}

            select new {{ Name }}UpdateDto
            {
                Id = a.Id,
                {%- assign my_index = 0 -%}
                {%- for classProperty in UpdatePropertys -%}
                    {%- unless classProperty.build_from_config -%}  
                        {{ classProperty.name }} = a.{{ classProperty.name }},
                        {%- if classProperty.class_attributes != null -%}
                            {%- for classAttribute in classProperty.class_attributes -%}
                                {%- if classAttribute.name == "PasteShort" -%}
                                    {%- assign my_index = my_index | plus: 1 -%}
                                    {{ classAttribute.Args2 }} = b{{ my_index }}3 != null ? b{{ my_index }}3.{{ classAttribute.Args3 }} : null,
                                {%- endif -%}
                            {%- endfor -%}
                        {%- endif -%}
                    {%- endunless -%}
                {%- endfor -%}
            };

			var _info = await _query.Where(x => x.Id == id).AsNoTracking().FirstOrDefaultAsync();
            if (_info == null || _info == default)
            {
                throw new PasteCodeException("查询的信息不存在,无法执行编辑操作!");
            }
            var _dataModel = BuilderHelper.DynamicReadModelProperty<{{ Name }}UpdateDto>(_info);
            return _dataModel;
        }

        /// <summary>
        /// 读取Dto的数据模型 用于查看详情
        /// </summary>
        /// <returns></returns>
        [HttpGet]
		[TypeFilter(typeof(RoleAttribute), Arguments = new object[] { "data", "edit" })]
        public async Task<VoloModelInfo> ReadDetailModel({{ EntityType | replace : '"',"" }} id)
        {
			var _info = await _dbContext.{{ Name }}.Where(x => x.Id == id)
            {%- for classProperty in UpdatePropertys -%}
                {%- if classProperty.bool_other_model -%}
            .Include(x=>x.{{classProperty.name}})
                {%- endif -%}
            {%- endfor -%}
            .AsNoTracking().FirstOrDefaultAsync();
            if (_info == null || _info == default)
            {
                throw new PasteCodeException("查询的信息不存在,无法执行编辑操作!");
            }
            var dto = ObjectMapper.Map<{{ Name }}, {{ Name }}Dto>(_info);
            {%- for classProperty in UpdatePropertys -%}
                {%- if classProperty.class_attributes != null -%}
                    {%- for classAttribute in classProperty.class_attributes -%}
                         {%- if classAttribute.name == "PasteShort" -%}
            if(dto.{{ classProperty.name }}.PasteHasValue()){
                dto.{{ classAttribute.Args2 }} = await _dbContext.{{ classAttribute.Args1 }}
                .Where(x=>x.Id ==dto.{{ classProperty.name }})
                .Select(x=>x.{{ classAttribute.Args3 }})
                .AsNoTracking()
                .FirstOrDefaultAsync();
            }
                            {%- endif -%}
                    {%- endfor -%}
                {%- endif -%}
            {%- endfor -%}
            var _dataModel = BuilderHelper.DynamicReadModelProperty<{{ Name }}Dto>(dto);
            return _dataModel;
        }
		
		/// <summary>
        /// 读取ListDto的数据模型
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public VoloModelInfo ReadListModel()
        {
            var _model = BuilderHelper.DynamicReadModelProperty<{{ Name }}ListDto>(new {{ Name }}ListDto());
			var _query_model = BuilderHelper.DynamicReadModelProperty(new InputQuery{{ Name }}());
            if (_query_model != null)
            {
                _model.QueryProperties = _query_model.Properties;
            }
            return _model;
        }

        /// <summary>
        /// 获取
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<PagedResultDto<{{ Name }}ListDto>> Page([FromQuery] InputQuery{{ Name }} input)
        {
            var _query = from a in _dbContext.{{ Name }}
            {%- assign my_index = 0 -%}
            {%- for classProperty in ListPropertys -%}
                {%- if classProperty.class_attributes != null -%}
                    {%- for classAttribute in classProperty.class_attributes -%}
                        {%- if classAttribute.name == "PasteShort" -%}
                            {%- assign my_index = my_index | plus: 1 -%}
                            join b{{my_index}}1 in _dbContext.{{ classAttribute.Args1 }} on a.{{ classProperty.name }} equals b{{my_index}}1.Id into b{{my_index}}2
                            from b{{my_index}}3 in b{{my_index}}2.DefaultIfEmpty()
                        {%- endif -%}
                    {%- endfor -%}
                {%- endif -%}
            {%- endfor -%}

            select new {{ Name }}ListDto
            {
                Id = a.Id,
                {%- assign my_index = 0 -%}
                {%- for classProperty in ListPropertys -%}
                    {%- unless classProperty.build_from_config -%}
                        {{ classProperty.name }} = a.{{ classProperty.name }},
                        {%- if classProperty.class_attributes != null -%}
                            {%- for classAttribute in classProperty.class_attributes -%}
                                {%- if classAttribute.name == "PasteShort" -%}
                                    {%- assign my_index = my_index | plus: 1 -%}
                                    {{ classAttribute.Args2 }} = b{{ my_index }}3 != null ? b{{ my_index }}3.{{ classAttribute.Args3 }} : null,
                                {%- endif -%}
                            {%- endfor -%}
                        {%- endif -%}
                    {%- endunless -%}
                {%- endfor -%}
            };

            _query = _query
            {%- for prop in ListPropertys -%}
                {%- if prop.class_attributes != null -%}
                    {%- for attr in prop.class_attributes -%}
                        {%- if attr.name == "PasteQuery" -%}
                            {%- if prop.property_type == "int" or prop.property_type == "long" -%}
            .WhereIf(input.{{ prop.name }} != 0, x => x.{{ prop.name }} == input.{{ prop.name }})
                            {%- elsif prop.property_type == "string" -%}
            .WhereIf(!String.IsNullOrEmpty(input.{{ prop.name }}), x => x.{{ prop.name }} == input.{{ prop.name }})
                            {%- endif -%}
                        {%- endif -%}
                    {%- endfor -%}
                {%- endif -%}
            {%- endfor -%}
            {%- comment %} 严格按您要求:input.word搜索所有未标记的string字段 {% endcomment -%}
            {%- assign search_fields = ListPropertys | where: "property_type", "string" -%}
            {%- if search_fields.size > 0 -%}
            .WhereIf(!String.IsNullOrEmpty(input.word), x => 
                {%- for field in search_fields -%}
                    {%- unless forloop.first %} || {% endunless -%}
                    x.{{ field.name }}.Contains(input.word)
                {%- endfor -%}
            )
            {%- endif -%}
            ;


            var _pagedto = new PagedResultDto<{{ Name }}ListDto>();
            if (input.page == 1)
            {
                _pagedto.TotalCount = await _query.CountAsync();
            }
            var dataList = await _query
                .OrderByDescending(x => x.Id)
                .Page(input.page, input.size)
                .AsNoTracking()
                .ToListAsync();
            if(dataList==null||dataList.Count == 0){
                return null;
            }
            _pagedto.Items = dataList;
            return _pagedto;
        }

        /// <summary>
        /// 添加一个
        ///</summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
		[TypeFilter(typeof(RoleAttribute), Arguments = new object[] { "data", "add" })]
        public async Task<{{ Name }}Dto> CreateItemAsync({{ Name }}AddDto input)
        {
            var _userid = base.ReadCurrentAdminId();
            var newu = ObjectMapper.Map<{{ Name }}AddDto, {{ Name }}>(input);
            //添加自定义
            _dbContext.Add(newu);
            {%- for item in AddPropertys -%}
                {%- if item.Name == "create_time" -%}
                    newu.create_time = DateTime.Now.ToUnixTimeSeconds();
                {%- endif -%}
                {%- if item.Name == "CreateDate" -%}
                    newu.CreateDate = DateTime.Now;
                {%- endif -%}
                {%- if item.Name == "CreateUserId" -%}
                    newu.CreateUserId = _userid;
                {%- endif -%}
            {%- endfor -%}
            await _dbContext.SaveChangesAsync();
            var backinfo = ObjectMapper.Map<{{ Name }}, {{ Name }}Dto>(newu);
            return backinfo;
        }
		
        /// <summary>
        /// 更新一个
        ///</summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        [TypeFilter(typeof(RoleAttribute), Arguments = new object[] { "data", "edit" })]
        public async Task<{{ Name }}Dto> UpdateItemAsync({{ Name }}UpdateDto input)
        {
            var info = await _dbContext.{{ Name }}.Where(x => x.Id == input.Id).FirstOrDefaultAsync();
            if (info == null || info == default)
            {
                throw new PasteCodeException("需要查询的信息不存在", 404);
            }
            ObjectMapper.Map<{{ Name }}UpdateDto, {{ Name }}>(input, info);
            await _dbContext.SaveChangesAsync();
            var backinfo = ObjectMapper.Map<{{ Name }}, {{ Name }}Dto>(info);
            return backinfo;
        }
		
        /// <summary>
        /// 更新状态,或者更新绑定
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        /// <exception cref="PasteCodeException"></exception>
        [HttpPost]
        [TypeFilter(typeof(RoleAttribute), Arguments = new object[] { "data", "state" })]
        public async Task<string> UpdateState(InputQueryUpdateState input)
        {
            {%- if entity_type != "string" -%}
            if (!{{ EntityType | replace : '"',"" }}.TryParse(input.id, out var _id))
            {
                throw new PasteCodeException("提供的参数id错误,无法继续执行");
            }
            {%- else -%}
                var _id -input.id;
            {%- endif -%}

                //因为只有一个isenable,直接写
                var find = await _dbContext.{{ Name }}.Where(x => x.Id == _id).FirstOrDefaultAsync();
                if (find == null || find == default)
                {
                    throw new PasteCodeException("没有找到对应的操作对象,无法继续执行");
                }

                switch(input.name){
                 {%- for item in ListPropertys -%}
                    {%- if item.property_type == "bool" -%}
                        case "{{ item.lower_name }}":
                        {
                            find.{{item.name}} = input.state;
                        }
                        break;
                    {%- endif -%}
                {%- endfor -%}
                }
                await _dbContext.SaveChangesAsync();
                //需要自行实现
            return "提交成功";
        }
		
		/// <summary>
        /// 删除数据,不需要删除的话注意注释这个 ListDto中附带disable标注忽略删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        [TypeFilter(typeof(RoleAttribute), Arguments = new object[] { "data", "del" })]
        public async Task<string> Remove({{ EntityType | replace : '"',"" }} id)
        {
            var info = await _dbContext.{{ Name }}.Where(x => x.Id == id).FirstOrDefaultAsync();
            if (info == null || info == default)
            {
                throw new PasteCodeException("需要查询的信息不存在", 404);
            }
            _dbContext.Remove(info);
            await _dbContext.SaveChangesAsync();
            return "删除成功";
        }
    }
}
​ ​