'use strict'; const _ = require('lodash'); const { BusinessError, ErrorCode } = require('naf-core').Error; const Controller = require('egg').Controller; class HomeController extends Controller { async index() { const { ctx } = this; ctx.body = 'hi, egg'; } async m1() { let score = 5; let arr = [ 5, 4, 1, 2, 4, 3, 2, 5, 5, 2, 3, 5, 0, 5, 2, 3 ]; arr = arr.sort((a, b) => b - a); const length = arr.length; console.log(arr); console.log(length); let mid, midIndex; const wl = [], gl = []; if (length % 2 !== 0) { midIndex = this.ctx.plus(Math.floor(length / 2), 1); mid = arr[midIndex]; } else { midIndex = this.ctx.divide(length, 2); mid = this.ctx.divide(this.ctx.plus(arr[midIndex], arr[midIndex + 1]), 2); } for (const i of arr) { if (i >= mid) gl.push(i); else wl.push(i); } const offset = _.floor(this.ctx.divide(gl.length, 10)); for (let i = 0; i < offset.length; i++) { wl.pop(); } score = this.ctx.minus(score, this.ctx.multiply(wl.length, 0.01)); this.ctx.ok({ data: score }); } async makeCoupons() { const dictDataModel = this.ctx.model.Dev.DictData; const couponModel = this.ctx.model.Trade.Coupon; // 过期字典 const expList = await dictDataModel.find({ code: 'coupon_expire_type' }); // 减免字典 const disList = await dictDataModel.find({ code: 'coupon_discount_type' }); // 使用字典 const useList = await dictDataModel.find({ code: 'coupon_use_limit' }); // 领取字典 const getList = await dictDataModel.find({ code: 'coupon_get_limit' }); const arr = []; for (const e of expList) { const { value: expire_type, label: el } = e; for (const d of disList) { const { value: discount_type, label: dl } = d; for (const u of useList) { const { value: use_limit, label: ul } = u; for (const g of getList) { const { value: get_limit, label: gl } = g; const obj = { issue: '0', expire_type, discount_type, use_limit, get_limit, num: 100, status: '0' }; obj.name = `平台测试优惠券-${el}-${dl}-${ul}-${gl}`; arr.push(obj); } } } } await couponModel.insertMany(arr); return arr; } } module.exports = HomeController;