package com.crm.manager.controller;

import com.crm.manager.service.CommonService;
import com.crm.manager.service.emial.EmailTemplateFeignService;
import com.crm.rely.backend.core.constant.Constants;
import com.crm.rely.backend.core.dto.base.BaseResultDto;
import com.crm.rely.backend.core.entity.base.SingleLongEntity;
import com.crm.rely.backend.core.entity.system.email.template.*;
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;

/**
 * 配置邮件模版
 *
 * @author houn
 */
@RestController
@RequestMapping("/email/template")
public class SysEmailTemplateController {

    @Autowired
    private EmailTemplateFeignService feignService;
    @Autowired
    private CommonService commonService;

    /**
     * 添加邮件模版
     *
     * @param entity
     * @return
     * @throws Exception
     */
    @PostMapping("/add")
    public BaseResultDto add(@RequestBody @Validated SysEmailTemplateAddEntity entity) throws Exception {

        return feignService.add(entity);
    }

    /**
     * 删除邮件模版
     *
     * @param entity
     * @return
     * @throws Exception
     */
    @PostMapping("/delete")
    public BaseResultDto delete(@RequestBody @Validated SysEmailTemplateDeleteEntity entity) throws Exception {

        return feignService.delete(entity);
    }

    @PostMapping("/batch/update")
    public BaseResultDto updateBatchRate(@RequestBody @Validated SysEmailTemplateBatchEntity entity) throws Exception {

        return feignService.updateBatchRate(entity);
    }

    @PostMapping("/batch/update/send/code")
    public BaseResultDto updateBatchEmailSendEmailCode() throws Exception {

        return feignService.updateBatchEmailSendEmailCode();
    }

    /**
     * 修改邮件模版
     *
     * @param entity
     * @return
     * @throws Exception
     */
    @PostMapping("/update")
    public BaseResultDto update(@RequestBody @Validated SysEmailTemplateUpdateEntity entity) throws Exception {

        return feignService.update(entity);
    }

    /**
     * 列表邮件模版
     *
     * @param entity
     * @return
     * @throws Exception
     */
    @PostMapping("/search/list")
    public BaseResultDto pageList(@RequestBody @Validated SysEmailTemplateSearchEntity entity) throws Exception {

        return feignService.pageList(entity);
    }

    @PostMapping("/list")
    public BaseResultDto list() throws Exception {

        return feignService.list();
    }

    /**
     * 单个查询
     *
     * @param entity
     * @return
     * @throws Exception
     */
    @PostMapping("/search/single")
    public BaseResultDto single(@RequestBody @Validated SingleLongEntity entity) throws Exception {

        return feignService.single(entity);
    }

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

        if (!"add".equals(method) && !"update".equals(method)) {
            return BaseResultDto.error(Constants.SYSTEM_ERROR);
        }

        String middlePath = "/email/template";
        String path = commonService.uploadFile(middlePath, file);

        return BaseResultDto.success(path);
    }

    @PostMapping("/add/send/code")
    public BaseResultDto addChannelSendEmailCode() throws Exception {

        return feignService.addChannelSendEmailCode();
    }

    @PostMapping("/update/send/code")
    public BaseResultDto updateEmailSendEmailCode() throws Exception {

        return feignService.updateEmailSendEmailCode();
    }

    @PostMapping("/delete/send/code")
    public BaseResultDto deleteChannelSendEmailCode() throws Exception {
        return feignService.deleteChannelSendEmailCode();
    }

}
