package com.jwipc.supplier.logic.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.jwipc.page.PageUtil;
import com.jwipc.page.PageView;
import com.jwipc.supplier.convert.BankInfoConvert;
import com.jwipc.supplier.dto.BankInfoBaseDTO;
import com.jwipc.supplier.entity.BankInfo;
import com.jwipc.supplier.logic.BankLogic;
import com.jwipc.supplier.model.query.BankInfoQuery;
import com.jwipc.supplier.service.IBankInfoService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import java.util.List;

@Service
@RequiredArgsConstructor
public class BankLogicImpl implements BankLogic {
    private final IBankInfoService bankInfoService;
    private final BankInfoConvert bankInfoConvert;


    @Override
    public PageView list(BankInfoQuery query) {
        final LambdaQueryWrapper<BankInfo> queryWrapper = new LambdaQueryWrapper<BankInfo>()
                .apply("RowNumber=1")
                .eq(BankInfo::getDisabledState,"否")
                .eq(BankInfo::getSysFlag,0)
                .like( StrUtil.isNotEmpty(query.getBankName()),BankInfo::getBankName,query.getBankNameLike());
        Integer count = bankInfoService.findCount(queryWrapper);
        final Integer rows = query.getRows();
        final Integer page = query.getPage();
        Integer offset = (page - 1) * rows;
        List<BankInfo> list = bankInfoService.findListForPage(offset, rows, queryWrapper);
        List<BankInfoBaseDTO> dtos = bankInfoConvert.entities2Dto(list);
        return PageUtil.getPV(dtos, count);
    }
}