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.Tools.Encry;

namespace JLHHJSvr.Excutor.APP
{
    internal sealed class SaveBillExcutor : ExcutorBase<SaveBillRequest, SaveBillResponse>
    {
        protected override void ExcuteInternal(SaveBillRequest request, object state, SaveBillResponse rslt)
        {
            var tokendata = BllHelper.GetToken(request.token);
            if (tokendata == null)
            {
                rslt.ErrMsg = "会话已经中断，请重新登录";
                return;
            }
            if (request.billInfo == null)
            {
                rslt.ErrMsg = "信息接收异常，请重试";
                return;
            }
            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 (request.billInfo.pictureList == null || request.billInfo.pictureList.Count == 0)
            {
                rslt.ErrMsg = "图片信息接收异常";
                return;
            }
            if (request.billInfo.pictureList.Count < 2)
            {
                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;
                }
            }
            using (var con = new SqlConnection(GlobalVar.ConnectionString))
            using (var cmd = con.CreateCommand())
            {
                con.Open();

                var carInfo = new st_car {carnum = request.billInfo.carnum};
                if (DbSqlHelper.SelectOne(cmd,carInfo,"owner, owner_tel") != 1)
                {
                    carInfo.owner = string.Empty;
                    carInfo.owner_tel = string.Empty;
                }
                using (cmd.Transaction = con.BeginTransaction())
                {
                    try
                    {
                        //主表
                        var stBill = new st_bill
                        {
                            billid = BllHelper.GetID(cmd, "st_bill"),
                            roadid = request.billInfo.roadid,
                            carnum = carInfo.carnum,
                            owner = carInfo.owner,
                            owner_tel = carInfo.owner_tel,
                            dscrp = request.billInfo.dscrp,
                            flag = 0,
                            opemp = tokendata.username,
                            opdate = DateTime.Now
                        };
                        DbSqlHelper.Insert(cmd, stBill,
                            "billid,roadid,roadname,carnum,owner,owner_tel,dscrp,flag,opemp,opdate");

                        //附件明细
                        MD5 md5 = new MD5();
                        var printid = 0;
                        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();
                        return;
                    }
                }
            }
        }
    }
}
