orders.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. 'use strict';
  2. const ManagerController = require('../manager.js');
  3. /**
  4. * [exports 订单控制器]
  5. * @type {[type]}
  6. */
  7. module.exports = class OrdersController extends ManagerController {
  8. /**
  9. * [useModel 使用模型]
  10. * @return {[type]} [description]
  11. */
  12. get useModel() {
  13. const that = this;
  14. return that.app.model.Orders;
  15. }
  16. /**
  17. * [selectValidate 订单查询验证器]
  18. * @return {[type]} [description]
  19. */
  20. get selectValidate() {
  21. const that = this;
  22. return {
  23. page: that.ctx.rules.default(1)
  24. .number(),
  25. limit: that.ctx.rules.default(20)
  26. .number(),
  27. product_name: that.ctx.rules.default('')
  28. .required(),
  29. uname: that.ctx.rules.default('')
  30. .required(),
  31. order_status: that.ctx.rules.default('')
  32. .required(),
  33. order_time: that.ctx.rules.default('')
  34. .required(),
  35. order_id: that.ctx.rules.default(0)
  36. .number(),
  37. };
  38. }
  39. /**
  40. * [deliverValidate 订单发货验证器]
  41. * @return {[type]} [description]
  42. */
  43. get deliverValidate() {
  44. const that = this;
  45. return {
  46. goods: that.ctx.rules.name('商品发货信息')
  47. .required()
  48. .isArray(),
  49. is_express: that.ctx.rules.name('是否需要物流')
  50. .required()
  51. .notEmpty()
  52. .number(),
  53. express_sn: that.ctx.rules.default('')
  54. .required(),
  55. order_id: that.ctx.rules.name('订单ID')
  56. .required()
  57. .notEmpty()
  58. .number(),
  59. express_id: that.ctx.rules.default(0)
  60. .number(),
  61. deliver_desc: that.ctx.rules.default('')
  62. .required(),
  63. admin_id: that.ctx.rules.default(that.service.manager.ActionAdminUserId())
  64. .number(),
  65. deliver_time: that.ctx.rules.default(that.app.szjcomo.date('Y-m-d H:i:s'))
  66. .required(),
  67. };
  68. }
  69. /**
  70. * [logsValidate 订单操作日志验证器]
  71. * @return {[type]} [description]
  72. */
  73. get logsValidate() {
  74. const that = this;
  75. return {
  76. order_id: that.ctx.rules.name('订单ID')
  77. .required()
  78. .notEmpty()
  79. .number(),
  80. };
  81. }
  82. /**
  83. * [select 查询订单列表]
  84. * @return {[type]} [description]
  85. */
  86. async select() {
  87. const that = this;
  88. try {
  89. const data = await that.ctx.validate(that.selectValidate, await that.ctx.getParse());
  90. let result;
  91. if (data.order_id) {
  92. result = await that.selectInfo(data.order_id);
  93. } else {
  94. result = await that.selectList(data);
  95. }
  96. return that.ctx.appJson(that.app.szjcomo.appResult('SUCCESS', result, false));
  97. } catch (err) {
  98. return that.ctx.appJson(that.app.szjcomo.appResult(err.message));
  99. }
  100. }
  101. /**
  102. * [selectInfo 订单详情]
  103. * @param {[type]} order_id [description]
  104. * @return {[type]} [description]
  105. */
  106. async selectInfo(order_id) {
  107. const that = this;
  108. const options = {
  109. where: { order_id }, include: [
  110. { model: that.app.model.PaysConfig, attributes: [ 'pay_name' ], as: 'pays_config' },
  111. { model: that.app.model.Users, as: 'users', attributes: [ 'account_name', 'nickname' ] },
  112. {
  113. model: that.app.model.OrdersProducts, as: 'orders_products', attributes: {
  114. exclude: [ 'product_id', 'create_time', 'order_id', 'activity_name', 'activity_id', 'activity_desc' ],
  115. },
  116. },
  117. { model: that.app.model.Payments, as: 'payments', attributes: [ 'create_time' ], required: false },
  118. ], attributes: {
  119. exclude: [ 'seller_remarks', 'province_id', 'city_id', 'county_id', 'shipping_id', 'shipping_fee', 'activity_id', 'activity_name', 'activity_desc' ],
  120. },
  121. };
  122. const selectBean = await that.app.comoBean.instance({}, options);
  123. return await that.service.base.select(selectBean, that.useModel, '查询订单详情失败,请稍候重试', false, false);
  124. }
  125. /**
  126. * [selectList 订单列表]
  127. * @param {Object} data [description]
  128. * @return {[type]} [description]
  129. */
  130. async selectList(data = {}) {
  131. const that = this;
  132. const seq = that.app.Sequelize;
  133. const productWhere = {};
  134. let userWhere = {};
  135. if (data.product_name) productWhere.product_name = { [seq.Op.regexp]: data.product_name };
  136. if (data.uname) userWhere = { [seq.Op.or]: [{ nickname: { [seq.Op.regexp]: data.uname } }, { account_name: { [seq.Op.regexp]: data.uname } }] };
  137. const opts = {
  138. offset: (data.page - 1) * data.limit, limit: data.limit, where: {}, include: [
  139. { model: that.app.model.PaysConfig, attributes: [ 'pay_name' ], as: 'pays_config' },
  140. { model: that.app.model.Users, as: 'users', attributes: [ 'account_name', 'nickname' ], where: userWhere },
  141. {
  142. model: that.app.model.OrdersProducts, as: 'orders_products', attributes: {
  143. exclude: [ 'product_id', 'create_time', 'order_id', 'activity_name', 'activity_id', 'activity_desc' ],
  144. }, where: productWhere, order: [[ 'rec_id', 'asc' ]],
  145. },
  146. ], attributes: {
  147. exclude: [ 'seller_remarks', 'province_id', 'city_id', 'county_id', 'shipping_id', 'shipping_fee', 'activity_id', 'activity_name', 'activity_desc' ],
  148. }, order: [[ 'order_id', 'desc' ]],
  149. };
  150. if (data.order_status !== '') opts.where.order_status = Number(data.order_status);
  151. if (data.order_time) opts.where.create_time = { [seq.Op.between]: data.order_time.split(',') };
  152. const selectBean = await that.app.comoBean.instance(data, opts);
  153. const result = await that.service.base.select(selectBean, that.useModel, '订单列表查询失败,请稍候重试', true, true);
  154. return result;
  155. }
  156. /**
  157. * [orderDeliver 订单发货操作]
  158. * @return {[type]} [description]
  159. */
  160. async orderDeliver() {
  161. const that = this;
  162. let transaction;
  163. try {
  164. const data = await that.ctx.validate(that.deliverValidate, await that.ctx.anyParse());
  165. transaction = await that.app.model.transaction();
  166. const updateBean = await that.app.comoBean.instance({
  167. is_express: data.is_express, express_sn: data.express_sn, express_id: data.express_id,
  168. deliver_desc: data.deliver_desc, deliver_time: data.deliver_time, order_status: 2,
  169. }, { where: { order_id: data.order_id }, transaction });
  170. const result = await that.service.base.update(updateBean, that.useModel, '订单发货失败,请稍候重试');
  171. for (let i = 0; i < data.goods.length; i++) {
  172. const item = data.goods[i];
  173. const tmp = await that.app.model.OrdersProducts.update({
  174. deliver_count: Number(item.deliver_count), is_deliver: 1,
  175. }, { where: { rec_id: item.rec_id, order_id: data.order_id }, transaction });
  176. if (!tmp) throw new Error('订单发货失败,请稍候重试');
  177. }
  178. // 记录订单操作日志
  179. await that.service.order.orderAction({
  180. admin_id: data.admin_id,
  181. order_id: data.order_id,
  182. action_desc: '订单发货',
  183. });
  184. // 订单发货时扣除库存,受控于后台配置的扣除时机
  185. await that.service.order.productStockSub(data.order_id, transaction, 2);
  186. await transaction.commit();
  187. return that.ctx.appJson(that.app.szjcomo.appResult('SUCCESS', result, false));
  188. } catch (err) {
  189. if (transaction) await transaction.rollback();
  190. return that.ctx.appJson(that.app.szjcomo.appResult(err.message));
  191. }
  192. }
  193. /**
  194. * [orderLogs 查看订单操作日志]
  195. * @return {[type]} [description]
  196. */
  197. async orderLogs() {
  198. const that = this;
  199. try {
  200. const data = await that.ctx.validate(that.loginValidate, await that.ctx.getParse());
  201. const result = await that.service.order.orderLogs(data.order_id);
  202. return that.ctx.appJson(that.app.szjcomo.appResult('SUCCESS', result, false));
  203. } catch (err) {
  204. return that.ctx.appJson(that.app.szjcomo.appResult(err.message));
  205. }
  206. }
  207. /**
  208. * [orderPrinter 订单打印]
  209. * @return {[type]} [description]
  210. */
  211. async orderPrinter() {
  212. const that = this;
  213. try {
  214. const data = await that.ctx.validate(that.loginValidate, await that.ctx.getParse());
  215. const result = await that.service.shop.orderPrinter(data.order_id, true);
  216. return that.ctx.appJson(that.app.szjcomo.appResult('SUCCESS', result, false));
  217. } catch (err) {
  218. return that.ctx.appJson(that.app.szjcomo.appResult(err.message));
  219. }
  220. }
  221. };