using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ICSSoft.Frame.Data.Entity;
using System.ComponentModel;
using System.IO;
using Newtonsoft.Json.Linq;
namespace ICSSoft.Frame.Common
{
public class File4Kod
{
///
/// 下载,保存到项目根目录,路径/文件名与可道云一致,同时将信息保存到按钮tag中.
/// 非长连接,每次都执行:登录-下载-登出.
///
/// 物料编码,用于文件夹规则匹配
/// 客户料号,用于文件夹规则匹配
/// 可道云维护的基本信息
/// 按钮tag,保存图纸类型,本地路径,远端路径
/// 工序次序,工序图纸和刀具图纸文件名匹配规则时使用
/// 是否是测试,测试阶段取固定'客户料号'下的图纸
public static void DownLoad(string ITEMCODE, string custITEMCODE, KODFolder KodSetting, BtnTagPDF tag, int OPSEQ, bool test)
{
KodboxHelper kodhelper = new KodboxHelper(KodSetting.URL);
string filename = "";
string DrawingFolder = "";
string fullFilename = "";
//路径:
//URL/Dispalypath/物料1~3/物料4~6_物料1~3/客户料号/图纸文件夹/图纸.pdf
string SubDic1 = ITEMCODE.Substring(0, 3) + "/" + ITEMCODE.Substring(3, 3) + "_" + ITEMCODE.Substring(0, 3);// +"/" + custITEMCODE;
string SubDic2 = custITEMCODE;
string SubDic3 = "";
switch (tag.type)
{
case DrawingType.General:
DrawingFolder = KodSetting.General;
break;
case DrawingType.OP:
DrawingFolder = KodSetting.OP;
break;
case DrawingType.Cutters:
DrawingFolder = KodSetting.Cutters;
break;
case DrawingType.SOP:
DrawingFolder = KodSetting.SOP;
break;
case DrawingType.SIP:
DrawingFolder = KodSetting.SIP;
break;
default:
break;
}
SubDic2 = Path.Combine(SubDic2, DrawingFolder);
if (test)
{
KodSetting.PATH = @"{source:91040000922230}/";
SubDic1 = @"B58/407_B58/";
SubDic2 = "3AAM1087001";
SubDic3 = DrawingFolder;
}
try
{
//1.登录
string msgKod = kodhelper.Login(KodSetting.USER, KodSetting.PWD);
JObject o = (JObject)JToken.Parse(msgKod);
msgKod = o["data"].ToString();
if (msgKod != "ok")
{
throw new Exception(msgKod);
}
try
{
string searchResJson = "";
//2.获取路径
string sourse = "";
string dispalypath = "";
string savepath = "";
string filenameMatch = OPSEQ.ToString().PadLeft(3, '0');
List listRes = kodhelper.GetSourcePath(KodSetting.PATH, "pdf,xls,xlsx", SubDic1, SubDic2, SubDic3, out searchResJson);
foreach (string res in listRes)
{
bool m = false;
sourse = res.Split('|')[0];
filename = res.Split('|')[1];
dispalypath = res.Split('|')[2];
switch (tag.type)
{
case DrawingType.OP:
if (filename.Substring(0, filename.LastIndexOf('.')).EndsWith(filenameMatch))
{
m = true;
}
break;
case DrawingType.Cutters:
if (filename.StartsWith(filenameMatch))
{
m = true;
}
break;
default:
break;
}
if (m)
{
break;
}
}
savepath = System.AppDomain.CurrentDomain.BaseDirectory + "\\" + dispalypath;
if (!Directory.Exists(savepath))
{
Directory.CreateDirectory(savepath);
}
fullFilename = Path.Combine(savepath, filename);
if (string.IsNullOrEmpty(tag.local) || tag.local.ToString() != fullFilename || !File.Exists(fullFilename))
{
//3.下载
string downres = kodhelper.DownFile(sourse, savepath, filename);
if (downres != "OK")
{
throw new Exception(downres);
}
}
tag.remote = sourse;
tag.local = fullFilename;
}
finally
{
//4.登出
msgKod = kodhelper.Logout(false);
}
}
finally
{
//5.释放
kodhelper.Dispose();
}
}
public static void DownLoad(KODFolder KodSetting, string sourse, string savePath, string saveName)
{
KodboxHelper kodhelper = new KodboxHelper(KodSetting.URL);
try
{
//登录
string msgKod = kodhelper.Login(KodSetting.USER, KodSetting.PWD);
JObject o = (JObject)JToken.Parse(msgKod);
msgKod = o["data"].ToString();
if (msgKod != "ok")
{
throw new Exception(msgKod);
}
try
{
string fullfilepath = "";
if (!Directory.Exists(savePath))
{
Directory.CreateDirectory(savePath);
}
fullfilepath = Path.Combine(savePath, saveName);
//下载
string downres = kodhelper.DownFile(sourse, savePath, saveName);
if (downres != "OK")
{
throw new Exception(downres);
}
}
finally
{
//登出
msgKod = kodhelper.Logout(false);
}
}
finally
{
//释放
kodhelper.Dispose();
}
}
public class BtnTagPDF
{
public DrawingType type { get; set; }
public string remote { get; set; }
public string local { get; set; }
}
public enum DrawingType
{
[Description("总图")]
[DescriptionEn("General Drawing")]
General,
[Description("工序图")]
[DescriptionEn("Process Diagram")]
OP,
[Description("刀具单")]
[DescriptionEn("Cutters")]
Cutters,
[Description("作业指导书")]
[DescriptionEn("Standard Operation Procedure")]
SOP,
[Description("检验指导书")]
[DescriptionEn("Standard Inspection Procedure")]
SIP
}
private class DescriptionEn : Attribute
{
private string _showName;
public DescriptionEn(string desc)
{
_showName = desc;
}
public string Description
{
get
{
return _showName;
}
}
}
}
}