package com.crm.manager.controller;

import com.crm.manager.dto.export.SysApproveEmailExportDto;
import com.crm.manager.entity.excel.SysApproveEmailExcelEntity;
import com.crm.manager.service.SysApproveEmailService;
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.approve.SysApproveEmailListDto;
import com.crm.rely.backend.core.entity.base.SingleLongEntity;
import com.crm.rely.backend.core.entity.system.email.approve.SysApproveEmailAddEntity;
import com.crm.rely.backend.core.entity.system.email.approve.SysApproveEmailDeleteEntitys;
import com.crm.rely.backend.core.entity.system.email.approve.SysApproveEmailSearchEntity;
import com.crm.rely.backend.core.entity.system.email.approve.SysApproveEmailUpdateEntity;
import com.crm.rely.backend.util.EasyExcelUti;
import com.crm.rely.backend.util.ExportUtil;
import com.crm.rely.backend.util.StringUtil;
import com.google.common.collect.Lists;
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 javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 审核邮箱管理
 */
@RestController
@RequestMapping("/approve/email")
public class SysApproveEmailController {

    @Autowired
    private SysApproveEmailService sysApproveEmailService;

    /**
     * 添加邮箱
     */
    @PostMapping("/add")
    public BaseResultDto add(@RequestBody @Validated SysApproveEmailAddEntity entity) throws Exception {

        sysApproveEmailService.add(entity);
        return BaseResultDto.success();
    }

    /**
     * 删除邮箱
     */
    @PostMapping("/delete")
    public BaseResultDto delete(@RequestBody @Validated SysApproveEmailDeleteEntitys entitys) throws Exception {

        sysApproveEmailService.delete(entitys);
        return BaseResultDto.success();
    }

    /**
     * 修改邮箱
     */
    @PostMapping("/update")
    public BaseResultDto update(@RequestBody @Validated SysApproveEmailUpdateEntity entity) throws Exception {
        sysApproveEmailService.update(entity);
        return BaseResultDto.success();
    }

    @PostMapping("/search/list")
    public ResultWithPagerDto<SysApproveEmailListDto> pageList(@RequestBody SysApproveEmailSearchEntity entity) throws Exception {

        return sysApproveEmailService.searchPageList(entity);
    }

    @PostMapping("/search/single")
    public BaseResultDto searchSingle(@RequestBody @Validated SingleLongEntity entity) throws Exception {
        SysApproveEmailListDto result = sysApproveEmailService.searchSingle(entity);
        return BaseResultDto.success(result);
    }

    @GetMapping("/template/export")
    public void templateExport(HttpServletResponse response) throws Exception {
        List<SysApproveEmailExportDto> dtos = Lists.newArrayList();
        ExportUtil.transferToResponse(StringUtil.getXlsxFileName("EMAIL-TEMPLATE", false), dtos,
                SysApproveEmailExportDto.class, response);
    }

    @PostMapping("/excel")
    public BaseResultDto excel(@RequestParam("file") MultipartFile file) throws Exception {
        List<SysApproveEmailExcelEntity> emailEntities = EasyExcelUti.readExcel(file,
                new SysApproveEmailExcelEntity(), 1, SysApproveEmailExcelEntity.class);

        if (emailEntities == null || emailEntities.size() <= 0) {
            return BaseResultDto.success();
        }
        sysApproveEmailService.importSysApproveEmail(emailEntities);
        return BaseResultDto.success();
    }

}
