package com.ruoyi.message.frequency.goeasymessagenew.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.enums.user.UserEnums;
import com.ruoyi.frequency.customer.entity.Customer;
import com.ruoyi.frequency.customer.service.CustomerService;
import com.ruoyi.frequency.customer.service.UserService;
import com.ruoyi.frequency.store.entity.Store;
import com.ruoyi.frequency.store.service.StoreService;
import com.ruoyi.message.frequency.goeasymessagenew.entity.GoeasyMessageNew;
import com.ruoyi.message.frequency.goeasymessagenew.mapper.GoeasyMessageNewMapper;
import com.ruoyi.message.frequency.goeasymessagenew.service.GoeasyMessageNewService;
import com.ruoyi.vo.UserInfo;
import com.ruoyi.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * goeasy最新消息Service业务层处理
 *
 * @author liwenlong
 * @date 2024-04-20
 */
@Service
public class GoeasyMessageNewServiceImpl extends ServiceImpl<GoeasyMessageNewMapper, GoeasyMessageNew> implements GoeasyMessageNewService {
    @Autowired
    private GoeasyMessageNewMapper goeasyMessageNewMapper;

    /**
     * 查询goeasy最新消息
     *
     * @param id goeasy最新消息主键
     * @return goeasy最新消息
     */
    @Override
    public GoeasyMessageNew selectGoeasyMessageNewById(Long id) {
        return goeasyMessageNewMapper.selectGoeasyMessageNewById(id);
    }


    @Autowired
    private UserService userService;

    @Autowired
    private CustomerService customerService;
    @Autowired
    private StoreService storeService;

    /**
     * 查询goeasy最新消息列表
     *
     * @param goeasyMessageNew goeasy最新消息
     * @return goeasy最新消息
     */
    @Override
    public List<GoeasyMessageNew> selectGoeasyMessageNewList(GoeasyMessageNew goeasyMessageNew) {
        List<GoeasyMessageNew> goeasyMessageNewList = goeasyMessageNewMapper.selectGoeasyMessageNewList(goeasyMessageNew);

        if (goeasyMessageNewList.size() > 0) {
            List<Long> userId = new ArrayList<>();
            goeasyMessageNewList.stream().forEach(s -> {
                userId.add(s.getUserId());
                userId.add(s.getOtherUserId());
            });
            Set<UserVo> userVoSet = new HashSet<>();
            List<Customer> customerList = customerService.listByIds(userId);

            Map<Long, Long> customerMap = new HashMap<>();
            customerList.stream().forEach(x -> {
                customerMap.put(x.getId(), x.getId());
                userVoSet.add(new UserVo(UserEnums.customer.getCode(), x.getId()));

            });

            List<Store> storeList = storeService.listByIds(userId);

            Map<Long, Long> storeMap = new HashMap<>();
            storeList.stream().forEach(x -> {
                storeMap.put(x.getId(), x.getId());
                userVoSet.add(new UserVo(UserEnums.store.getCode(), x.getId()));

            });


            //查询用户信息
            Map<UserVo, UserInfo> userMap = userService.selectUserInfoMap(userVoSet, null);

            goeasyMessageNewList.stream().forEach(s -> {
                // 取 userId
                if (customerMap.containsKey(s.getUserId())) {
                    s.setSendUserInfo(userMap.get(new UserVo(UserEnums.customer.getCode(), s.getUserId())));
                    s.setSenderUserId(s.getSendUserInfo().getUserId());
                    s.setSenderUserType(UserEnums.customer.getCode());
                } else if (storeMap.containsKey(s.getUserId())) {
                    s.setSendUserInfo(userMap.get(new UserVo(UserEnums.store.getCode(), s.getUserId())));
                    s.setSenderUserId(s.getSendUserInfo().getUserId());
                    s.setSenderUserType(UserEnums.store.getCode());
                }


                if (customerMap.containsKey(s.getOtherUserId())) {
                    s.setReceiveUserInfo(userMap.get(new UserVo(UserEnums.customer.getCode(), s.getOtherUserId())));
                    s.setReceiverUserId(s.getReceiveUserInfo().getUserId());
                    s.setReceiverUserType(UserEnums.customer.getCode());
                } else if (storeMap.containsKey(s.getOtherUserId())) {
                    s.setReceiveUserInfo(userMap.get(new UserVo(UserEnums.store.getCode(), s.getOtherUserId())));
                    s.setReceiverUserId(s.getReceiveUserInfo().getUserId());
                    s.setReceiverUserType(UserEnums.store.getCode());
                }

            });
        }
        return goeasyMessageNewList;
    }

    /**
     * 新增goeasy最新消息
     *
     * @param goeasyMessageNew goeasy最新消息
     * @return 结果
     */
    @Override
    public int insertGoeasyMessageNew(GoeasyMessageNew goeasyMessageNew) {
        goeasyMessageNew.setCreateData();
        return goeasyMessageNewMapper.insert(goeasyMessageNew);
    }

    /**
     * 修改goeasy最新消息
     *
     * @param goeasyMessageNew goeasy最新消息
     * @return 结果
     */
    @Override
    public int updateGoeasyMessageNew(GoeasyMessageNew goeasyMessageNew) {
        goeasyMessageNew.setUpdateData();
        return goeasyMessageNewMapper.updateGoeasyMessageNew(goeasyMessageNew);
    }

    /**
     * 批量删除goeasy最新消息
     *
     * @param ids 需要删除的goeasy最新消息主键
     * @return 结果
     */
    @Override
    public int deleteGoeasyMessageNewByIds(Long[] ids, Integer delFlag) {
        this.lambdaUpdate().set(GoeasyMessageNew::getDelFlag, delFlag).in(GoeasyMessageNew::getId, ids).update();
        return ids.length;
    }

}
