package com.sckw.company.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sckw.company.entity.req.KwBusinessCompanyListReq;
import com.sckw.company.entity.req.KwBusinessDeptListReq;
import com.sckw.company.entity.res.KwBusinessCompanyListRes;
import com.sckw.company.entity.res.KwBusinessDeptListRes;
import com.sckw.company.mapper.KwBusinessCompanyMapper;
import com.sckw.company.mapper.KwBusinessDeptMapper;
import com.sckw.core.model.page.PageRes;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @desc:
 * @author: Lt
 * @date: 2024-05-06
 */
@Service
public class CompanyService {

    @Autowired
    KwBusinessCompanyMapper kwBusinessCompanyMapper;

    @Autowired
    KwBusinessDeptMapper kwBusinessDeptMapper;

    public PageRes<KwBusinessCompanyListRes> findList(KwBusinessCompanyListReq kwBusinessCompany) {
        List<KwBusinessCompanyListRes> resultList;

        // 判断是否需要分页
        if (kwBusinessCompany.getPage() != null && kwBusinessCompany.getPageSize() != null) {
            // 分页查询
            PageHelper.startPage(kwBusinessCompany.getPage(), kwBusinessCompany.getPageSize());
            resultList = kwBusinessCompanyMapper.findList(kwBusinessCompany);
            return new PageRes<>(new PageInfo<>(resultList)); // 注意这里应该返回resultList而不是kwBusinessGoods
        } else {
            // 不分页查询
            resultList = kwBusinessCompanyMapper.findList(kwBusinessCompany);
            // 直接返回结果列表，不包装PageInfo，除非你有其他非分页的PageRes构造方式
            return new PageRes<>(resultList); // 假设PageRes有合适的构造方法处理非分页情况
        }
    }

    public PageRes<KwBusinessDeptListRes> deptList(KwBusinessDeptListReq kwBusinessDeptListReq) {
        List<KwBusinessDeptListRes> resultList;

        // 判断是否需要分页
        if (kwBusinessDeptListReq.getPage() != null && kwBusinessDeptListReq.getPageSize() != null) {
            // 分页查询
            PageHelper.startPage(kwBusinessDeptListReq.getPage(), kwBusinessDeptListReq.getPageSize());
            resultList = kwBusinessDeptMapper.findList(kwBusinessDeptListReq);
            return new PageRes<>(new PageInfo<>(resultList)); // 注意这里应该返回resultList而不是kwBusinessGoods
        } else {
            // 不分页查询
            resultList = kwBusinessDeptMapper.findList(kwBusinessDeptListReq);
            // 直接返回结果列表，不包装PageInfo，除非你有其他非分页的PageRes构造方式
            return new PageRes<>(resultList); // 假设PageRes有合适的构造方法处理非分页情况
        }
    }
}
