import { Controller } from 'egg';
import { STATUS } from '@consts';

export default class MailController extends Controller {
    public async getGMMailList() {
        const { ctx } = this;
        const { page, pageSize, sortField, sortOrder, form } = ctx.request.body;
        ctx.body = await ctx.service.mail.getGMMailList(page, pageSize, sortField, sortOrder, form);
        return
    }

    public async updateGMMail() {
        const { ctx } = this;
        const { _id, content, sendName, sendTime, endTime, useTempTime, continueHour, goods } = ctx.request.body;
        if(!_id) return ctx.body = ctx.service.utils.resResult(STATUS.WRONG_PARMS);
        if(useTempTime) {
            if(!sendTime || !endTime) return ctx.body = ctx.service.utils.resResult(STATUS.WRONG_PARMS);
        } else {
            if(!continueHour) return ctx.body = ctx.service.utils.resResult(STATUS.WRONG_PARMS);
        }
        let updateGoods = [];
        try{
            updateGoods = JSON.parse(goods);
        } catch(e) {
            return ctx.body = ctx.service.utils.resResult(STATUS.WRONG_PARMS);
        }
        ctx.body = await ctx.service.mail.updateGMMail(_id, content, sendName, useTempTime, sendTime, endTime, continueHour, updateGoods);
        return
    }

    public async getGMMailRecords() {
        const { ctx } = this;
        const { page, pageSize, sortField, sortOrder, form } = ctx.request.body;
        ctx.body = await ctx.service.mail.getGMMailRecords(page, pageSize, sortField, sortOrder, form);
        return
    }
}
