orders.js 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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. * 确认收货验证器
  71. * @date:2024/7/8
  72. */
  73. get receiveValidate() {
  74. const that = this;
  75. return {
  76. order_status: that.ctx.rules.default(3)
  77. .number(),
  78. };
  79. }
  80. /**
  81. * [logsValidate 订单操作日志验证器]
  82. * @return {[type]} [description]
  83. */
  84. get logsValidate() {
  85. const that = this;
  86. return {
  87. order_id: that.ctx.rules.name('订单ID')
  88. .required()
  89. .notEmpty()
  90. .number(),
  91. };
  92. }
  93. /**
  94. * [select 查询订单列表]
  95. * @return {[type]} [description]
  96. */
  97. async select() {
  98. const that = this;
  99. try {
  100. const data = await that.ctx.validate(that.selectValidate, await that.ctx.getParse());
  101. let result;
  102. if (data.order_id) {
  103. result = await that.selectInfo(data.order_id);
  104. } else {
  105. result = await that.selectList(data);
  106. }
  107. return that.ctx.appJson(that.app.szjcomo.appResult('SUCCESS', result, false));
  108. } catch (err) {
  109. return that.ctx.appJson(that.app.szjcomo.appResult(err.message));
  110. }
  111. }
  112. /**
  113. * [selectInfo 订单详情]
  114. * @param {[type]} order_id [description]
  115. * @return {[type]} [description]
  116. */
  117. async selectInfo(order_id) {
  118. const that = this;
  119. const options = {
  120. where: { order_id },
  121. include: [
  122. { model: that.app.model.PaysConfig, attributes: [ 'pay_name' ], as: 'pays_config' },
  123. { model: that.app.model.Users, as: 'users', attributes: [ 'account_name', 'nickname' ] },
  124. { model: that.app.model.Users, as: 'inviter', attributes: [ 'account_name', 'nickname' ] },
  125. {
  126. model: that.app.model.OrdersProducts, as: 'orders_products', attributes: {
  127. exclude: [ 'product_id', 'create_time', 'order_id', 'activity_name', 'activity_id', 'activity_desc' ],
  128. },
  129. },
  130. { model: that.app.model.Payments, as: 'payments', attributes: [ 'create_time' ], required: false },
  131. ], attributes: {
  132. exclude: [ 'seller_remarks', 'province_id', 'city_id', 'county_id', 'shipping_id', 'shipping_fee', 'activity_id', 'activity_name', 'activity_desc' ],
  133. },
  134. };
  135. const selectBean = await that.app.comoBean.instance({}, options);
  136. return await that.service.base.select(selectBean, that.useModel, '查询订单详情失败,请稍候重试', false, false);
  137. }
  138. /**
  139. * [selectList 订单列表]
  140. * @param {Object} data [description]
  141. * @return {[type]} [description]
  142. */
  143. async selectList(data = {}) {
  144. const that = this;
  145. const seq = that.app.Sequelize;
  146. const productWhere = {};
  147. let userWhere = {};
  148. if (data.product_name) productWhere.product_name = { [seq.Op.regexp]: data.product_name };
  149. if (data.uname) userWhere = { [seq.Op.or]: [{ nickname: { [seq.Op.regexp]: data.uname } }, { account_name: { [seq.Op.regexp]: data.uname } }] };
  150. const opts = {
  151. offset: (data.page - 1) * data.limit, limit: data.limit, where: {}, include: [
  152. { model: that.app.model.PaysConfig, attributes: [ 'pay_name' ], as: 'pays_config' },
  153. { model: that.app.model.Users, as: 'users', attributes: [ 'account_name', 'nickname' ], where: userWhere },
  154. { model: that.app.model.Users, as: 'inviter', attributes: [ 'account_name', 'nickname' ] },
  155. {
  156. model: that.app.model.OrdersProducts, as: 'orders_products', attributes: {
  157. exclude: [ 'product_id', 'create_time', 'order_id', 'activity_name', 'activity_id', 'activity_desc' ],
  158. }, where: productWhere, order: [[ 'rec_id', 'asc' ]],
  159. },
  160. ], attributes: {
  161. exclude: [ 'seller_remarks', 'province_id', 'city_id', 'county_id', 'shipping_id', 'shipping_fee', 'activity_id', 'activity_name', 'activity_desc' ],
  162. }, order: [[ 'order_id', 'desc' ]],
  163. };
  164. if (data.order_status !== '') opts.where.order_status = Number(data.order_status);
  165. if (data.order_time) opts.where.create_time = { [seq.Op.between]: data.order_time.split(',') };
  166. const selectBean = await that.app.comoBean.instance(data, opts);
  167. const result = await that.service.base.select(selectBean, that.useModel, '订单列表查询失败,请稍候重试', true, true);
  168. return result;
  169. }
  170. /**
  171. * [orderDeliver 订单发货操作]
  172. * @return {[type]} [description]
  173. */
  174. async orderDeliver() {
  175. const that = this;
  176. let transaction;
  177. try {
  178. const data = await that.ctx.validate(that.deliverValidate, await that.ctx.anyParse());
  179. transaction = await that.app.model.transaction();
  180. const updateBean = await that.app.comoBean.instance({
  181. is_express: data.is_express, express_sn: data.express_sn, express_id: data.express_id,
  182. deliver_desc: data.deliver_desc, deliver_time: data.deliver_time, order_status: 2,
  183. }, { where: { order_id: data.order_id }, transaction });
  184. const result = await that.service.base.update(updateBean, that.useModel, '订单发货失败,请稍候重试');
  185. for (let i = 0; i < data.goods.length; i++) {
  186. const item = data.goods[i];
  187. const tmp = await that.app.model.OrdersProducts.update({
  188. deliver_count: Number(item.deliver_count), is_deliver: 1,
  189. }, { where: { rec_id: item.rec_id, order_id: data.order_id }, transaction });
  190. if (!tmp) throw new Error('订单发货失败,请稍候重试');
  191. }
  192. // 记录订单操作日志
  193. await that.service.order.orderAction({
  194. admin_id: data.admin_id,
  195. order_id: data.order_id,
  196. action_desc: '订单发货',
  197. });
  198. // 订单发货时扣除库存,受控于后台配置的扣除时机
  199. await that.service.order.productStockSub(data.order_id, transaction, 2);
  200. await transaction.commit();
  201. return that.ctx.appJson(that.app.szjcomo.appResult('SUCCESS', result, false));
  202. } catch (err) {
  203. if (transaction) await transaction.rollback();
  204. return that.ctx.appJson(that.app.szjcomo.appResult(err.message));
  205. }
  206. }
  207. /**
  208. * [orderDeliver 订单确认收货操作]
  209. * @return {[type]} [description]
  210. */
  211. async orderReceive() {
  212. const that = this;
  213. let transaction;
  214. try {
  215. const data = await that.ctx.validate(that.receiveValidate, await that.ctx.anyParse());
  216. transaction = await that.app.model.transaction();
  217. const updateBean = await that.app.comoBean.instance({
  218. order_status: 3,
  219. }, { where: { order_id: data.order_id }, transaction });
  220. const result = await that.service.base.update(updateBean, that.useModel, '订单确认收货失败,请稍候重试');
  221. // 记录订单操作日志
  222. await that.service.order.orderAction({
  223. admin_id: data.admin_id,
  224. order_id: data.order_id,
  225. action_desc: '订单确认收货',
  226. });
  227. await transaction.commit();
  228. return that.ctx.appJson(that.app.szjcomo.appResult('SUCCESS', result, false));
  229. } catch (err) {
  230. if (transaction) await transaction.rollback();
  231. return that.ctx.appJson(that.app.szjcomo.appResult(err.message));
  232. }
  233. }
  234. /**
  235. * [orderLogs 查看订单操作日志]
  236. * @return {[type]} [description]
  237. */
  238. async orderLogs() {
  239. const that = this;
  240. try {
  241. const data = await that.ctx.validate(that.loginValidate, await that.ctx.getParse());
  242. const result = await that.service.order.orderLogs(data.order_id);
  243. return that.ctx.appJson(that.app.szjcomo.appResult('SUCCESS', result, false));
  244. } catch (err) {
  245. return that.ctx.appJson(that.app.szjcomo.appResult(err.message));
  246. }
  247. }
  248. /**
  249. * [orderPrinter 订单打印]
  250. * @return {[type]} [description]
  251. */
  252. async orderPrinter() {
  253. const that = this;
  254. try {
  255. const data = await that.ctx.validate(that.loginValidate, await that.ctx.getParse());
  256. const result = await that.service.shop.orderPrinter(data.order_id, true);
  257. return that.ctx.appJson(that.app.szjcomo.appResult('SUCCESS', result, false));
  258. } catch (err) {
  259. return that.ctx.appJson(that.app.szjcomo.appResult(err.message));
  260. }
  261. }
  262. };