using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using JLHHJSvr.BLL;
using JLHHJSvr.Com;
using JLHHJSvr.Com.Model;
using JLHHJSvr.LJException;
using JLHHJSvr.Tools;
using LJLib.DAL.SQL;
using LJLib.Net.SPI.Server;

namespace JLHHJSvr.Excutor
{
    internal sealed class DelMattressExcutor : ExcutorBase<DelMattressRequest, DelMattressResponse>
    {
        protected override void ExcuteInternal(DelMattressRequest request, object state, DelMattressResponse rslt)
        {
            var tokendata = BllHelper.GetToken(request.token);
            if (tokendata == null)
            {
                rslt.ErrMsg = "会话已经中断，请重新登录";
                return;
            }

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

                // 初始化属性
                //AutoInit.AutoInitS(cmd, request.mattress);

                using (cmd.Transaction = con.BeginTransaction())
                {
                    try
                    {
                        var power77 = UserHelper.CheckFuncPower(cmd, tokendata.empid, 77);
                        if (!power77)
                        {
                            throw new LJCommonException("你没有删除权限");
                        }

                        foreach (int itemid in request.mattressids)
                        {
                            var mattressInfo = new u_mattress() { mattressid = itemid };
                            if (DbSqlHelper.SelectOne(cmd, mattressInfo, "flag, js1_flag") != 1)
                            {
                                rslt.ErrMsg = "查找报价单据失败：" + itemid;
                                return;
                            }

                            if (mattressInfo.flag == 1)
                            {
                                rslt.ErrMsg = "床垫已审核，不能删除！（" + itemid + ")";
                                return;
                            }
                            if (mattressInfo.js1_flag == 1)
                            {
                                rslt.ErrMsg = "资料已技术审核不能删除！（" + itemid + ")";
                                return;
                            }

                            if (DbSqlHelper.Delete(cmd, mattressInfo) <= 0)
                            {
                                throw new LJCommonException("因网络或其它原因，删除床垫报价操作失败!");
                            }

                        }

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