package com.tofly.feesapi.bmgl.service.impl;

import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tofly.feesapi.bmgl.entity.BmCbpq;
import com.tofly.feesapi.bmgl.entity.dto.BmCbpqFilter;
import com.tofly.feesapi.bmgl.entity.dto.BmCbpqSelect;
import com.tofly.feesapi.bmgl.mapper.BmCbpqMapper;
import com.tofly.feesapi.bmgl.service.BmCbpqService;
import com.tofly.feesapi.common.core.dto.PageDto;
import com.tofly.feesapi.common.core.vo.PageResult;
import com.tofly.feesapi.udb.entity.User;
import com.tofly.feesapi.udb.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("bmCbpqService")
public class BmCbpqServiceImpl extends ServiceImpl<BmCbpqMapper, BmCbpq> implements BmCbpqService {

    @Autowired
    private IUserService userService;

    @Override
    public PageResult<BmCbpqSelect> getPage(PageDto pageDto, BmCbpqFilter filter) {
        PageDto<BmCbpqSelect> page = this.baseMapper.getPage(pageDto, filter);
        List<User> users = userService.list();
        page.getRecords().forEach(e -> {
            String xgry = e.getXgry();
            if (StrUtil.isEmpty(xgry)) {
                return;
            }

            if (NumberUtil.isNumber(xgry)) {
                User user = users.stream().filter(u -> u.getId().equals(Long.valueOf(xgry))).findFirst().orElse(null);
                if (user != null) {
                    e.setXgrymc(user.getRealName());
                } else {
                    e.setXgrymc(xgry);
                }
            }
        });
        return page.toVo();
    }
}
