package com.jwipc.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jwipc.page.PageUtil;
import com.jwipc.page.PageView;
import com.jwipc.sys.entity.Company;
import com.jwipc.sys.mapper.CompanyMapper;
import com.jwipc.sys.model.param.ChoseSupplierListParam;
import com.jwipc.sys.model.po.CompanyPO;
import com.jwipc.sys.model.query.CompanyQuery;
import com.jwipc.sys.service.ICompanyService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 公司主数据 服务实现类
 * </p>
 *
 * @author zhangjie
 * @since 2024-01-29
 */
@Service
public class CompanyServiceImpl extends ServiceImpl<CompanyMapper, Company> implements ICompanyService {
    @Override
    public List<CompanyPO> findPageByQuery(Page<Company> dbPage, CompanyQuery query) {
        if (dbPage==null){
            return baseMapper.findPageByQuery(query);
        }
        return baseMapper.findPageByQuery(dbPage,query);
    }

    @Override
    public PageView getSupplierList(ChoseSupplierListParam choseSupplierListQuery) {
        final Page<Company> page = PageUtil.toPage(choseSupplierListQuery);
        List<Company> contracts = baseMapper.selectList(page,
            new LambdaQueryWrapper<Company>()
            .like(Objects.nonNull(choseSupplierListQuery.getNameCOde()),Company::getCompanyCode, choseSupplierListQuery.getNameCOde())
                    .or()
                    .like(Objects.nonNull(choseSupplierListQuery.getNameCOde()),Company::getCompanyName, choseSupplierListQuery.getNameCOde())
        );
        return PageUtil.getPV(contracts, page.getTotal());
    }

}
