package com.jwipc.message.logic.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jwipc.message.convert.MessageAddresseeConvert;
import com.jwipc.message.convert.MessageCenterConvert;
import com.jwipc.message.dto.AddresseeUserDTO;
import com.jwipc.message.dto.MessageAddresseeDTO;
import com.jwipc.message.dto.MessageCenterDTO;
import com.jwipc.message.dto.MessageCenterParamDTO;
import com.jwipc.message.entity.MessageAddressee;
import com.jwipc.message.entity.MessageCenter;
import com.jwipc.message.logic.MessageCenterLogic;
import com.jwipc.message.model.po.MessageCenterPO;
import com.jwipc.message.model.query.AddresseeUserQuery;
import com.jwipc.message.model.query.MessageCenterQuery;
import com.jwipc.message.service.IMessageAddresseeService;
import com.jwipc.message.service.IMessageCenterService;
import com.jwipc.page.PageUtil;
import com.jwipc.page.PageView;
import com.jwipc.security.LoginUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

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

/**
 * @author shenyang.ou
 * @date 2024/7/3 10:52
 * @desc
 */
@Service
@RequiredArgsConstructor
public class MessageCenterLogicImpl implements MessageCenterLogic {
    private final MessageCenterConvert messageCenterConvert;
    private final MessageAddresseeConvert messageAddresseeConvert;
    private final IMessageCenterService messageCenterService;
    private final IMessageAddresseeService messageAddresseeService;
    @Override
    public MessageCenterDTO getById(Long id) {
        MessageCenterPO messageCenter = messageCenterService.getDataById(id);
        return messageCenterConvert.po2DTO(messageCenter);
    }

    @Override
    public List<AddresseeUserDTO> userList(AddresseeUserQuery query) {
        //查询供应商用户
        if (query.getType()==2){
            return messageCenterConvert.userPO2DTOList(messageCenterService.selectSupplierUser(query));
        }else {
            return messageCenterConvert.userPO2DTOList(messageCenterService.selectUser(query));
        }
    }

    @Override
    public void saveOrUpdate(MessageCenterParamDTO param) {
        if (param.getId() == null) {
            param.setIsDraft(0);
            param.setMessageSource(1);
            param.setInitiatorType(LoginUtil.isSupplier()?2:1);
            param.setCreateTime(LocalDateTime.now());
            param.setCreateUser(LoginUtil.getUserId());
        } else {
            param.setUpdateTime(LocalDateTime.now());
            param.setUpdateUser(LoginUtil.getUserId());
            //删除收信人
            messageAddresseeService.remove(Wrappers.lambdaQuery(MessageAddressee.class).eq(MessageAddressee::getMessageCenterId,param.getId()));
        }
        //保存消息主数据
        MessageCenter messageCenter = messageCenterConvert.DTO2Entity(param);
        messageCenterService.saveOrUpdate(messageCenter);

        List<MessageAddresseeDTO> messageAddresseeList = param.getMessageAddressee();
        messageAddresseeList.forEach(messageAddresseeDTO -> {
            messageAddresseeDTO.setMessageCenterId(messageCenter.getId());
            messageAddresseeDTO.setCreateTime(LocalDateTime.now());
            messageAddresseeDTO.setCreateUser(LoginUtil.getUserId());
            messageAddresseeDTO.setCreateUserName(LoginUtil.getName());
        });
        messageAddresseeService.saveBatch(messageAddresseeConvert.dTO2EntityList(messageAddresseeList));
    }

    @Override
    public PageView findByPage(MessageCenterQuery query) {
        final Page<MessageCenter> dbPage = PageUtil.toPage(query);
        List<MessageCenterPO> inquiryPOS = messageCenterService.findPageByQuery(dbPage, query);
        List<MessageCenterDTO> inquiryDTOS = messageCenterConvert.po2DTOList(inquiryPOS);
        return PageUtil.getPV(inquiryDTOS, dbPage.getTotal());
    }

    @Override
    public void publish(Long id) {
        MessageCenter messageCenter = messageCenterService.getById(id);
        messageCenter.setIsDraft(1);
        messageCenterService.updateById(messageCenter);
    }

    @Override
    public void remove(Long id) {
        messageCenterService.removeById(id);
        //删除收信人
        messageAddresseeService.remove(Wrappers.lambdaQuery(MessageAddressee.class).eq(MessageAddressee::getMessageCenterId,id));
    }

    @Override
    public Long getNewMessageNum() {
        return messageAddresseeService.getNewMessageNum(LoginUtil.getUserId());
    }
}
