仲国强
2021-09-03 5a679187b1dbdfd030b4c99bb89fb7d9d6f82cde
WebAPI/Controllers/CJGL/Cj_StationOutBillController.cs
@@ -21,7 +21,7 @@
        SQLHelper.ClsCN oCN = new SQLHelper.ClsCN();
        DataSet ds;
        DataSet ds1;
        string fileip = System.Configuration.ConfigurationManager.AppSettings["FileIP"];
        /// <summary>
        /// 新增单据-保存按钮
@@ -39,6 +39,8 @@
                //反序列化
                msg1 = "[" + msg1.ToString() + "]";
                List<StationBill> list = Newtonsoft.Json.JsonConvert.DeserializeObject<List<StationBill>>(msg1);
                long HMainInterID = 0;
                HMainInterID = list[0].HMainInterID;
                string eventType = list[0].eventType;
                string BillType = "3791";
                string HBillSubType = "3791";
@@ -261,16 +263,16 @@
                    ",HGroupID,HDeptID,HEmpID,HBarCode,HAddr,HBarCodeMaker,HBarCodeMakeDate,HSourceID2,HSourceID3,HSourceID4,HSourceID5" +
                    ",HSupID,HQty,HPrice,HMoney,HBadCount,HCenterID,HProcNo,HOrderProcNO,HSourceNameList" +
                    ",HMainSourceInterID,HMainSourceBillNo,HMainSourceBillType,HLastSubProc" +
                    ",HEmpID2,HEmpID3,HEmpID4,HEmpID5,HDSQty,HChongQty,HPriceRate,HWorkTimes,HQCCheckID" +
                    ",HEmpID2,HEmpID3,HEmpID4,HEmpID5,HDSQty,HChongQty,HPriceRate,HWorkTimes,HQCCheckID,HMainInterID" +
                    ") " +
                    " values('" + BillType + "','" + HBillSubType + "'," + HInterID + ",'" + HBillNo + "'," + HBillStatus + ",getdate(),'" + HMaker + "',getdate(),'" + HMouldNum + "'" +
                    "," + HYear + "," + HPeriod + ",'" + HRemark + "','" + HSourceName + "'," + HPieceQty + "," + HWasterQty + "," + HPlanPieceQty + "," + HBadPNL +
                    "," + HICMOInterID + ",'" + HICMOBillNo + "'," + HProcPlanInterID + "," + HProcPlanEntryID + ",'" + HProcPlanBillNo + "'," + HProcExchInterID + "," + HProcExchEntryID +
                    ",'" + HProcExchBillNo + "'," + HMaterID + "," + HProcID + "," + HICMOQty + "," + HPlanQty + ",'" + HStationOutTime + "'," + HSourceID + "," + HPayProcID +
                    ",'" + HProcExchBillNo + "'," + HMaterID + "," + HProcID + "," + HICMOQty + "," + HPlanQty + ",getdate()," + HSourceID + "," + HPayProcID +
                    "," + HGroupID + "," + HDeptID + "," + HEmpID + ",'" + HBarCode + "','" + HAddr + "','" + HBarCodeMaker + "',getdate()" + "," + HSourceID2 + "," + HSourceID3 + "," + HSourceID4 + "," + HSourceID5 +
                    "," + HSupID + "," + HQty + "," + HPrice + "," + HMoney + "," + HBadCount + "," + HCenterID + "," + HProcNo + ",'" + HOrderProcNO + "'" + ",'" + HSourceNameList + "'" +
                    "," + HMainSourceInterID + ",'" + HMainSourceBillNo + "','" + HMainSourceBillType + "'," + Convert.ToString(HLastSubProc ? 1 : 0) +
                    "," + HEmpID2 + "," + HEmpID3 + "," + HEmpID4 + "," + HEmpID5 + "," + HDSQty + "," + HChongQty + "," + HPriceRate + "," + HWorkTimes + "," + HQCCheckID +
                    "," + HEmpID2 + "," + HEmpID3 + "," + HEmpID4 + "," + HEmpID5 + "," + HDSQty + "," + HChongQty + "," + HPriceRate + "," + HWorkTimes + "," + HQCCheckID + "," + HMainInterID +
                    ") ");
                }
                else if ("Modify".Equals(eventType))
@@ -390,15 +392,15 @@
        ///参数:string sql。
        ///返回值:object。
        /// </summary>
        [Route("Cj_StationOutBill/list")]
        [Route("Cj_StationOutBill/get_Display")]
        [HttpGet]
        public object list(string sWhere)
        public object get_Display(string sWhere)
        {
            try
            {
                if (sWhere == null || sWhere.Equals(""))
                {
                    ds = oCN.RunProcReturn("select * from h_v_MES_StationOutBillList " + sWhere, "h_v_MES_StationOutBillList");
                    ds = oCN.RunProcReturn("select * from h_v_MES_StationOutBillList " + sWhere + " order by 出站时间 desc", "h_v_MES_StationOutBillList");
                }
                else
                {
@@ -473,137 +475,247 @@
        }
        //
        #region  工序出站汇报单文件上传处理
        [Route("Cj_StationOutBill/UploadFile")]
        [Route("Cj_StationOutBill/UploadFile1")]
        [HttpPost]
        public object UploadFile()
        public object UploadFile1()
        {
            string HBillNo = HttpContext.Current.Request.Params["HBillNo"];  //单据号
            string HRemark = HttpContext.Current.Request.Params["HRemark"];  //备注
            string HUserName = HttpContext.Current.Request.Params["HUserName"];  //创建人
            HttpPostedFile files = HttpContext.Current.Request.Files["file"];
            string path = HttpContext.Current.Server.MapPath("~/../Files/" + HBillNo);
            //string path = @"D:\\Files\\"+ HBillNo;
            dynamic dyResult = UpLoadFile(files, path, HBillNo, HRemark, HUserName);
            if (dyResult != null && dyResult.result == 1)
            {
                objJsonResult.code = "1";
                objJsonResult.count = 1;
                objJsonResult.Message = "上传成功!";
                objJsonResult.data = null;
                return objJsonResult;
            }
            else
            {
                objJsonResult.code = "0";
                objJsonResult.count = 0;
                objJsonResult.Message = dyResult.returnval;
                objJsonResult.data = null;
                return objJsonResult;
            }
        }
        public dynamic UpLoadFile(HttpPostedFile files, string path, string HBillNo, string HRemark, string HUserName)
        {
            dynamic Result_Ob = new { result = 1, returnval = "上传成功!" };
            string filePath = Path.GetFullPath(files.FileName);//文件上传路径
            string fileExtension = Path.GetExtension(files.FileName);// 文件扩展名
            string filename = files.FileName;//文件名
            string fileSavePath = path;// 上传保存路径
            int filesize = files.ContentLength;//获取上传文件的大小单位为字节byte
            int Maxsize = 40000 * 1024;//定义上传文件的最大空间大小为40M
            try
            {
                if (files == null || files.ContentLength <= 0)
                {
                    Result_Ob = new { result = 0, returnval = "文件不能为空!" };
                    return Result_Ob;
                }
                if (filesize >= Maxsize)
                {
                    Result_Ob = new { result = 0, returnval = "上传文件超过40M,不能上传!" };
                    return Result_Ob;
                }
                string fileurl = Path.Combine(fileSavePath, filename);
                if (Directory.Exists(fileurl) == true)  //如果存在重名文件就提示
                {
                    Result_Ob = new { result = 0, returnval = "存在同名文件!" };
                    return Result_Ob;
                }
                //删除数据表数据
                ds = oCN.RunProcReturn("delete from MES_AccessoriesList where HSourceBillNo ='" + HBillNo + "' and HFileName='"+ filename + "'", "MES_AccessoriesList");
                if (Directory.Exists(path))
                {
                    File.Delete(fileurl);      //删除指定文件
                    files.SaveAs(fileurl);
                    string StrPath = "/files/"+ HBillNo+"/"+ filename;
                    if (File.Exists(fileurl))
                    {
                        //这里可以执行一些其它的操作,比如更新数据库
                        //写入数据表
                        oCN.RunProc("Insert into MES_AccessoriesList (HFileName,HFilePath,HFilePath_Cus,HFileType" +
                                    ",HLoadMan,HLoadDate,HRemark,HVerNum,HFileSize" +
                                    ",HFileClsID,HSourceBillNo" +
                                   ") values('"
                                    + filename.ToString() + "','" + StrPath.ToString() + "','" + filePath.ToString() + "','" + fileExtension.ToString() + "'" +
                                    ",'" + HUserName + "',getdate(),'" + HRemark + "','V1','" + filesize +
                                    "','" + 0 + "','" + HBillNo +
                                   "') ");
                    }
                    else
                    {
                        Result_Ob = new { result = 0, returnval = "上传失败!此文件为恶意文件" };
                    }
                }
                else
                {
                    Directory.CreateDirectory(fileSavePath); //添加文件夹
                    files.SaveAs(fileurl);
                    string StrPath = "/files/" + HBillNo + "/" + filename;
                    if (File.Exists(fileurl))
                    {
                        //这里可以执行一些其它的操作,比如更新数据库
                        //写入数据表
                        oCN.RunProc("Insert into MES_AccessoriesList (HFileName,HFilePath,HFilePath_Cus,HFileType" +
                                    ",HLoadMan,HLoadDate,HRemark,HVerNum,HFileSize" +
                                    ",HFileClsID,HSourceBillNo" +
                                   ") values('"
                                    + filename.ToString() + "','" + StrPath.ToString() + "','" + filePath.ToString() + "','" + fileExtension.ToString() + "'" +
                                    ",'" + HUserName + "',getdate(),'" + HRemark + "','V1','" + filesize +
                                    "','" + 0 + "','" + HBillNo +
                                   "') ");
                    }
                    else
                    {
                        Result_Ob = new { result = 0, returnval = "上传失败!此文件为恶意文件" };
                    }
                }
            }
            catch(Exception e)
            {
                Result_Ob = new { result = 0, returnval = e.Message };
            }
            return Result_Ob;
        }
        /// <summary>
        /// 输出json结果 返回2个值 result (输出succes的值)   returnval (输出str的值)
        /// </summary>
        /// <param name="context"></param>
        /// <param name="success">是否操作成功,0表示失败;1表示成功</param>
        /// <param name="str">输出字符串</param>
        protected void JsonResult(HttpContext context, int success, string str)
        {
            context.Response.Write("{\"result\" :\"" + success.ToString() + "\",\"returnval\" :\"" + str + "\"}");
            context.Response.End();
        }
        /// <summary>
        /// 根据单据号查找上传文件列表
        /// </summary>
        /// <param name="sWhere"></param>
        /// <returns></returns>
        [Route("Cj_StationOutBill/Filelist")]
        [HttpGet]
        public object Filelist(string HBillNo)
        {
            var url = fileip + "/files/" + HBillNo+"/";
            //@"C:\\files\\"
            try
            {
                ds = oCN.RunProcReturn("select *,'" + url + "'+CAST(HFileName as varchar(200))as url  from MES_AccessoriesList where HSourceBillNo='" + HBillNo+"'", "MES_AccessoriesList");
                objJsonResult.code = "1";
                objJsonResult.count = 1;
                objJsonResult.Message = "Sucess!";
                objJsonResult.data = ds.Tables[0];
                return objJsonResult;
            }
            catch (Exception e)
            {
                objJsonResult.code = "0";
                objJsonResult.count = 0;
                objJsonResult.Message = "Exception!" + e.ToString();
                objJsonResult.data = null;
                return objJsonResult;
            }
        }
        /// <summary>
        /// 根据ID,单据号,文件名 删除文件
        /// </summary>
        /// <param name="sWhere"></param>
        /// <returns></returns>
        [Route("Cj_StationOutBill/DeleteFilelist")]
        [HttpGet]
        public object DeleteFilelist(string HItemID, string HSourceBillNo,string HFileName)
        {
            try
            {
                int num = 0;
                string HBillNo = HttpContext.Current.Request.Params["HBillNo"];  //单据号
                string HRemark = HttpContext.Current.Request.Params["HRemark"];  //备注
                string HUserName = HttpContext.Current.Request.Params["HUserName"];  //单据号
                string uploadPath = HttpContext.Current.Server.MapPath("~/App_Data/");
                HttpRequest request = System.Web.HttpContext.Current.Request;
                HttpFileCollection files = request.Files;
                string savePath = "";
                if (files == null || files.Count <= 0)
                oCN.RunProc("delete from MES_AccessoriesList where HItemID =" + HItemID);
                string fileurl = Path.Combine(HttpContext.Current.Server.MapPath("~/../Files/" + HSourceBillNo), HFileName);
                File.Delete(fileurl);      //删除指定文件
                objJsonResult.code = "1";
                objJsonResult.count = 1;
                objJsonResult.Message = "删除成功!";
                objJsonResult.data = null;
                return objJsonResult;
            }
            catch (Exception e)
            {
                objJsonResult.code = "0";
                objJsonResult.count = 0;
                objJsonResult.Message = "Exception!" + e.ToString();
                objJsonResult.data = null;
                return objJsonResult;
            }
        }
        #endregion
        //批量出站
        #region 批量出站模块返回明细信息列表
        [Route("Cj_StationOutBill/get_BarCodeDetail")]
        [HttpGet]
        public object get_BarCodeDetail(string HMainInterID)
        {
            try
            {
                ds = oCN.RunProcReturn(
                    "select a.HBillNo,a.HInterID,a.HQty,a.HProcExchBillNo,a.HICMOBillNo" +
                    ",b.HNumber HMaterNumber,b.HName HMaterName,b.HModel HMaterModel,c.HName HSourceName,d.HName HGroupName" +
                    ",e.HName HEmpName,a.HMaker,a.HMakeDate " +
                    " from Sc_StationOutBillMain a " +
                    " left join Gy_Material b on a.HMaterID = b.HItemID " +
                    " left join Gy_Source c on a.HSourceID = c.HItemID " +
                    " left join Gy_Group d on a.HGroupID = d.HItemID " +
                    " left join Gy_Employee e on a.HEmpID = e.HItemID " +
                    " where HMainInterID = " + HMainInterID + " order by a.HMakeDate desc", "Sc_StationOutBillMain");
                if (ds == null || ds.Tables[0].Rows.Count == 0)
                {
                    objJsonResult.code = "0";
                    objJsonResult.count = 0;
                    objJsonResult.Message = "文件不能为空!";
                    objJsonResult.Message = "无返回记录!";
                    objJsonResult.data = null;
                    return objJsonResult;
                }
                else
                {
                    for (int i = 0; i < files.Count; i++)
                    {
                        // 获取文件
                        HttpPostedFile httpPostedFile = files[i];
                        string filePath = Path.GetFullPath(httpPostedFile.FileName);//文件上传路径
                        string fileExtension = Path.GetExtension(httpPostedFile.FileName);// 文件扩展名
                        string filename = httpPostedFile.FileName;
                        string fileSavePath = uploadPath;// 上传保存路径
                        int filesize = files[i].ContentLength;//获取上传文件的大小单位为字节byte
                        int Maxsize = 40000 * 1024;//定义上传文件的最大空间大小为40M
                        if (filesize >= Maxsize)
                        {
                            objJsonResult.code = "0";
                            objJsonResult.count = 0;
                            objJsonResult.Message = "上传文件超过40M,不能上传!";
                            objJsonResult.data = null;
                            return objJsonResult;
                        }
                        if (Directory.Exists(fileSavePath) == false)//如果不存在就创建file文件夹
                        {
                            Directory.CreateDirectory(fileSavePath); //添加文件夹
                        }
                        savePath = Path.Combine(fileSavePath, filename);
                        //查询文件关联表,是否已存在上传数据
                        ds = oCN.RunProcReturn("select * from MES_AccessoriesList where HSourceBillNo = " + HBillNo, "MES_AccessoriesList");
                        if (ds == null || ds.Tables[0].Rows.Count == 0)
                        {
                            //写入服务器文件
                            httpPostedFile.SaveAs(savePath);
                            //写入数据表
                            oCN.RunProc("Insert into MES_AccessoriesList (HFileName,HFilePath,HFilePath_Cus,HFileType" +
                                        ",HLoadMan,HLoadDate,HRemark,HVerNum,HFileSize" +
                                        ",HFileClsID,HSourceBillNo" +
                                       ") values('"
                                        + filename.ToString() + "','" + savePath.ToString() + "','" + filePath.ToString() + "','" + fileExtension.ToString()+ "'" +
                                        ",'" + HUserName + "',getdate(),'" + HRemark + "','V1','" + filesize +
                                        "','" + 0 + "','" + HBillNo +
                                       "') ");
                        }
                        else
                        {
                            //删除服务器文件
                            FileInfo file = new FileInfo(HttpContext.Current.Server.MapPath(savePath));
                            file.Delete();
                            //删除数据表数据
                            ds = oCN.RunProcReturn("delete from MES_AccessoriesList where HSourceBillNo =  " + HBillNo, "MES_AccessoriesList");
                            //写入服务器文件
                            httpPostedFile.SaveAs(savePath);
                            //写入数据表
                            //写入数据表
                            oCN.RunProc("Insert into MES_AccessoriesList (HFileName,HFilePath,HFilePath_Cus,HFileType" +
                                       ",HLoadMan,HLoadDate,HRemark,HVerNum,HFileSize" +
                                       ",HFileClsID,HSourceBillNo" +
                                      ") values('"
                                       + filename.ToString() + "','" + savePath.ToString() + "','" + filePath.ToString() + "','" + fileExtension.ToString() + "'" +
                                       ",'" + HUserName + "',getdate(),'" + HRemark + "','V1','" + filesize +
                                       "','" + 0 + "','" + HBillNo +
                                      "') ");
                        }
                        //if (Directory.Exists(savePath) == true)  //如果存在重名文件就提示
                        //{
                        //    objJsonResult.code = "0";
                        //    objJsonResult.count = 0;
                        //    objJsonResult.Message = "存在同名文件!";
                        //    objJsonResult.data = null;
                        //    return objJsonResult;
                        //}
                        httpPostedFile.SaveAs(savePath);
                        string StrPath = "/File/" + filename;
                        //写入数据表
                        num = num + 1;
                    }
                    if (num == files.Count)
                    {
                        objJsonResult.code = "0";
                        objJsonResult.count = 0;
                        objJsonResult.Message = "上传成功!";
                        objJsonResult.data = null;
                        return objJsonResult;
                    }
                    else
                    {
                        objJsonResult.code = "0";
                        objJsonResult.count = 0;
                        objJsonResult.Message = "上传失败!";
                        objJsonResult.data = null;
                        return objJsonResult;
                    }
                    objJsonResult.code = "1";
                    objJsonResult.count = 1;
                    objJsonResult.Message = "返回记录成功!";
                    objJsonResult.data = ds.Tables[0];
                    return objJsonResult;
                }
            }
            catch (Exception e)
            {
                objJsonResult.code = "0";
                objJsonResult.count = 0;
                objJsonResult.Message = e.Message;
                objJsonResult.Message = "查询数据异常,请与管理员联系!" + e.ToString();
                objJsonResult.data = null;
                return objJsonResult;
            }
        }
        #endregion
    }
}