package com.hc.business.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hc.business.mapper.AreaDistrictMapper;
import com.hc.business.model.AreaDistrict;
import com.hc.business.service.AreaDistrictService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 省份表(AreaDistrict)表服务实现类
 *
 * @author java
 * @since 2020-11-20 12:48:47
 */

@Service("areaDistrictService")
public class AreaDistrictServiceImpl implements AreaDistrictService {

    private final AreaDistrictMapper areaDistrictMapper;

    public AreaDistrictServiceImpl(AreaDistrictMapper areaDistrictMapper) {
        this.areaDistrictMapper = areaDistrictMapper;
    }

    /**
     * 通过ID查询单条数据
     *
     * @param areaId 主键
     * @return 实例对象
     */
    @Override
    public AreaDistrict queryById(Integer areaId) {
        return this.areaDistrictMapper.selectByPrimaryKey(areaId);
    }

    /**
     * 新增数据
     *
     * @param areaDistrict 实例对象
     * @return 实例对象
     */
    @Override
    public AreaDistrict insert(AreaDistrict areaDistrict) {
        this.areaDistrictMapper.insert(areaDistrict);
        return areaDistrict;
    }

    /**
     * 修改数据
     *
     * @param areaDistrict 实例对象
     * @return 实例对象
     */
    @Override
    public AreaDistrict update(AreaDistrict areaDistrict) {
        this.areaDistrictMapper.updateByPrimaryKeySelective(areaDistrict);
        return this.queryById(areaDistrict.getAreaId());
    }

    /**
     * 通过主键删除数据
     *
     * @param areaId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer areaId) {
        return this.areaDistrictMapper.deleteByPrimaryKey(areaId) > 0;
    }

    /**
     * 分页查询多条数据
     *
     * @param page 页码
     * @param size 查询条数
     * @return 对象列表
     */
    @Override
    public PageInfo queryAllByLimit(int page, int size) {
        //分页开启
        PageHelper.startPage(page, size);
        List<AreaDistrict> list = areaDistrictMapper.queryAllByParentId(0);
        list.forEach(areaDistrict -> {
            List<AreaDistrict> areaDistricts = areaDistrictMapper.queryAllByParentId(areaDistrict.getAreaId());
            areaDistrict.setAreaDistrictList(areaDistricts);
        });
        return new PageInfo(list);
    }

}