package com.yuchen.saas.park.service.service.building.impl;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yuchen.saas.manage.user.api.entity.User;
import com.yuchen.saas.manage.user.api.feign.IUserClient;
import com.yuchen.saas.park.api.dto.building.PmgBatBuildingDynamicQueryDto;
import com.yuchen.saas.park.api.entity.building.PmgBatBuildingDynamic;
import com.yuchen.saas.park.api.vo.building.PmgBatBuildingDynamicDetailVO;
import com.yuchen.saas.park.api.vo.building.PmgBatBuildingDynamicPageVO;
import com.yuchen.saas.park.service.mapper.building.PmgBatBuildingDynamicMapper;
import com.yuchen.saas.park.service.service.building.IPmgBatBuildingDynamicService;
import org.nest.springwrap.core.mp.base.BaseServiceImpl;
import org.nest.springwrap.core.tool.api.R;
import org.nest.springwrap.core.tool.utils.Func;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 楼盘动态 服务实现类
 * </p>
 *
 * @author yuanzhelin
 * @since 2024-10-24
 */
@Service
public class PmgBatBuildingDynamicServiceImpl extends BaseServiceImpl<PmgBatBuildingDynamicMapper, PmgBatBuildingDynamic> implements IPmgBatBuildingDynamicService {

    @Resource
    private IUserClient userClient;
    @Override
    public Page<PmgBatBuildingDynamicPageVO> page(PmgBatBuildingDynamicQueryDto dto) {
        Page<PmgBatBuildingDynamicPageVO> page = new Page<>(dto.getCurrent(), dto.getSize());
        if (Func.isNotEmpty(dto.getCreateUserName())){
            R<List<User>> userList = userClient.getUserList();
            if (userList.isSuccess()&&userList.getData()!=null){
                List<Long> ids = userList.getData().stream()
                        .filter(user -> user.getName().contains(dto.getCreateUserName()))
                        .map(User::getId)
                        .collect(Collectors.toList());
                dto.setCreateUserIds(ids);
            }
        }
        Page<PmgBatBuildingDynamicPageVO> result = baseMapper.page(dto, page);
        result.getRecords().forEach(e->{
            R<User> userR = userClient.userInfoById(e.getCreateUser());
            if (userR.isSuccess()&&Func.isNotEmpty(userR.getData().getId())){
                e.setCreateUserName(userR.getData().getName());
            }
        });
        return result;
    }

    @Override
    public PmgBatBuildingDynamicDetailVO detail(Long id) {
        PmgBatBuildingDynamicDetailVO detail = new PmgBatBuildingDynamicDetailVO();
        PmgBatBuildingDynamic byId = getById(id);
        BeanUtils.copyProperties(byId, detail);
        R<User> userR = userClient.userInfoById(byId.getCreateUser());
        if (userR.isSuccess()&&Func.isNotEmpty(userR.getData().getId())){
            detail.setCreateUserName(userR.getData().getName());
        }
        return detail;
    }

    @Override
    public PmgBatBuildingDynamic getByIdForUpdate(Long id) {
        return baseMapper.selectByIdForUpdate(id);
    }
}
