package com.crm.manager.controller;

import com.crm.manager.service.EmailSendFeignService;
import com.crm.manager.service.SysEmailConfigService;
import com.crm.rely.backend.core.constant.Constants;
import com.crm.rely.backend.core.constant.GlobalSystemEmailCodeConstants;
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.config.SysEmailConfigSearchDto;
import com.crm.rely.backend.core.entity.system.config.SendUpdateEmailCodeEntity;
import com.crm.rely.backend.core.entity.system.email.SysEmailTestSendEntity;
import com.crm.rely.backend.core.entity.system.email.config.*;
import com.crm.rely.backend.model.entity.user.info.UserInfoEntity;
import com.crm.rely.backend.util.GetIpAndMac;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

/**
 * 配置
 *
 * @author houn
 */
@RestController
@RequestMapping("/email/config")
public class SysEmailConfigController {

    @Autowired
    private SysEmailConfigService sysEmailConfigService;

    @Autowired
    private EmailSendFeignService emailSendFeignService;

    /**
     * 添加配置
     *
     * @param entity
     * @return
     * @throws Exception
     */
    @PostMapping("/add")
    public BaseResultDto add(@RequestBody @Validated SysEmailConfigAddEntity entity, UserInfoEntity infoEntity) throws Exception {
        entity.setEmail(infoEntity.getEmail());

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

    /**
     * 删除配置
     *
     * @param entity
     * @return
     * @throws Exception
     */
    @PostMapping("/delete")
    public BaseResultDto delete(@RequestBody @Validated SysEmailConfigDeleteEntity entity, UserInfoEntity infoEntity) throws Exception {

        entity.setEmail(infoEntity.getEmail());

        sysEmailConfigService.delete(entity);
        return BaseResultDto.success();
    }

    /**
     * 修改配置
     *
     * @param entity
     * @return
     * @throws Exception
     */
    @PostMapping("/update")
    public BaseResultDto update(@RequestBody @Validated SysEmailConfigUpdateEntity entity, UserInfoEntity infoEntity) throws Exception {

        entity.setEmail(infoEntity.getEmail());

        sysEmailConfigService.update(entity);
        return BaseResultDto.success();
    }

    @PostMapping("/batch/update")
    public BaseResultDto updateBatch(@RequestBody @Validated SysEmailConfigBatchUpdateEntity entity, UserInfoEntity infoEntity) throws Exception {
        if (entity == null) {
            return BaseResultDto.error(Constants.SYSTEM_ERROR);
        }

        entity.setEmail(infoEntity.getEmail());
        sysEmailConfigService.save(entity, entity.getData());
        return BaseResultDto.success();
    }

    /**
     * 列表查询
     *
     * @param entity
     * @return
     * @throws Exception
     */
    @PostMapping("/search/list")
    public BaseResultDto searchList(@RequestBody @Validated SysEmailConfigSearchEntity entity) throws Exception {


        ResultWithPagerDto<SysEmailConfigSearchDto> result = sysEmailConfigService.searchPageList(entity);

        return result;
    }

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

        SysEmailConfigSearchDto result = sysEmailConfigService.searchSingle(entity);

        return BaseResultDto.success(result);
    }

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

        List<SysEmailConfigSearchDto> result = sysEmailConfigService.searchList();

        return BaseResultDto.success(result);
    }

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

        return sendEmailCode(infoEntity);
    }

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

        return sendEmailCode(infoEntity);
    }

    @PostMapping("/batch/update/send/code")
    public BaseResultDto updateBatchSendEmailCode(UserInfoEntity infoEntity) throws Exception {
        return sendEmailCode(infoEntity);
    }

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

        return sendEmailCode(infoEntity);
    }

    @PostMapping("/test/send")
    public BaseResultDto testSendEmail(@RequestBody @Validated SysEmailTestSendEntity entity) throws Exception {
        entity.setTemplateName(GlobalSystemEmailCodeConstants.SYSTEM_TEST);
        return emailSendFeignService.sendEmailTest(entity);
    }

    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());

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