package com.crm.manager.controller;

import com.crm.manager.service.MarketEmailFeignService;
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.market.MarketEmailStatusNumberDto;
import com.crm.rely.backend.core.dto.market.email.MarketEmailListDto;
import com.crm.rely.backend.core.dto.market.email.record.MarketEmailRecordListDto;
import com.crm.rely.backend.core.dto.market.upload.email.MarketUploadEmailListDto;
import com.crm.rely.backend.core.dto.system.email.template.SysEmailTemplateSearchDto;
import com.crm.rely.backend.core.entity.base.SingleLongEntity;
import com.crm.rely.backend.core.entity.market.email.MarketEmailAddEntity;
import com.crm.rely.backend.core.entity.market.email.MarketEmailSearchPageEntity;
import com.crm.rely.backend.core.entity.market.email.record.MarketEmailRecordSearchPageEntity;
import com.crm.rely.backend.core.entity.market.email.record.MarketEmailRecordSendEntity;
import com.crm.rely.backend.core.entity.market.email.record.MarketEmailRecordStatusNumberEntity;
import com.crm.rely.backend.core.entity.market.email.record.MarketEmailRecordSupplementSendEntity;
import com.crm.rely.backend.exception.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 营销群发邮件
 *
 * @author houn
 */
@RestController
@RequestMapping("/market/email")
public class MarketEmailController {

    @Autowired
    private MarketEmailFeignService marketEmailFeignService;


    /**
     * @param entity
     * @return
     * @throws Exception
     */
    @PostMapping("/add")
    public BaseResultDto add(@RequestBody @Validated MarketEmailAddEntity entity) throws Exception {
        if (entity.getCountrys() == null || entity.getCountrys().size() <= 0) {
            entity.setCountrys(null);
        }
        return marketEmailFeignService.add(entity);
    }

    @PostMapping("/add/email/template")
    public BaseResultDto<List<SysEmailTemplateSearchDto>> getEmailTemplate() throws Exception {

        return   marketEmailFeignService.getEmailTemplate();
    }

    @PostMapping("/add/upload/email")
    public BaseResultDto<List<MarketUploadEmailListDto>> getUploadEmialList() throws Exception {
        return   marketEmailFeignService.getUploadEmialList();
    }

    @PostMapping("/send/email")
    public BaseResultDto send(@RequestBody @Validated MarketEmailRecordSendEntity entity) throws ServiceException {

        return   marketEmailFeignService.send(entity);
    }


    @PostMapping("/search/list")
    public ResultWithPagerDto<MarketEmailListDto> pageList(@RequestBody @Validated MarketEmailSearchPageEntity entity) throws Exception {

        return   marketEmailFeignService.pageList(entity);
    }

    @PostMapping("/search/single")
    public BaseResultDto<MarketEmailListDto> single(@RequestBody @Validated SingleLongEntity entity) throws Exception {

        return   marketEmailFeignService.single(entity);
    }

    /**
     * @param entity
     * @return
     * @throws Exception
     */
    @PostMapping("/search/list/record")
    public ResultWithPagerDto<MarketEmailRecordListDto> pageListRecord(@RequestBody @Validated MarketEmailRecordSearchPageEntity entity) throws Exception {
        return   marketEmailFeignService.pageListRecord(entity);
    }

    @PostMapping("/search/list/record/status/number")
    public BaseResultDto<MarketEmailStatusNumberDto> statusNumber(@RequestBody @Validated MarketEmailRecordStatusNumberEntity entity) throws Exception {
        return   marketEmailFeignService.statusNumber(entity);
    }

    @PostMapping("/supplement/send")
    public BaseResultDto supplementSend(@RequestBody @Validated SingleLongEntity entity) throws Exception {

        return   marketEmailFeignService.supplementSend(entity);
    }

    @PostMapping("/supplement/send/batch")
    public BaseResultDto supplementSend(@RequestBody @Validated MarketEmailRecordSupplementSendEntity entity) throws Exception {

        return   marketEmailFeignService.supplementSend(entity);
    }

    @PostMapping("/add/upload")
    public BaseResultDto upload(@RequestParam(value = "file", required = false) MultipartFile file) throws Exception {
        return   marketEmailFeignService.upload(file);
    }
}
