package com.gree.mall.manager.controller.policy;

import cn.hutool.core.lang.TypeReference;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gree.mall.manager.annotation.ZfireList;
import com.gree.mall.manager.bean.policy.*;
import com.gree.mall.manager.exception.RemoteServiceException;
import com.gree.mall.manager.helper.ResponseHelper;
import com.gree.mall.manager.logic.policy.AgreementLogic;
import com.gree.mall.manager.plus.entity.Mailbox;
import com.gree.mall.manager.plus.entity.MailboxSendRecord;
import com.gree.mall.manager.plus.entity.MailboxSendRecordPolicy;
import com.gree.mall.manager.zfire.bean.ZfireParamBean;
import com.gree.mall.manager.zfire.util.FieldUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.mail.MessagingException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;

@Slf4j
@RestController
@Api(value = "邮箱配置和邮箱记录", tags = {"邮箱配置和邮箱记录"})
@RequestMapping(value = "/mailbox", produces = "application/json; charset=utf-8")
public class MailboxController {

    @Autowired
    private AgreementLogic agreementLogic;


    @PostMapping("/detail")
    @ApiOperation("邮箱配置详情")
    public ResponseHelper<MailboxAdd> detail(
            HttpServletRequest request
    ) throws ParseException {
        MailboxAdd mailbox = agreementLogic.detailMailbox();
        return ResponseHelper.success(mailbox);
    }

    @PostMapping("/add")
    @ApiOperation("邮箱配置修改")
    public ResponseHelper add(
            @RequestBody MailboxAdd mailbox,
            HttpServletRequest request
    ) throws ParseException {
        agreementLogic.addDetail(mailbox);
        return ResponseHelper.success();
    }


    @ZfireList
    @PostMapping("/list")
    @ApiOperation(value = "已发邮箱记录列表")
    public ResponseHelper<IPage<MailBoxSendRecordVo>> list(
            @RequestBody ZfireParamBean zfireParamBean
    ) throws RemoteServiceException {
        ZfireParamBean zfireParam = FieldUtils.supplyParam(zfireParamBean);
        IPage<MailBoxSendRecordVo> increVOIPage = agreementLogic.listMailBoxRecord(new Page(zfireParam.getPageNum(), zfireParam.getPageSize()), zfireParam);
        return ResponseHelper.success(increVOIPage, new TypeReference<MailBoxSendRecordVo>() {});
    }

    @PostMapping("/list/export")
    @ApiOperation(value = "已发邮箱记录列表导出")
    public void listExport(
            @RequestBody ZfireParamBean zfireParamBean,
            HttpServletRequest request,
            HttpServletResponse response
    ) throws Exception {
        //1.组装查询条件
        ZfireParamBean zfireParam = FieldUtils.supplyParam(zfireParamBean);
        //2.查询要导出的内容
        IPage<MailBoxSendRecordVo> increVOIPage = agreementLogic.listMailBoxRecord(new Page(zfireParam.getPageNum(), zfireParam.getPageSize()), zfireParam);
        //3.导出
        FieldUtils.exportData(increVOIPage.getRecords(), zfireParam.getExportFields(), request, response);
    }

    @PostMapping("/detailRecord")
    @ApiOperation("邮箱记录详情")
    public ResponseHelper<MailboxSendRecord> detail(
            @ApiParam(value = "id", required = true) @RequestParam String id,
            HttpServletRequest request
    ) throws ParseException {
        MailboxSendRecord mailboxSendRecord = agreementLogic.detailRecord(id);
        return ResponseHelper.success(mailboxSendRecord);
    }


    @ZfireList
    @PostMapping("/listPolicy")
    @ApiOperation(value = "保单信息列表")
    public ResponseHelper<IPage<MailboxSendRecordPolicyVo>> listPolicy(
            @RequestBody ZfireParamBean zfireParamBean
    ) throws RemoteServiceException {
        ZfireParamBean zfireParam = FieldUtils.supplyParam(zfireParamBean);
        IPage<MailboxSendRecordPolicyVo> increVOIPage = agreementLogic.listPolicy(new Page(zfireParam.getPageNum(), zfireParam.getPageSize()), zfireParam);
        return ResponseHelper.success(increVOIPage, new TypeReference<MailboxSendRecordPolicyVo>() {});
    }

    @PostMapping("/listPolicy/export")
    @ApiOperation(value = "保单信息列表导出")
    public void listPolicyExport(
            @RequestBody ZfireParamBean zfireParamBean,
            HttpServletRequest request,
            HttpServletResponse response
    ) throws Exception {
        //1.组装查询条件
        ZfireParamBean zfireParam = FieldUtils.supplyParam(zfireParamBean);
        //2.查询要导出的内容
        IPage<MailboxSendRecordPolicyVo> increVOIPage = agreementLogic.listPolicy(new Page(zfireParam.getPageNum(), zfireParam.getPageSize()), zfireParam);
        //3.导出
        FieldUtils.exportData(increVOIPage.getRecords(), zfireParam.getExportFields(), request, response);
    }

    @ApiOperation(value = "已发邮箱记录-重新发送")
    @PostMapping("resend")
    public ResponseHelper send(@ApiParam(value = "批次号", required = false)
                                   @RequestParam(required = false) String sendBatch) throws IOException {
        agreementLogic.sendEmail(sendBatch);
        return ResponseHelper.success();
    }


    @ApiOperation(value = "发送邮件")
    @PostMapping("test")
    public ResponseHelper send(){
        agreementLogic.sendEmailSche();
        return ResponseHelper.success();
    }


    @ApiOperation(value = "回执")
    @PostMapping("huizhi")
    public ResponseHelper huizhi() throws IOException, MessagingException {
        agreementLogic.receEmail();
        return ResponseHelper.success();
    }

}
