package com.neusoft.smart.pos.address.repository;

import com.neusoft.smart.pos.address.persistent.domain.UserCargoAddress;
import com.neusoft.smart.pos.address.persistent.domain.UserCargoAddressExample;
import com.neusoft.smart.pos.address.persistent.mapper.UserCargoAddressMapper;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Component
public class UserCargoAddressRepository {

    @Resource
    public UserCargoAddressMapper userCargoAddressMapper;

    public int insertSelective(UserCargoAddress address){
        return userCargoAddressMapper.insertSelective(address);
    }

    public UserCargoAddress selectById(Integer id){return userCargoAddressMapper.selectByPrimaryKey(id);}


    public List<UserCargoAddress> selectByUserId(Integer userId)
    {
        List<UserCargoAddress> rs = userCargoAddressMapper.selectByExample(createExample(userId));
        return rs;
    }

    private UserCargoAddressExample createExample(Integer userId){
        UserCargoAddressExample example = new UserCargoAddressExample();
        UserCargoAddressExample.Criteria criteria = example.createCriteria();
        criteria.andUserIdEqualTo(userId);
        example.setOrderByClause("priority ASC"); //priority升序排列
        return example;
    }

    public List<UserCargoAddress> selectByUserIdAndAddressType(Integer userId, Integer addressType) {
        UserCargoAddressExample example = new UserCargoAddressExample();
        UserCargoAddressExample.Criteria criteria = example.createCriteria();
        criteria.andUserIdEqualTo(userId);
        criteria.andAddressTypeEqualTo(addressType);
        example.setOrderByClause("priority ASC"); //priority升序排列
        List<UserCargoAddress> rs = userCargoAddressMapper.selectByExample(example);
        return rs;
    }

    public List<UserCargoAddress> selectByUserIdAndPriority(Integer userId, Integer priority) {
        UserCargoAddressExample example = new UserCargoAddressExample();
        UserCargoAddressExample.Criteria criteria = example.createCriteria();
        criteria.andUserIdEqualTo(userId);
        criteria.andPriorityEqualTo(priority);
        List<UserCargoAddress> rs = userCargoAddressMapper.selectByExample(example);
        return rs;
    }

    public List<UserCargoAddress> selectAddress(Integer userId, Integer addressType, Integer priority) {
        List<UserCargoAddress> rs = userCargoAddressMapper.selectByExample(createExample(userId,addressType,priority));
        return rs;
    }

    private UserCargoAddressExample createExample(Integer userId, Integer addressType, Integer priority) {
        UserCargoAddressExample example = new UserCargoAddressExample();
        UserCargoAddressExample.Criteria orgCriteria = example.createCriteria();
        orgCriteria.andUserIdEqualTo(userId);
        orgCriteria.andAddressTypeEqualTo(addressType);
        orgCriteria.andPriorityEqualTo(priority);
        return example;
    }

    public int updateByPrimaryKeySelective(UserCargoAddress address){
        address.setUpdateTime(new Date());
        return userCargoAddressMapper.updateByPrimaryKeySelective(address);
    }

    public UserCargoAddress selectLastByUserId(Integer userId) {
        List<UserCargoAddress> rs = userCargoAddressMapper.selectByExample(createExampleForLast(userId));
        if(rs.size()==1) return rs.get(0);
        else return null;
    }

    private UserCargoAddressExample createExampleForLast(Integer userId) {
        UserCargoAddressExample example = new UserCargoAddressExample();
        UserCargoAddressExample.Criteria orgCriteria = example.createCriteria();
        orgCriteria.andUserIdEqualTo(userId);
        example.setLimit(1);
        example.setOffset(0);
        example.setOrderByClause("create_time DESC");
        return example;
    }

    public Integer deleteCargoAddress(Integer id) {
        return userCargoAddressMapper.deleteByPrimaryKey(id);
    }
}
