using System;
using System.Data.SqlClient;
using System.Linq;
using JLHHJSvr.BLL;
using JLHHJSvr.Com;
using JLHHJSvr.LJException;
using LJLib.DAL.SQL;
using LJLib.Net.SPI.Server;
using LJLib.SQLEX;

namespace JLHHJSvr.Excutor
{
    internal sealed class DeleteMattressExtraExcutor : ExcutorBase<DeleteMattressExtraRequest, DeleteMattressExtraResponse>
    {
        protected override void ExcuteInternal(DeleteMattressExtraRequest request, object state, DeleteMattressExtraResponse rslt)
        {
            if (!request.list.Any())
            {
                rslt.ErrMsg = "至少提交一条需要删除的记录";
                return;
            }

            using (var con = GlobalVar.ConnectionString.NewSqlConnection())
            using (var cmd = con.CreateCommand())
            {
                con.Open();

                using (cmd.Transaction = con.BeginTransaction())
                {
                    try
                    {
                        foreach (var area in request.list)
                        {
                            if (DbSqlHelper.Delete(cmd, area) == 0)
                            {
                                throw new LJCommonException($"床垫额外费用类型定义资料不存在");
                            }
                        }

                        cmd.Transaction.Commit();
                    }
                    catch (Exception e)
                    {
                        cmd.Transaction?.Rollback();
                        rslt.ErrMsg = e.Message;
                    }
                }
            }
        }
    }
}
