package com.njzscloud.dispose.sys.district.service;

import cn.hutool.core.util.StrUtil;
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.mp.support.PageParam;
import com.njzscloud.common.mp.support.PageResult;
import com.njzscloud.dispose.sys.district.mapper.DistrictMapper;
import com.njzscloud.dispose.sys.district.pojo.entity.DistrictEntity;
import com.njzscloud.dispose.sys.district.pojo.param.SearchDistrictParam;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 省市区表
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class DistrictService extends ServiceImpl<DistrictMapper, DistrictEntity> implements IService<DistrictEntity> {

    /**
     * 新增
     */
    public void add(DistrictEntity districtEntity) {
        this.save(districtEntity);
    }

    /**
     * 修改
     */
    public void modify(DistrictEntity districtEntity) {
        this.updateById(districtEntity);
    }

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

    /**
     * 详情
     */
    public DistrictEntity detail(Long id) {
        return this.getById(id);
    }

    /**
     * 分页查询
     */
    public PageResult<DistrictEntity> paging(PageParam pageParam, SearchDistrictParam searchDistrictParam) {
        String pid = searchDistrictParam.getPid();
        String districtName = searchDistrictParam.getDistrictName();
        return PageResult.of(this.page(pageParam.toPage(), Wrappers.<DistrictEntity>lambdaQuery()
            .eq(DistrictEntity::getPid, pid)
            .like(StrUtil.isNotBlank(districtName), DistrictEntity::getDistrictName, districtName)));
    }

    /**
     * 查询所有
     */
    public List<DistrictEntity> list(SearchDistrictParam searchDistrictParam) {
        String pid = searchDistrictParam.getPid();
        String districtName = searchDistrictParam.getDistrictName();
        return this.list(Wrappers.<DistrictEntity>lambdaQuery()
            .eq(DistrictEntity::getPid, pid)
            .like(StrUtil.isNotBlank(districtName), DistrictEntity::getDistrictName, districtName));
    }
}
