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

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tofly.feesapi.bmgl.entity.BmFffs;
import com.tofly.feesapi.bmgl.service.IBmFffsService;
import com.tofly.feesapi.common.core.dto.PageDto;
import com.tofly.feesapi.common.core.vo.PageResult;
import com.tofly.feesapi.common.enums.dzmxbzEnum;
import com.tofly.feesapi.sfds.entity.BankDsdzxx;
import com.tofly.feesapi.sfds.entity.dto.BankDsdzxxFilter;
import com.tofly.feesapi.sfds.entity.dto.BankDsdzxxSelect;
import com.tofly.feesapi.sfds.service.BankDsdzxxService;
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 com.tofly.feesapi.sfds.mapper.BankDsdzxxMapper;

import java.util.List;

/**
 * 代收对账信息
 *
 * @author admin
 * @date Thu Mar 06 00:00:00 CST 2025
 */
@Service("bankDsdzxxService")
public class BankDsdzxxServiceImpl extends ServiceImpl<BankDsdzxxMapper, BankDsdzxx> implements BankDsdzxxService {
    @Autowired
    private IUserService userService;
    @Autowired
    private IBmFffsService bmFffsService;


    @Override
    public PageResult<BankDsdzxxSelect> getPage(PageDto pageDto, BankDsdzxxFilter filter) {
        PageDto<BankDsdzxxSelect> page = this.baseMapper.getPage(pageDto, filter);
        List<User> userList = userService.getAllUser();
        List<BmFffs> bmFffsList = bmFffsService.getAllBmFffs();
        page.getRecords().forEach(e -> {
            if (e.getYhdm() != null) {
                User user = userList.stream().filter(u -> u.getId().equals(Long.valueOf(e.getYhdm()))).findFirst().orElse(null);
                if (user != null) {
                    e.setYhdmmc(user.getRealName());
                } else {
                    e.setYhdmmc(e.getYhdm());
                }
            }

            if (e.getFffs() != null) {
                BmFffs bmFffs = bmFffsList.stream().filter(u->u.getCode().equals(e.getFffs())).findFirst().orElse(null);
                if (bmFffs != null) {
                    e.setFffsmc(bmFffs.getName());
                } else {
                    e.setFffsmc(e.getFffs());
                }
            }

            if (e.getDzbz() != null) {
                String dzbzForValue = dzmxbzEnum.getDzbzForValue(e.getDzbz());
                if (dzbzForValue != null) {
                    e.setDzbzmc(dzbzForValue);
                } else {
                    e.setDzbzmc(e.getDzbz());
                }
            }

        });
        return page.toVo();
    }

    @Override
    public List<BankDsdzxxSelect> bankDsdzxxList(BankDsdzxxFilter filter) {
        List<BankDsdzxxSelect> list = this.baseMapper.getPage(filter);
        List<User> userList = userService.getAllUser();
        List<BmFffs> bmFffsList = bmFffsService.getAllBmFffs();
        list.forEach(e -> {
            if (e.getYhdm() != null) {
                User user = userList.stream().filter(u -> u.getId().equals(Long.valueOf(e.getYhdm()))).findFirst().orElse(null);
                if (user != null) {
                    e.setYhdmmc(user.getRealName());
                } else {
                    e.setYhdmmc(e.getYhdm());
                }
            }

            if (e.getFffs() != null) {
                BmFffs bmFffs = bmFffsList.stream().filter(u->u.getCode().equals(e.getFffs())).findFirst().orElse(null);
                if (bmFffs != null) {
                    e.setFffsmc(bmFffs.getName());
                } else {
                    e.setFffsmc(e.getFffs());
                }
            }

            if (e.getDzbz() != null) {
                String dzbzForValue = dzmxbzEnum.getDzbzForValue(e.getDzbz());
                if (dzbzForValue != null) {
                    e.setDzbzmc(dzbzForValue);
                } else {
                    e.setDzbzmc(e.getDzbz());
                }
            }

        });
        return list;
    }
}
