import { Controller } from 'egg';
import { GM_MAIL_TYPE, STATUS } from '@consts';
import { UpdateMailParams } from '@domain/backEndField/params';

export default class MailController extends Controller {

    public async getSingleMail() {
        const { ctx } = this;
        const { page, pageSize, sortField, sortOrder, form } = ctx.request.body;
        ctx.body = await ctx.service.mail.getGMMailList(page, pageSize, sortField, sortOrder, {...form, mailType: GM_MAIL_TYPE.SINGLE, hasGoods: true });
        return
    }
    public async getSingleMailTxt() {
        const { ctx } = this;
        const { page, pageSize, sortField, sortOrder, form } = ctx.request.body;
        ctx.body = await ctx.service.mail.getGMMailList(page, pageSize, sortField, sortOrder, {...form, mailType: GM_MAIL_TYPE.SINGLE, hasGoods: false });
        return
    }
    public async getServerMail() {
        const { ctx } = this;
        const { page, pageSize, sortField, sortOrder, form } = ctx.request.body;
        ctx.body = await ctx.service.mail.getGMMailList(page, pageSize, sortField, sortOrder, {...form, mailType: GM_MAIL_TYPE.SERVER, hasGoods: true });
        return
    }
    public async getServerMailTxt() {
        const { ctx } = this;
        const { page, pageSize, sortField, sortOrder, form } = ctx.request.body;
        ctx.body = await ctx.service.mail.getGMMailList(page, pageSize, sortField, sortOrder, {...form, mailType: GM_MAIL_TYPE.SERVER, hasGoods: false });
        return
    }
    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 findRoleByIdOrName() {
        const { ctx } = this;
        const { serverId, roleId, roleName }: { serverId: number, roleId?: string, roleName?: string } = ctx.request.body;
        if(!serverId || (!roleId && !roleName)) return ctx.body = ctx.service.utils.resResult(STATUS.WRONG_PARMS);
        ctx.body = await ctx.service.mail.findRoleByIdOrName(serverId, roleId, roleName);

        return
    }

    public async updateGMMail() {
        const { ctx } = this;
        const msg = ctx.request.body;
        let params = new UpdateMailParams(msg);
        let check = params.checkParams();
        if(!check) return ctx.body = ctx.service.utils.resResult(STATUS.WRONG_PARMS);
        if(msg._id == 'new') {
            ctx.body = await ctx.service.mail.createGmMail(params);
        } else if(msg._id) {
            ctx.body = await ctx.service.mail.updateGMMail(msg._id, params);
        } else {
            return ctx.body = ctx.service.utils.resResult(STATUS.WRONG_PARMS);
        }

        return
    }
}
