package com.house365.service.impl;

import com.house365.beans.vo.Department;
import com.house365.common.beans.ReturnResult;
import com.house365.common.searchBeans.CommonSearchResponse;
import com.house365.common.searchBeans.QueryParams;
import com.house365.dao.DepartmentMapper;
import com.house365.service.IDepartmentService;
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("departmentService")
public class DepartmentService implements IDepartmentService {

    @Autowired
    private DepartmentMapper departmentMapper;

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

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

    @Override
    public Department selectByPrimaryKey(Integer id) {
        return departmentMapper.selectByPrimaryKey(id);
    }

    @Override
    public int save(Department department) {
        return departmentMapper.insertSelective(department);
    }

    @Override
    public int updateByPrimaryKey(Department record) {
        return departmentMapper.updateByPrimaryKey(record);
    }

    @Override
    public int deleteByPrimaryKey(Integer id) {
        return departmentMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int nextId(String companyId) {
        return departmentMapper.nextId(companyId);
    }

    @Override
    public Department selectByCompanyIdAndDepartmentId(String companyId, String departmentId) {
        return departmentMapper.selectByCompanyIdAndDepartmentId(companyId, departmentId);
    }

    @Override
    public Department selectByCompanyIdAndWeChatDepartmentId(String companyId, String weChatDepartmentId) {
        return departmentMapper.selectByCompanyIdAndWeChatDepartmentId(companyId, weChatDepartmentId);
    }
}
