<%@ WebHandler Language="C#" Class="UploadHandler" %>

using System;
using System.Web;
using System.Data;
using System.Collections.Generic;
using System.Linq;
//using BizDataAccess;
using System.IO;
using System.Net;
using System.Web.Services;
using System.Text;
using System.Data.SqlClient;

using DataLinkMesWork;


public class UploadHandler : IHttpHandler
{
    private static string connectionString = System.Configuration.ConfigurationManager.AppSettings.GetValues("ConnectionString")[0].ToString();

    public void ProcessRequest(HttpContext context)
    {
        context.Response.ContentType = "text/plain";
        string filePath = "../webpage/file";
        filePath = HttpContext.Current.Server.MapPath(filePath);
        string invoiceNum = HttpContext.Current.Request["invoiceNum"];
        string money = HttpContext.Current.Request["money"];
        string supplierValue = HttpContext.Current.Request["supplierValue"];
        string paid = HttpContext.Current.Request["paid"];
        string unpaid = HttpContext.Current.Request["unpaid"];
        string tax = HttpContext.Current.Request["tax"];
        string isCost = HttpContext.Current.Request["isCost"];
        string remark = HttpContext.Current.Request["remark"];
        int applyOrderNum = 0; // 发票结算申请单流水号
        string result = "";
        bool isOK1;
        bool isOK2;
        try
        {
            Guid guid = Guid.NewGuid();
            string uid = guid.ToString().Replace("-", "").ToUpper();
            HttpFileCollection files = context.Request.Files;
            if (files.Count > 0)
            {
                for (int i = 0; i < files.Count; i++)
                {
                    HttpPostedFile hpFile = files[0];
                    if (hpFile.ContentLength > 0)
                    {
                        string fileName = System.IO.Path.GetFileName(hpFile.FileName);
                        string suffix = fileName.Split(new Char[] { '.' })[1];
                        string Name = fileName.Split(new Char[] { '.' })[0];
                        string procedureName = "车间采购管理_发票结算申请单_增加数据";
                        string errorMessage;
                        SqlParameter[] thisParms = new SqlParameter[9];
                        thisParms[0] = new SqlParameter("@发票结算申请单流水号", applyOrderNum);
                        thisParms[1] = new SqlParameter("@发票号", invoiceNum);
                        thisParms[2] = new SqlParameter("@发票金额", money);
                        thisParms[3] = new SqlParameter("@供应商流水号", supplierValue);
                        thisParms[4] = new SqlParameter("@已付款项", paid);
                        thisParms[5] = new SqlParameter("@尚欠金额", unpaid);
                        thisParms[6] = new SqlParameter("@税额", tax);
                        thisParms[7] = new SqlParameter("@是否费用类", isCost);
                        thisParms[8] = new SqlParameter("@备注", remark);
                        thisParms[0].Direction = ParameterDirection.Output;
                        isOK1 = SQLCommon.ExecuteStoredProcedure(procedureName, connectionString, ref thisParms, out errorMessage);
                        applyOrderNum = Convert.ToInt32(thisParms[0].Value);



                            
                                                    //--业务代码  1：
                        //-- 1 ：发货管理_附件
                        //-- 2 ：合同信息管理_履约保证金_履约保函
                        //-- 3 ：合同管理_文件路径
                        //-- 4 ：PDM_机床总图_文件列表
                        //-- 5 ：采购管理_采购发票_附件
                        //-- 6 ：车间采购管理_发票结算申请单
                        //-- 7 ：设备管理_设备监控_图片
                        //-- 8 ：车间生产管理_质检管理
                        //-- 9 ：人员管理_图片
                        //--10  tableName 


                        if (isOK1)
                        {
                            string sql2 = "insert into 车间采购管理_采购发票_附件 values(" + applyOrderNum + ",'" + uid + "','" + Name + "','" + suffix + "'," + '1' + ")";
                            isOK2 = SQLCommon.ExecuteNonQuery(sql2, connectionString, out result);
                            
                                

                                
                                
                            if (isOK2)
                            {
                                filePath = filePath + "/" + uid + "." + suffix;
                                hpFile.SaveAs(filePath);
                                result = "[{ \"result\":\"1\"}]";
                            }
                            else
                            {
                                result = "[{ \"result\":\"0\"}]";
                            }
                        }
                        else
                        {
                            result = "[{ \"result\":\"0\"}]";
                        }

                        try
                        {
                            //增加文件保存到数据库的功能
                            byte[]bytes = new byte[files[0].InputStream.Length];
                            files[0].InputStream.Read(bytes, 0, bytes.Length);
                            string procedureName1 = "_文件存储_高精_增加";
                            string errorMessage1;
                            SqlParameter []thisParms1 = new SqlParameter[5];
                            thisParms1[0] = new SqlParameter("@业务代码", 6);
                            thisParms1[1] = new SqlParameter("@文件标识", uid);
                            thisParms1[2] = new SqlParameter("@文件名称", Name);
                            thisParms1[3] = new SqlParameter("@文件后缀", suffix);
                            thisParms1[4] = new SqlParameter("@文件内容", bytes);
                            DataLinkMesWork.DataLink.ExecuteStoredProcedure(procedureName1, ref thisParms1, out errorMessage1);


                            if(DataLinkMesWork.DataLink.isBakup=="1")
                            {
                                isOK1 = DataLinkMesWork.DataLink.ExecuteStoredProcedure_Bakup(procedureName, ref thisParms, out errorMessage);
                                applyOrderNum = Convert.ToInt32(thisParms[0].Value);

                                DataLinkMesWork.DataLink.ExecuteStoredProcedure_Bakup(procedureName1, ref thisParms1, out errorMessage1);

                                if (isOK1)
                                {
                                    string sql2 = "insert into 车间采购管理_采购发票_附件 values(" + applyOrderNum + ",'" + uid + "','" + Name + "','" + suffix + "'," + '1' + ")";
                                    isOK2 = DataLinkMesWork.DataLink.ExecuteNonQuery_Bakup(sql2);
                                    if (isOK2)
                                    {
                                        //filePath = filePath + "/" + uid + "." + suffix;
                                        //hpFile.SaveAs(filePath);
                                        //result = "[{ \"result\":\"1\"}]";
                                    }
                                    else
                                    {
                                        //result = "[{ \"result\":\"0\"}]";
                                    }
                                }
                                else
                                {
                                    //result = "[{ \"result\":\"0\"}]";
                                }
                            }
                        }
                        catch
                        {

                        }


                    }
                    else
                    {
                        context.Response.Write(result);
                    }
                }
            }
        }
        catch (Exception ex)
        {
            result = "[{ \"result\":\"0\"}]";
        }
        context.Response.Write(result);
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
}
