OrderImport.java 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714
  1. package restcontroller.webmanage.sale.order;
  2. import java.math.BigDecimal;
  3. import java.util.ArrayList;
  4. import java.util.HashMap;
  5. import org.apache.commons.lang.StringUtils;
  6. import org.apache.poi.ss.usermodel.CellStyle;
  7. import org.apache.poi.ss.usermodel.DataFormat;
  8. import org.apache.poi.xssf.usermodel.XSSFCellStyle;
  9. import org.apache.poi.xssf.usermodel.XSSFFont;
  10. import org.apache.poi.xssf.usermodel.XSSFRow;
  11. import org.apache.poi.xssf.usermodel.XSSFSheet;
  12. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  13. import com.alibaba.fastjson.JSONArray;
  14. import com.alibaba.fastjson.JSONObject;
  15. import beans.datacontrllog.DataContrlLog;
  16. import beans.itemprice.ItemPrice;
  17. import common.Controller;
  18. import common.YosException;
  19. import common.annotation.API;
  20. import common.annotation.CACHEING_CLEAN;
  21. import common.data.ExcelFactory;
  22. import common.data.Row;
  23. import common.data.Rows;
  24. import common.data.SQLFactory;
  25. import restcontroller.R;
  26. import restcontroller.system.attachment.Attachment;
  27. import utility.tools.Math;
  28. import static beans.order.Order.getOrderRows;
  29. public class OrderImport extends Controller {
  30. public OrderImport(JSONObject arg0) throws YosException {
  31. super(arg0);
  32. // TODO Auto-generated constructor stub
  33. }
  34. /**
  35. * 下载订单导入单模板
  36. *
  37. * @return
  38. */
  39. @API(title = "下载订单导入单模板", apiversion = R.ID20230227194703.v1.class)
  40. public String downloadOrderExcel() throws YosException {
  41. ExcelFactory excelFactory = new ExcelFactory("ImportTemplateForOrder");
  42. XSSFSheet sheet = excelFactory.getXssfWorkbook().createSheet("Sheet1");
  43. XSSFWorkbook xssfFWorkbook = excelFactory.getXssfWorkbook();
  44. CellStyle style = xssfFWorkbook.createCellStyle();
  45. // 设置为文本格式,防止身份证号变成科学计数法
  46. DataFormat format = xssfFWorkbook.createDataFormat();
  47. style.setDataFormat(format.getFormat("@"));
  48. // 对单独某一列进行样式赋值,第一个参数为列数,第二个参数为样式
  49. sheet.setDefaultColumnStyle(0, style);
  50. ExportExcel.setBatchDetailSheetColumn1(sheet);// 设置工作薄列宽
  51. XSSFCellStyle titleCellStyle1 = ExportExcel.createTitleCellStyle1(xssfFWorkbook);
  52. XSSFCellStyle titleCellStyle2 = ExportExcel.createTitleCellStyle2(xssfFWorkbook);
  53. XSSFCellStyle titleCellStyle3 = ExportExcel.createBodyCellStyle(xssfFWorkbook);
  54. XSSFCellStyle titleCellStyle4 = ExportExcel.createTitleCellStyle3(xssfFWorkbook);
  55. ExportExcel.batchDetail(sheet, titleCellStyle1, titleCellStyle2, titleCellStyle3, titleCellStyle4,
  56. xssfFWorkbook);// 写入标题
  57. Rows aa = uploadExcelToObs(excelFactory);
  58. String url = "";
  59. if (!aa.isEmpty()) {
  60. url = aa.get(0).getString("url");
  61. }
  62. return getSucReturnObject().setData(url).toString();
  63. }
  64. /**
  65. * 下载订单导入单模板
  66. *
  67. * @return
  68. */
  69. @API(title = "下载订单导入单模板", apiversion = R.ID20230308155703.v1.class)
  70. public String downloadOrderExcelSpecial() throws YosException {
  71. ExcelFactory excelFactory = new ExcelFactory("ImportTemplateForOrder");
  72. XSSFSheet sheet = excelFactory.getXssfWorkbook().createSheet("Sheet1");
  73. XSSFWorkbook xssfFWorkbook = excelFactory.getXssfWorkbook();
  74. CellStyle style = xssfFWorkbook.createCellStyle();
  75. // 设置为文本格式,防止身份证号变成科学计数法
  76. DataFormat format = xssfFWorkbook.createDataFormat();
  77. style.setDataFormat(format.getFormat("@"));
  78. // 对单独某一列进行样式赋值,第一个参数为列数,第二个参数为样式
  79. sheet.setDefaultColumnStyle(0, style);
  80. ExportExcelSpecial.setBatchDetailSheetColumn1(sheet);// 设置工作薄列宽
  81. XSSFCellStyle titleCellStyle1 = ExportExcelSpecial.createTitleCellStyle1(xssfFWorkbook);
  82. XSSFCellStyle titleCellStyle2 = ExportExcelSpecial.createTitleCellStyle2(xssfFWorkbook);
  83. XSSFCellStyle titleCellStyle3 = ExportExcelSpecial.createBodyCellStyle(xssfFWorkbook);
  84. XSSFCellStyle titleCellStyle4 = ExportExcelSpecial.createTitleCellStyle3(xssfFWorkbook);
  85. ExportExcelSpecial.batchDetail(sheet, titleCellStyle1, titleCellStyle2, titleCellStyle3, titleCellStyle4,
  86. xssfFWorkbook);// 写入标题
  87. Rows aa = uploadExcelToObs(excelFactory);
  88. String url = "";
  89. if (!aa.isEmpty()) {
  90. url = aa.get(0).getString("url");
  91. }
  92. return getSucReturnObject().setData(url).toString();
  93. }
  94. /**
  95. * 导入订单
  96. *
  97. * @return
  98. */
  99. @API(title = "导入订单明细", apiversion = R.ID20230227194803.v1.class)
  100. @CACHEING_CLEAN(apiClass = { Order.class, OrderItems.class, restcontroller.sale.order.Order.class })
  101. public String uploadOrderMxByExcel() throws YosException {
  102. long sa_orderid = content.getLongValue("sa_orderid");
  103. long sa_projectid = 0;
  104. Rows orderRows = getOrderRows(this, sa_orderid);
  105. Rows sa_orderitemsrows = dbConnect
  106. .runSqlQuery("select itemid from sa_orderitems where sa_orderid=" + sa_orderid);
  107. ArrayList<Long> itemids = sa_orderitemsrows.toArrayList("itemid", new ArrayList<>());
  108. if (orderRows.isEmpty()) {
  109. return getErrReturnObject().setErrMsg("订单不存在,无法导入明细").toString();
  110. } else {
  111. if (!orderRows.get(0).getString("status").equals("新建")) {
  112. return getErrReturnObject().setErrMsg("非新建状态下的订单无法导入明细").toString();
  113. }
  114. }
  115. long sa_contractid = orderRows.get(0).getLong("sa_contractid");
  116. sys_enterpriseid = orderRows.get(0).getLong("sys_enterpriseid");
  117. String type = orderRows.get(0).getString("type");
  118. // select t1.userid,t1.siteid,min(t2.discountrate) discountrate from sys_hr t1
  119. // inner join sa_salearea_hr t2 on t1.hrid=t2.hrid and t1.siteid=t2.siteid group
  120. // by t1.userid,t1.siteid
  121. JSONArray jsonArray = new JSONArray();
  122. jsonArray.add(content.getLong("attachmentid"));
  123. content.put("ownertable", "sa_order");
  124. content.put("ownerid", sa_orderid);
  125. content.put("usetype", "default");
  126. content.put("attachmentids", jsonArray);
  127. Attachment attachment = new Attachment(content);
  128. attachment.createFileLink();
  129. ExcelFactory e;
  130. try {
  131. // 华为云
  132. e = getPostExcelFactory(content.getLong("attachmentid"));
  133. // 本地
  134. // e = getPostExcelFactory();
  135. // Rows keyRow = dbConnect.runSqlQuery("select fagentnum,faddress from tagents
  136. // where 1=2");
  137. // Rows row1 = e.getSheetRows(1, keyRow, 1);
  138. ArrayList<String> keys = new ArrayList<>();
  139. ArrayList<String> sqllist = new ArrayList<>();
  140. keys.add("itemno");
  141. keys.add("qty");
  142. Rows rows = e.getSheetRows(0, keys, 3);
  143. int a = 1;
  144. int i = 0;
  145. // String msg = "";
  146. boolean iserr = false;
  147. Rows rowserr = new Rows();
  148. Rows rowssuc = new Rows();
  149. for (Row row : rows) {
  150. if (StringUtils.isEmpty(row.getString("itemno")) || StringUtils.isEmpty(row.getString("qty"))) {
  151. iserr = true;
  152. row.put("msg", "错误信息:商品编号,数量不能为空");
  153. rowserr.add(row);
  154. // rows.remove(row);
  155. // msg = "手机号,省市县及来源不能为空";
  156. } else {
  157. if (sa_projectid > 0) {
  158. Rows itemsRows = dbConnect.runSqlQuery(
  159. "select t1.itemid from sa_project_items t1 inner join plm_item t2 on t1.itemid=t2.itemid and t1.siteid=t2.siteid where t1.deleted=0 and t1.sa_projectid='"
  160. + sa_projectid + "' and t2.itemno='" + row.getString("itemno")
  161. + "' and t1.siteid='" + siteid + "'");
  162. if (!itemsRows.isEmpty()) {
  163. row.put("itemid", itemsRows.get(0).getString("itemid"));
  164. rowssuc.add(row);
  165. } else {
  166. iserr = true;
  167. row.put("msg", "错误信息:该报价单所属项目中不存在商品编号为" + row.getString("itemno") + "的商品");
  168. rowserr.add(row);
  169. }
  170. } else {
  171. Rows itemsRows = dbConnect.runSqlQuery("select itemid from plm_item where itemno='"
  172. + row.getString("itemno").trim() + "' and siteid='" + siteid + "'");
  173. if (!itemsRows.isEmpty()) {
  174. row.put("itemid", itemsRows.get(0).getString("itemid"));
  175. rowssuc.add(row);
  176. } else {
  177. iserr = true;
  178. row.put("msg", "错误信息:不存在商品编号为" + row.getString("itemno").trim() + "的商品");
  179. rowserr.add(row);
  180. }
  181. }
  182. }
  183. }
  184. long[] sa_orderitemsid = createTableID("sa_orderitems", rowssuc.size());
  185. if (!rowssuc.isEmpty()) {
  186. SQLFactory sqlFactoryupload = null;
  187. for (Row row : rowssuc) {
  188. BigDecimal qty = BigDecimal.valueOf(row.getDouble("qty"));
  189. if (!itemids.isEmpty()) {
  190. if (itemids.contains(row.getLong("itemid"))) {
  191. sqlFactoryupload = new SQLFactory(this, "订单商品明细_更新");
  192. Rows sa_orderitemsids = dbConnect
  193. .runSqlQuery("select sa_orderitemsid from sa_orderitems where sa_orderid="
  194. + sa_orderid + " and itemid=" + row.getLong("itemid"));
  195. sqlFactoryupload.addParameter("sa_orderitemsid",
  196. sa_orderitemsids.get(0).getLong("sa_orderitemsid"));
  197. } else {
  198. sqlFactoryupload = new SQLFactory(this, "订单商品明细_新增");
  199. sqlFactoryupload.addParameter("sa_orderitemsid", sa_orderitemsid[i]);
  200. }
  201. } else {
  202. sqlFactoryupload = new SQLFactory(this, "订单商品明细_新增");
  203. sqlFactoryupload.addParameter("sa_orderitemsid", sa_orderitemsid[i]);
  204. }
  205. sqlFactoryupload.addParameter("siteid", siteid);
  206. sqlFactoryupload.addParameter("userid", userid);
  207. sqlFactoryupload.addParameter("username", username);
  208. sqlFactoryupload.addParameter("sa_orderid", sa_orderid);
  209. sqlFactoryupload.addParameter("rowno", getRowNo(sa_orderid));
  210. sqlFactoryupload.addParameter("itemid", row.getLong("itemid"));
  211. sqlFactoryupload.addParameter("remarks", "");
  212. // 商品信息
  213. Row itemRow = getItemRow(row.getLong("itemid"));
  214. BigDecimal conversionrate = itemRow.getBigDecimal("conversionrate");
  215. if (conversionrate.compareTo(BigDecimal.ZERO) <= 0) {
  216. conversionrate = BigDecimal.valueOf(1);
  217. }
  218. sqlFactoryupload.addParameter("itemno", itemRow.getString("itemno"));
  219. sqlFactoryupload.addParameter("itemname", itemRow.getString("itemname"));
  220. sqlFactoryupload.addParameter("model", itemRow.getString("model"));
  221. sqlFactoryupload.addParameter("unit", itemRow.getString("unit"));
  222. sqlFactoryupload.addParameter("auxunit", itemRow.getString("auxunit"));
  223. sqlFactoryupload.addParameter("batchcontrol", itemRow.getLong("batchcontrol"));
  224. sqlFactoryupload.addParameter("delivery", itemRow.getLong("delivery"));
  225. sqlFactoryupload.addParameter("needdate", "null");
  226. sqlFactoryupload.addParameter("deliverydate", "null");
  227. sqlFactoryupload.addParameter("conversionrate", conversionrate);
  228. sqlFactoryupload.addParameter("stockid", 0);
  229. sqlFactoryupload.addParameter("position", 0);
  230. sqlFactoryupload.addParameter("batchno", "");
  231. sqlFactoryupload.addParameter("stockno", "");
  232. // 订购数量
  233. sqlFactoryupload.addParameter("qty", qty);
  234. // 辅助单位数量
  235. sqlFactoryupload.addParameter("auxqty", qty.divide(conversionrate));
  236. BigDecimal price;
  237. // 价格
  238. ItemPrice itemPrice = ItemPrice.getItemPrice(this, sys_enterpriseid, row.getLong("itemid"));
  239. // 判断单价是否高于系统的单价
  240. // 价格
  241. BigDecimal defaultprice;
  242. switch (type) {
  243. case "项目订单":
  244. defaultprice = itemPrice.getContractprice(sa_contractid);
  245. break;
  246. case "工具订单":
  247. defaultprice = itemPrice.getMarketprice();
  248. break;
  249. default:
  250. defaultprice = itemPrice.getContractprice();
  251. }
  252. // 单价,折后价(元),取合同价
  253. sqlFactoryupload.addParameter("defaultprice", defaultprice);
  254. // 金额,折后金额(元)
  255. sqlFactoryupload.addParameter("defaultamount", defaultprice.multiply(qty));
  256. // 牌价、市场价(元),标准订单牌价取商品价格,项目订单取合同里的牌价
  257. sqlFactoryupload.addParameter("marketprice", itemPrice.getMarketprice());
  258. sqlFactoryupload.addParameter("price", defaultprice);
  259. // 折前金额(元)
  260. sqlFactoryupload.addParameter("amount", defaultprice.multiply(qty));
  261. sqllist.add(sqlFactoryupload.getSQL());
  262. i++;
  263. }
  264. }
  265. if (sqllist != null && !sqllist.isEmpty()) {
  266. sqllist.add(DataContrlLog.createLog(this, "sa_order", sa_orderid, "导入", "订单明细导入成功").getSQL());
  267. dbConnect.runSqlUpdate(sqllist);
  268. // 重新排序
  269. updateRowNo(sa_orderid);
  270. }
  271. if (iserr) {
  272. ExcelFactory excelFactory = new ExcelFactory("sa_ordererr");
  273. HashMap<String, String> map = new HashMap<String, String>();
  274. map.put("itemno", "商品编号");
  275. map.put("qty", "数量");
  276. map.put("msg", "错误信息");
  277. ArrayList<String> colNameList = new ArrayList<String>();
  278. HashMap<String, Class> keytypemap = new HashMap<String, Class>();
  279. colNameList.add("itemno");
  280. colNameList.add("qty");
  281. colNameList.add("msg");
  282. keytypemap.put("itemno", String.class);
  283. keytypemap.put("qty", String.class);
  284. keytypemap.put("msg", String.class);
  285. rowserr.setFieldList(colNameList);
  286. rowserr.setFieldTypeMap(keytypemap);
  287. addSheet(excelFactory, "Sheet1", rowserr, map);
  288. Rows aa = uploadExcelToObs(excelFactory);
  289. String url = "";
  290. if (!aa.isEmpty()) {
  291. url = aa.get(0).getString("url");
  292. }
  293. return getSucReturnObject().setData(url).toString();
  294. }
  295. } catch (Exception e1) {
  296. // TODO Auto-generated catch block
  297. // dbConnect.runSqlUpdate("delete from sa_order where sa_orderid=" +
  298. // sa_orderid);
  299. e1.printStackTrace();
  300. return getErrReturnObject().setErrMsg(e1.getMessage()).toString();
  301. }
  302. return getSucReturnObject().toString();
  303. }
  304. /**
  305. * 导入订单
  306. *
  307. * @return
  308. */
  309. @API(title = "导入订单明细", apiversion = R.ID20230308155803.v1.class)
  310. @CACHEING_CLEAN(apiClass = { Order.class, OrderItems.class, restcontroller.sale.order.Order.class })
  311. public String uploadOrderMxByExcelSpecial() throws YosException {
  312. long sa_orderid = content.getLongValue("sa_orderid");
  313. //通过版本更新订单表头数据
  314. beans.order.Order.updateOrderWithVersion(this);
  315. long sa_projectid = 0;
  316. Rows orderRows = getOrderRows(this, sa_orderid);
  317. Rows sa_orderitemsrows = dbConnect
  318. .runSqlQuery("select itemid from sa_orderitems where sa_orderid=" + sa_orderid);
  319. ArrayList<Long> itemids = sa_orderitemsrows.toArrayList("itemid", new ArrayList<>());
  320. if (orderRows.isEmpty()) {
  321. return getErrReturnObject().setErrMsg("订单不存在,无法导入明细").toString();
  322. } else {
  323. if (!orderRows.get(0).getString("status").equals("新建")) {
  324. return getErrReturnObject().setErrMsg("非新建状态下的订单无法导入明细").toString();
  325. }
  326. }
  327. long sa_contractid = orderRows.get(0).getLong("sa_contractid");
  328. sys_enterpriseid = orderRows.get(0).getLong("sys_enterpriseid");
  329. String type = orderRows.get(0).getString("type");
  330. // select t1.userid,t1.siteid,min(t2.discountrate) discountrate from sys_hr t1
  331. // inner join sa_salearea_hr t2 on t1.hrid=t2.hrid and t1.siteid=t2.siteid group
  332. // by t1.userid,t1.siteid
  333. JSONArray jsonArray = new JSONArray();
  334. jsonArray.add(content.getLong("attachmentid"));
  335. content.put("ownertable", "sa_order");
  336. content.put("ownerid", sa_orderid);
  337. content.put("usetype", "default");
  338. content.put("attachmentids", jsonArray);
  339. Attachment attachment = new Attachment(content);
  340. attachment.createFileLink();
  341. ExcelFactory e;
  342. try {
  343. // 华为云
  344. e = getPostExcelFactory(content.getLong("attachmentid"));
  345. // 本地
  346. //e = getPostExcelFactory();
  347. // Rows keyRow = dbConnect.runSqlQuery("select fagentnum,faddress from tagents
  348. // where 1=2");
  349. // Rows row1 = e.getSheetRows(1, keyRow, 1);
  350. ArrayList<String> keys = new ArrayList<>();
  351. ArrayList<String> sqllist = new ArrayList<>();
  352. keys.add("itemno");
  353. keys.add("qty");
  354. keys.add("defaultprice");
  355. Rows rows = e.getSheetRows(0, keys, 3);
  356. int a = 1;
  357. int i = 0;
  358. // String msg = "";
  359. boolean iserr = false;
  360. Rows rowserr = new Rows();
  361. Rows rowssuc = new Rows();
  362. for (Row row : rows) {
  363. if (StringUtils.isEmpty(row.getString("itemno")) || StringUtils.isEmpty(row.getString("qty")) || StringUtils.isEmpty(row.getString("defaultprice"))) {
  364. iserr = true;
  365. row.put("msg", "错误信息:商品编号,数量,折前价不能为空");
  366. rowserr.add(row);
  367. // rows.remove(row);
  368. // msg = "手机号,省市县及来源不能为空";
  369. } else {
  370. if (sa_projectid > 0) {
  371. Rows itemsRows = dbConnect.runSqlQuery(
  372. "select t1.itemid from sa_project_items t1 inner join plm_item t2 on t1.itemid=t2.itemid and t1.siteid=t2.siteid where t1.deleted=0 and t1.sa_projectid='"
  373. + sa_projectid + "' and t2.itemno='" + row.getString("itemno")
  374. + "' and t1.siteid='" + siteid + "'");
  375. if (!itemsRows.isEmpty()) {
  376. row.put("itemid", itemsRows.get(0).getString("itemid"));
  377. rowssuc.add(row);
  378. } else {
  379. iserr = true;
  380. row.put("msg", "错误信息:该报价单所属项目中不存在商品编号为" + row.getString("itemno") + "的商品");
  381. rowserr.add(row);
  382. }
  383. } else {
  384. Rows itemsRows = dbConnect.runSqlQuery("select itemid from plm_item where itemno='"
  385. + row.getString("itemno").trim() + "' and siteid='" + siteid + "'");
  386. if (!itemsRows.isEmpty()) {
  387. row.put("itemid", itemsRows.get(0).getString("itemid"));
  388. rowssuc.add(row);
  389. } else {
  390. iserr = true;
  391. row.put("msg", "错误信息:不存在商品编号为" + row.getString("itemno").trim() + "的商品");
  392. rowserr.add(row);
  393. }
  394. }
  395. }
  396. }
  397. long[] sa_orderitemsid = createTableID("sa_orderitems", rowssuc.size());
  398. if (!rowssuc.isEmpty()) {
  399. SQLFactory sqlFactoryupload = null;
  400. for (Row row : rowssuc) {
  401. BigDecimal qty = BigDecimal.valueOf(row.getDouble("qty"));
  402. BigDecimal defaultprice = BigDecimal.valueOf(row.getDouble("defaultprice"));
  403. if (!itemids.isEmpty()) {
  404. if (itemids.contains(row.getLong("itemid"))) {
  405. sqlFactoryupload = new SQLFactory(this, "订单商品明细_更新");
  406. Rows sa_orderitemsids = dbConnect
  407. .runSqlQuery("select sa_orderitemsid from sa_orderitems where sa_orderid="
  408. + sa_orderid + " and itemid=" + row.getLong("itemid"));
  409. sqlFactoryupload.addParameter("sa_orderitemsid",
  410. sa_orderitemsids.get(0).getLong("sa_orderitemsid"));
  411. } else {
  412. sqlFactoryupload = new SQLFactory(this, "订单商品明细_新增");
  413. sqlFactoryupload.addParameter("sa_orderitemsid", sa_orderitemsid[i]);
  414. }
  415. } else {
  416. sqlFactoryupload = new SQLFactory(this, "订单商品明细_新增");
  417. sqlFactoryupload.addParameter("sa_orderitemsid", sa_orderitemsid[i]);
  418. }
  419. sqlFactoryupload.addParameter("siteid", siteid);
  420. sqlFactoryupload.addParameter("userid", userid);
  421. sqlFactoryupload.addParameter("username", username);
  422. sqlFactoryupload.addParameter("sa_orderid", sa_orderid);
  423. sqlFactoryupload.addParameter("rowno", getRowNo(sa_orderid));
  424. sqlFactoryupload.addParameter("itemid", row.getLong("itemid"));
  425. sqlFactoryupload.addParameter("remarks", "");
  426. sqlFactoryupload.addParameter("customproperties", "");
  427. // 商品信息
  428. Row itemRow = getItemRow(row.getLong("itemid"));
  429. BigDecimal conversionrate = itemRow.getBigDecimal("conversionrate");
  430. if (conversionrate.compareTo(BigDecimal.ZERO) <= 0) {
  431. conversionrate = BigDecimal.valueOf(1);
  432. }
  433. sqlFactoryupload.addParameter("itemno", itemRow.getString("itemno"));
  434. sqlFactoryupload.addParameter("itemname", itemRow.getString("itemname"));
  435. sqlFactoryupload.addParameter("model", itemRow.getString("model"));
  436. sqlFactoryupload.addParameter("unit", itemRow.getString("unit"));
  437. sqlFactoryupload.addParameter("auxunit", itemRow.getString("auxunit"));
  438. sqlFactoryupload.addParameter("batchcontrol", itemRow.getLong("batchcontrol"));
  439. sqlFactoryupload.addParameter("delivery", itemRow.getLong("delivery"));
  440. sqlFactoryupload.addParameter("needdate", "null");
  441. sqlFactoryupload.addParameter("deliverydate", "null");
  442. sqlFactoryupload.addParameter("conversionrate", conversionrate);
  443. sqlFactoryupload.addParameter("stockid", 0);
  444. sqlFactoryupload.addParameter("position", 0);
  445. sqlFactoryupload.addParameter("batchno", "");
  446. sqlFactoryupload.addParameter("stockno", "");
  447. // 订购数量
  448. sqlFactoryupload.addParameter("qty", qty);
  449. // 辅助单位数量
  450. sqlFactoryupload.addParameter("auxqty", qty.divide(conversionrate));
  451. BigDecimal price;
  452. // 价格
  453. ItemPrice itemPrice = ItemPrice.getItemPrice(this, sys_enterpriseid, row.getLong("itemid"));
  454. // 判断单价是否高于系统的单价
  455. // 价格
  456. // switch (type) {
  457. // case "项目订单":
  458. // defaultprice = itemPrice.getContractprice(sa_contractid);
  459. // break;
  460. // case "工具订单":
  461. // defaultprice = itemPrice.getMarketprice();
  462. // break;
  463. // default:
  464. // defaultprice = itemPrice.getContractprice();
  465. // }
  466. // 单价,折后价(元),取合同价
  467. sqlFactoryupload.addParameter("defaultprice", defaultprice);
  468. // 金额,折后金额(元)
  469. sqlFactoryupload.addParameter("defaultamount", defaultprice.multiply(qty));
  470. // 牌价、市场价(元),标准订单牌价取商品价格,项目订单取合同里的牌价
  471. sqlFactoryupload.addParameter("marketprice", itemPrice.getMarketprice());
  472. sqlFactoryupload.addParameter("price", defaultprice);
  473. // 折前金额(元)
  474. sqlFactoryupload.addParameter("amount", defaultprice.multiply(qty));
  475. sqllist.add(sqlFactoryupload.getSQL());
  476. i++;
  477. }
  478. }
  479. if (sqllist != null && !sqllist.isEmpty()) {
  480. sqllist.add(DataContrlLog.createLog(this, "sa_order", sa_orderid, "导入", "订单明细导入成功").getSQL());
  481. dbConnect.runSqlUpdate(sqllist);
  482. // 重新排序
  483. updateRowNo(sa_orderid);
  484. }
  485. if (iserr) {
  486. ExcelFactory excelFactory = new ExcelFactory("sa_ordererr");
  487. HashMap<String, String> map = new HashMap<String, String>();
  488. map.put("itemno", "商品编号");
  489. map.put("qty", "数量");
  490. map.put("msg", "错误信息");
  491. ArrayList<String> colNameList = new ArrayList<String>();
  492. HashMap<String, Class> keytypemap = new HashMap<String, Class>();
  493. colNameList.add("itemno");
  494. colNameList.add("qty");
  495. colNameList.add("msg");
  496. keytypemap.put("itemno", String.class);
  497. keytypemap.put("qty", String.class);
  498. keytypemap.put("msg", String.class);
  499. rowserr.setFieldList(colNameList);
  500. rowserr.setFieldTypeMap(keytypemap);
  501. addSheet(excelFactory, "Sheet1", rowserr, map);
  502. Rows aa = uploadExcelToObs(excelFactory);
  503. String url = "";
  504. if (!aa.isEmpty()) {
  505. url = aa.get(0).getString("url");
  506. }
  507. return getSucReturnObject().setData(url).toString();
  508. }
  509. } catch (Exception e1) {
  510. // TODO Auto-generated catch block
  511. // dbConnect.runSqlUpdate("delete from sa_order where sa_orderid=" +
  512. // sa_orderid);
  513. e1.printStackTrace();
  514. return getErrReturnObject().setErrMsg(e1.getMessage()).toString();
  515. }
  516. return getSucReturnObject().toString();
  517. }
  518. public XSSFSheet addSheet(ExcelFactory excelFactory, String sheetname, Rows datarows,
  519. HashMap<String, String> titlemap) {
  520. ArrayList<String> keylist = datarows.getFieldList();
  521. XSSFSheet sheet = excelFactory.getXssfWorkbook().createSheet(sheetname);
  522. XSSFWorkbook xssfFWorkbook = excelFactory.getXssfWorkbook();
  523. XSSFCellStyle xssfCellStyle1 = xssfFWorkbook.createCellStyle();
  524. XSSFFont font = xssfFWorkbook.createFont();
  525. font.setColor((short) 0xa);
  526. font.setFontHeightInPoints((short) 12);
  527. font.setBold(true);
  528. xssfCellStyle1.setFont(font);
  529. ExportExcel.setBatchDetailSheetColumn2(sheet);// 设置工作薄列宽
  530. XSSFCellStyle titleCellStyle1 = ExportExcel.createTitleCellStyle1(xssfFWorkbook);
  531. XSSFCellStyle titleCellStyle2 = ExportExcel.createTitleCellStyle2(xssfFWorkbook);
  532. XSSFCellStyle titleCellStyle3 = ExportExcel.createTitleCellStyle3(xssfFWorkbook);
  533. ExportExcel.batchDetailErr(sheet, titleCellStyle1, titleCellStyle2, titleCellStyle3, xssfFWorkbook);// 写入标题
  534. for (int n = 0; n < datarows.size(); n++) {
  535. Row row = datarows.get(n);
  536. XSSFRow datarow = sheet.createRow(n + 3);
  537. for (int i1 = 0; i1 < keylist.size(); i1++) {
  538. Class fieldclazztype = datarows.getFieldMeta(keylist.get(i1)).getFieldtype();
  539. if (fieldclazztype == Integer.class) {
  540. datarow.createCell(i1).setCellValue(row.getInteger((String) keylist.get(i1)).intValue());
  541. } else if (fieldclazztype == Long.class) {
  542. datarow.createCell(i1).setCellValue(row.getLong((String) keylist.get(i1)));
  543. } else if (fieldclazztype == Float.class) {
  544. datarow.createCell(i1).setCellValue(row.getFloat((String) keylist.get(i1)));
  545. } else if (fieldclazztype == Double.class) {
  546. datarow.createCell(i1).setCellValue(row.getDouble((String) keylist.get(i1)));
  547. } else {
  548. datarow.createCell(i1).setCellValue(row.getString((String) keylist.get(i1)));
  549. }
  550. if (i1 == 2) {
  551. datarow.getCell(i1).setCellStyle(xssfCellStyle1);
  552. }
  553. }
  554. }
  555. return sheet;
  556. }
  557. public XSSFSheet addSheetSpecial(ExcelFactory excelFactory, String sheetname, Rows datarows,
  558. HashMap<String, String> titlemap) {
  559. ArrayList<String> keylist = datarows.getFieldList();
  560. XSSFSheet sheet = excelFactory.getXssfWorkbook().createSheet(sheetname);
  561. XSSFWorkbook xssfFWorkbook = excelFactory.getXssfWorkbook();
  562. XSSFCellStyle xssfCellStyle1 = xssfFWorkbook.createCellStyle();
  563. XSSFFont font = xssfFWorkbook.createFont();
  564. font.setColor((short) 0xa);
  565. font.setFontHeightInPoints((short) 12);
  566. font.setBold(true);
  567. xssfCellStyle1.setFont(font);
  568. ExportExcel.setBatchDetailSheetColumn2(sheet);// 设置工作薄列宽
  569. XSSFCellStyle titleCellStyle1 = ExportExcel.createTitleCellStyle1(xssfFWorkbook);
  570. XSSFCellStyle titleCellStyle2 = ExportExcel.createTitleCellStyle2(xssfFWorkbook);
  571. XSSFCellStyle titleCellStyle3 = ExportExcel.createTitleCellStyle3(xssfFWorkbook);
  572. ExportExcel.batchDetailErr(sheet, titleCellStyle1, titleCellStyle2, titleCellStyle3, xssfFWorkbook);// 写入标题
  573. for (int n = 0; n < datarows.size(); n++) {
  574. Row row = datarows.get(n);
  575. XSSFRow datarow = sheet.createRow(n + 3);
  576. for (int i1 = 0; i1 < keylist.size(); i1++) {
  577. Class fieldclazztype = datarows.getFieldMeta(keylist.get(i1)).getFieldtype();
  578. if (fieldclazztype == Integer.class) {
  579. datarow.createCell(i1).setCellValue(row.getInteger((String) keylist.get(i1)).intValue());
  580. } else if (fieldclazztype == Long.class) {
  581. datarow.createCell(i1).setCellValue(row.getLong((String) keylist.get(i1)));
  582. } else if (fieldclazztype == Float.class) {
  583. datarow.createCell(i1).setCellValue(row.getFloat((String) keylist.get(i1)));
  584. } else if (fieldclazztype == Double.class) {
  585. datarow.createCell(i1).setCellValue(row.getDouble((String) keylist.get(i1)));
  586. } else {
  587. datarow.createCell(i1).setCellValue(row.getString((String) keylist.get(i1)));
  588. }
  589. if (i1 == 3) {
  590. datarow.getCell(i1).setCellStyle(xssfCellStyle1);
  591. }
  592. }
  593. }
  594. return sheet;
  595. }
  596. /**
  597. * 获取当前订单的最大行号
  598. *
  599. * @param sa_orderid
  600. * @return
  601. * @throws YosException
  602. */
  603. public Long getRowNo(Long sa_orderid) throws YosException {
  604. Rows rows = dbConnect.runSqlQuery("SELECT COUNT(0) count from sa_orderitems WHERE sa_orderid = " + sa_orderid
  605. + " AND siteid = '" + siteid + "'");
  606. return rows.get(0).getLong("count") + 1;
  607. }
  608. /**
  609. * 重新对商品行排序
  610. *
  611. * @param sa_orderid
  612. * @throws YosException
  613. */
  614. public void updateRowNo(Long sa_orderid) throws YosException {
  615. String sql = "SELECT sa_orderitemsid from sa_orderitems WHERE sa_orderid= " + sa_orderid + " and siteid = '"
  616. + siteid + "' ORDER BY createdate ";
  617. ArrayList<Long> sa_orderitemsids = dbConnect.runSqlQuery(sql).toArrayList("sa_orderitemsid", new ArrayList<>());
  618. if (sa_orderitemsids.size() > 0) {
  619. int rowno = 1;
  620. StringBuffer where = new StringBuffer("");
  621. for (Long id : sa_orderitemsids) {
  622. where.append(" WHEN " + id + " THEN " + rowno);
  623. rowno++;
  624. }
  625. SQLFactory sqlFactory = new SQLFactory(this, "更新行号");
  626. sqlFactory.addParameter("siteid", siteid);
  627. sqlFactory.addParameter_SQL("where", where);
  628. sqlFactory.addParameter_in("sa_orderitemsid", sa_orderitemsids);
  629. sql = sqlFactory.getSQL(false);
  630. dbConnect.runSqlUpdate(sql);
  631. }
  632. }
  633. /**
  634. * 查询商品信息
  635. *
  636. * @param itemid
  637. * @return
  638. * @throws YosException
  639. */
  640. public Row getItemRow(Long itemid) throws YosException {
  641. SQLFactory sqlFactory = new SQLFactory(this, "查询商品信息");
  642. sqlFactory.addParameter("siteid", siteid);
  643. sqlFactory.addParameter("itemid", itemid);
  644. Rows rows = dbConnect.runSqlQuery(sqlFactory.getSQL(false));
  645. return rows.isNotEmpty() ? rows.get(0) : new Row();
  646. }
  647. }