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;
using LJLib.SQLEX;

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

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

                var updateField = string.Empty;

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

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

                    if (request.type == 0 && price.flag == 1)
                    {
                        throw new LJCommonException("价格表已审核，请检查!");
                    }
                    else if (request.type == 1 && price.flag == 0)
                    {
                        throw new LJCommonException("价格表未审核，请检查!");
                    }
                    else if (request.type == 2 && price.inuse == 0)
                    {
                        throw new LJCommonException("价格表未禁用，请检查!");
                    }
                    else if (request.type == 3 && price.inuse == 1)
                    {
                        throw new LJCommonException("价格表已禁用，请检查!");
                    }
                }

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

                            if (DbSqlHelper.Update(cmd, "u_pricelist", null, price, "pricelistid", updateField) <= 0)
                            {
                                throw new LJCommonException("价格表更新失败!");
                            }
                        }

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