package com.njzscloud.supervisory.district.service;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.njzscloud.common.core.tree.Tree;
import com.njzscloud.common.mp.support.PageParam;
import com.njzscloud.common.mp.support.PageResult;
import com.njzscloud.supervisory.district.mapper.SysDistrictMapper;
import com.njzscloud.supervisory.district.pojo.DistrictTreeResult;
import com.njzscloud.supervisory.district.pojo.SysDistrictEntity;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 省市区表
 */
@Slf4j
@Service
public class SysDistrictService extends ServiceImpl<SysDistrictMapper, SysDistrictEntity> implements IService<SysDistrictEntity> {

    @Value("${app.district.province}")
    private String province;
    @Value("${app.district.city}")
    private String city;

    /**
     * 新增
     *
     * @param sysDistrictEntity 数据
     */
    public void add(SysDistrictEntity sysDistrictEntity) {
        this.save(sysDistrictEntity);
    }

    /**
     * 修改
     *
     * @param sysDistrictEntity 数据
     */
    public void modify(SysDistrictEntity sysDistrictEntity) {
        this.updateById(sysDistrictEntity);
    }

    /**
     * 删除
     *
     * @param ids Ids
     */
    @Transactional(rollbackFor = Exception.class)
    public void del(List<Long> ids) {
        this.removeBatchByIds(ids);
    }

    /**
     * 详情
     *
     * @param id Id
     * @return SysDistrictEntity 结果
     */
    public SysDistrictEntity detail(Long id) {
        return this.getById(id);
    }

    /**
     * 分页查询
     *
     * @param sysDistrictEntity 筛选条件
     * @param pageParam         分页参数
     * @return PageResult&lt;SysDistrictEntity&gt; 分页结果
     */
    public PageResult<SysDistrictEntity> paging(PageParam pageParam, SysDistrictEntity sysDistrictEntity) {
        return PageResult.of(this.page(pageParam.toPage(), Wrappers.<SysDistrictEntity>query(sysDistrictEntity)));
    }

    public List<DistrictTreeResult> tree() {
        List<DistrictTreeResult> list = baseMapper.selectByTier(province, city);
        return Tree.listToTree(list, DistrictTreeResult::getId, DistrictTreeResult::getPid, DistrictTreeResult::setChildren, "0");
    }
}
