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.dzhzbzEnum;
import com.tofly.feesapi.sfds.entity.BankDsdzhz;
import com.tofly.feesapi.sfds.entity.dto.BankDsdzhzFilter;
import com.tofly.feesapi.sfds.entity.dto.BankDsdzhzSelect;
import com.tofly.feesapi.sfds.service.BankDsdzhzService;
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.BankDsdzhzMapper;

import java.util.List;

/**
 * 代收对账信息
 */
@Service("bankDsdzhzService")
public class BankDsdzhzServiceImpl extends ServiceImpl<BankDsdzhzMapper, BankDsdzhz> implements BankDsdzhzService {

    @Autowired
    private IUserService userService;
    @Autowired
    private IBmFffsService bmFffsService;

    @Override
    public PageResult<BankDsdzhzSelect> getPage(PageDto pageDto, BankDsdzhzFilter filter) {
        PageDto<BankDsdzhzSelect> page = this.baseMapper.getPage(pageDto, filter,null);
        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 = dzhzbzEnum.getDzbzForValue(e.getDzbz());
                if (dzbzForValue != null) {
                    e.setDzbzmc(dzbzForValue);
                } else {
                    e.setDzbzmc(e.getDzbz());
                }
            }

        });
        return page.toVo();
    }

    @Override
    public List<BankDsdzhzSelect> bankDsdzhzList(BankDsdzhzFilter filter) {
        List<BankDsdzhzSelect> list = this.baseMapper.getPage(filter,"1");
        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(f->f.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 = dzhzbzEnum.getDzbzForValue(e.getDzbz());
                if (dzbzForValue != null) {
                    e.setDzbzmc(dzbzForValue);
                } else {
                    e.setDzbzmc(e.getDzbz());
                }
            }
        });
        return list;
    }
}
