package com.jwipc.message.logic.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.jwipc.message.convert.MessageAddresseeConvert;
import com.jwipc.message.dto.MessageAddresseeDTO;
import com.jwipc.message.entity.MessageAddressee;
import com.jwipc.message.entity.MessageCenter;
import com.jwipc.message.logic.MessageAddresseeLogic;
import com.jwipc.message.model.po.MessageAddresseePO;
import com.jwipc.message.model.po.MessageCenterPO;
import com.jwipc.message.service.IMessageAddresseeService;
import com.jwipc.message.service.IMessageCenterService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

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

/**
 * @author shenyang.ou
 * @date 2024/7/3 14:44
 * @desc
 */
@Service
@RequiredArgsConstructor
public class MessageAddresseeLogicImpl implements MessageAddresseeLogic {
    private final IMessageAddresseeService messageAddresseeService;
    private final MessageAddresseeConvert messageAddresseeConvert;
    private final IMessageCenterService messageCenterService;
    @Override
    public List<MessageAddresseeDTO> list(Long messageCenterId) {
        List<MessageAddressee> messageAddressees = messageAddresseeService.list(Wrappers.lambdaQuery(MessageAddressee.class).eq(MessageAddressee::getMessageCenterId,messageCenterId));
        return messageAddresseeConvert.entity2DTOList(messageAddressees);
    }

    @Override
    public List<MessageAddresseeDTO> getReceived(Long userId) {
        List<MessageAddresseePO> messageAddressees = messageAddresseeService.getReceived(userId);
        return messageAddresseeConvert.po2DTOList(messageAddressees);
    }

    @Override
    public void setRead(Long id) {
        MessageAddressee messageAddressee = messageAddresseeService.getById(id);

        //添加查询是否上传文件，是否已回复，是否已确认
        MessageCenter centerServiceById = messageCenterService.getById(messageAddressee.getMessageCenterId());
        boolean flag = true;
        boolean flag1 = true;
        boolean flag2 = true;
        if(centerServiceById != null){
            if(centerServiceById.getIsSupplierConfirm() == 1){//是否需要确认
                if(messageAddressee.getIsSupplierConfirmed() == 0){
                    flag = false;
                }
            }
            if(centerServiceById.getIsSupplierReply() == 1){//是否需要回复

                if(messageAddressee.getIsSupplierReplied() == 0){
                    flag1 = false;
                }
            }
           if(centerServiceById.getIsSupplierFile() == 1){//是否需要上传文件
               if(messageAddressee.getIsSupplierFiled() == 0){
                   flag2 = false;
               }
           }
        }
        if(flag && flag1 && flag2){
            messageAddressee.setSupplierIsRead(1);
            messageAddresseeService.updateById(messageAddressee);
        }
    }

    @Override
    public void confirm(Long id) {
        MessageAddressee messageAddressee = messageAddresseeService.getById(id);
        MessageCenter centerServiceById = messageCenterService.getById(messageAddressee.getMessageCenterId());
        boolean flag = true;
        boolean flag1 = true;
        if(centerServiceById != null){
            if(centerServiceById.getIsSupplierReply() == 1){//是否需要回复
                if(messageAddressee.getIsSupplierReplied() == 0){
                    flag = false;
                }
            }
            if(centerServiceById.getIsSupplierFile() == 1){//是否需要上传文件
                if(messageAddressee.getIsSupplierFiled() == 0){
                    flag1 = false;
                }
            }
        }



        messageAddressee.setIsSupplierConfirmed(1);
        if(flag && flag1){
            messageAddressee.setSupplierIsRead(1);
        }
        //需要增加判断是否必须要做的动作都做完了，做完了就要修改已读状态
        messageAddresseeService.updateById(messageAddressee);
    }

    @Override
    public void reply(Long id, String replyContent) {
        MessageAddressee messageAddressee = messageAddresseeService.getById(id);
        MessageCenter centerServiceById = messageCenterService.getById(messageAddressee.getMessageCenterId());
        boolean flag = true;
        boolean flag1 = true;
        if(centerServiceById != null){
            if(centerServiceById.getIsSupplierFile() == 1){//是否需要上传文件
                if(messageAddressee.getIsSupplierFiled() == 0){
                    flag1 = false;
                }
            }
            if(centerServiceById.getIsSupplierConfirm() == 1){//是否需要确认
                if(messageAddressee.getIsSupplierConfirmed() == 0){
                    flag = false;
                }
            }
        }
        if(flag && flag1){
            messageAddressee.setSupplierIsRead(1);
        }

        messageAddressee.setIsSupplierReplied(1);
        messageAddressee.setReplyContent(replyContent);
        messageAddressee.setLastReplyTime(LocalDateTime.now());
        //需要增加判断是否必须要做的动作都做完了，做完了就要修改已读状态
        messageAddresseeService.updateById(messageAddressee);
    }

    @Override
    public void replyFile(Long id, String replyFile) {
        MessageAddressee messageAddressee = messageAddresseeService.getById(id);
        MessageCenter centerServiceById = messageCenterService.getById(messageAddressee.getMessageCenterId());
        boolean flag = true;
        boolean flag1 = true;
        if(centerServiceById != null){
            if(centerServiceById.getIsSupplierConfirm() == 1){//是否需要确认
                if(messageAddressee.getIsSupplierConfirmed() == 0){
                    flag = false;
                }
            }
            if(centerServiceById.getIsSupplierReply() == 1){//是否需要回复
                if(messageAddressee.getIsSupplierReplied() == 0){
                    flag = false;
                }
            }
        }
        if(flag && flag1){
            messageAddressee.setSupplierIsRead(1);
        }
        messageAddressee.setIsSupplierFiled(1);
        messageAddressee.setReplyFile(replyFile);
        //需要增加判断是否必须要做的动作都做完了，做完了就要修改已读状态
        messageAddresseeService.updateById(messageAddressee);
    }

    @Override
    public MessageAddresseeDTO getById(Long addresseeId) {
        MessageAddresseePO messageAddressee = messageAddresseeService.getDataById(addresseeId);
        return messageAddresseeConvert.po2DTO(messageAddressee);
    }
}
