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 AuditWorkmanshipExcutor : ExcutorBase<AuditWorkmanshipRequest, AuditWorkmanshipResponse>
    {
        protected override void ExcuteInternal(AuditWorkmanshipRequest request, object state, AuditWorkmanshipResponse 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 item in request.list)
                {
                    if (DbSqlHelper.SelectOne(cmd, item, "inuse") <= 0)
                    {
                        throw new LJCommonException("工艺加点设置不存在，请检查!");
                    }

                    if (request.type == 2 && item.inuse == 0)
                    {
                        throw new LJCommonException("工艺加点设置未禁用，请检查!");
                    }
                    else if (request.type == 3 && item.inuse == 1)
                    {
                        throw new LJCommonException("工艺加点设置已禁用，请检查!");
                    }
                }

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

                            if (DbSqlHelper.Update(cmd, "u_workmanship_add", null, item, "workmanshipid", updateField) <= 0)
                            {
                                throw new LJCommonException("工艺加点设置更新失败!");
                            }
                        }

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