OrderImport.java 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708
  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. long sa_projectid = 0;
  314. Rows orderRows = getOrderRows(this, sa_orderid);
  315. Rows sa_orderitemsrows = dbConnect
  316. .runSqlQuery("select itemid from sa_orderitems where sa_orderid=" + sa_orderid);
  317. ArrayList<Long> itemids = sa_orderitemsrows.toArrayList("itemid", new ArrayList<>());
  318. if (orderRows.isEmpty()) {
  319. return getErrReturnObject().setErrMsg("订单不存在,无法导入明细").toString();
  320. } else {
  321. if (!orderRows.get(0).getString("status").equals("新建")) {
  322. return getErrReturnObject().setErrMsg("非新建状态下的订单无法导入明细").toString();
  323. }
  324. }
  325. long sa_contractid = orderRows.get(0).getLong("sa_contractid");
  326. sys_enterpriseid = orderRows.get(0).getLong("sys_enterpriseid");
  327. String type = orderRows.get(0).getString("type");
  328. // select t1.userid,t1.siteid,min(t2.discountrate) discountrate from sys_hr t1
  329. // inner join sa_salearea_hr t2 on t1.hrid=t2.hrid and t1.siteid=t2.siteid group
  330. // by t1.userid,t1.siteid
  331. JSONArray jsonArray = new JSONArray();
  332. jsonArray.add(content.getLong("attachmentid"));
  333. content.put("ownertable", "sa_order");
  334. content.put("ownerid", sa_orderid);
  335. content.put("usetype", "default");
  336. content.put("attachmentids", jsonArray);
  337. Attachment attachment = new Attachment(content);
  338. attachment.createFileLink();
  339. ExcelFactory e;
  340. try {
  341. // 华为云
  342. e = getPostExcelFactory(content.getLong("attachmentid"));
  343. // 本地
  344. //e = getPostExcelFactory();
  345. // Rows keyRow = dbConnect.runSqlQuery("select fagentnum,faddress from tagents
  346. // where 1=2");
  347. // Rows row1 = e.getSheetRows(1, keyRow, 1);
  348. ArrayList<String> keys = new ArrayList<>();
  349. ArrayList<String> sqllist = new ArrayList<>();
  350. keys.add("itemno");
  351. keys.add("qty");
  352. keys.add("defaultprice");
  353. Rows rows = e.getSheetRows(0, keys, 3);
  354. int a = 1;
  355. int i = 0;
  356. // String msg = "";
  357. boolean iserr = false;
  358. Rows rowserr = new Rows();
  359. Rows rowssuc = new Rows();
  360. for (Row row : rows) {
  361. if (StringUtils.isEmpty(row.getString("itemno")) || StringUtils.isEmpty(row.getString("qty")) || StringUtils.isEmpty(row.getString("defaultprice"))) {
  362. iserr = true;
  363. row.put("msg", "错误信息:商品编号,数量,折前价不能为空");
  364. rowserr.add(row);
  365. // rows.remove(row);
  366. // msg = "手机号,省市县及来源不能为空";
  367. } else {
  368. if (sa_projectid > 0) {
  369. Rows itemsRows = dbConnect.runSqlQuery(
  370. "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='"
  371. + sa_projectid + "' and t2.itemno='" + row.getString("itemno")
  372. + "' and t1.siteid='" + siteid + "'");
  373. if (!itemsRows.isEmpty()) {
  374. row.put("itemid", itemsRows.get(0).getString("itemid"));
  375. rowssuc.add(row);
  376. } else {
  377. iserr = true;
  378. row.put("msg", "错误信息:该报价单所属项目中不存在商品编号为" + row.getString("itemno") + "的商品");
  379. rowserr.add(row);
  380. }
  381. } else {
  382. Rows itemsRows = dbConnect.runSqlQuery("select itemid from plm_item where itemno='"
  383. + row.getString("itemno").trim() + "' and siteid='" + siteid + "'");
  384. if (!itemsRows.isEmpty()) {
  385. row.put("itemid", itemsRows.get(0).getString("itemid"));
  386. rowssuc.add(row);
  387. } else {
  388. iserr = true;
  389. row.put("msg", "错误信息:不存在商品编号为" + row.getString("itemno").trim() + "的商品");
  390. rowserr.add(row);
  391. }
  392. }
  393. }
  394. }
  395. long[] sa_orderitemsid = createTableID("sa_orderitems", rowssuc.size());
  396. if (!rowssuc.isEmpty()) {
  397. SQLFactory sqlFactoryupload = null;
  398. for (Row row : rowssuc) {
  399. BigDecimal qty = BigDecimal.valueOf(row.getDouble("qty"));
  400. BigDecimal defaultprice = BigDecimal.valueOf(row.getDouble("defaultprice"));
  401. if (!itemids.isEmpty()) {
  402. if (itemids.contains(row.getLong("itemid"))) {
  403. sqlFactoryupload = new SQLFactory(this, "订单商品明细_更新");
  404. Rows sa_orderitemsids = dbConnect
  405. .runSqlQuery("select sa_orderitemsid from sa_orderitems where sa_orderid="
  406. + sa_orderid + " and itemid=" + row.getLong("itemid"));
  407. sqlFactoryupload.addParameter("sa_orderitemsid",
  408. sa_orderitemsids.get(0).getLong("sa_orderitemsid"));
  409. } else {
  410. sqlFactoryupload = new SQLFactory(this, "订单商品明细_新增");
  411. sqlFactoryupload.addParameter("sa_orderitemsid", sa_orderitemsid[i]);
  412. }
  413. } else {
  414. sqlFactoryupload = new SQLFactory(this, "订单商品明细_新增");
  415. sqlFactoryupload.addParameter("sa_orderitemsid", sa_orderitemsid[i]);
  416. }
  417. sqlFactoryupload.addParameter("siteid", siteid);
  418. sqlFactoryupload.addParameter("userid", userid);
  419. sqlFactoryupload.addParameter("username", username);
  420. sqlFactoryupload.addParameter("sa_orderid", sa_orderid);
  421. sqlFactoryupload.addParameter("rowno", getRowNo(sa_orderid));
  422. sqlFactoryupload.addParameter("itemid", row.getLong("itemid"));
  423. sqlFactoryupload.addParameter("remarks", "");
  424. // 商品信息
  425. Row itemRow = getItemRow(row.getLong("itemid"));
  426. BigDecimal conversionrate = itemRow.getBigDecimal("conversionrate");
  427. if (conversionrate.compareTo(BigDecimal.ZERO) <= 0) {
  428. conversionrate = BigDecimal.valueOf(1);
  429. }
  430. sqlFactoryupload.addParameter("itemno", itemRow.getString("itemno"));
  431. sqlFactoryupload.addParameter("itemname", itemRow.getString("itemname"));
  432. sqlFactoryupload.addParameter("model", itemRow.getString("model"));
  433. sqlFactoryupload.addParameter("unit", itemRow.getString("unit"));
  434. sqlFactoryupload.addParameter("auxunit", itemRow.getString("auxunit"));
  435. sqlFactoryupload.addParameter("batchcontrol", itemRow.getLong("batchcontrol"));
  436. sqlFactoryupload.addParameter("delivery", itemRow.getLong("delivery"));
  437. sqlFactoryupload.addParameter("needdate", "null");
  438. sqlFactoryupload.addParameter("deliverydate", "null");
  439. sqlFactoryupload.addParameter("conversionrate", conversionrate);
  440. sqlFactoryupload.addParameter("stockid", 0);
  441. sqlFactoryupload.addParameter("position", 0);
  442. sqlFactoryupload.addParameter("batchno", "");
  443. sqlFactoryupload.addParameter("stockno", "");
  444. // 订购数量
  445. sqlFactoryupload.addParameter("qty", qty);
  446. // 辅助单位数量
  447. sqlFactoryupload.addParameter("auxqty", qty.divide(conversionrate));
  448. BigDecimal price;
  449. // 价格
  450. ItemPrice itemPrice = ItemPrice.getItemPrice(this, sys_enterpriseid, row.getLong("itemid"));
  451. // 判断单价是否高于系统的单价
  452. // 价格
  453. // switch (type) {
  454. // case "项目订单":
  455. // defaultprice = itemPrice.getContractprice(sa_contractid);
  456. // break;
  457. // case "工具订单":
  458. // defaultprice = itemPrice.getMarketprice();
  459. // break;
  460. // default:
  461. // defaultprice = itemPrice.getContractprice();
  462. // }
  463. // 单价,折后价(元),取合同价
  464. sqlFactoryupload.addParameter("defaultprice", defaultprice);
  465. // 金额,折后金额(元)
  466. sqlFactoryupload.addParameter("defaultamount", defaultprice.multiply(qty));
  467. // 牌价、市场价(元),标准订单牌价取商品价格,项目订单取合同里的牌价
  468. sqlFactoryupload.addParameter("marketprice", itemPrice.getMarketprice());
  469. sqlFactoryupload.addParameter("price", defaultprice);
  470. // 折前金额(元)
  471. sqlFactoryupload.addParameter("amount", defaultprice.multiply(qty));
  472. sqllist.add(sqlFactoryupload.getSQL());
  473. i++;
  474. }
  475. }
  476. if (sqllist != null && !sqllist.isEmpty()) {
  477. sqllist.add(DataContrlLog.createLog(this, "sa_order", sa_orderid, "导入", "订单明细导入成功").getSQL());
  478. dbConnect.runSqlUpdate(sqllist);
  479. // 重新排序
  480. updateRowNo(sa_orderid);
  481. }
  482. if (iserr) {
  483. ExcelFactory excelFactory = new ExcelFactory("sa_ordererr");
  484. HashMap<String, String> map = new HashMap<String, String>();
  485. map.put("itemno", "商品编号");
  486. map.put("qty", "数量");
  487. map.put("msg", "错误信息");
  488. ArrayList<String> colNameList = new ArrayList<String>();
  489. HashMap<String, Class> keytypemap = new HashMap<String, Class>();
  490. colNameList.add("itemno");
  491. colNameList.add("qty");
  492. colNameList.add("msg");
  493. keytypemap.put("itemno", String.class);
  494. keytypemap.put("qty", String.class);
  495. keytypemap.put("msg", String.class);
  496. rowserr.setFieldList(colNameList);
  497. rowserr.setFieldTypeMap(keytypemap);
  498. addSheet(excelFactory, "Sheet1", rowserr, map);
  499. Rows aa = uploadExcelToObs(excelFactory);
  500. String url = "";
  501. if (!aa.isEmpty()) {
  502. url = aa.get(0).getString("url");
  503. }
  504. return getSucReturnObject().setData(url).toString();
  505. }
  506. } catch (Exception e1) {
  507. // TODO Auto-generated catch block
  508. // dbConnect.runSqlUpdate("delete from sa_order where sa_orderid=" +
  509. // sa_orderid);
  510. e1.printStackTrace();
  511. return getErrReturnObject().setErrMsg(e1.getMessage()).toString();
  512. }
  513. return getSucReturnObject().toString();
  514. }
  515. public XSSFSheet addSheet(ExcelFactory excelFactory, String sheetname, Rows datarows,
  516. HashMap<String, String> titlemap) {
  517. ArrayList<String> keylist = datarows.getFieldList();
  518. XSSFSheet sheet = excelFactory.getXssfWorkbook().createSheet(sheetname);
  519. XSSFWorkbook xssfFWorkbook = excelFactory.getXssfWorkbook();
  520. XSSFCellStyle xssfCellStyle1 = xssfFWorkbook.createCellStyle();
  521. XSSFFont font = xssfFWorkbook.createFont();
  522. font.setColor((short) 0xa);
  523. font.setFontHeightInPoints((short) 12);
  524. font.setBold(true);
  525. xssfCellStyle1.setFont(font);
  526. ExportExcel.setBatchDetailSheetColumn2(sheet);// 设置工作薄列宽
  527. XSSFCellStyle titleCellStyle1 = ExportExcel.createTitleCellStyle1(xssfFWorkbook);
  528. XSSFCellStyle titleCellStyle2 = ExportExcel.createTitleCellStyle2(xssfFWorkbook);
  529. XSSFCellStyle titleCellStyle3 = ExportExcel.createTitleCellStyle3(xssfFWorkbook);
  530. ExportExcel.batchDetailErr(sheet, titleCellStyle1, titleCellStyle2, titleCellStyle3, xssfFWorkbook);// 写入标题
  531. for (int n = 0; n < datarows.size(); n++) {
  532. Row row = datarows.get(n);
  533. XSSFRow datarow = sheet.createRow(n + 3);
  534. for (int i1 = 0; i1 < keylist.size(); i1++) {
  535. Class fieldclazztype = datarows.getFieldMeta(keylist.get(i1)).getFieldtype();
  536. if (fieldclazztype == Integer.class) {
  537. datarow.createCell(i1).setCellValue(row.getInteger((String) keylist.get(i1)).intValue());
  538. } else if (fieldclazztype == Long.class) {
  539. datarow.createCell(i1).setCellValue(row.getLong((String) keylist.get(i1)));
  540. } else if (fieldclazztype == Float.class) {
  541. datarow.createCell(i1).setCellValue(row.getFloat((String) keylist.get(i1)));
  542. } else if (fieldclazztype == Double.class) {
  543. datarow.createCell(i1).setCellValue(row.getDouble((String) keylist.get(i1)));
  544. } else {
  545. datarow.createCell(i1).setCellValue(row.getString((String) keylist.get(i1)));
  546. }
  547. if (i1 == 2) {
  548. datarow.getCell(i1).setCellStyle(xssfCellStyle1);
  549. }
  550. }
  551. }
  552. return sheet;
  553. }
  554. public XSSFSheet addSheetSpecial(ExcelFactory excelFactory, String sheetname, Rows datarows,
  555. HashMap<String, String> titlemap) {
  556. ArrayList<String> keylist = datarows.getFieldList();
  557. XSSFSheet sheet = excelFactory.getXssfWorkbook().createSheet(sheetname);
  558. XSSFWorkbook xssfFWorkbook = excelFactory.getXssfWorkbook();
  559. XSSFCellStyle xssfCellStyle1 = xssfFWorkbook.createCellStyle();
  560. XSSFFont font = xssfFWorkbook.createFont();
  561. font.setColor((short) 0xa);
  562. font.setFontHeightInPoints((short) 12);
  563. font.setBold(true);
  564. xssfCellStyle1.setFont(font);
  565. ExportExcel.setBatchDetailSheetColumn2(sheet);// 设置工作薄列宽
  566. XSSFCellStyle titleCellStyle1 = ExportExcel.createTitleCellStyle1(xssfFWorkbook);
  567. XSSFCellStyle titleCellStyle2 = ExportExcel.createTitleCellStyle2(xssfFWorkbook);
  568. XSSFCellStyle titleCellStyle3 = ExportExcel.createTitleCellStyle3(xssfFWorkbook);
  569. ExportExcel.batchDetailErr(sheet, titleCellStyle1, titleCellStyle2, titleCellStyle3, xssfFWorkbook);// 写入标题
  570. for (int n = 0; n < datarows.size(); n++) {
  571. Row row = datarows.get(n);
  572. XSSFRow datarow = sheet.createRow(n + 3);
  573. for (int i1 = 0; i1 < keylist.size(); i1++) {
  574. Class fieldclazztype = datarows.getFieldMeta(keylist.get(i1)).getFieldtype();
  575. if (fieldclazztype == Integer.class) {
  576. datarow.createCell(i1).setCellValue(row.getInteger((String) keylist.get(i1)).intValue());
  577. } else if (fieldclazztype == Long.class) {
  578. datarow.createCell(i1).setCellValue(row.getLong((String) keylist.get(i1)));
  579. } else if (fieldclazztype == Float.class) {
  580. datarow.createCell(i1).setCellValue(row.getFloat((String) keylist.get(i1)));
  581. } else if (fieldclazztype == Double.class) {
  582. datarow.createCell(i1).setCellValue(row.getDouble((String) keylist.get(i1)));
  583. } else {
  584. datarow.createCell(i1).setCellValue(row.getString((String) keylist.get(i1)));
  585. }
  586. if (i1 == 3) {
  587. datarow.getCell(i1).setCellStyle(xssfCellStyle1);
  588. }
  589. }
  590. }
  591. return sheet;
  592. }
  593. /**
  594. * 获取当前订单的最大行号
  595. *
  596. * @param sa_orderid
  597. * @return
  598. * @throws YosException
  599. */
  600. public Long getRowNo(Long sa_orderid) throws YosException {
  601. Rows rows = dbConnect.runSqlQuery("SELECT COUNT(0) count from sa_orderitems WHERE sa_orderid = " + sa_orderid
  602. + " AND siteid = '" + siteid + "'");
  603. return rows.get(0).getLong("count") + 1;
  604. }
  605. /**
  606. * 重新对商品行排序
  607. *
  608. * @param sa_orderid
  609. * @throws YosException
  610. */
  611. public void updateRowNo(Long sa_orderid) throws YosException {
  612. String sql = "SELECT sa_orderitemsid from sa_orderitems WHERE sa_orderid= " + sa_orderid + " and siteid = '"
  613. + siteid + "' ORDER BY createdate ";
  614. ArrayList<Long> sa_orderitemsids = dbConnect.runSqlQuery(sql).toArrayList("sa_orderitemsid", new ArrayList<>());
  615. if (sa_orderitemsids.size() > 0) {
  616. int rowno = 1;
  617. StringBuffer where = new StringBuffer("");
  618. for (Long id : sa_orderitemsids) {
  619. where.append(" WHEN " + id + " THEN " + rowno);
  620. rowno++;
  621. }
  622. SQLFactory sqlFactory = new SQLFactory(this, "更新行号");
  623. sqlFactory.addParameter("siteid", siteid);
  624. sqlFactory.addParameter_SQL("where", where);
  625. sqlFactory.addParameter_in("sa_orderitemsid", sa_orderitemsids);
  626. sql = sqlFactory.getSQL(false);
  627. dbConnect.runSqlUpdate(sql);
  628. }
  629. }
  630. /**
  631. * 查询商品信息
  632. *
  633. * @param itemid
  634. * @return
  635. * @throws YosException
  636. */
  637. public Row getItemRow(Long itemid) throws YosException {
  638. SQLFactory sqlFactory = new SQLFactory(this, "查询商品信息");
  639. sqlFactory.addParameter("siteid", siteid);
  640. sqlFactory.addParameter("itemid", itemid);
  641. Rows rows = dbConnect.runSqlQuery(sqlFactory.getSQL(false));
  642. return rows.isNotEmpty() ? rows.get(0) : new Row();
  643. }
  644. }