package com.crm.manager.controller;

import com.crm.manager.service.MarketEmailRecordService;
import com.crm.manager.service.MarketEmailService;
import com.crm.manager.service.SysEmailTemplateService;
import com.crm.rely.backend.core.constant.Constants;
import com.crm.rely.backend.core.constant.PrefixEnum;
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.template.SysEmailTemplateSearchDto;
import com.crm.rely.backend.core.entity.base.SingleLongEntity;
import com.crm.rely.backend.core.entity.login.InfoEntity;
import com.crm.rely.backend.core.entity.system.email.template.SysEmailTemplateSearchEntity;
import com.crm.rely.backend.core.exception.ServiceException;
import com.crm.rely.backend.model.dto.market.MarketEmailStatusNumberDto;
import com.crm.rely.backend.model.dto.market.email.MarketEmailListDto;
import com.crm.rely.backend.model.dto.market.email.record.MarketEmailRecordListDto;
import com.crm.rely.backend.model.entity.market.MarketEmailAddEntity;
import com.crm.rely.backend.model.entity.market.MarketEmailSearchPageEntity;
import com.crm.rely.backend.model.entity.market.MarketEmailTestEntity;
import com.crm.rely.backend.model.entity.market.record.MarketEmailRecordSearchPageEntity;
import com.crm.rely.backend.model.entity.market.record.MarketEmailRecordSendEntity;
import com.crm.rely.backend.model.entity.market.record.MarketEmailRecordStatusNumberEntity;
import com.crm.rely.backend.model.entity.market.record.MarketEmailRecordSupplementSendEntity;
import com.crm.rely.backend.model.pojo.table.MarketEmailTable;
import com.crm.rely.backend.service.EmailService;
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 MarketEmailService marketEmailService;

    @Autowired
    private MarketEmailRecordService marketEmailRecordService;

    @Autowired
    private SysEmailTemplateService sysEmailTemplateService;

    @Autowired
    private EmailService emailService;

    /**
     * @param entity
     * @return
     * @throws Exception
     */
    @PostMapping("/add")
    public synchronized BaseResultDto add(@RequestBody @Validated MarketEmailAddEntity entity) throws Exception {


        MarketEmailTable marketEmailTable = marketEmailService.add(entity);

        return BaseResultDto.success(marketEmailTable.getId());
    }

    @PostMapping("/test")
    public synchronized BaseResultDto test(@RequestBody @Validated MarketEmailTestEntity entity) throws Exception {

        marketEmailService.test(entity);

        return BaseResultDto.success();
    }

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

        marketEmailRecordService.send(entity);
        return BaseResultDto.success();
    }

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

        if (PrefixEnum.PREFIX_SALE.equals(infoEntity.getPrefix()) || PrefixEnum.PREFIX_MARKET.equals(infoEntity.getPrefix())) {
            entity.setAddUser(infoEntity.getId());
        } else {
            entity.setAddUser(null);
        }
        ResultWithPagerDto<MarketEmailListDto> result = marketEmailService.searchPageList(entity);

        return result;
    }

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

        MarketEmailListDto marketEmailListDto = marketEmailService.searchSingle(entity);

        return BaseResultDto.success(marketEmailListDto);
    }

    /**
     * @param entity
     * @return
     * @throws Exception
     */
    @PostMapping("/search/list/record")
    public ResultWithPagerDto<MarketEmailRecordListDto> pageListRecord(@RequestBody @Validated MarketEmailRecordSearchPageEntity entity, InfoEntity infoEntity) throws Exception {
        if (PrefixEnum.PREFIX_SALE.equals(infoEntity.getPrefix()) || PrefixEnum.PREFIX_MARKET.equals(infoEntity.getPrefix())) {
            entity.setAddUser(infoEntity.getId());
        } else {
            entity.setAddUser(null);
        }
        ResultWithPagerDto<MarketEmailRecordListDto> result = marketEmailRecordService.searchPageList(entity);

        return result;
    }

    @PostMapping("/search/list/record/status/number")
    public BaseResultDto<MarketEmailStatusNumberDto> statusNumber(@RequestBody @Validated MarketEmailRecordStatusNumberEntity entity, InfoEntity infoEntity) throws Exception {
        if (PrefixEnum.PREFIX_SALE.equals(infoEntity.getPrefix()) || PrefixEnum.PREFIX_MARKET.equals(infoEntity.getPrefix())) {
            entity.setAddUser(infoEntity.getId());
        } else {
            entity.setAddUser(null);
        }
        MarketEmailStatusNumberDto marketEmailStatusNumberDto = marketEmailRecordService.statusNumber(entity);

        return BaseResultDto.success(marketEmailStatusNumberDto);
    }

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

        BaseResultDto result = marketEmailRecordService.supplementSend(entity);

        return result;
    }

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

        marketEmailRecordService.supplementSend(entity);

        return BaseResultDto.success();
    }

    @PostMapping("/upload")
    public BaseResultDto upload(@RequestParam(value = "file", required = false) MultipartFile file) throws Exception {

        marketEmailRecordService.validated(file);
        String path = marketEmailRecordService.uploadFile(file);
        return BaseResultDto.success(Constants.SUCCESS, path);
    }

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

        SysEmailTemplateSearchEntity entity = new SysEmailTemplateSearchEntity();
        entity.setNotificationObject(3);

        return sysEmailTemplateService.searchList(entity);
    }
}
