代码生成增加service、Repository、Controller层

This commit is contained in:
izory
2021-09-08 18:12:08 +08:00
parent 2d72257945
commit 8478df3a11
17 changed files with 897 additions and 288 deletions

View File

@@ -0,0 +1,124 @@
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using ZR.Admin.WebApi.Filters;
using ZR.Admin.WebApi.Controllers;
using ZR.Service;
using SqlSugar;
using Infrastructure;
using Infrastructure.Attribute;
using Infrastructure.Enums;
using Infrastructure.Model;
using Mapster;
using ZR.Admin.WebApi.Extensions;
using ZR.Model;
namespace ZRAdmin.Controllers
{
/// <summary>
/// T4代码自动生成
/// </summary>
[Verify]
[Route("bus/<#=ModelName#>")]
public class <#=ControllerName#>Controller: BaseController
{
/// <summary>
/// <#=FileName#>接口
/// </summary>
private readonly I<#=ServiceName#> _<#=ServiceName#>;
public <#=ControllerName#>Controller(I<#=ServiceName#> <#=ServiceName#>)
{
_<#=ServiceName#> = <#=ServiceName#>;
}
/// <summary>
/// 查询<#=FileName#>列表
/// </summary>
/// <returns></returns>
[HttpGet("list")]
[ActionPermissionFilter(Permission = "<#=ModelName#>:list")]
public IActionResult Query([FromQuery] <#=ModelName#>QueryDto parm)
{
//开始拼装查询条件
var predicate = Expressionable.Create<<#=ModelName#>>();
//TODO 搜索条件
//predicate = predicate.And(m => m.Name.Contains(parm.Name));
var response = _<#=ServiceName#>.GetPages(predicate.ToExpression(), parm);
return SUCCESS(response);
}
/// <summary>
/// 查询<#=FileName#>详情
/// </summary>
/// <param name="{primaryKey}"></param>
/// <returns></returns>
[HttpGet("{{primaryKey}}")]
public IActionResult Get(int {primaryKey})
{
var response = _<#=ServiceName#>.GetId({primaryKey});
return SUCCESS(response);
}
/// <summary>
/// 添加<#=FileName#>
/// </summary>
/// <returns></returns>
[HttpPost]
[ActionPermissionFilter(Permission = "<#=ModelName#>:add")]
[Log(Title = "<#=FileName#>添加", BusinessType = BusinessType.INSERT)]
public IActionResult Create([FromBody] <#=ModelName#>Dto parm)
{
if (parm == null)
{
throw new CustomException("请求参数错误");
}
//从 Dto 映射到 实体
var addModel = parm.Adapt<<#=ModelName#>>().ToCreate();
//addModel.CreateID = User.Identity.Name;
return SUCCESS(_<#=ServiceName#>.Add(addModel));
}
/// <summary>
/// 更新<#=FileName#>
/// </summary>
/// <returns></returns>
[HttpPut("edit")]
[ActionPermissionFilter(Permission = "<#=ModelName#>:update")]
[Log(Title = "<#=FileName#>修改", BusinessType = BusinessType.UPDATE)]
public IActionResult Update([FromBody] <#=ModelName#>Dto parm)
{
//从 Dto 映射到 实体
var addModel = parm.Adapt<<#=ModelName#>>().ToCreate();
//addModel.CreateID = User.Identity.Name;
//TODO 字段映射
var response = _<#=ServiceName#>.Update(addModel);
return SUCCESS(response);
}
/// <summary>
/// 删除<#=FileName#>
/// </summary>
/// <returns></returns>
[HttpDelete("{{primaryKey}}")]
[ActionPermissionFilter(Permission = "<#=ModelName#>:delete")]
[Log(Title = "<#=FileName#>删除", BusinessType = BusinessType.DELETE)]
public IActionResult Delete(int {primaryKey} = 0)
{
if ({primaryKey} <= 0) { return OutputJson(ApiResult.Error($"删除失败Id 不能为空")); }
// 删除<#=FileName#>
var response = _<#=ServiceName#>.Delete({primaryKey});
return SUCCESS(response);
}
}
}

View File

@@ -0,0 +1,13 @@
using System;
using ZR.Model.System;
using ZR.Model;
namespace {IServicsNamespace}
{
/// <summary>
/// 定义{TableNameDesc}服务接口
/// </summary>
public interface I{ModelTypeName}Service: IBaseService<{ModelTypeName}>
{
}
}

View File

@@ -0,0 +1,24 @@
using System;
using Infrastructure.Attribute;
using {RepositoriesNamespace}.System;
using {ModelsNamespace};
namespace {RepositoriesNamespace}
{
/// <summary>
/// {TableNameDesc}仓储接口的实现
/// </summary>
[AppService(ServiceLifetime = LifeTime.Transient)]
public class {ModelTypeName}Repository : BaseRepository
{
public {ModelTypeName}Repository()
{
}
#region 业务逻辑代码
#endregion
}
}

View File

@@ -0,0 +1,28 @@
using Infrastructure;
using Infrastructure.Attribute;
using Infrastructure.Extensions;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ZR.Common;
using ZR.Model;
using ZR.Repository;
using ZR.Service.IService;
namespace {ServicesNamespace}
{
/// <summary>
/// {TableNameDesc}服务接口实现
/// </summary>
[AppService(ServiceType = typeof(I{ModelTypeName}Service), ServiceLifetime = LifeTime.Transient)]
public class {ModelTypeName}Service: BaseService<{ModelTypeName}>, I{ModelTypeName}Service
{
private readonly {ModelTypeName}Repository _repository;
public {ModelTypeName}Service({ModelTypeName}Repository repository)
{
_repository = repository;
}
}
}

View File

@@ -0,0 +1,85 @@
import http from '@/utils/request'
import defaultSettings from '@/settings'
/**
* {ModelTypeDesc}分页查询
* @param {查询条件} data
*/
export function get{ModelTypeName}ListWithPager(data) {
return http.request({
url: '{ModelTypeName}/FindWithPagerAsync',
method: 'post',
data: data,
baseURL: defaultSettings.api{fileClassName}Url // 直接通过覆盖的方式
})
}/**
* 获取所有可用的{ModelTypeDesc}
*/
export function getAll{ModelTypeName}List() {
return http.request({
url: '{ModelTypeName}/GetAllEnable',
method: 'get',
baseURL: defaultSettings.api{fileClassName}Url // 直接通过覆盖的方式
})
}
/**
* 新增或修改保存{ModelTypeDesc}
* @param data
*/
export function save{ModelTypeName}(data, url) {
return http.request({
url: url,
method: 'post',
data: data,
baseURL: defaultSettings.api{fileClassName}Url // 直接通过覆盖的方式
})
}
/**
* 获取{ModelTypeDesc}详情
* @param {Id} {ModelTypeDesc}Id
*/
export function get{ModelTypeName}Detail(id) {
return http({
url: '{ModelTypeName}/GetById',
method: 'get',
params: { id: id },
baseURL: defaultSettings.api{fileClassName}Url // 直接通过覆盖的方式
})
}
/**
* 批量设置启用状态
* @param {id集合} ids
*/
export function set{ModelTypeName}Enable(data) {
return http({
url: '{ModelTypeName}/SetEnabledMarktBatchAsync',
method: 'post',
data: data,
baseURL: defaultSettings.api{fileClassName}Url // 直接通过覆盖的方式
})
}
/**
* 批量软删除
* @param {id集合} ids
*/
export function deleteSoft{ModelTypeName}(data) {
return http({
url: '{ModelTypeName}/DeleteSoftBatchAsync',
method: 'post',
data: data,
baseURL: defaultSettings.api{fileClassName}Url // 直接通过覆盖的方式
})
}
/**
* 批量删除
* @param {id集合} ids
*/
export function delete{ModelTypeName}(data) {
return http({
url: '{ModelTypeName}/DeleteBatchAsync',
method: 'delete',
data: data,
baseURL: defaultSettings.api{fileClassName}Url // 直接通过覆盖的方式
})
}

View File

@@ -0,0 +1,249 @@
<template>
<div class="app-container">
<el-row :gutter="24">
<!-- :model属性用于表单验证使用 比如下面的el-form-item 的 prop属性用于对表单值进行验证操作 -->
<el-form :model="queryParams" label-position="left" inline ref="queryForm" :label-width="labelWidth" v-show="showSearch" @submit.native.prevent>
<el-col :span="6">
<el-form-item label="文本文字">
<el-input v-model="queryParams.xxx" placeholder="" />
</el-form-item>
</el-col>
<el-col :span="6">
<el-form-item label="数字">
<el-input v-model.number="queryParams.xxx" placeholder="" />
</el-form-item>
</el-col>
<el-col :span="6">
<el-form-item label="下拉框">
<el-select v-model="queryParams.xxx" placeholder="">
<el-option v-for="dict in statusOptions" :key="dict.dictValue" :label="dict.dictLabel" :value="dict.dictValue" />
</el-select>
</el-form-item>
</el-col>
<el-col :span="6">
<el-form-item label="时间范围">
<el-date-picker size="small" style="width: 240px" v-model="timeRange" value-format="yyyy-MM-dd" type="daterange" range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期">
</el-date-picker>
</el-form-item>
</el-col>
<el-col :span="24" style="text-align:center;">
<el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
<el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
</el-col>
</el-form>
</el-row>
<!-- 工具区域 -->
<el-row :gutter="10" class="mb8">
<el-col :span="1.5">
<el-button type="primary" v-hasPermi="['{ModelTypeName}:add']" plain icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>
</el-col>
<el-col :span="1.5">
<el-button type="success" v-hasPermi="['{ModelTypeName}:update']" plain icon="el-icon-edit" size="mini" @click="handleUpdate">修改</el-button>
</el-col>
<el-col :span="1.5">
<el-button type="danger" v-hasPermi="['{ModelTypeName}:delete']" plain icon="el-icon-delete" size="mini" @click="handleDelete">删除</el-button>
</el-col>
<!-- <el-col :span="1.5">
<el-button type="info" plain icon="el-icon-upload2" size="mini" @click="handleImport">导入</el-button>
</el-col>
<el-col :span="1.5">
<el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport">导出</el-button>
</el-col> -->
<right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
</el-row>
<!-- 数据区域 -->
<el-table :data="dataList" ref="table" border @selection-change="handleSelectionChange">
<el-table-column type="selection" width="50" />
{VueViewListContent}
<el-table-column label="操作" align="center" width="200">
<template slot-scope="scope">
<el-button size="mini" type="text" icon="el-icon-view" @click="handleView(scope.row)">详情</el-button>
<el-button size="mini" v-hasPermi="['{ModelTypeName}:update']" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)">编辑</el-button>
<el-popconfirm title="确定删除吗?" @onConfirm="handleDelete(scope.row)" style="margin-left:10px">
<el-button slot="reference" v-hasPermi="['{ModelTypeName}:del']" size="mini" type="text" icon="el-icon-delete">删除</el-button>
</el-popconfirm>
</template>
</el-table-column>
</el-table>
<pagination :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />
<!-- 添加或修改菜单对话框 -->
<el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
<el-form ref="form" :model="form" :rules="rules" label-width="80px">
{VueViewFromContent}
</el-form>
<div slot="footer" class="dialog-footer" v-if="btnSubmitVisible">
<el-button type="primary" @click="submitForm">确 定</el-button>
<el-button @click="cancel">取 消</el-button>
</div>
</el-dialog>
</div>
</template>
<script>
// import { list{ModelTypeName},Add{ModelTypeName},Del{ModelTypeName},Edit{ModelTypeName} } from '@/api/{ModelTypeName}.js'
export default {
name: '{ModelTypeName}',
data() {
return {
labelWidth: "70px",
// 选中数组
ids: [],
// 非多个禁用
multiple: true,
// 遮罩层
loading: true,
// 显示搜索条件
showSearch: true,
// 查询参数
queryParams: {},
// 弹出层标题
title: "",
// 是否显示弹出层
open: false,
// 表单参数
form: {},
// 时间范围数组
timeRange: [],
// xxx下拉框
statusOptions: [],
// 数据列表
dataList: [],
// 总记录数
total: 0,
// 提交按钮是否显示
btnSubmitVisible: true,
// 表单校验
rules: {
name: [{ required: true, message: "名称不能为空", trigger: "blur" }],
userId: [
{
type: "number",
required: true,
message: "id不能为空,且不能为非数字",
trigger: "blur",
},
],
},
};
},
mounted() {
// 列表数据查询
this.getList();
// 下拉框绑定
// this.getDicts("sys_normal_disable").then((response) => {
// this.statusOptions = response.data;
// });
},
methods: {
// 查询数据
getList() {
console.log(JSON.stringify(this.queryParams));
list{ModelTypeName}(this.addDateRange(this.queryParams, this.timeRange)).then(res => {
if (res.code == 200) {
this.dataList = res.data.result;
this.total = res.data.totalCount;
}
})
},
// 取消按钮
cancel() {
this.open = false;
this.reset();
},
// 重置数据表单
reset() {
this.form = {
{VueViewEditFromContent}
//需个性化处理内容
};
this.resetForm("form");
},
/** 重置查询操作 */
resetQuery() {
this.timeRange = [];
this.resetForm("queryForm");
this.queryParams = {
pageNum: 1,
//TODO 重置字段
};
},
// 多选框选中数据
handleSelectionChange(selection) {
this.ids = selection.map((item) => item.{primaryKey});
this.multiple = !selection.length;
},
/** 搜索按钮操作 */
handleQuery() {
this.getList();
},
/** 新增按钮操作 */
handleAdd() {
this.reset();
this.open = true;
this.title = "添加";
//TODO 业务代码
},
/** 删除按钮操作 */
handleDelete(row) {
del{ModelTypeName}().then((res) => {
this.msgSuccess("删除成功");
this.handleQuery();
});
},
/** 修改按钮操作 */
handleUpdate(row) {
this.reset();
this.open = true;
this.title = "编辑";
//TODO 业务代码
console.log(JSON.stringify(row));
// TODO 给表单赋值
this.form = {
content: row.content,
userId: row.userId,
name: row.name,
sortId: row.sortId,
};
},
/** 提交按钮 */
submitForm: function () {
this.$refs["form"].validate((valid) => {
if (valid) {
console.log(JSON.stringify(this.form));
// TODO 记得改成表的主键
if (this.form.{primaryKey} != undefined) {
//TODO 编辑业务代码
} else {
//TODO 新增业务代码
}
}
});
},
// 详情
handleView(row) {
this.open = true;
this.title = "详情";
// TODO 给表单赋值
this.form = {
content: row.content,
userId: row.userId,
name: row.name,
sortId: row.sortId,
};
},
handleImport() {},
handleExport() {},
},
};
</script>
<style scoped>
.table-td-thumb {
width: 80px;
}
</style>