package com.hc.business.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hc.business.mapper.ZccmAreaDistrictMapper;
import com.hc.business.model.ZccmAreaDistrict;
import com.hc.business.service.ZccmAreaDistrictService;
import org.springframework.stereotype.Service;

import java.util.List;

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

@Service("zccmAreaDistrictService")
public class ZccmAreaDistrictServiceImpl implements ZccmAreaDistrictService {

    private final ZccmAreaDistrictMapper areaDistrictMapper;

    public ZccmAreaDistrictServiceImpl(ZccmAreaDistrictMapper areaDistrictMapper) {
        this.areaDistrictMapper = areaDistrictMapper;
    }

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

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

    /**
     * 修改数据
     *
     * @param areaDistrict 实例对象
     * @return 实例对象
     */
    @Override
    public ZccmAreaDistrict update(ZccmAreaDistrict 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<ZccmAreaDistrict> list = areaDistrictMapper.queryAllByParentId(0);
        list.forEach(areaDistrict -> {
            List<ZccmAreaDistrict> areaDistricts = areaDistrictMapper.queryAllByParentId(areaDistrict.getAreaId());
            areaDistrict.setAreaDistrictList(areaDistricts);
        });
        return new PageInfo(list);
    }

}