home.js 2.4 KB

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