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.Helper;
using JLHHJSvr.LJException;
using JLHHJSvr.Tools;
using LJLib.DAL.SQL;
using LJLib.Net.SPI.Server;
using LJLib.SQLEX;

namespace JLHHJSvr.Excutor
{
    internal sealed class SaveMattressAuditingExcutor : ExcutorBase<SaveMattressAuditingRequest, SaveMattressAuditingResponse>
    {
        /*protected override void ExcuteInternal(SaveMattressAuditingRequest request, object state, SaveMattressAuditingResponse 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
                    {
                        // 只处理主规格
                        Dictionary<int, u_mattress> mattressDic = new Dictionary<int, u_mattress>();

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

                            if (!mattressDic.ContainsKey(mattressInfo.mattressid))
                            {
                                mattressDic.Add(mattressInfo.mattressid, mattressInfo);
                            }
                        }

                        if (request.xd_flag != null)
                        {
                            // 业务下单/取消

                            var power94 = UserHelper.CheckFuncPower(cmd, tokendata.empid, 94);
                            if (!power94 && request.xd_flag == 1)
                            {
                                throw new LJCommonException("你没有业务下单权限");
                            }

                            var power95 = UserHelper.CheckFuncPower(cmd, tokendata.empid, 95);
                            if (!power95 && request.xd_flag == 0)
                            {
                                throw new LJCommonException("你没有取消业务下单权限");
                            }


                            var updateField = "xd_flag, xd_auditingrep, xd_auditingdate";

                            foreach (var item in mattressDic)
                            {
                                var mattressInfo = item.Value;

                                if (request.xd_flag == 1)
                                {
                                    if (mattressInfo.flag == 1)
                                    {
                                        rslt.ErrMsg = "床垫已审核，不能业务下单！（" + mattressInfo.mattresscode + ")";
                                        return;
                                    }
                                    if (mattressInfo.xd_flag == 1)
                                    {
                                        rslt.ErrMsg = "床垫已完成业务下单（" + mattressInfo.mattresscode + ")";
                                        return;
                                    }

                                    mattressInfo.xd_flag = 1;
                                    mattressInfo.xd_auditingrep = tokendata.username;
                                    mattressInfo.xd_auditingdate = DateTime.Now;


                                    if (DbSqlHelper.Update(cmd, "u_mattress", null, mattressInfo, "mattressid", updateField) <= 0)
                                    {
                                        throw new LJCommonException("因网络或其它原因，【业务下单/取消】操作失败!");
                                    }

                                    //UpdateChildState(cmd, mattressInfo, "xd_flag", 1);
                                }
                                else
                                {
                                    if (mattressInfo.js1_flag == 1)
                                    {
                                        rslt.ErrMsg = "已产品补充审核，不能取消下单！（" + mattressInfo.mattresscode + ")";
                                        return;
                                    }
                                    if (mattressInfo.js2_flag == 1)
                                    {
                                        rslt.ErrMsg = "已清单补充审核，不能取消下单！（" + mattressInfo.mattresscode + ")";
                                        return;
                                    }
                                    if (mattressInfo.xd_flag == 0)
                                    {
                                        rslt.ErrMsg = "未进行业务下单，不能取消下单！（" + mattressInfo.mattresscode + ")";
                                        return;
                                    }
                                    //if (mattressInfo.yw_flag == 1)
                                    //{
                                    //    rslt.ErrMsg = "床垫清单已业务补充审核，不能取消下单！（" + mattressInfo.mattresscode + ")";
                                    //    return;
                                    //}

                                    mattressInfo.xd_flag = 0;
                                    mattressInfo.xd_auditingrep = "";
                                    mattressInfo.xd_auditingdate = null;

                                    if (DbSqlHelper.Update(cmd, "u_mattress", null, mattressInfo, "mattressid", updateField) <= 0)
                                    {
                                        throw new LJCommonException("因网络或其它原因，【业务下单/取消】操作失败!");
                                    }

                                    //UpdateChildState(cmd, mattressInfo, "xd_flag", 0);
                                }
                            }

                        }
                        else if (request.flag != null)
                        {
                            // 审核

                            var power73 = UserHelper.CheckFuncPower(cmd, tokendata.empid, 73);
                            if (!power73 && request.flag == 1)
                            {
                                throw new LJCommonException("你没有审核权限");
                            }

                            var power74 = UserHelper.CheckFuncPower(cmd, tokendata.empid, 74);
                            if (!power74 && request.flag == 0)
                            {
                                throw new LJCommonException("你没有取消审核权限");
                            }

                            var updateField = "flag, auditingrep, auditingdate";

                            foreach (var item in mattressDic)
                            {
                                var mattressInfo = item.Value;

                                if (request.flag == 1)
                                {
                                    if (mattressInfo.flag == 1)
                                    {
                                        rslt.ErrMsg = "床报价已完成审核（" + mattressInfo.mattresscode + ")";
                                        return;
                                    }

                                    mattressInfo.flag = 1;
                                    mattressInfo.auditingrep = tokendata.username;
                                    mattressInfo.auditingdate = DateTime.Now;

                                    if (DbSqlHelper.Update(cmd, "u_mattress", null, mattressInfo, "mattressid", updateField) <= 0)
                                    {
                                        throw new LJCommonException("因网络或其它原因，【审核消】操作失败!");
                                    }

                                    //UpdateChildState(cmd, mattressInfo, "flag", 1);
                                }
                                else
                                {
                                    if (mattressInfo.flag == 0)
                                    {
                                        rslt.ErrMsg = "床报价已完成撤销审核！（" + mattressInfo.mattresscode + ")";
                                        return;
                                    }

                                    mattressInfo.flag = 0;
                                    mattressInfo.auditingrep = "";
                                    mattressInfo.auditingdate = null;

                                    if (DbSqlHelper.Update(cmd, "u_mattress", null, mattressInfo, "mattressid", updateField) <= 0)
                                    {
                                        throw new LJCommonException("因网络或其它原因，【撤销审核】操作失败!");
                                    }

                                    //UpdateChildState(cmd, mattressInfo, "flag", 0);
                                }
                            }

                        }




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

        public void UpdateChildState(SqlCommand cmd, u_mattress mattress, string fieldStr, int value)
        {
            var updateStr = "";
            var rep = "";
            DateTime? date = null;
            if (fieldStr == "xd_flag")
            {
                if (mattress.auditingdate.HasValue)
                {
                    updateStr = @"
                        xd_auditingrep = @rep,
                        xd_auditingdate = @date
                    ";
                    rep = mattress.xd_auditingrep;
                    date = mattress.xd_auditingdate;
                }
                else
                {
                    updateStr = @"
                        xd_auditingrep = @rep,
                        xd_auditingdate = null
                    ";
                    rep = mattress.xd_auditingrep;
                }
            } else
            {
                if (mattress.auditingdate.HasValue)
                {
                    updateStr = @"
                        auditingrep = @rep,
                        auditingdate = @date
                    ";
                    rep = mattress.auditingrep;
                    date = mattress.auditingdate;
                }
                else
                {
                    updateStr = @"
                        auditingrep = @rep,
                        auditingdate = null
                    ";
                    rep = mattress.auditingrep;
                }
            }
            cmd.CommandText = @"UPDATE u_mattress
                                SET " + fieldStr + @" = @value, 
                                    " + updateStr + @"
                                FROM u_mattress
                                WHERE u_mattress.parentid = @mattressid";
            cmd.Parameters.Clear();
            cmd.Parameters.AddWithValue("@mattressid", mattress.mattressid);
            cmd.Parameters.AddWithValue("@value", value);
            cmd.Parameters.AddWithValue("@rep", rep);
            if (date.HasValue)
            {
                cmd.Parameters.AddWithValue("@date", date);
            }
            cmd.ExecuteNonQuery();
        }*/

        protected override void ExcuteInternal(SaveMattressAuditingRequest request, object state, SaveMattressAuditingResponse 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();

                var mattressHelper = HelperBase.GetHelper<MattressHelper>(cmd, new HelperBase.Context() { tokendata = tokendata });
                using (cmd.Transaction = con.BeginTransaction())
                {
                    try
                    {
                        foreach (int mattressid in request.mattressids)
                        {
                            if (request.xd_flag != null)
                            {
                                // 业务下单 / 取消业务下单
                                if (request.xd_flag == 1) mattressHelper.MattressXDAudit(mattressid);
                                else mattressHelper.MattressXDCAudit(mattressid);
                            }
                            else if (request.flag != null)
                            {
                                // 审核 / 撤审
                                if (request.flag == 1) mattressHelper.MattressAudit(mattressid);
                                else mattressHelper.MattressCAudit(mattressid);
                            }
                        }

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