using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using DirectService.Tools;
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;
using LJLib.SQLEX;

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 = GlobalVar.ConnectionString.NewSqlConnection())
            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, mattresscode, parentid,erp_mtrlid") != 1)
                            {
                                rslt.ErrMsg = "查找报价单据失败：" + itemid;
                                return;
                            }

                            if (mattressInfo.flag == 1)
                            {
                                throw new LJCommonException("床垫已审核，不能删除！（" + mattressInfo.mattresscode + ")");
                            }
                            if (mattressInfo.js1_flag == 1)
                            {
                                throw new LJCommonException("资料已技术审核不能删除！（" + mattressInfo.mattresscode + ")");
                            }
                            if (mattressInfo.erp_mtrlid > 0)
                            {
                                throw new LJCommonException($"床垫报价[{mattressInfo.mattresscode}]已生成L1物料编码,无法删除!");
                            }

                            if (mattressInfo.parentid == null || mattressInfo.parentid == 0)
                            {

                                var list = new List<u_mattress>();
                                var outputFields = @"flag, js1_flag, mattresscode";
                                var selectStr = @" SELECT flag, js1_flag, mattresscode, parentid FROM u_mattress";

                                var whereList = new List<string>();
                                whereList.Add("parentid = @parentid");


                                DbSqlHelper.SelectJoin(cmd, selectStr, ListEx.GetWhereStr(whereList), new Dictionary<string, object>() { { "@parentid", mattressInfo.mattressid } }, "mattressid", outputFields, 0, 0, list);

                                if (list.Count > 0)
                                {
                                    foreach(var mtitem in list)
                                    {

                                        if (mtitem.flag == 1)
                                        {
                                            throw new LJCommonException("副规格床垫已审核，不能删除！（" + mtitem.mattresscode + ")");
                                        }
                                        if (mtitem.js1_flag == 1)
                                        {
                                            throw new LJCommonException("副规格床垫资料已技术审核不能删除！（" + mtitem.mattresscode + ")");
                                        }
                                    }
                                }
                            }

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

                            cmd.CommandText = @"DELETE u_mattress WHERE parentid = @parentid";
                            cmd.Parameters.Clear();
                            cmd.Parameters.AddWithValue("@parentid", itemid);
                            cmd.ExecuteNonQuery();

                        }

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