package com.crm.manager.controller;

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.system.config.SendUpdateEmailCodeEntity;
import com.crm.rely.backend.core.entity.system.email.template.*;
import com.crm.rely.backend.model.entity.user.info.UserInfoEntity;
import com.crm.rely.backend.util.GetIpAndMac;
import com.google.common.base.Strings;
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.Date;

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

    @Autowired
    private SysEmailTemplateService sysEmailTemplateService;

    /**
     * 添加邮件模版
     *
     * @param entity
     * @return
     * @throws Exception
     */
    @PostMapping("/add")
    public BaseResultDto add(@RequestBody @Validated SysEmailTemplateAddEntity entity, UserInfoEntity infoEntity) throws Exception {
        if (Constants.EMAIL_TYPE_SYSTEM.equals(entity.getType())) {
            if (!PrefixEnum.PREFIX_SYSTEM.equals(infoEntity.getPrefix())) {
                return BaseResultDto.error(Constants.NOT_PERMIT);
            }
        }
        if (!Strings.isNullOrEmpty(entity.getType())) {
            if (!Constants.EMAIL_TYPE_SYSTEM.equals(entity.getType())) {
                entity.setType(Constants.EMAIL_TYPE_COMMON);
            }
        }
        entity.setEmail(infoEntity.getEmail());
        sysEmailTemplateService.add(entity);
        return BaseResultDto.success();
    }

    /**
     * 删除邮件模版
     *
     * @param entity
     * @return
     * @throws Exception
     */
    @PostMapping("/delete")
    public BaseResultDto delete(@RequestBody @Validated SysEmailTemplateDeleteEntity entity, UserInfoEntity infoEntity) throws Exception {
        entity.setEmail(infoEntity.getEmail());
        sysEmailTemplateService.delete(entity, infoEntity);
        return BaseResultDto.success();
    }

    @PostMapping("/batch/update")
    public BaseResultDto updateBatchRate(@RequestBody @Validated SysEmailTemplateBatchEntity entity, UserInfoEntity infoEntity) throws Exception {
        if (entity == null) {
            return BaseResultDto.error(Constants.SYSTEM_ERROR);
        }
        entity.setEmail(infoEntity.getEmail());
        for (SysEmailTemplateBaseEntity sysEmailTemplateBaseEntity : entity.getData()) {
            sysEmailTemplateBaseEntity.setNotificationObject(entity.getNotificationObject());
        }
        sysEmailTemplateService.save(entity, entity.getData());
        return BaseResultDto.success();
    }

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

        return sendEmailCode(infoEntity);
    }

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

        if (Constants.EMAIL_TYPE_SYSTEM.equals(entity.getType())) {
            if (!PrefixEnum.PREFIX_SYSTEM.equals(infoEntity.getPrefix())) {
                return BaseResultDto.error(Constants.NOT_PERMIT);
            }
        }
        if (!Strings.isNullOrEmpty(entity.getType())) {
            if (!Constants.EMAIL_TYPE_SYSTEM.equals(entity.getType())) {
                entity.setType(Constants.EMAIL_TYPE_COMMON);
            }
        }
        entity.setEmail(infoEntity.getEmail());
        sysEmailTemplateService.update(entity, infoEntity);
        return BaseResultDto.success();
    }

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

        if (!Strings.isNullOrEmpty(entity.getType())) {
            if (!Constants.EMAIL_TYPE_SYSTEM.equals(entity.getType())) {
                entity.setType(Constants.EMAIL_TYPE_COMMON);
            }
        }
        ResultWithPagerDto<SysEmailTemplateSearchDto> result = sysEmailTemplateService.searchPageList(entity);

        return result;
    }

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

        BaseResultDto result = sysEmailTemplateService.searchList();

        return result;
    }

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

        SysEmailTemplateSearchDto result = sysEmailTemplateService.searchSingle(entity);

        return BaseResultDto.success(result);
    }

    @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);
        }
        sysEmailTemplateService.validated(file);

        String path = sysEmailTemplateService.uploadFile(file);

        return BaseResultDto.success(Constants.UPLOAD_SUCCESS, path);
    }

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

        return sendEmailCode(infoEntity);
    }

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

        return sendEmailCode(infoEntity);
    }

    @PostMapping("/delete/send/code")
    public BaseResultDto deleteChannelSendEmailCode(UserInfoEntity infoEntity) throws Exception {

        return sendEmailCode(infoEntity);
    }

    public BaseResultDto sendEmailCode(UserInfoEntity infoEntity) throws Exception {
        if (infoEntity == null || infoEntity == null) {
            return BaseResultDto.error(Constants.SYSTEM_ERROR);
        }
        SendUpdateEmailCodeEntity emailCodeEntity = new SendUpdateEmailCodeEntity();

        emailCodeEntity.setEmail(infoEntity.getEmail());
        emailCodeEntity.setTime(new Date());
        emailCodeEntity.setUser(infoEntity.getId());
        emailCodeEntity.setIp(GetIpAndMac.getIp());

        sysEmailTemplateService.sendEmailCode(emailCodeEntity);
        return BaseResultDto.success(Constants.SEND_SUCCESS);
    }
}
