using System.Data;
using System.Threading.Tasks;
using Wicture.DbRESTFul;
using Wicture.DbRESTFul.Infrastructure.Repository;
using System.Collections.Generic;
using JiaZhiQuan.Common.Models.MallGoodsModel;

namespace JiaZhiQuan.Common
{
    public static partial class RepositoryExtension
    {
        /// <summary>
        /// 新增商品变更事件
        /// </summary>
        /// <param name="conn"></param>
        /// <param name="trans"></param>
        /// <param name="goodsId">商品id</param>
        /// <param name="operatorId">操作人id</param>
        /// <param name="operatorType">操作人类型：0 平台 1 买家 2 卖家 3 运营人员 MallGoodsChangeOperatorType 枚举</param>
        /// <param name="eventName">事件类型</param>
        /// <param name="eventDesc">事件描述</param>
        /// <param name="eventType">操作类型(1,创建、编辑2，审核，3，刊登， 4,删除, 5,启用禁用, 6,销售) 枚举MallGoodsChangeEventType</param>
        /// <returns></returns>
        /// <exception cref="LogicalException"></exception>
        public static async Task AddGoodsChangeEvent(this DbRESTFulRepository repository, List<MallGoodsChangeEvent> chages, IDbConnection conn = null, IDbTransaction trans = null)
        {
            int count = await repository.QuerySingleOrDefaultAsync<int>(@"
                     insert into mall_goods_change_events(
                                            goodsId,
                                            eventName,
                                            eventDesc,
                                            operatorId,
                                            operatorType,
                                            eventType,
                                            createAt)
                                     values(
                                            @goodsId,
                                            @eventName,
                                            @eventDesc,
                                            @operatorId,
                                            @operatorType,
                                            @eventType,
                                            @createAt);select row_count() as count;",
                                            chages, conn, trans);

            if (count > 0) return;
            throw new LogicalException("新增商品变更事件失败");
        }
    }
}
