package org.linlinjava.jz.db.service;

import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.linlinjava.jz.db.dao.SysCityMapper;
import org.linlinjava.jz.db.domain.SysCity;
import org.linlinjava.jz.db.domain.SysCityExample;
import org.springframework.stereotype.Service;

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

@Service
public class JzSysCityService {

    @Resource
    private SysCityMapper sysCityMapper;

    public List<SysCity> querySelective(Integer page, Integer limit, String sort, String order) {
        SysCityExample example = new SysCityExample();
        SysCityExample.Criteria criteria = SysCityExample.newAndCreateCriteria();

        criteria.andIsDeleteEqualTo(false);

        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
            example.setOrderByClause(sort + " " + order);
        }
        PageHelper.startPage(page, limit);
        return sysCityMapper.selectByExample(example);
    }

    public SysCity add(SysCity city) {
        city.setCreateTime(LocalDateTime.now());
        city.setUpdateTime(LocalDateTime.now());
        sysCityMapper.insertSelective(city);
        return city;
    }

    public SysCity queryById(long id) {
        return sysCityMapper.selectByPrimaryKey(id);
    }

    public int update(SysCity city) {
        city.setUpdateTime(LocalDateTime.now());
        return sysCityMapper.updateByPrimaryKeySelective(city);
    }

    public int delete(SysCity city) {
        city.setUpdateTime(LocalDateTime.now());
        city.setIsDelete(true);
        return sysCityMapper.updateByPrimaryKeySelective(city);
    }

    public List<SysCity> queryAll() {
        SysCityExample example = new SysCityExample();
        example.or().andIsDeleteEqualTo(false);

        return sysCityMapper.selectByExample(example);
    }


    public SysCity queryByCityName(String cityName) {
        SysCityExample example = new SysCityExample();
        example.or().andCityNameLike("%" + cityName + "%");
        return sysCityMapper.selectOneByExample(example);
    }
}
