home.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. 'use strict';
  2. const _ = require('lodash');
  3. const { BusinessError, ErrorCode } = require('naf-core').Error;
  4. const Controller = require('egg').Controller;
  5. class HomeController extends Controller {
  6. async index() {
  7. const { ctx } = this;
  8. ctx.body = 'hi, egg';
  9. }
  10. async m1() {
  11. let score = 5;
  12. let arr = [ 5, 4, 1, 2, 4, 3, 2, 5, 5, 2, 3, 5, 0, 5, 2, 3 ];
  13. arr = arr.sort((a, b) => b - a);
  14. const length = arr.length;
  15. console.log(arr);
  16. console.log(length);
  17. let mid,
  18. midIndex;
  19. const wl = [],
  20. gl = [];
  21. if (length % 2 !== 0) {
  22. midIndex = this.ctx.plus(Math.floor(length / 2), 1);
  23. mid = arr[midIndex];
  24. } else {
  25. midIndex = this.ctx.divide(length, 2);
  26. mid = this.ctx.divide(this.ctx.plus(arr[midIndex], arr[midIndex + 1]), 2);
  27. }
  28. for (const i of arr) {
  29. if (i >= mid) gl.push(i);
  30. else wl.push(i);
  31. }
  32. const offset = _.floor(this.ctx.divide(gl.length, 10));
  33. for (let i = 0; i < offset.length; i++) {
  34. wl.pop();
  35. }
  36. score = this.ctx.minus(score, this.ctx.multiply(wl.length, 0.01));
  37. this.ctx.ok({ data: score });
  38. }
  39. async makeCoupons() {
  40. const dictDataModel = this.ctx.model.Dev.DictData;
  41. const couponModel = this.ctx.model.Trade.Coupon;
  42. // 过期字典
  43. const expList = await dictDataModel.find({ code: 'coupon_expire_type' });
  44. // 减免字典
  45. const disList = await dictDataModel.find({ code: 'coupon_discount_type' });
  46. // 使用字典
  47. const useList = await dictDataModel.find({ code: 'coupon_use_limit' });
  48. // 领取字典
  49. const getList = await dictDataModel.find({ code: 'coupon_get_limit' });
  50. const arr = [];
  51. for (const e of expList) {
  52. const { value: expire_type, label: el } = e;
  53. for (const d of disList) {
  54. const { value: discount_type, label: dl } = d;
  55. for (const u of useList) {
  56. const { value: use_limit, label: ul } = u;
  57. for (const g of getList) {
  58. const { value: get_limit, label: gl } = g;
  59. const obj = { issue: '0', expire_type, discount_type, use_limit, get_limit, num: 100, status: '0' };
  60. obj.name = `平台测试优惠券-${el}-${dl}-${ul}-${gl}`;
  61. arr.push(obj);
  62. }
  63. }
  64. }
  65. }
  66. await couponModel.insertMany(arr);
  67. return arr;
  68. }
  69. }
  70. module.exports = HomeController;