.netCore 如何新增一个修改字段值的功能

62 阅读2分钟

项目结构

序号 项目名称 说明 引用的类库 备注
1 DTcms.Core.Admin 后台管理项目 作为后台管理项目,可能是供管理员使用的Web应用,管理整个系统。独立性较强,不引用其他类库,确保管理员工具的安全性。
2 DTcms.Core.API RESTful API接口项目 Model、IServices、Services、DBFactory、Common 提供RESTful API服务,可能是为外部应用或前端提供数据接口。
3 DTcms.Core.Common 公共组件方法类库 包含一系列公共工具和方法,为其他项目提供支持。
4 DTcms.Core.DBFactory 数据库工厂类库 Model、Common 提供数据库上下文和数据库策略功能,连接模型与实际数据库。
5 DTcms.Core.IServices 数据访问层接口类库 Model、Common 定义数据访问层的接口,方便依赖注入。
6 DTcms.Core.Model 数据库表实体类库 定义与数据库相关的数据模型,同时包括数据传输对象 (DTO)。
7 DTcms.Core.Services 数据访问层接口实现类库 Model、IServices、DBFactory、Common 实现数据访问层的接口,完成与数据库的交互。
8 DTcms.Core.Web 网站前台项目 Model、Common 前台展示项目,供用户访问和互动。
### -1、前端html代码片段
<el-table :data="model.orderGoods" border class="table-form">
    <el-table-column label="商品名称" min-width="220">
        <template slot-scope="scope">
            <el-image class="pic" fit="contain" :src="$api.loadFile(scope.row.imgUrl)">
                <div slot="error" class="image-slot">
                    <i class="el-icon-picture-outline"></i>
                </div>
            </el-image>
            <h4>{{scope.row.goodsTitle}}</h4>
            <span class="date">{{scope.row.goodsNo}}</span>
        </template>
    </el-table-column>
    <el-table-column label="规格" min-width="120">
        <template slot-scope="scope">
            <span class="nowrap">{{scope.row.specText}}</span>
        </template>
    </el-table-column>
    <el-table-column label="现价格" width="100">
        <template slot-scope="scope">
            <div v-if="model.paymentStatus=='1'">
                <span class="nowrap">{{scope.row.realPrice}}</span>
            </div>
            <div v-else>
                <el-input v-model="scope.row.realPrice" @blur="handleQuantityChange(scope.row)" type="number"></el-input>
            </div>
        </template>
    </el-table-column>
    <el-table-column prop="goodsPrice" label="原售价" width="80"></el-table-column>
    <el-table-column label="数量" width="100">
        <template slot-scope="scope">
            <div v-if="model.paymentStatus=='1'">
                <span class="nowrap">{{scope.row.quantity}}</span>
            </div>
            <div v-else>
                <el-input v-model="scope.row.quantity" @blur="handleQuantityChange(scope.row)" type="number"></el-input>
            </div>
        </template>
    </el-table-column>
</el-table>

0、前端JS代码片段

			//未付款订单修改数量回调事件
			handleQuantityChange(row) {
				let _this = this; //当前页面
				// 获取变更后的数量
				const newQuantity = row.quantity;
				// 对新的数量进行验证,例如:
				if (newQuantity < 1) {
					this.$message.error('数量不能小于1');
					return;
				}
				if (row.realPrice < 1) {
					this.$message.error('价格不能小于1');
					return;
				}
				// 向后端发送更新请求
				_this.$api.request({
					method: 'put',
					url: '/admin/order/ordergoods/' + _this.model.id,
					data: _this.model.orderGoods,
					successMsg: '商品信息修改成功',
					beforeSend() {
						_this.loading = true
					},
					success(res) {
						//_this.$common.linkUrl('/order/list');
					},
					complete() {
						_this.loading = false
					}
				});
			},

1、先在DTcms.Core.API增加一个方法“UpdateOrderGoods”修改待付款商品实际成交价格和购买数量

        /// <summary>
        /// 修改待付款商品实际成交价格和购买数量
        /// 2023-10-20  by朱光明 增加修改商品价格和购买数量
        /// 示例:/admin/order/ordergoods/1
        /// </summary>
        [HttpPut("ordergoods/{id}")]
        [Authorize]
        [AuthorizeFilter("Order", ActionType.Edit)]
        public async Task<IActionResult> UpdateOrderGoods([FromRoute] long id, [FromBody] List<OrderGoodsEditDto> modelDto)
        {

            //处理多个OrderGoods更新逻辑
            foreach (var item in modelDto)
            {
                //保存数据
                var result = await _orderService.UpdateOrderGoodsAsync(item.Id, item);
            }
            return NoContent();
        }

2、DTcms.Core.IServices 在数据访问层增加一个接口

        /// <summary>
        /// 修改待付款商品实际成交价格和购买数量
        /// </summary>
        Task<bool> UpdateOrderGoodsAsync(long id, OrderGoodsEditDto modelDto, WriteRoRead writeRoRead = WriteRoRead.Write);

3、DTcms.Core.Services 在数据访问层增加一个接口实现

        /// <summary>
        /// 修改待付款商品实际成交价格和购买数量
        /// </summary>
        public async Task<bool> UpdateOrderGoodsAsync(long id, OrderGoodsEditDto modelDto, WriteRoRead writeAndRead = WriteRoRead.Write)
        {
            _context = _contextFactory.CreateContext(writeAndRead);//连接数据库
            //根据ID获取记录
            var model = await _context.Set<OrderGoods>().FirstOrDefaultAsync(x => x.Id == id);
            //如果不存在则抛出异常
            if (model == null)
            {
                throw new ResponseException("订单商品不存在或已删除");
            }
            //修改订单商品价格
            model.RealPrice = modelDto.RealPrice;
            model.Quantity = modelDto.Quantity;
            //保存到数据库
            _context.Set<OrderGoods>().Update(model);
            return await this.SaveAsync();            
        }

4、DTcms.Core.Model在数据表实体类 增加一个Dto OrderGoodsEditDto

    public class OrderGoodsEditDto
    {
        /// <summary>
        /// 自增ID
        /// </summary>
        [Display(Name = "自增ID")]
        public long Id { get; set; }

        /// <summary>
        /// 所属订单ID
        /// </summary>
        [Display(Name = "所属订单")]
        public long OrderId { get; set; }

        /// <summary>
        /// 所属货品
        /// </summary>
        [Display(Name = "所属货品")]
        public long ProductId { get; set; }

        /// <summary>
        /// 购买数量
        /// </summary>
        [Display(Name = "购买数量")]
        public int Quantity { get; set; } = 1;

        /// <summary>
        /// 实际价格
        ///</summary>
        [Display(Name ="实际价格")]
        [Column(TypeName = "decimal(18, 2)")]
        public decimal RealPrice { get; set; } = 0M;
        
    }