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

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
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.manage.user.api.feign.UserFeignServer;
import com.yuchen.saas.park.api.entity.park.SpaceUseTypeEntity;
import com.yuchen.saas.park.api.request.SpaceUseTypeRequest;
import com.yuchen.saas.park.api.response.SpaceUseResponse;
import com.yuchen.saas.park.service.mapper.SpaceUseTypeMapper;
import com.yuchen.saas.park.service.service.SpaceUseTypeService;
import org.apache.commons.lang3.StringUtils;
import org.nest.springwrap.core.mp.base.BaseServiceImpl;
import org.nest.springwrap.core.mp.support.Condition;
import org.nest.springwrap.core.mp.support.Query;
import org.nest.springwrap.core.tool.api.R;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

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

/**
 * @author 肖祥
 * @create 2022/11/29 14:49
 */
@Service
public class SpaceUseTypeServiceImpl extends BaseServiceImpl<SpaceUseTypeMapper, SpaceUseTypeEntity> implements SpaceUseTypeService {

    @Resource
    private IUserClient iUserClient;

    @Override
    public R save(SpaceUseTypeRequest spaceUseTypeRequest) {
        SpaceUseTypeEntity spaceUseTypeEntity = new SpaceUseTypeEntity();
        BeanUtils.copyProperties(spaceUseTypeRequest,spaceUseTypeEntity);
        save(spaceUseTypeEntity);
        return R.success("操作成功");
    }

    @Override
    public R update(SpaceUseTypeRequest spaceUseTypeRequest) {
        SpaceUseTypeEntity spaceUseTypeEntity = new SpaceUseTypeEntity();
        BeanUtils.copyProperties(spaceUseTypeRequest,spaceUseTypeEntity);
        updateById(spaceUseTypeEntity);
        return R.success("操作成功");
    }

    @Override
    public R delete(SpaceUseTypeRequest spaceUseTypeRequest) {
        removeById(spaceUseTypeRequest.getId());
        return R.success("操作成功");
    }

    @Override
    public R<IPage<SpaceUseResponse>> List(SpaceUseTypeRequest spaceUseTypeRequest, Query query) {
        LambdaQueryWrapper<SpaceUseTypeEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if(StringUtils.isNotBlank(spaceUseTypeRequest.getName())){
            lambdaQueryWrapper.like(SpaceUseTypeEntity::getName,spaceUseTypeRequest.getName());
        }
        IPage<SpaceUseTypeEntity> page =this.page(Condition.getPage(query),lambdaQueryWrapper);
        IPage<SpaceUseResponse> useResponseIPage = new Page(page.getCurrent(),page.getSize(),page.getTotal());
        List<SpaceUseResponse> records = this.listVO(page.getRecords());
        useResponseIPage.setRecords(records);
        return R.data(useResponseIPage);
    }

    private  List<SpaceUseResponse> listVO(List<SpaceUseTypeEntity> list) {
        return (List)list.stream().map(entity->{
            SpaceUseResponse spaceUseResponse = new SpaceUseResponse();
            BeanUtils.copyProperties(entity,spaceUseResponse);
            User user=  iUserClient.userInfoById(spaceUseResponse.getCreateUser()).getData();
            if(user != null){
                spaceUseResponse.setCreateUserName(user.getName());
            }
            return spaceUseResponse;
        }).collect(Collectors.toList());
    }
}
