using System;
using System.Collections.Generic;
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;

namespace JLHHJSvr.Excutor
{
    internal sealed class AuditSpringExcutor : ExcutorBase<AuditSpringRequest, AuditSpringResponse>
    {
        protected override void ExcuteInternal(AuditSpringRequest request, object state, AuditSpringResponse rslt)
        {
            var tokendata = BllHelper.GetToken(request.token);
            if (tokendata == null)
            {
                rslt.ErrMsg = "会话已经中断，请重新登录";
                return;
            }
            if (!request.list.Any())
            {
                rslt.ErrMsg = "至少提交一条需要删除的记录";
                return;
            }

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

                var updateField = string.Empty;

                if(request.type < 2) // 审核
                {
                    updateField = "flag";
                } else
                {
                    updateField = "inuse";
                }

                foreach(var spring in request.list)
                {
                    if(DbSqlHelper.SelectOne(cmd, spring, "flag,inuse") <= 0)
                    {
                        throw new LJCommonException("弹簧资料不存在或已审核，请检查!");
                    }

                    if(request.type == 0 && spring.flag == 1)
                    {
                        throw new LJCommonException("弹簧资料已审核，请检查!");
                    }else if(request.type== 1 && spring.flag == 0)
                    {
                        throw new LJCommonException("弹簧资料未审核，请检查!");
                    }else if(request.type == 2 && spring.inuse == 0)
                    {
                        throw new LJCommonException("弹簧资料未启用，请检查!");
                    }else if(request.type == 3 && spring.inuse == 1)
                    {
                        throw new LJCommonException("弹簧资料已启用，请检查!");
                    }
                }

                using (cmd.Transaction = con.BeginTransaction())
                {
                    try
                    {
                        foreach (var spring in request.list)
                        {
                            switch(request.type)
                            {
                                case 0:
                                    spring.flag = 1;
                                    break;
                                case 1:
                                    spring.flag = 0;
                                    break;
                                case 2:
                                    spring.inuse = 0;
                                    break;
                                case 3:
                                    spring.inuse = 1;
                                    break;
                            }

                            if (DbSqlHelper.Update(cmd, "u_spring", null, spring, "springid", updateField) <= 0)
                            {
                                throw new LJCommonException("弹簧资料更新失败!");
                            }
                        }

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