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

import com.neusoft.smart.pos.dto.org.request.ShopQueryRequest;
import com.neusoft.smart.pos.org.persistent.domain.ShopExample;
import com.neusoft.smart.pos.org.persistent.domain.ShopExample.Criteria;
import com.neusoft.smart.pos.org.persistent.domain.Shop;
import com.neusoft.smart.pos.org.persistent.mapper.ShopMapper;

import lombok.val;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @author tjm
 * @date 2018-03-01 13：57
 * @description 门店管理使用Repository
 */
@Component
public class ShopRepository {

	@Autowired
	public ShopMapper shopMapper;

	public Shop selectByPrimaryKey(int id){
		return shopMapper.selectByPrimaryKey(id);
	}
	
	public int insertSelective(Shop shop){
		return shopMapper.insertSelective(shop);
	}
	
	public int updateByPrimaryKeySelective(Shop shop){
		return shopMapper.updateByPrimaryKeySelective(shop);
	}
	
	public List<Shop> selectByExample(List<Integer> ids){
		val example = new ShopExample();
        example.createCriteria().andIdIn(ids);
		return shopMapper.selectByExample(example);
	}
	
	public List<Shop> selectByExample(ShopExample example){
		return shopMapper.selectByExample(example);
	}
	
	public List<Shop> selectByExample(ShopQueryRequest shopQueryRequest){
		return shopMapper.selectByExample(createExample(shopQueryRequest));
	}
	
	public int countByExample(ShopQueryRequest shopQueryRequest){
		return shopMapper.countByExample(createExample(shopQueryRequest));
	}

	public ShopExample createExample(ShopQueryRequest shopQueryRequest){
		ShopExample shopExample = new ShopExample();
        if (!StringUtils.isEmpty(shopQueryRequest.getOrderField()) && !StringUtils.isEmpty(shopQueryRequest.getDire())) shopExample.setOrderByClause(shopQueryRequest.getOrderField() + " " + shopQueryRequest.getDire());
        if (!StringUtils.isEmpty(shopQueryRequest.getOffset()) && !StringUtils.isEmpty(shopQueryRequest.getLimit())){
        	shopExample.setOffset(shopQueryRequest.getOffset());
            shopExample.setLimit(shopQueryRequest.getLimit());
        }        
        Criteria shopCriteria = shopExample.createCriteria();
        shopCriteria.andStatusEqualTo("1");
        if (!StringUtils.isEmpty(shopQueryRequest.getId())) shopCriteria.andIdEqualTo(Integer.parseInt(shopQueryRequest.getId()));
        if (!StringUtils.isEmpty(shopQueryRequest.getName())) shopCriteria.andNameLike("%" + shopQueryRequest.getName() + "%");
        if (!StringUtils.isEmpty(shopQueryRequest.getOrg())) shopCriteria.andOrgEqualTo(Integer.parseInt(shopQueryRequest.getOrg()));
        if (!StringUtils.isEmpty(shopQueryRequest.getProvince())) shopCriteria.andProvinceLike("%" + shopQueryRequest.getProvince() + "%");
        if (!StringUtils.isEmpty(shopQueryRequest.getCity())) shopCriteria.andCityLike("%" + shopQueryRequest.getCity() + "%");
        if (!StringUtils.isEmpty(shopQueryRequest.getAddress())) shopCriteria.andAddressLike("%" + shopQueryRequest.getAddress() + "%");
        if (!StringUtils.isEmpty(shopQueryRequest.getIds()) && shopQueryRequest.getIds().size() > 0) shopCriteria.andIdIn(shopQueryRequest.getIds());
        return shopExample;
	}
	
	public Shop queryShopByName(String name, int orgId){
    	val example = new ShopExample();
    	example.createCriteria().andNameEqualTo(name).andOrgEqualTo(orgId);
    	val shops = shopMapper.selectByExample(example);
    	return shops.size() > 0 ? shops.get(0) : null;
    }
}
