package org.linlinjava.jz.db.service;

import com.github.pagehelper.PageHelper;
import org.linlinjava.jz.db.dao.JzMallRegionMapper;
import org.linlinjava.jz.db.domain.JzMallRegion;
import org.linlinjava.jz.db.domain.JzMallRegionExample;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

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

/**
 * 商城 三级国标地址处理类
 */
@Service
public class JzMallRegionService {

    @Resource
    private JzMallRegionMapper regionMapper;

    public List<JzMallRegion> getAll(){
        JzMallRegionExample example = new JzMallRegionExample();
        byte b = 4;
        example.or().andTypeNotEqualTo(b);
        return regionMapper.selectByExample(example);
    }

    public List<JzMallRegion> queryByPid(Integer parentId) {
        JzMallRegionExample example = new JzMallRegionExample();
        example.or().andPidEqualTo(parentId);
        return regionMapper.selectByExample(example);
    }

    public JzMallRegion findById(Integer id) {
        return regionMapper.selectByPrimaryKey(id);
    }

    public List<JzMallRegion> querySelective(String name, Integer code, Integer page, Integer size, String sort, String order) {
        JzMallRegionExample example = new JzMallRegionExample();
        JzMallRegionExample.Criteria criteria = example.createCriteria();

        if (!StringUtils.isEmpty(name)) {
            criteria.andNameLike("%" + name + "%");
        }
        if (!StringUtils.isEmpty(code)) {
            criteria.andCodeEqualTo(code);
        }

        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
            example.setOrderByClause(sort + " " + order);
        }

        PageHelper.startPage(page, size);
        return regionMapper.selectByExample(example);
    }

}
