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

import cn.ey.biz.common.exception.ErrorCode;
import cn.ey.biz.module.system.api.msg.dto.AlterCustomMessageSingleDTO;
import cn.ey.biz.module.system.api.msg.dto.DeleteCustomMessageSingleDTO;
import cn.ey.biz.module.system.api.msg.dto.SendCustomMessageSingleDTO;
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.weaver.api.msg.AlterCustomMessageSingleService;
import cn.ey.biz.module.weaver.api.msg.DeleteCustomMessageSingleService;
import cn.ey.biz.module.weaver.api.msg.SendCustomMessageSingleService;
import cn.ey.biz.module.weaver.api.msg.SendEmailService;
import cn.ey.biz.module.weaver.common.HrmGetter;
import cn.ey.biz.module.weaver.common.UserContextHolder;
import com.alibaba.fastjson.JSON;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
import javax.annotation.Resource;
import static cn.ey.biz.common.exception.util.ServiceExceptionUtil.exception;

@Service
@Validated
public class MessageCenterServiceImpl implements MessageCenterService {

    @Resource
    CategoryDataService categoryDataService;
    @Resource
    SendEmailService sendEmailService;
    @Resource
    SendCustomMessageSingleService sendCustomMessageSingleService;
    @Resource
    AlterCustomMessageSingleService alterCustomMessageSingleService;
    @Resource
    DeleteCustomMessageSingleService deleteCustomMessageSingleService;


    @Override
    public Boolean sendCustomMessageSingle(SendCustomMessageSingleDTO sendDTO) {
        CategoryDataDO categoryDataDO=categoryDataService.getCategoryDataKey(CategoryAppEnum.ADMIN.getApp(), BpmEnum.WEAVER_FLOW.getCode(), HrmEnum.WEAVER_USERID.getCode());
        UserContextHolder.set(HrmGetter.USER_ID,categoryDataDO.getValue());
        SendCustomMessageSingleService.SendCustomMessageSingleRequest request=new SendCustomMessageSingleService.SendCustomMessageSingleRequest();
        request.setCode(sendDTO.getCode());
        request.setWorkCodeList(sendDTO.getWorkCodeList());
        request.setCreater(sendDTO.getCreater());
        request.setTitle(sendDTO.getTitle());
        request.setContext(sendDTO.getContext());
        request.setLinkUrl(sendDTO.getLinkUrl());
        request.setLinkMobileUrl(sendDTO.getLinkMobileUrl());
        request.setTargetId(sendDTO.getTargetId());
        request.setBizState(sendDTO.getBizState());
        SendCustomMessageSingleService.SendCustomMessageSingleResponse  response=sendCustomMessageSingleService.sendCustomMessageSingle(request);
        if(response.getStatus()){
            return response.getStatus();
        }else{
            throw exception(new ErrorCode(-1, JSON.toJSONString(response)));
        }
    }

    @Override
    public Boolean sendCustomMessageSingle(AlterCustomMessageSingleDTO singleDTO) {
        CategoryDataDO categoryDataDO=categoryDataService.getCategoryDataKey(CategoryAppEnum.ADMIN.getApp(), BpmEnum.WEAVER_FLOW.getCode(), HrmEnum.WEAVER_USERID.getCode());
        UserContextHolder.set(HrmGetter.USER_ID,categoryDataDO.getValue());
        AlterCustomMessageSingleService.AlterCustomMessageSingleRequest  request=new AlterCustomMessageSingleService.AlterCustomMessageSingleRequest();
        request.setTargetId(singleDTO.getTargetId());
        request.setWorkCodeList(singleDTO.getWorkCodeList());
        request.setBizState(singleDTO.getBizState());
        request.setCode(singleDTO.getCode());
        AlterCustomMessageSingleService.AlterCustomMessageSingleResponse response=alterCustomMessageSingleService.alterCustomMessageSingle(request);
        if(response.getStatus()){
            return response.getStatus();
        }else{
            throw exception(new ErrorCode(-1, JSON.toJSONString(response)));
        }
    }

    @Override
    public Boolean deleteCustomMessageSingle(DeleteCustomMessageSingleDTO singleDTO) {
        CategoryDataDO categoryDataDO=categoryDataService.getCategoryDataKey(CategoryAppEnum.ADMIN.getApp(), BpmEnum.WEAVER_FLOW.getCode(), HrmEnum.WEAVER_USERID.getCode());
        UserContextHolder.set(HrmGetter.USER_ID,categoryDataDO.getValue());
        DeleteCustomMessageSingleService.DeleteCustomMessageSingleRequest request=new DeleteCustomMessageSingleService.DeleteCustomMessageSingleRequest();
        request.setTargetId(singleDTO.getTargetId());
        request.setWorkCodeList(singleDTO.getWorkCodeList());
        request.setCode(singleDTO.getCode());
        DeleteCustomMessageSingleService.DeleteCustomMessageSingleResponse response=deleteCustomMessageSingleService.deleteCustomMessageSingle(request);
        if(response.getStatus()){
            return response.getStatus();
        }else{
            throw exception(new ErrorCode(-1, JSON.toJSONString(response)));
        }
    }

}
