package com.crm.email.controller;

import com.crm.email.service.SysEmailRecordService;
import com.crm.rely.backend.core.constant.Constants;
import com.crm.rely.backend.core.constant.FeignClientAnnotation;
import com.crm.rely.backend.core.dto.base.BaseResultDto;
import com.crm.rely.backend.core.dto.base.ResultWithPagerDto;
import com.crm.rely.backend.core.dto.system.email.record.SysEmailRecordSearchDto;
import com.crm.rely.backend.core.entity.system.email.SupplementSendEmailBatchEntity;
import com.crm.rely.backend.core.entity.system.email.SupplementSendEmailEntity;
import com.crm.rely.backend.core.entity.system.email.SysEmailSendEntity;
import com.crm.rely.backend.core.entity.system.email.SysEmailTestSendEntity;
import com.crm.rely.backend.core.entity.system.email.record.SysEmailRecordSearchEntity;
import com.crm.rely.backend.util.DateUtil;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping("/email")
public class EmailController {

    @Resource
    private SysEmailRecordService sysEmailRecordService;

    /**
     * 正常发送邮件
     *
     * @param entity
     * @return
     * @throws Exception
     */
    @PostMapping("/send")
    public BaseResultDto sendEmail(@RequestBody SysEmailSendEntity entity) throws Exception {

        return sysEmailRecordService.send(entity);
    }

    @PostMapping("/manager/test/send")
    @FeignClientAnnotation
    public BaseResultDto sendEmailTest(@RequestBody SysEmailTestSendEntity entity) throws Exception {

        return sysEmailRecordService.send(entity);
    }

    /**
     * 补充发送 即正常发送失败后 可以手动点击发送记录 根据记录id和内容再次发送邮件
     *
     * @param entity
     * @return
     * @throws Exception
     */
    @PostMapping("/supplement/send")
    public BaseResultDto supplementSendEmail(@RequestBody @Validated SupplementSendEmailEntity entity) throws Exception {

        return sysEmailRecordService.send(entity);
    }

    @PostMapping("/manager/supplement/send")
    @FeignClientAnnotation
    public BaseResultDto managerSupplementSendEmail(@RequestBody @Validated SupplementSendEmailEntity entity) throws Exception {

        return sysEmailRecordService.send(entity);
    }

    @PostMapping("/manager/supplement/send/batch")
    @FeignClientAnnotation
    public BaseResultDto managerSupplementSendBatchEmail(@RequestBody @Validated SupplementSendEmailBatchEntity entity) throws Exception {

        for (Long id : entity.getIds()) {
            sysEmailRecordService.send(id);
        }

        return BaseResultDto.success(Constants.MAIL_SUCCESS);
    }

    @PostMapping("/manager/search/list")
    @FeignClientAnnotation
    public ResultWithPagerDto<SysEmailRecordSearchDto> searchList(@RequestBody @Validated SysEmailRecordSearchEntity entity) throws Exception {
        //格式化时间
        if (entity.getEndDate() != null) {
            entity.setEndDate(DateUtil.operationDay(entity.getEndDate(), 1));
        }
        if (entity.getStartDate() != null) {
            entity.setStartDate(DateUtil.parseDate(entity.getStartDate()));
        }
        return sysEmailRecordService.searchList(entity);
    }

}

