package com.izouma.awesomeadmin.service.impl;

import java.util.*;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.izouma.awesomeadmin.constant.AppConstant;
import com.izouma.awesomeadmin.dto.Page;
import com.izouma.awesomeadmin.model.UserAddress;
import com.izouma.awesomeadmin.service.UserAddressService;
import com.izouma.awesomeadmin.dao.UserAddressMapper;

/**
 * service接口实现类
 */
@Service
public class UserAddressServiceImpl implements UserAddressService {

    private static Logger logger = Logger.getLogger(UserAddressServiceImpl.class);

    @Autowired
    private UserAddressMapper userAddressMapper;

    @Override
    public List<UserAddress> getUserAddressList(UserAddress record) {

        logger.info("getUserAddressList");
        try {

            return userAddressMapper.queryAllUserAddress(record);
        } catch (Exception e) {
            logger.error("getUserAddressList", e);
        }

        return null;
    }

    @Override
    public List<UserAddress> getUserAddressByPage(Page page, UserAddress record) {

        logger.info("getUserAddressByPage");
        try {

            Map<String, Object> parameter = new HashMap<String, Object>();
            parameter.put("record", record);
            parameter.put(AppConstant.PAGE, page);

            return userAddressMapper.queryUserAddressByPage(parameter);
        } catch (Exception e) {
            logger.error("getUserAddressByPage", e);
        }

        return null;
    }

    @Override
    public UserAddress getUserAddressById(String id) {

        logger.info("getUserAddressyId");
        try {

            return userAddressMapper.selectByPrimaryKey(Integer.valueOf(id));
        } catch (Exception e) {
            logger.error("getUserAddressById", e);
        }

        return null;
    }

    @Override
    public UserAddress getUserAddress(UserAddress record) {

        logger.info("getUserAddress");
        try {

            return userAddressMapper.queryUserAddress(record);
        } catch (Exception e) {
            logger.error("getUserAddress", e);
        }

        return null;
    }

    @Override
    public boolean createUserAddress(UserAddress record) {

        logger.info("createUserAddress");
        try {

            int updates = userAddressMapper.insertSelective(record);

            if (updates > 0) {
                return true;
            }
        } catch (Exception e) {
            logger.error("createUserAddress", e);
        }

        return false;
    }

    @Override
    public boolean deleteUserAddress(String id) {

        logger.info("deleteUserAddress");
        try {

            int updates = userAddressMapper.delete(id);

            if (updates > 0) {
                return true;
            }
        } catch (Exception e) {
            logger.error("deleteUserAddress", e);
        }

        return false;
    }

    @Override
    public boolean updateUserAddress(UserAddress record) {

        logger.info("updateUserAddress");
        try {

            int updates = userAddressMapper.updateByPrimaryKeySelective(record);

            if (updates > 0) {
                return true;
            }
        } catch (Exception e) {
            logger.error("updateUserAddress", e);
        }

        return false;
    }

    @Override
    public boolean setDefaultUserAddress(String userId, String addressId) {

        logger.info("updateUserAddress");
        try {

            Map<String, Object> parameter = new HashMap<String, Object>();
            parameter.put("userId", userId);
            parameter.put("addressId", addressId);

            int updates = userAddressMapper.setDefaultUserAddress(parameter);

            if (updates > 0) {
                return true;
            }
        } catch (Exception e) {
            logger.error("updateUserAddress", e);
        }

        return false;
    }
}

