Нет описания

MaterialManageController.cs 24KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582
  1. using CallCenter.Utility;
  2. using CallCenterApi.Common;
  3. using CallCenterApi.DB;
  4. using CallCenterApi.Interface.Controllers.Base;
  5. using CallCenterApi.Interface.Models.Input;
  6. using CallCenterApi.Model;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Data;
  10. using System.IO;
  11. using System.Linq;
  12. using System.Text.RegularExpressions;
  13. using System.Web;
  14. using System.Web.Mvc;
  15. namespace CallCenterApi.Interface.Controllers.workorder
  16. {
  17. public class MaterialManageController : BaseController
  18. {
  19. BLL.T_Wo_MaterialManage mmBLL = new BLL.T_Wo_MaterialManage();
  20. BLL.T_Sys_DictionaryValue dvBLL = new BLL.T_Sys_DictionaryValue();
  21. BLL.T_Sys_UserAccount userBLL = new BLL.T_Sys_UserAccount();
  22. // 获取物料列表
  23. public ActionResult GetList(string MaterialCode, string MaterialName, string Model, string Specs, string Brand, string Pipeline, int pageindex = 1, int pagesize = 10)
  24. {
  25. string sql = " and F_IsDelete=0";
  26. #region 筛选
  27. if (!string.IsNullOrWhiteSpace(MaterialCode))
  28. {//先验证物料编码为11位纯数字,再查询
  29. if (!valcode(MaterialCode))
  30. return Error("物料编号必须是11位数字");
  31. sql += " and F_MaterialCode='" + MaterialCode + "'";
  32. }
  33. if(!string.IsNullOrWhiteSpace(MaterialName))
  34. {
  35. sql += " and F_MaterialName like '%" + MaterialName + "%'";
  36. }
  37. if (!string.IsNullOrWhiteSpace(Model))
  38. {
  39. sql += " and F_Model like '%" + Model + "%'";
  40. }
  41. if (!string.IsNullOrWhiteSpace(Specs))
  42. {
  43. sql += " and F_Specs like '%" + Specs + "%'";
  44. }
  45. if (!string.IsNullOrWhiteSpace(Brand))
  46. {
  47. sql += " and F_Brand like '%" + Brand + "%'";
  48. }
  49. if (!string.IsNullOrWhiteSpace(Pipeline))
  50. {
  51. sql += " and F_Pipeline like '%" + Pipeline + "%'";
  52. }
  53. #endregion
  54. int recordCount = 0;
  55. DataTable dt = BLL.PagerBLL.GetListPager(
  56. "T_Wo_MaterialManage",
  57. "F_Id",
  58. "*,dbo.GetUserName(F_CreateBy) as F_CreateByName",
  59. sql,
  60. "ORDER BY F_Id desc",
  61. pagesize,
  62. pageindex,
  63. true,
  64. out recordCount);
  65. var obj = new
  66. {
  67. rows = dt,
  68. total = recordCount
  69. };
  70. return Content(obj.ToJson());
  71. }
  72. //根据物料编码获取物料信息
  73. public ActionResult GetCodeModel(string code)
  74. {
  75. if (code != null && code.Trim() != "")
  76. {
  77. Model.T_Wo_MaterialManage dModel = mmBLL.GetModel(code.Trim());
  78. if (dModel != null)
  79. {
  80. var dvList = dvBLL.GetModelList(" F_DictionaryFlag in ('WLPP','WLFHFFX','WLCPX') ");
  81. var createusername = userBLL.GetModel(dModel.F_CreateBy);
  82. var obj = new
  83. {
  84. model = dModel,
  85. CreateByName = createusername
  86. };
  87. return Success("获取物料信息成功", obj);
  88. }
  89. else
  90. {
  91. return Success(null );
  92. }
  93. }
  94. else
  95. {
  96. return Success("请输入物料编码", null);
  97. }
  98. }
  99. //获取物料
  100. public ActionResult GetModel(string id)
  101. {
  102. if (id != null && id.Trim() != "")
  103. {
  104. Model.T_Wo_MaterialManage dModel = mmBLL.GetModel(int.Parse(id.Trim()));
  105. if (dModel != null)
  106. {
  107. var dvList = dvBLL.GetModelList(" F_DictionaryFlag in ('WLPP','WLFHFFX','WLCPX') ");
  108. var createusername = userBLL.GetModel(dModel.F_CreateBy);
  109. var obj = new
  110. {
  111. model = dModel,
  112. CreateByName = createusername
  113. };
  114. return Success("获取物料信息成功", obj);
  115. }
  116. else
  117. {
  118. return Error("获取物料信息失败");
  119. }
  120. }
  121. else
  122. {
  123. return Error("获取参数失败");
  124. }
  125. }
  126. //[Authority]
  127. //添加物料
  128. public int Add(string materialcode,string materialname,string model,string specs,
  129. string level1,string level2,string level3,string brand,string materialeffect,string
  130. craft,string pipeline,string remark,int id=0)
  131. {
  132. Model.T_Wo_MaterialManage dModel = new Model.T_Wo_MaterialManage();
  133. if (id >0)
  134. dModel = new BLL.T_Wo_MaterialManage().GetModel(id);
  135. #region 验证物料编码为11位纯数字
  136. #endregion
  137. #region
  138. dModel.F_MaterialCode = materialcode;//物料编号
  139. dModel.F_MaterialName = materialname;//物料名称
  140. dModel.F_Model = model;//型号
  141. dModel.F_Specs = specs;//规格
  142. dModel.F_Level1 = level1;//一级分类
  143. dModel.F_Level2 = level2;//二级分类
  144. dModel.F_Level3 = level3;//三级分类
  145. dModel.F_Brand = brand;//品牌
  146. dModel.F_MaterialEffect = materialeffect;//复合肥肥效
  147. dModel.F_Craft = craft;//工艺
  148. dModel.F_Pipeline = pipeline;//产品线
  149. dModel.F_Remark = remark;
  150. dModel.F_IsDelete = 0;
  151. dModel.F_CreateOn = DateTime.Now;
  152. dModel.F_CreateBy ="系统外添加";
  153. #endregion
  154. if (id >0)
  155. {
  156. bool b = mmBLL.Update(dModel);
  157. if (b)
  158. {
  159. addoptlog("系统外添加", DTRequest.GetIP(), EnumOpt.edit.ToString(), dModel.F_Id.ToString(), "编辑成功,物料编号为" + dModel.F_MaterialCode + "的资料");
  160. return id ;
  161. }
  162. else
  163. {
  164. return 0;
  165. }
  166. }
  167. else
  168. {
  169. int b = mmBLL.Add(dModel);
  170. if (b > 0)
  171. {
  172. addoptlog("系统外添加", DTRequest.GetIP(), EnumOpt.add.ToString(), b.ToString(), "添加成功,物料编号为" + dModel.F_MaterialCode + "的资料");
  173. return b;
  174. }
  175. else
  176. {
  177. return 0;
  178. }
  179. }
  180. }
  181. //[Authority]
  182. //添加物料
  183. public ActionResult AddModel(WoMaterialManageInput input)
  184. {
  185. Model.T_Wo_MaterialManage dModel = new Model.T_Wo_MaterialManage();
  186. #region 验证物料编码为11位纯数字
  187. if (string.IsNullOrEmpty(input.F_MaterialCode))
  188. {
  189. return Error("请输入物料编码");
  190. }
  191. if (!string.IsNullOrWhiteSpace(input.F_MaterialCode))
  192. {
  193. if (!valcode(input.F_MaterialCode))
  194. return Error("物料编号必须是11位数字");
  195. }
  196. #endregion
  197. #region
  198. dModel.F_MaterialCode = input.F_MaterialCode;//物料编号
  199. dModel.F_MaterialName = input.F_MaterialName;//物料名称
  200. dModel.F_Model = input.F_Model;//型号
  201. dModel.F_Specs = input.F_Specs;//规格
  202. dModel.F_Level1 = input.F_Level1;//一级分类
  203. dModel.F_Level2 = input.F_Level2;//二级分类
  204. dModel.F_Level3 = input.F_Level3;//三级分类
  205. dModel.F_Brand = input.F_Brand;//品牌
  206. dModel.F_MaterialEffect = input.F_MaterialEffect;//复合肥肥效
  207. dModel.F_Craft = input.F_Craft;//工艺
  208. dModel.F_Pipeline = input.F_Pipeline;//产品线
  209. dModel.F_Remark = input.F_Remark;
  210. dModel.F_IsDelete = 0;
  211. dModel.F_CreateOn = DateTime.Now;
  212. dModel.F_CreateBy = CurrentUser.UserData.F_UserCode;
  213. #endregion
  214. int b = mmBLL.Add(dModel);
  215. if (b > 0)
  216. {
  217. addoptlog(CurrentUser.UserData.F_UserCode, DTRequest.GetIP(), EnumOpt.add.ToString(), b.ToString(), "添加成功,物料编号为"+ dModel.F_MaterialCode+"的资料");
  218. return Success("添加成功");
  219. }
  220. else
  221. {
  222. return Error("添加失败");
  223. }
  224. }
  225. //[Authority]
  226. //编辑物料
  227. public ActionResult EditModel(WoMaterialManageInput input)
  228. {
  229. if (input.F_Id > 0)
  230. {
  231. Model.T_Wo_MaterialManage dModel = mmBLL.GetModel(input.F_Id);
  232. if (dModel != null)
  233. {
  234. #region 验证物料编码为11位纯数字
  235. if (string.IsNullOrWhiteSpace(input.F_MaterialCode))
  236. return Error("物料编号不能为空");
  237. if (!valcode(input.F_MaterialCode))
  238. return Error("物料编号必须是11位数字");
  239. #endregion
  240. #region
  241. dModel.F_MaterialCode = input.F_MaterialCode;//物料编号
  242. dModel.F_MaterialName = input.F_MaterialName;//物料名称
  243. dModel.F_Model = input.F_Model;//型号
  244. dModel.F_Specs = input.F_Specs;//规格
  245. dModel.F_Level1 = input.F_Level1;//一级分类
  246. dModel.F_Level2 = input.F_Level2;//二级分类
  247. dModel.F_Level3 = input.F_Level3;//三级分类
  248. dModel.F_Brand = input.F_Brand;//品牌
  249. dModel.F_MaterialEffect = input.F_MaterialEffect;//复合肥肥效
  250. dModel.F_Craft = input.F_Craft;//工艺
  251. dModel.F_Pipeline = input.F_Pipeline;//产品线
  252. dModel.F_Remark = input.F_Remark;
  253. dModel.F_UpdateBy = CurrentUser.UserData.F_UserCode;
  254. dModel.F_UpdateOn = DateTime.Now;
  255. dModel.F_UpdateCount += 1;
  256. #endregion
  257. bool b = mmBLL.Update(dModel);
  258. if (b)
  259. {
  260. addoptlog(CurrentUser.UserData.F_UserCode, DTRequest.GetIP(), EnumOpt.edit.ToString(), dModel.F_Id.ToString(), "编辑成功,物料编号为" + dModel.F_MaterialCode + "的资料");
  261. return Success("编辑成功");
  262. }
  263. else
  264. {
  265. return Success("编辑失败");
  266. }
  267. }
  268. else
  269. return Error("获取信息失败");
  270. }
  271. else
  272. {
  273. return Error("获取参数失败");
  274. }
  275. }
  276. //[Authority]
  277. //批量删除物料
  278. public bool Del( string ids)
  279. {
  280. string optuser = "系统外删除";
  281. DateTime dtnow = DateTime.Now;
  282. if (new BLL.T_Wo_MaterialManage().DeleteList(ids, optuser, dtnow))
  283. {
  284. if (ids.Length > 1)
  285. addoptlog(optuser, DTRequest.GetIP(), EnumOpt.deletion.ToString(), ids, "批量删除成功");
  286. else
  287. addoptlog(optuser, DTRequest.GetIP(), EnumOpt.delete.ToString(), ids, "删除成功");
  288. return true ;
  289. }
  290. else
  291. return false ;
  292. }
  293. //[Authority]
  294. //批量删除物料
  295. public ActionResult DelModels(string[] ids)
  296. {
  297. if (ids != null && ids.Length > 0)
  298. {
  299. string idd = " ";
  300. foreach (string str in ids)
  301. {
  302. idd += str + ",";
  303. }
  304. string optuser = CurrentUser.UserData.F_UserCode;
  305. DateTime dtnow = DateTime.Now;
  306. if (new BLL.T_Wo_MaterialManage().DeleteList(idd.TrimEnd(','), optuser, dtnow))
  307. {
  308. if (ids.Length > 1)
  309. addoptlog(optuser, DTRequest.GetIP(), EnumOpt.deletion.ToString(), idd.TrimEnd(','), "批量删除成功");
  310. else
  311. addoptlog(optuser, DTRequest.GetIP(), EnumOpt.delete.ToString(), idd.TrimEnd(','), "删除成功");
  312. return Success("删除成功");
  313. }
  314. else
  315. return Error("删除失败");
  316. }
  317. else
  318. {
  319. return Error("获取参数失败");
  320. }
  321. }
  322. /// <summary>
  323. /// 导入excel
  324. /// </summary>
  325. /// <returns></returns>
  326. public ActionResult ImportExcel()
  327. {
  328. string userscode = CurrentUser.UserData.F_UserCode;
  329. string ip = DTRequest.GetIP();
  330. HttpPostedFile _upFile = RequestString.GetFile("upFile");
  331. if (_upFile != null)
  332. {
  333. int headrow = 0;
  334. #region 上传文件
  335. string filepath = "";
  336. string datepath = DateTime.Now.ToString("yyyyMMddHHMMss");
  337. string aLastName = Path.GetExtension(_upFile.FileName);
  338. string oriname = Path.GetFileNameWithoutExtension(_upFile.FileName);
  339. if (aLastName != ".xls" && aLastName != ".xlsx")
  340. {
  341. return Error("文件类型错误,请选择Excel文件");
  342. }
  343. string newpath = datepath + "_" + _upFile.FileName;
  344. if (!Directory.Exists(Server.MapPath(this.Request.ApplicationPath + "\\ExcelData")))
  345. {
  346. Directory.CreateDirectory(Server.MapPath(this.Request.ApplicationPath + "\\ExcelData"));
  347. }
  348. filepath = this.Request.ApplicationPath + "/ExcelData/" + newpath;
  349. string PhysicalPath = Server.MapPath(filepath);
  350. _upFile.SaveAs(PhysicalPath);
  351. #endregion
  352. #region 添加附件日志
  353. Model.T_Sys_Accessories model_T_Sys_Accessories = new Model.T_Sys_Accessories();
  354. model_T_Sys_Accessories.F_AddTime = DateTime.Now;//上传时间
  355. model_T_Sys_Accessories.F_FileName = newpath;//附件名称
  356. model_T_Sys_Accessories.F_FileType = aLastName;//附件类型
  357. model_T_Sys_Accessories.F_FileUrl = filepath;//附件地址
  358. model_T_Sys_Accessories.F_UserCode = userscode;//上传人
  359. model_T_Sys_Accessories.F_OriName = oriname;
  360. int fid = new BLL.T_Sys_Accessories().Add(model_T_Sys_Accessories);
  361. #endregion
  362. NPOIHelper np = new NPOIHelper();
  363. DataTable dt = np.ExcelToTable(_upFile, headrow);
  364. string msg = string.Empty;
  365. if (dt == null || dt.Rows.Count == 0)
  366. return Error("文件没有数据");
  367. else
  368. {
  369. foreach (DataRow dr in dt.Rows)
  370. {
  371. #region 数据入库
  372. headrow = headrow + 1;
  373. if (dr["物料编码"].ToString() != "" && valcode(dr["物料编码"].ToString()))
  374. {
  375. if (!getunique(0, dr["物料编码"].ToString()))
  376. {
  377. Model.T_Wo_MaterialManage dModel = new Model.T_Wo_MaterialManage();
  378. dModel.F_MaterialCode = dr["物料编码"].ToString();//物料编码
  379. dModel.F_MaterialName = dr["物料名称"].ToString();//物料名称
  380. dModel.F_Model = dr["型号"].ToString();//型号
  381. dModel.F_Specs = dr["规格"].ToString();//规格
  382. dModel.F_Level1 = dr["一级分类"].ToString();//一级分类
  383. dModel.F_Level2 = dr["二级分类"].ToString();//二级分类
  384. dModel.F_Level3 = dr["三级分类"].ToString();//三级分类
  385. dModel.F_Brand = dr["品牌"].ToString();//品牌
  386. dModel.F_MaterialEffect = dr["复合肥肥效"].ToString();//复合肥肥效
  387. dModel.F_Pipeline = dr["产品线"].ToString();//产品线
  388. dModel.F_Craft = dr["工艺"].ToString();//工艺
  389. dModel.F_Remark = dr["备注"].ToString();//工艺
  390. dModel.F_CreateBy = userscode;
  391. dModel.F_CreateOn = DateTime.Now;
  392. dModel.F_IsDelete = 0;
  393. var res = mmBLL.Add(dModel);
  394. if (res > 0)
  395. {
  396. addoptlog(userscode, ip, EnumOpt.import.ToString(), res.ToString(), "导入成功,导入的文件为:" + newpath + ",当前为第" + headrow + "行");
  397. }
  398. else
  399. {
  400. msg = msg + "第" + headrow + "行,导入失败<br>";
  401. }
  402. }
  403. else
  404. {
  405. msg = msg + "第" + headrow + "行,物料编码重复,未导入<br>";
  406. }
  407. }
  408. else
  409. msg = msg + "第" + headrow + "行,物料编码为空或格式不正确,未导入<br>";
  410. #endregion
  411. }
  412. if (string.IsNullOrEmpty(msg))
  413. return Success("导入成功 ");
  414. else
  415. return Error(msg);
  416. }
  417. }
  418. return Error("文件不能为空");
  419. }
  420. /// <summary>
  421. /// 验证客户编号是否唯一
  422. /// </summary>
  423. public bool getunique(int id, string code)
  424. {
  425. var sql = " F_IsDelete=0 ";
  426. sql += "and (F_MaterialCode='" + code + "')";
  427. if (id > 0)
  428. sql += " and F_Id<>" + id;
  429. var count = mmBLL.GetModelList(sql).Count();
  430. return count > 0;
  431. }
  432. #region 上传附件
  433. public DataTable GetFileData(string ids, string prefix)
  434. {
  435. DataTable dt = new DataTable();
  436. if (!string.IsNullOrEmpty(ids))
  437. {
  438. dt = DbHelperSQL.Query("select * from T_Sys_Accessories where F_FileId in (" + ids + ")").Tables[0];
  439. foreach (DataRow dr in dt.Rows)
  440. {
  441. dr["F_FileUrl"] = prefix + dr["F_FileUrl"].ToString();
  442. }
  443. }
  444. return dt;
  445. }
  446. /// <summary>
  447. /// 上传附件
  448. /// </summary>
  449. /// <returns></returns>
  450. [Authority]
  451. public ActionResult UpLoadProcess(string id, string name, string type, string lastModifiedDate, int size, HttpPostedFileBase file)
  452. {
  453. try
  454. {
  455. if (file == null) return Error("参数传入失败");
  456. if (Request.Files.Count == 0) return Error("保存失败");
  457. string userCode = CurrentUser.UserData.F_UserCode;
  458. string filePathName = string.Empty;
  459. #region 保存文件到本地路径
  460. string path = "/Upload/Files/" + DateTime.Now.ToString("yyyy/MM/dd") + "/";
  461. string localPath = Server.MapPath(Path.Combine(HttpRuntime.AppDomainAppPath, path));
  462. string ex = Path.GetExtension(file.FileName);
  463. string oriname = Path.GetFileNameWithoutExtension(file.FileName);
  464. filePathName = DateTime.Now.ToString("yyyyMMddHHmmssfff") + "_" + Guid.NewGuid().ToString("N") + ex;
  465. if (!System.IO.Directory.Exists(localPath))
  466. {
  467. System.IO.Directory.CreateDirectory(localPath);
  468. }
  469. string physicalpath = Path.Combine(localPath, filePathName);
  470. file.SaveAs(physicalpath);
  471. #endregion
  472. #region 读取配置的上传路径-原文件和修改过格式的文件均上传至此
  473. string savedir = Configs.GetValue("saveloc");
  474. if (!System.IO.Directory.Exists(savedir))
  475. {
  476. System.IO.Directory.CreateDirectory(savedir);
  477. }
  478. #endregion
  479. if (!string.IsNullOrEmpty(physicalpath))
  480. {
  481. #region 上传到ftp
  482. uploadFile upfile = new uploadFile();
  483. //通过读取配置文件,获取数据库
  484. string _ftp = Configs.GetValue("ftp");
  485. string _acc = Configs.GetValue("account");
  486. string _pwd = Configs.GetValue("password");
  487. upfile.ftpPath = _ftp;
  488. upfile.ftpUserID = _acc;
  489. upfile.ftpPassword = _pwd;
  490. //20180207 原文件同时保存
  491. string uploadBeforres = upfile.UploadLocalToFtp(physicalpath);
  492. #endregion
  493. if (uploadBeforres.Equals("上传成功!"))
  494. {
  495. #region 添加日志
  496. Model.T_Sys_Accessories model_T_Sys_Accessories = new Model.T_Sys_Accessories();
  497. model_T_Sys_Accessories.F_AddTime = DateTime.Now;//上传时间
  498. model_T_Sys_Accessories.F_FileName = filePathName;//附件名称
  499. model_T_Sys_Accessories.F_FileType = type;//附件类型
  500. model_T_Sys_Accessories.F_FileUrl = (filePathName).Replace('\\', '/');//附件地址
  501. model_T_Sys_Accessories.F_Size = size;
  502. model_T_Sys_Accessories.F_UserCode = userCode;//上传人
  503. model_T_Sys_Accessories.F_OriName = oriname;
  504. int fid = new BLL.T_Sys_Accessories().Add(model_T_Sys_Accessories);
  505. #endregion
  506. if (fid > 0)
  507. {
  508. //返回附件的ID
  509. model_T_Sys_Accessories.F_FileId = fid;//修改为返回对象以便查看图片
  510. return Success("文件上传成功", model_T_Sys_Accessories);
  511. }
  512. else
  513. return Success("文件上传成功,日志记录失败");
  514. }
  515. else
  516. return Error("文件上传失败,请重新上传,失败原因:" + uploadBeforres);
  517. }
  518. else
  519. return Error("格式修改出错,请重新上传");
  520. }
  521. catch (Exception ex)
  522. {
  523. return Error(ex.Message);
  524. }
  525. }
  526. #endregion
  527. #region 私有方法
  528. /// <summary>
  529. /// 验证物料编码为11位纯数字
  530. /// </summary>
  531. public bool valcode(string code)
  532. {
  533. var res = false;
  534. //11位数字
  535. if (code.Length == 11 && Regex.IsMatch(code, @"^\d{11}$"))
  536. res = true;
  537. return res;
  538. }
  539. /// <summary>
  540. /// 操作日志
  541. /// </summary>
  542. /// <param name="usercode">操作人工号</param>
  543. /// <param name="ip">操作人IP</param>
  544. /// <param name="optcode">操作编号</param>
  545. /// <param name="optid">操作id</param>
  546. /// <param name="des">操作描述</param>
  547. private void addoptlog(string usercode, string ip, string optcode, string optid, string des)
  548. {
  549. new BLL.T_Sys_OperateLogs().AddOptLog("物料管理", usercode, ip, des, optcode, "T_Wo_MaterialManage", optid);
  550. }
  551. #endregion
  552. }
  553. }