/*
 * Author: wangjian
 * 生成ServiceImpl
 */
package com.tofly.feesapi.bjgl.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tofly.common.core.util.StringUtil;
import com.tofly.feesapi.bjgl.entity.YkHbLog;
import com.tofly.feesapi.bjgl.entity.vo.YhHbLogSelect;
import com.tofly.feesapi.bjgl.service.YkHbLogService;
import com.tofly.feesapi.common.core.dto.Order;
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 com.tofly.feesapi.yhgl.entity.dto.YhywLogFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.tofly.feesapi.bjgl.mapper.YkHbLogMapper;

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

/**
 * 换表（含普表换表）
 *
 * @author sxb
 * @date Mon Mar 04 00:00:00 CST 2024
 */
@Service("ykHbLogService")
public class YkHbLogServiceImpl extends ServiceImpl<YkHbLogMapper, YkHbLog> implements YkHbLogService {
    @Autowired
    private IUserService userService;

    @Override
    public PageResult<YhHbLogSelect> getYkHbLogPage(PageDto pageDto, YhywLogFilter filter) {
        List<Order> orders = pageDto.getOrders();
        orders = orders.stream().map(item -> {
            if (item.getColumn().equals("yhbh")) {
                Order order = new Order();
                order.setColumn("a.yhbh");
                order.setAsc(item.isAsc());
                return order;
            }
            return item;
        }).collect(Collectors.toList());
        pageDto.setOrders(orders);
        PageDto<YhHbLogSelect> page = this.baseMapper.getYkHbLogPage(pageDto, filter);
        PageResult<YhHbLogSelect> pageResult = page.toVo();
        List<User> userList = userService.list();
        pageResult.getRecords().forEach(item -> {
            if (StringUtil.isNotEmpty(item.getSpry())) {
                User user = userList.stream().filter(u -> u.getId().equals(Long.valueOf(item.getSpry()))).findFirst().orElse(null);
                if (user != null) {
                    item.setSpry(user.getRealName());
                } else {
                    item.setSpry(item.getSpry());
                }
            }
//            if (StringUtil.isNotEmpty(item.getHbry())) {
//                User user = userList.stream().filter(u -> u.getId().equals(Long.valueOf(item.getHbry()))).findFirst().orElse(null);
//                if (user != null) {
//                    item.setHbry(user.getRealName());
//                } else {
//                    item.setHbry(item.getHbry());
//                }
//            }
        });

        return pageResult;
    }

    @Override
    public List<YhHbLogSelect> getYkHbLoglist(YhywLogFilter filter) {
        List<YhHbLogSelect> list = this.baseMapper.getYkHbLogList(filter);
        List<User> userList = userService.list();
        list.forEach(item -> {
            if (StringUtil.isNotEmpty(item.getSpry())) {
                User user = userList.stream().filter(u -> u.getId().equals(Long.valueOf(item.getSpry()))).findFirst().orElse(null);
                if (user != null) {
                    item.setSpry(user.getRealName());
                } else {
                    item.setSpry(item.getSpry());
                }
            }
            if (StringUtil.isNotEmpty(item.getHbry())) {
                User user = userList.stream().filter(u -> u.getId().equals(Long.valueOf(item.getHbry()))).findFirst().orElse(null);
                if (user != null) {
                    item.setHbry(user.getRealName());
                } else {
                    item.setHbry(item.getHbry());
                }
            }
        });
        return list;
    }
}
