using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using JLHHJSvr.BLL;
using JLHHJSvr.Com.APP;
using JLHHJSvr.DBA.DBModle;
using LJLib.DAL.SQL;
using LJLib.Net.SPI.Server;
using LJLib.SQLEX;
using LJLib.Tools.Encry;

namespace JLHHJSvr.Excutor.APP
{
    internal sealed class EditBillExcutor : ExcutorBase<EditBillRequest, EditBillResponse>
    {
        protected override void ExcuteInternal(EditBillRequest request, object state, EditBillResponse rslt)
        {
            var tokendata = BllHelper.GetToken(request.token);
            if (tokendata == null)
            {
                rslt.ErrMsg = "会话已经中断，请重新登录";
                return;
            }

            if (request.billInfo == null)
            {
                rslt.ErrMsg = "数据异常，获取数据信息失败";
                return;
            }

            if (request.billInfo.billid == null || request.billInfo.billid == 0)
            {
                rslt.ErrMsg = "数据异常，获取单号失败";
                return;
            }

            if (request.billInfo.flag == 1)
            {
                if (request.billInfo.roadid == null || request.billInfo.roadid == 0)
                {
                    rslt.ErrMsg = "未选择路段";
                    return;
                }
                if (string.IsNullOrEmpty(request.billInfo.carnum))
                {
                    rslt.ErrMsg = "请先填写车牌号码";
                    return;
                }
                request.billInfo.carnum = request.billInfo.carnum.ToUpper();
                if (request.billInfo.dscrp == null)
                {
                    rslt.ErrMsg = "备注信息接收异常，请重试";
                    return;
                }
                if (string.IsNullOrEmpty(request.billInfo.owner))
                {
                    rslt.ErrMsg = "车主姓名不能为空";
                    return;
                }
                if (string.IsNullOrEmpty(request.billInfo.owner_tel))
                {
                    rslt.ErrMsg = "车主电话不能为空";
                    return;
                }
                if (request.billInfo.paytype == null || request.billInfo.paytype == 0)
                {
                    rslt.ErrMsg = "支付方式未选择";
                    return;
                }
                if (request.billInfo.auditdate == null)
                {
                    request.billInfo.auditdate = DateTime.Now;
                }
                if (request.billInfo.payamt == null)
                {
                    rslt.ErrMsg = "未填写金额";
                    return;
                }
                if (request.billInfo.pictureList == null || request.billInfo.pictureList.Count == 0)
                {
                    rslt.ErrMsg = "图片信息接收异常";
                    return;
                }
                foreach (var fileInfo in request.billInfo.pictureList)
                {
                    if (string.IsNullOrEmpty(fileInfo.base64))
                    {
                        rslt.ErrMsg = "文件数据获取失败，请重新选择";
                        return;
                    }
                    if (string.IsNullOrEmpty(fileInfo.fileType))
                    {
                        rslt.ErrMsg = "文件类型获取失败，请重新选择";
                        return;
                    }
                }
            }
            else if (request.billInfo.flag == 2)
            {
            }
            else
            {
                rslt.ErrMsg = "所提交的状态未定义";
                return;
            }

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

                var stBill = new st_bill
                {
                    billid = request.billInfo.billid
                };
                if (DbSqlHelper.SelectOne(cmd,stBill,"flag") != 1)
                {
                    rslt.ErrMsg = "未查找到所提交的违停记录，可能已被删除";
                    return;
                }
                if (stBill.flag != 0)
                {
                    rslt.ErrMsg = "该单据已处理，不能重复进行处理";
                    return;
                }
                using (cmd.Transaction = con.BeginTransaction())
                {
                    try
                    {
                        if (request.billInfo.flag == 2)
                        {
                            //撤销
                            stBill = new st_bill
                            {
                                billid = request.billInfo.billid,
                                flag = request.billInfo.flag,
                                auditdate = DateTime.Now,
                                auditemp = tokendata.username
                            };
                            DbSqlHelper.Update(cmd, stBill, "flag,auditdate,auditemp");
                        }
                        else
                        {
                            //处理保存
                            stBill = new st_bill
                            {
                                billid = request.billInfo.billid,
                                flag = request.billInfo.flag,
                                auditdate = request.billInfo.auditdate,
                                auditemp = tokendata.username,
                                moddate = DateTime.Now,
                                modemp = tokendata.username,
                                roadid = request.billInfo.roadid,
                                carnum = request.billInfo.carnum,
                                dscrp = request.billInfo.dscrp,
                                owner = request.billInfo.owner,
                                owner_tel = request.billInfo.owner_tel,
                                paytype = request.billInfo.paytype,
                                payamt = request.billInfo.payamt
                            };
                            DbSqlHelper.Update(cmd, stBill,
                                "flag,auditdate,auditemp,moddate,modemp,roadid,carnum,dscrp,owner,owner_tel,paytype,payamt");
                            //保存图片
                            //附件明细
                            MD5 md5 = new MD5();
                            var printid = 2;
                            cmd.CommandText = @"SELECT MAX(printid) AS maxPrintid FROM st_bill_mx WHERE billid = @billid";
                            cmd.Parameters.Clear();
                            cmd.Parameters.AddWithValue("@billid", stBill.billid);
                            using (var reader = cmd.ExecuteReader())
                            {
                                if (reader.Read())
                                {
                                    var maxPrint = Convert.ToInt32(reader["maxPrintid"]);
                                    if (maxPrint > 2)
                                    {
                                        printid = maxPrint;
                                    }
                                }
                            }
                            foreach (var file in request.billInfo.pictureList)
                            {
                                printid++;
                                // 格式为data:image/jpeg;base64,AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
                                if (file.base64 == null || !file.base64.StartsWith("data"))
                                {
                                    rslt.ErrMsg = "获取图片数据失败，请刷新后重试";
                                    return;
                                }
                                // 插入新附件
                                var base64 = file.base64;
                                var typeEndIndex = base64.IndexOf(";base64");
                                if (typeEndIndex <= 0)
                                {
                                    rslt.ErrMsg = "图片格式异常，请重新上传";
                                    return;
                                }
                                var fileData = Convert.FromBase64String(base64.Substring(typeEndIndex + 8));
                                var filemd5 = md5.GetMD5(fileData);
                                var filemap = new st_file { filemd5 = filemd5 };
                                if (DbSqlHelper.SelectOne(cmd, filemap, "filemd5") != 1)
                                {
                                    filemap.filedata = fileData;
                                    filemap.fileType = file.fileType;
                                    DbSqlHelper.Insert(cmd, filemap, "filemd5, filedata, fileType");
                                }
                                var billmx = new st_bill_mx
                                {
                                    billid = stBill.billid,
                                    printid = printid,
                                    filemd5 = filemd5
                                };
                                DbSqlHelper.Insert(cmd, billmx, "billid,printid,filemd5");
                            }
                        }

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