- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Xml;
- using Yefor.Framework.ToolKit;
- using System.Data;
- using System.IO;
- namespace Yefor.CM.Business.Extra
- {
- public class PictureChange
- {
- #region 属性值
- private string xmlconfig = "";
- public string Xmlconfig
- {
- set { xmlconfig = value; }
- get { return xmlconfig; }
- }
- private string xmlEmpConfig = "";
- public string XmlEmpConfig
- {
- set { xmlEmpConfig = value; }
- get { return xmlEmpConfig; }
- }
- XmlDocument xmldoc = null;
- public enum taobaoState { AMState, PMState, defaultState };
- #endregion
- #region 方法
- public PictureChange()
- {
- //初始化
- }
- public PictureChange(string configPath)
- {
- xmlconfig = configPath;
- }
- public DataSet GetShopList()
- {
- xmldoc = new XmlDocument();
- xmldoc.Load(xmlconfig);
- DataSet ds = new DataSet();
- ds.ReadXml(new System.IO.StringReader(xmldoc.InnerXml));//转化为DATASET对象
- return ds;
- }
- /// <summary>
- /// 从指定的XML文件读出数据为dataset
- /// </summary>
- /// <param name="filePath">文件路径</param>
- /// <returns></returns>
- public DataSet GetShopList(string filePath)
- {
- xmldoc = new XmlDocument();
- xmldoc.Load(filePath);
- DataSet ds = new DataSet();
- ds.ReadXml(new System.IO.StringReader(xmldoc.InnerXml));//转化为DATASET对象
- return ds;
- }
- /// <summary>
- /// 获取指定目录下所有文件
- /// </summary>
- /// <param name="directoryPath"></param>
- /// <returns></returns>
- public IList<string> GetFileList(string directoryPath)
- {
- string PhyPath = System.Web.HttpContext.Current.Server.MapPath(directoryPath);
- DirectoryInfo dirinfo = new DirectoryInfo(PhyPath);
- FileInfo[] fino = dirinfo.GetFiles();
- IList<string> arrlist = new List<string>();
- foreach (FileInfo fn in fino)
- {
- arrlist.Add(directoryPath + fn.Name);
- }//遍历所有文件名,拼接虚拟路径
- return arrlist;
- }
- /// <summary>
- /// 从指定的empid复制到指定的shopid
- /// </summary>
- /// <param name="empid"></param>
- /// <param name="shopid"></param>
- /// <returns></returns>
- public bool ExcuteEmp(string empid, string shopid)
- {
- bool isSuccess = false;
- string sourcePath = InitSourcePath(empid);
- string targetPath = InitTargetPath(shopid);
- IList<string> sourceFileList = GetFileList(sourcePath);
- IList<string> targetFileList = GetFileList(targetPath);
- try
- {
- for (int i = 0; i < sourceFileList.Count; i++)
- {
- FileHelper.CopyFile(sourceFileList[i], targetFileList[i]);
- //System.Web.HttpContext.Current.Response.Write(targetFileList[i]);
- }
- isSuccess = true;
- }
- catch (Exception ex)
- {
- isSuccess = false;
- //throw new Exception(ex.Message);
- }
- return isSuccess;
- }
- /// <summary>
- /// 根据ID获取内容
- /// </summary>
- /// <param name="shopid"></param>
- /// <returns></returns>
- private string InitTargetPath(string shopid)
- {
- string xnNodeName = "now";
- xmldoc = new XmlDocument();
- xmldoc.Load(xmlconfig);//tbshopstate.xml
- return GetXmlNoteRoot(xmldoc,shopid, xnNodeName);
- }
- /// <summary>
- /// 找到指定的XML内容
- /// </summary>
- /// <param name="xdc"></param>
- /// <param name="id"></param>
- /// <param name="xnNodeName"></param>
- /// <returns></returns>
- private string GetXmlNoteRoot(XmlDocument xdc, string id, string xnNodeName)
- {
- string xmlTextPath = string.Empty;
- XmlNodeList xlist2 = xdc.SelectSingleNode("root").ChildNodes;
- foreach (XmlNode xn in xlist2)//找到shoplist
- {
- if (xn.Attributes["id"].Value == id)
- {
- xmlTextPath = xn[xnNodeName].InnerText;
- break;//找到就退出循环
- }
- }
- return xmlTextPath;
- }
- /// <summary>
- /// 根据指定的id初始化
- /// </summary>
- /// <param name="empid"></param>
- /// <returns></returns>
- private string InitSourcePath(string empid)
- {
- xmldoc = new XmlDocument();
- xmldoc.Load(xmlEmpConfig);//empConfig.xml
- string xnNodeName = "dirPath";
- return GetXmlNoteRoot(xmldoc, empid, xnNodeName);
- }
- /// <summary>
- /// 正常交接班的情况,递归算法
- /// </summary>
- /// <param name="shopid"></param>
- /// <param name="x"></param>
- /// <returns></returns>
- public bool XmlFileCopy(string shopid, taobaoState x)
- {
- bool isSuccess = false;
- IList<string> source = null;//源文件
- IList<string> target = null;//目标文件
- string[] picPath ={ "", "", "", "" };//四张图片的路径
- xmldoc = new XmlDocument();
- xmldoc.Load(xmlconfig);
- XmlNodeList xnlist = xmldoc.SelectSingleNode("root").ChildNodes;
- if (shopid == "000")
- {
- //所有店铺均复制各自设定的代码
- foreach (XmlNode xnd in xnlist)//遍历所有店铺ID,执行覆盖操作
- {
- if (xnd.Attributes["id"].Value == "000")//防止进入死循环
- {
- continue;
- }
- XmlFileCopy(xnd.Attributes["id"].Value, x);
- }
- return true;
- }
- else//指定店铺操作
- {
- foreach (XmlNode xn in xnlist)
- {
- if (xn.Attributes["id"].Value == shopid)
- {
- picPath[0] = xn["now"].InnerText;
- picPath[1] = xn["picGroupA"].InnerText;
- picPath[2] = xn["picGroupB"].InnerText;
- picPath[3] = xn["picGroupC"].InnerText;
- break;//找到所需值,退出循环
- }
- }//找到该店铺的图片
- source = GetFileList(picPath[Convert.ToInt32(x) + 1]);//Source Directory File List
- target = GetFileList(picPath[0]);//target Directory File List
- }
- try
- {
- //if (source.Count != target.Count)
- //{
- // throw new Exception("源文件数量和目标文件数量不一致");
- //}
- for (int i = 0; i < source.Count; i++)
- {
- FileHelper.CopyFile(source[i], target[i]);
- }
- isSuccess = true;
- }
- catch (Exception ex)
- {
- //throw new Exception(ex.Message);
- isSuccess = false;
- }
- return isSuccess;
- }
- #endregion
- }
- }