"use strict";Object.defineProperty(exports,"__esModule",{value:!0});const egg_1=require("egg");var HonorType;(function(b){b.MAX_SCORE="max_score",b.MAX_PARTICIPATION_RATE="max_participation_rate",b.MAX_SATISFIED_RATE="max_satisfied_rate"})(HonorType=exports.HonorType||(exports.HonorType={}));const limitedDay=20,limitedMonth=8;class HonorService extends egg_1.Service{async add(g,a,b,c,d){const{mysql:e}=this.app;e.query("INSERT INTO honor (station, type, value, time_type, time_value) VALUES (?, ?, ?, ?, ?)",[g,a,b,c,d])}async setMonthScoreHonor(d,a){if(0!==d.length){const b=d.reduce((c,a)=>a.score>c.score?a:c);this.add(b.stationId,HonorType.MAX_SCORE,b.score.toString(),"month",a)}}async setMonthParticipationHonor(d,a){const b=d.filter(b=>b.updateTime>=limitedDay);if(0!==b.length){const c=b.reduce((f,a)=>{const b=(f.excellent+f.good+f.ordinary+f.bad)/f.sumTrade,c=(a.excellent+a.good+a.ordinary+a.bad)/a.sumTrade,d=[c-b,a.excellent-f.excellent,a.good-f.good];for(const b of d)if(0!==b)return 0<b?a:f;return f}),d=(100*((c.excellent+c.good+c.ordinary+c.bad)/c.sumTrade)).toFixed(2);this.add(c.stationId,HonorType.MAX_PARTICIPATION_RATE,d,"month",a)}}async setMonthSatisfiedRateHonor(d,a){const b=d.filter(b=>b.updateTime>=limitedDay);if(0!==b.length){const c=b.reduce((f,a)=>{const b=(f.excellent+f.good)/f.sumTrade,c=(a.excellent+a.good)/a.sumTrade,d=[c-b,a.excellent-f.excellent,a.good-f.good];for(const b of d)if(0!==b)return 0<b?a:f;return f}),d=(100*((c.excellent+c.good)/c.sumTrade)).toFixed(2);this.add(c.stationId,HonorType.MAX_SATISFIED_RATE,d,"month",a)}}async setYearScoreHonor(d,a){if(0!==d.length){const b=d.reduce((c,a)=>a.score>c.score?a:c);this.add(b.stationId,HonorType.MAX_SCORE,b.score.toString(),"year",a)}}async setYearParticipationHonor(d,a){const b=d.filter(b=>b.updateTime>=limitedMonth);if(0!==b.length){const c=b.reduce((f,a)=>{const b=(f.excellent+f.good+f.ordinary+f.bad)/f.sumTrade,c=(a.excellent+a.good+a.ordinary+a.bad)/a.sumTrade,d=[c-b,a.excellent-f.excellent,a.good-f.good];for(const b of d)if(0!==b)return 0<b?a:f;return f}),d=(100*((c.excellent+c.good+c.ordinary+c.bad)/c.sumTrade)).toFixed(2);this.add(c.stationId,HonorType.MAX_PARTICIPATION_RATE,d,"year",a)}}async setYearSatisfiedRateHonor(d,a){const b=d.filter(b=>b.updateTime>=limitedMonth);if(0!==b.length){const c=b.reduce((f,a)=>{const b=(f.excellent+f.good)/f.sumTrade,c=(a.excellent+a.good)/a.sumTrade,d=[c-b,a.excellent-f.excellent,a.good-f.good];for(const b of d)if(0!==b)return 0<b?a:f;return f}),d=(100*((c.excellent+c.good)/c.sumTrade)).toFixed(2);this.add(c.stationId,HonorType.MAX_SATISFIED_RATE,d,"year",a)}}async select(e){const{app:a}=this;let b="SELECT honor.station AS stationId, type, value, time_type AS timeType, time_value AS timeValue, station.name AS stationName FROM honor, station WHERE honor.station = station.id";e.stationId&&e.stationId.trim()&&(b+=` AND honor.station = '${e.stationId}'`),e.type&&(b+=` AND type = '${e.type}'`),e.timeType&&(b+=` AND time_type = '${e.type}'`),e.timeStart&&e.timeEnd&&(b+=` AND time_value >= '${e.timeStart}' AND time_value <= '${e.timeEnd}'`),b+=` ORDER BY honor.id DESC LIMIT ${e.offset},${e.limit}`;const f=await a.mysql.query(b);return f}}exports.default=HonorService;