orders.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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 },
  110. include: [
  111. { model: that.app.model.PaysConfig, attributes: [ 'pay_name' ], as: 'pays_config' },
  112. { model: that.app.model.Users, as: 'users', attributes: [ 'account_name', 'nickname' ] },
  113. { model: that.app.model.Users, as: 'inviter', attributes: [ 'account_name', 'nickname' ] },
  114. {
  115. model: that.app.model.OrdersProducts, as: 'orders_products', attributes: {
  116. exclude: [ 'product_id', 'create_time', 'order_id', 'activity_name', 'activity_id', 'activity_desc' ],
  117. },
  118. },
  119. { model: that.app.model.Payments, as: 'payments', attributes: [ 'create_time' ], required: false },
  120. ], attributes: {
  121. exclude: [ 'seller_remarks', 'province_id', 'city_id', 'county_id', 'shipping_id', 'shipping_fee', 'activity_id', 'activity_name', 'activity_desc' ],
  122. },
  123. };
  124. const selectBean = await that.app.comoBean.instance({}, options);
  125. return await that.service.base.select(selectBean, that.useModel, '查询订单详情失败,请稍候重试', false, false);
  126. }
  127. /**
  128. * [selectList 订单列表]
  129. * @param {Object} data [description]
  130. * @return {[type]} [description]
  131. */
  132. async selectList(data = {}) {
  133. const that = this;
  134. const seq = that.app.Sequelize;
  135. const productWhere = {};
  136. let userWhere = {};
  137. if (data.product_name) productWhere.product_name = { [seq.Op.regexp]: data.product_name };
  138. if (data.uname) userWhere = { [seq.Op.or]: [{ nickname: { [seq.Op.regexp]: data.uname } }, { account_name: { [seq.Op.regexp]: data.uname } }] };
  139. const opts = {
  140. offset: (data.page - 1) * data.limit, limit: data.limit, where: {}, include: [
  141. { model: that.app.model.PaysConfig, attributes: [ 'pay_name' ], as: 'pays_config' },
  142. { model: that.app.model.Users, as: 'users', attributes: [ 'account_name', 'nickname' ], where: userWhere },
  143. { model: that.app.model.Users, as: 'inviter', attributes: [ 'account_name', 'nickname' ] },
  144. {
  145. model: that.app.model.OrdersProducts, as: 'orders_products', attributes: {
  146. exclude: [ 'product_id', 'create_time', 'order_id', 'activity_name', 'activity_id', 'activity_desc' ],
  147. }, where: productWhere, order: [[ 'rec_id', 'asc' ]],
  148. },
  149. ], attributes: {
  150. exclude: [ 'seller_remarks', 'province_id', 'city_id', 'county_id', 'shipping_id', 'shipping_fee', 'activity_id', 'activity_name', 'activity_desc' ],
  151. }, order: [[ 'order_id', 'desc' ]],
  152. };
  153. if (data.order_status !== '') opts.where.order_status = Number(data.order_status);
  154. if (data.order_time) opts.where.create_time = { [seq.Op.between]: data.order_time.split(',') };
  155. const selectBean = await that.app.comoBean.instance(data, opts);
  156. const result = await that.service.base.select(selectBean, that.useModel, '订单列表查询失败,请稍候重试', true, true);
  157. return result;
  158. }
  159. /**
  160. * [orderDeliver 订单发货操作]
  161. * @return {[type]} [description]
  162. */
  163. async orderDeliver() {
  164. const that = this;
  165. let transaction;
  166. try {
  167. const data = await that.ctx.validate(that.deliverValidate, await that.ctx.anyParse());
  168. transaction = await that.app.model.transaction();
  169. const updateBean = await that.app.comoBean.instance({
  170. is_express: data.is_express, express_sn: data.express_sn, express_id: data.express_id,
  171. deliver_desc: data.deliver_desc, deliver_time: data.deliver_time, order_status: 2,
  172. }, { where: { order_id: data.order_id }, transaction });
  173. const result = await that.service.base.update(updateBean, that.useModel, '订单发货失败,请稍候重试');
  174. for (let i = 0; i < data.goods.length; i++) {
  175. const item = data.goods[i];
  176. const tmp = await that.app.model.OrdersProducts.update({
  177. deliver_count: Number(item.deliver_count), is_deliver: 1,
  178. }, { where: { rec_id: item.rec_id, order_id: data.order_id }, transaction });
  179. if (!tmp) throw new Error('订单发货失败,请稍候重试');
  180. }
  181. // 记录订单操作日志
  182. await that.service.order.orderAction({
  183. admin_id: data.admin_id,
  184. order_id: data.order_id,
  185. action_desc: '订单发货',
  186. });
  187. // 订单发货时扣除库存,受控于后台配置的扣除时机
  188. await that.service.order.productStockSub(data.order_id, transaction, 2);
  189. await transaction.commit();
  190. return that.ctx.appJson(that.app.szjcomo.appResult('SUCCESS', result, false));
  191. } catch (err) {
  192. if (transaction) await transaction.rollback();
  193. return that.ctx.appJson(that.app.szjcomo.appResult(err.message));
  194. }
  195. }
  196. /**
  197. * [orderLogs 查看订单操作日志]
  198. * @return {[type]} [description]
  199. */
  200. async orderLogs() {
  201. const that = this;
  202. try {
  203. const data = await that.ctx.validate(that.loginValidate, await that.ctx.getParse());
  204. const result = await that.service.order.orderLogs(data.order_id);
  205. return that.ctx.appJson(that.app.szjcomo.appResult('SUCCESS', result, false));
  206. } catch (err) {
  207. return that.ctx.appJson(that.app.szjcomo.appResult(err.message));
  208. }
  209. }
  210. /**
  211. * [orderPrinter 订单打印]
  212. * @return {[type]} [description]
  213. */
  214. async orderPrinter() {
  215. const that = this;
  216. try {
  217. const data = await that.ctx.validate(that.loginValidate, await that.ctx.getParse());
  218. const result = await that.service.shop.orderPrinter(data.order_id, true);
  219. return that.ctx.appJson(that.app.szjcomo.appResult('SUCCESS', result, false));
  220. } catch (err) {
  221. return that.ctx.appJson(that.app.szjcomo.appResult(err.message));
  222. }
  223. }
  224. };