package com.house365.service.impl;

import com.house365.beans.vo.Company;
import com.house365.common.beans.ReturnResult;
import com.house365.common.searchBeans.CommonSearchResponse;
import com.house365.common.searchBeans.QueryParams;
import com.house365.dao.CompanyMapper;
import com.house365.service.ICompanyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Date: 2017/1/9</p>
 *
 * @author XN
 * @version 1.0
 */
@Service("companyService")
public class CompanyService implements ICompanyService {

    @Autowired
    private CompanyMapper companyMapper;

    @Override
    public CommonSearchResponse<Company> queryByPage(
        QueryParams queryParams
    ) {
        if (queryParams != null && queryParams.getPaging() != null) {
            int records = companyMapper.countByQueryParams(queryParams);
            // 如果查询出符合条件的记录数为0，那么就直接返回一个空的List，因为后面的已经没有执行的必要
            if (records == 0) {
                return new CommonSearchResponse();
            }
            queryParams.getPaging().setRecords(records);
        }

        List<Company> list = companyMapper.queryByPage(queryParams);
        CommonSearchResponse<Company> response = new CommonSearchResponse<>();
        response.setObjectList(list);
        response.setResultCode(ReturnResult.SUCCESS.getResultCode());
        response.setResultMessage(ReturnResult.SUCCESS.getResultMessage());
        response.setPaging(queryParams.getPaging());
        return response;
    }

    @Override
    public Company selectByPrimaryKey(Integer id) {
        return companyMapper.selectByPrimaryKey(id);
    }

}
