package cn.ey.biz.module.system.api.msg;

import cn.ey.biz.common.exception.ErrorCode;
import cn.ey.biz.common.pojo.R;
import cn.ey.biz.module.system.api.msg.dto.*;
import cn.ey.biz.module.system.dal.dataobject.category.CategoryDataDO;
import cn.ey.biz.module.system.enums.bpm.BpmEnum;
import cn.ey.biz.module.system.enums.category.CategoryAppEnum;
import cn.ey.biz.module.system.enums.hrm.HrmEnum;
import cn.ey.biz.module.system.service.category.CategoryDataService;
import cn.ey.biz.module.system.service.msg.MessageCenterService;
import cn.ey.biz.module.weaver.api.msg.*;
import cn.ey.biz.module.weaver.common.HrmGetter;
import cn.ey.biz.module.weaver.common.UserContextHolder;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;
import static cn.ey.biz.common.exception.util.ServiceExceptionUtil.exception;
import static cn.ey.biz.module.system.enums.ApiConstants.VERSION;

@RestController // 提供 RESTful API 接口，给 Feign 调用
@DubboService(version = VERSION) // 提供 Dubbo RPC 接口，给 Dubbo Consumer 调用
@Validated
@Slf4j
public class MsgApilmpl implements MsgApi {

    @Resource
    SendEmailService sendEmailService;
    @Resource
    SendSmsService sendSmsService;
    @Resource
    SendCustomMsgService sendCustomMsgService;
    @Resource
    AlterCustomMsgService alterCustomMsgService;
    @Resource
    DeleteCustomMsgService deleteCustomMsgService;
    @Resource
    MsgTemplateService msgTemplateService;
    @Resource
    CategoryDataService categoryDataService;
    @Resource
    MessageCenterService messageCenterService;

    @Override
    public R<Boolean> sendEmail( SendEmailDTO sendEmailDTO) {
        HttpServletRequest servletRequest=getRequest();
        servletRequest.setAttribute("sysCode",sendEmailDTO.getSysCode());
        CategoryDataDO categoryDataDO=categoryDataService.getCategoryDataKey(CategoryAppEnum.ADMIN.getApp(), BpmEnum.WEAVER_FLOW.getCode(), HrmEnum.WEAVER_USERID.getCode());
        UserContextHolder.set(HrmGetter.USER_ID,categoryDataDO.getValue());
        SendEmailService.SendEmailRequest request = new SendEmailService.SendEmailRequest();
        request.setTitle(sendEmailDTO.getTitle());
        request.setContent(sendEmailDTO.getContent());
        request.setSendTo(sendEmailDTO.getSendTo());
        request.setSendCc(sendEmailDTO.getSendCc());

        if(sendEmailDTO.getTemplate()!=null){
            SendEmailDTO.Template template=sendEmailDTO.getTemplate();
            SendEmailService.TemplateRequest templateRequest=new SendEmailService.TemplateRequest();
            templateRequest.setCode(template.getCode());
            templateRequest.setParams(template.getParams());
            request.setTemplate(templateRequest);
        }
        if(sendEmailDTO.getFiles()!=null) {
            List<SendEmailDTO.Files> files=sendEmailDTO.getFiles();
            List<SendEmailService.FilesRequest> filesList=new ArrayList<>();
            for(SendEmailDTO.Files fi:files){
                SendEmailService.FilesRequest filesRequest=new SendEmailService.FilesRequest();
                filesRequest.setName(fi.getName());
                filesRequest.setData(fi.getData());
                filesList.add(filesRequest);
            }
            request.setFiles(filesList);
        }
        log.info("泛微邮件入参:{}.", JSON.toJSONString(request));
        SendEmailService.SendEmailResponse response =sendEmailService.sendEmail(request);
        log.info("泛微邮件返回参数:{}.", JSON.toJSONString(response));
        if (response.getApi_status()) {
            return R.success((true));
        } else {
            throw exception(new ErrorCode(-1, JSON.toJSONString(response.getApi_msg())));
        }
    }

    @Override
    public R<Boolean> sendSms(@Valid SendSmsDTO sendSmsDTO) {
        SendSmsService.SendSmsRequest request=new SendSmsService.SendSmsRequest();
        request.setTemplateId(sendSmsDTO.getTemplateId());
        request.setMobile(sendSmsDTO.getMobile());
        request.setParams(sendSmsDTO.getParams());
        SendSmsService.SendSmsResponse response=sendSmsService.sendSms(request);
        if(response.getApi_status()){
            return R.success((true));
        }else{
            throw exception(new ErrorCode(-1, JSON.toJSONString(response.getApi_msg())));
        }
    }

    @Override
    public R<Boolean> sendCustomMsg(@Valid SendCustomMsgDTO sendSmsDTO) {
        SendCustomMsgService.SendCustomMsgRequest request=new SendCustomMsgService.SendCustomMsgRequest();
        request.setTargetId(sendSmsDTO.getTargetId());
        request.setParams(sendSmsDTO.getParams());
        request.setCreater(sendSmsDTO.getCreater());
        request.setReceiver(sendSmsDTO.getReceiver());
        request.setPcLinkUrl(sendSmsDTO.getPcLinkUrl());
        request.setMobileUrl(sendSmsDTO.getMobileUrl());
        request.setTargetId(sendSmsDTO.getTargetId());
        request.setBizState(sendSmsDTO.getBizState());
        SendCustomMsgService.SendCustomMsgResponse  response=sendCustomMsgService.sendCustomMsg(request);
        if(response.getApi_status()){
            return R.success((true));
        }else{
            throw exception(new ErrorCode(-1, JSON.toJSONString(response.getApi_msg())));
        }
    }

    @Override
    public R<Boolean> alterCustomMsg(@Valid AlterCustomMsgDTO sendSmsDTO) {
        AlterCustomMsgService.SendCustomMsgRequest request=new AlterCustomMsgService.SendCustomMsgRequest();
        request.setTargetId(sendSmsDTO.getTargetId());
        request.setReceiver(sendSmsDTO.getReceiver());
        request.setBizState(sendSmsDTO.getBizState());
        AlterCustomMsgService.AlterCustomMsgResponse response=alterCustomMsgService.alterCustomMsg(request);
        if(response.getApi_status()){
            return R.success((true));
        }else{
            throw exception(new ErrorCode(-1, JSON.toJSONString(response.getApi_msg())));
        }
    }

    @Override
    public R<Boolean> deleteCustomMsg(@Valid DeleteCustomMsgDTO sendSmsDTO) {
        DeleteCustomMsgService.AlterCustomMsgRequest request=new DeleteCustomMsgService.AlterCustomMsgRequest();
        request.setTargetId(sendSmsDTO.getTargetId());
        request.setReceiver(sendSmsDTO.getReceiver());
        DeleteCustomMsgService.AlterCustomMsgResponse  response=deleteCustomMsgService.alterCustomMsg(request);
        if(response.getApi_status()){
            return R.success((true));
        }else{
            throw exception(new ErrorCode(-1, JSON.toJSONString(response.getApi_msg())));
        }
    }

    @Override
    public R<Boolean> msgTemplate(@Valid MsgTemplateDTO sendSmsDTO) {
        HttpServletRequest servletRequest=getRequest();
        servletRequest.setAttribute("sysCode",sendSmsDTO.getSysCode());
        CategoryDataDO categoryDataDO=categoryDataService.getCategoryDataKey(CategoryAppEnum.ADMIN.getApp(), BpmEnum.WEAVER_FLOW.getCode(), HrmEnum.WEAVER_USERID.getCode());
        UserContextHolder.set(HrmGetter.USER_ID,categoryDataDO.getValue());
        MsgTemplateService.MsgTemplateRequest request=new MsgTemplateService.MsgTemplateRequest();
        request.setTitle(sendSmsDTO.getTitle());
        request.setType(sendSmsDTO.getType());
        request.setContext(sendSmsDTO.getContent());
        request.setCode(sendSmsDTO.getCode());
        request.setSource(sendSmsDTO.getSource());
        MsgTemplateService.MsgTemplateResponse response=msgTemplateService.msgTemplate(request);
        if(response.getApi_status()){
            return R.success((true));
        }else{
            throw exception(new ErrorCode(-1, JSON.toJSONString(response.getApi_msg())));
        }
    }

    @Override
    public R<Boolean> sendCustomMessageSingle(SendCustomMessageSingleDTO sendDTO) {
        HttpServletRequest servletRequest=getRequest();
        servletRequest.setAttribute("sysCode",sendDTO.getSysCode());
        return R.success(messageCenterService.sendCustomMessageSingle(sendDTO));
    }

    @Override
    public R<Boolean> alterCustomMessageSingle(@Valid AlterCustomMessageSingleDTO singleDTO) {
        HttpServletRequest servletRequest=getRequest();
        servletRequest.setAttribute("sysCode",singleDTO.getSysCode());
        return R.success(messageCenterService.sendCustomMessageSingle(singleDTO));
    }

    @Override
    public R<Boolean> deleteCustomMessageSingle(@Valid DeleteCustomMessageSingleDTO singleDTO) {
        HttpServletRequest servletRequest=getRequest();
        servletRequest.setAttribute("sysCode",singleDTO.getSysCode());
        return R.success(messageCenterService.deleteCustomMessageSingle(singleDTO));
    }

    protected HttpServletRequest getRequest(){
        return ((ServletRequestAttributes) (RequestContextHolder.currentRequestAttributes())).getRequest();
    }
}
