package com.tofly.qualitycontrol.service.impl;

import com.tofly.api.UserTaskClient;
import com.tofly.api.message.MessageClient;
import com.tofly.api.message.MessageStateClient;
import com.tofly.common.oauth.util.SecurityUtils;
import com.tofly.pojo.message.Messagepush;
import com.tofly.pojo.message.Messagerecipient;
import com.tofly.pojo.message.Usertask;
import com.tofly.qualitycontrol.common.QualityControlConstant;
import com.tofly.qualitycontrol.exception.QualityControlFeignException;
import com.tofly.qualitycontrol.qcenum.QualityControlMessageTopicEnum;
import com.tofly.qualitycontrol.service.IQualityControlMessageRecipientService;
import com.tofly.qualitycontrol.util.DateTimeUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.Objects;

@Slf4j
@Service
@RequiredArgsConstructor
public class QualityControlMessageRecipientServiceImpl implements IQualityControlMessageRecipientService, QualityControlConstant {

    private final MessageClient messageClient;
    private final MessageStateClient messageStateClient;
    private final UserTaskClient userTaskClient;

    @Override
    public void setMessageRecipient(Messagepush messagepush, Long userId) {
        this.setMessageRecipient(messagepush,userId,null,false);
    }

    @Override
    public void overrideSetMessageRecipient(Messagepush messagepush, Long userId) {
        this.setMessageRecipient(messagepush,userId,null,true);
    }

    @Override
    public void setMessageRecipient(Messagepush messagepush, Long userId, Date endDate) {
        this.setMessageRecipient(messagepush,userId,endDate,true);
    }

    @Override
    public void setMessageRecipient(Messagepush messagepush, Long userId, Date endDate,boolean isTask) {
        log.info("输出路由地址:"+messagepush.getRouteIp());
        try {
            Long messagePushId = messageClient.save(messagepush).getResult();
            Messagerecipient messagerecipient = new Messagerecipient();
            messagerecipient.setMessageId(messagePushId);
            messagerecipient.setRecipient(userId);
            messagerecipient.setFlag(FLAG);

            Boolean resultState = messageStateClient.save(messagerecipient).getResult();

            if(isTask) {
                Usertask usertask = new Usertask();
                if (endDate != null) {
                    usertask.setEndTime(endDate);
                    usertask.setStartTime(new Date());
                }
                usertask.setTknm(messagepush.getTopic());
                usertask.setTkty(messagepush.getType());
                usertask.setTableName(messagepush.getTableName());
                usertask.setTableId(messagepush.getTableId());
                usertask.setIshandle("0");
                usertask.setRouteIp(messagepush.getRouteIp());
                usertask.setRouteAppIp(messagepush.getRouteIpApp());
                usertask.setHandleUser(userId);
                userTaskClient.save(usertask);
                log.info("输出未处理任务路由地址："+usertask.getRouteIp());
            }
            if (Objects.isNull(messagepush) || !resultState) {
                throw new QualityControlFeignException(FEIGN_MESSAGE_CLIENT_ERROR_CODE,FEIGN_MESSAGE_CLIENT_ERROR);
            }
        } catch (QualityControlFeignException e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
    }

    /**
     * 消除待办
     * @param gdglinfoId 工单id
     * @param topicEnum 表名称/业务
     */
    @Override
    public void removeDB(Long gdglinfoId, QualityControlMessageTopicEnum topicEnum){
        // 待办事项处理状态置为完成
        // 待办事项处理
        Usertask usertask = new Usertask();
        usertask.setTableName(topicEnum.getTableName());
        usertask.setHandleUser(SecurityUtils.getUserId());
        usertask.setTknm(topicEnum.getTopic());
        usertask.setTableId(gdglinfoId);
        usertask.setIshandle("0");
        
        List<Usertask> result = userTaskClient.getUserTaskInfo(usertask).getResult();
        if(null != result && result.size()>0){
            for (Usertask task:result) {
                task.setHandleTime(DateTimeUtil.convertLDTToDate(LocalDateTime.now()));
                task.setHandleUser(SecurityUtils.getUserId());
                task.setIshandle("1");
                userTaskClient.updateById(task);
            }
        }
    }


}
