package com.house365.service.impl;

import com.house365.beans.vo.User;
import com.house365.common.beans.ReturnResult;
import com.house365.common.searchBeans.CommonSearchResponse;
import com.house365.common.searchBeans.QueryParams;
import com.house365.dao.UserMapper;
import com.house365.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Date: 2016/12/25</p>
 *
 * @author XN
 * @version 1.0
 */
@Service("userService")
public class UserService implements IUserService {

    @Autowired
    private UserMapper userMapper;

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

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

    @Override
    public User selectByPrimaryKey(Integer id) {
        return userMapper.selectByPrimaryKey(id);
    }

    @Override
    public int save(User department) {
        return userMapper.insertSelective(department);
    }

    @Override
    public int updateByPrimaryKey(User record) {
        return userMapper.updateByPrimaryKey(record);
    }

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

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

    @Override
    public User selectByCompanyIdAndUserId(String companyId, String companyUserId) {
        return userMapper.selectByCompanyIdAndUserId(companyId, companyUserId);
    }
}
