using log4net;
using Newtonsoft.Json;
using Quartz;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using JobAutoCreateFee.Model;
using System.IO;

namespace JobCreateFee
{
    public class JobD7BcEdiReply : IJob
    {
        private ILog log = LogManager.GetLogger(typeof(JobD7BcEdiReply));

        public void Execute(IJobExecutionContext context)
        {
            log.Debug($"Execute开始");
            try
            {
                string connStr = context.JobDetail.JobDataMap.GetString("ConnectString");
                string BcPath = context.JobDetail.JobDataMap.GetString("BcPath");



                string tempPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "BcTmp");
                //string BcPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "BcPath");

                if (!Directory.Exists(tempPath))
                {
                  Directory.CreateDirectory(tempPath);
                }



                string[] files = Directory.GetFiles(BcPath);
                if (files.Length > 0)
                {
                    foreach (string file in files)
                    {
                        FileInfo fileInfo = new FileInfo(file);
                        string pathtxt = File.ReadAllText(file);
                        var opList = pathtxt.Split('\'');
                        var MBLNO = "";
                        var EDISTATUS = "";
                        log.Debug($"文件名"+ fileInfo.Name);

                        foreach (var opstr in opList)
                        {
                            if (opstr.Length >= 10)
                            {
                                log.Debug($"" + opstr);
                                var head = opstr.TrimStart().Substring(0, 3);
                                if (head == "UCM")
                                {
                                    log.Debug($"UCM" + opstr);
                                    var headlist = opstr.Split('+');
                                    var fieldcount = 0;
                                    foreach (var str in headlist)
                                    {
                                        log.Debug($"UCMstr" + str);

                                        if (fieldcount == 1)
                                        {
                                            MBLNO = str;
                                            log.Debug($"业务编号:" + MBLNO);
                                        }
                                        fieldcount = fieldcount + 1;
                                    }
                                }
                            }
                        }
                        if (!string.IsNullOrEmpty(MBLNO))
                        {
                            log.Debug($"业务编号:" + MBLNO);
                            using (SqlConnection dbcon = new SqlConnection(connStr))
                            {
                                dbcon.Open();
                                var BSNO = GETBSNO(MBLNO, dbcon);

                                string sql = " insert op_status (ST_ID,BSNO,STATUS,ISCOMP,COMPTIME,COMPOP,INPUTTIME,STTYPE) Select NEWID(),BSNO,'订舱已接收',1,GETDATE(),'ADMIN',GETDATE(),'1' FROM OP_SEAE WHERE BSNO='"+BSNO+"'";
                                SqlCommand cmd = new SqlCommand(sql, dbcon);
                                cmd.ExecuteNonQuery();
                                dbcon.Close();
                            }

                            File.Copy(file, Path.Combine(tempPath, fileInfo.Name));
                            File.Delete(file);
                        }

                    }

                 }


            }
            catch (Exception ex)
            {
                log.Error(ex.Message);
                log.Error(ex.StackTrace);
            }


        }

        static public D7OPSEAE GETBSNO(string MBLNO, SqlConnection dbcon)
        {
            var OPSEAE = new D7OPSEAE();

            var strSql = new StringBuilder();
            strSql.Append("SELECT BSNO,BSSTATUS,FEESTATUS from op_seae where CUSTNO='" + MBLNO + "'");
            SqlDataAdapter adapter = new SqlDataAdapter(strSql.ToString(), dbcon);
            DataTable table = new DataTable();
            adapter.Fill(table);
            var BSNO = "";
            if (table.Rows.Count > 0)
            {
                foreach (DataRow row in table.Rows)
                {
                    OPSEAE.BSNO = row["BSNO"].ToString();
                    OPSEAE.FEESTATUS = Convert.ToBoolean(row["FEESTATUS"].ToString());
                    OPSEAE.BSSTATUS = Convert.ToBoolean(row["BSSTATUS"].ToString());

                }

            }


            return OPSEAE;
        }
    }
}