package com.yonge.cooleshow.biz.dal.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yonge.cooleshow.biz.dal.entity.CustomerServiceReceive;
import com.yonge.cooleshow.biz.dal.entity.SysUser;
import com.yonge.cooleshow.biz.dal.mapper.CustomerServiceReceiveMapper;
import com.yonge.cooleshow.biz.dal.mapper.SysUserMapper;
import com.yonge.cooleshow.biz.dal.service.CustomerServiceReceiveService;
import com.yonge.cooleshow.biz.dal.wrapper.im.CustomerServiceReceiveWrapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * 客服群发接收
 * 2022-12-09 10:49:10
 */
@Slf4j
@Service
public class CustomerServiceReceiveServiceImpl extends ServiceImpl<CustomerServiceReceiveMapper, CustomerServiceReceive> implements CustomerServiceReceiveService {

    @Autowired
    private SysUserMapper sysUserMapper;

	/**
     * 查询详情
     * @param id 详情ID
     * @return CustomerServiceReceive
     */
	@Override
    public CustomerServiceReceive detail(Long id) {
        
        return baseMapper.selectById(id);
    }
    
    /**
     * 分页查询
     * @param page IPage<CustomerServiceReceiveWrapper.CustomerServiceReceive>
     * @param query CustomerServiceReceiveWrapper.CustomerServiceReceiveQuery
     * @return IPage<CustomerServiceReceiveWrapper.CustomerServiceReceive>
     */
    @Override
    public IPage<CustomerServiceReceiveWrapper.CustomerServiceReceive> selectPage(IPage<CustomerServiceReceiveWrapper.CustomerServiceReceive> page,
                                                                                  CustomerServiceReceiveWrapper.CustomerServiceReceiveQuery query){

        List<CustomerServiceReceiveWrapper.CustomerServiceReceive> receives = baseMapper.selectPage(page, query);

        if (CollectionUtils.isNotEmpty(receives)) {

            List<Long> userIds = receives.stream()
                    .map(CustomerServiceReceiveWrapper.CustomerServiceReceive::getUserId).distinct().collect(Collectors.toList());

            Map<Long, String> usernameMap = sysUserMapper.selectBatchIds(userIds).stream()
                    .collect(Collectors.toMap(SysUser::getId, x -> Optional.ofNullable(x.getUsername()).orElse(""), (o, n) -> n));

            for (CustomerServiceReceiveWrapper.CustomerServiceReceive item : receives) {

                item.setUserName(usernameMap.getOrDefault(item.getUserId(), ""));
            }
        }

        return page.setRecords(receives);
    }
	
    /**
     * 添加
     * @param customerServiceReceive CustomerServiceReceiveWrapper.CustomerServiceReceive
     * @return Boolean
     */
    @Override
    public Boolean add(CustomerServiceReceiveWrapper.CustomerServiceReceive customerServiceReceive) {    	
        
        return this.save(JSON.parseObject(customerServiceReceive.jsonString(), CustomerServiceReceive.class));
    }

    /**
     * 更新
     * @param customerServiceReceive CustomerServiceReceiveWrapper.CustomerServiceReceive
     * @return Boolean
     */
    @Override
    public Boolean update(CustomerServiceReceiveWrapper.CustomerServiceReceive customerServiceReceive){

        return this.updateById(JSON.parseObject(customerServiceReceive.jsonString(), CustomerServiceReceive.class));       
    }
}