package com.ruoyi.zdsz.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.zdsz.service.IZBuildingService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.zdsz.domain.bo.ZAreaBo;
import com.ruoyi.zdsz.domain.vo.ZAreaVo;
import com.ruoyi.zdsz.domain.ZArea;
import com.ruoyi.zdsz.mapper.ZAreaMapper;
import com.ruoyi.zdsz.service.IZAreaService;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 小区管理Service业务层处理
 *
 * @author ruoyi
 * @date 2023-12-27
 */
@RequiredArgsConstructor
@Service
public class ZAreaServiceImpl implements IZAreaService {

    @Resource
    private ZAreaMapper baseMapper;


    /**
     * 小区下拉
     */
    @Override
    public List<ZAreaVo> getArea(String district) {
        return baseMapper.getArea(district);
    }

    /**
     * 查询小区管理
     */
    @Override
    public ZAreaVo queryById(String id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询小区管理列表
     */
    @Override
    public TableDataInfo<ZAreaVo> queryPageList(ZAreaBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ZArea> lqw = buildQueryWrapper(bo);
        Page<ZAreaVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询小区管理列表
     */
    @Override
    public List<ZAreaVo> queryList(ZAreaBo bo) {
        LambdaQueryWrapper<ZArea> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ZArea> buildQueryWrapper(ZAreaBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ZArea> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getName()), ZArea::getName, bo.getName());
        lqw.eq(StringUtils.isNotBlank(bo.getScale()), ZArea::getScale, bo.getScale());
        lqw.eq(bo.getReleaseIs() != null, ZArea::getReleaseIs, bo.getReleaseIs());
        lqw.eq(StringUtils.isNotBlank(bo.getReleaseBy()), ZArea::getReleaseBy, bo.getReleaseBy());
        lqw.eq(StringUtils.isNotBlank(bo.getDistrict()), ZArea::getDistrict, bo.getDistrict());
        lqw.eq(StringUtils.isNotBlank(bo.getCalculateDistrict()), ZArea::getCalculateDistrict, bo.getCalculateDistrict());
        lqw.eq(StringUtils.isNotBlank(bo.getBelongingPack()), ZArea::getBelongingPack, bo.getBelongingPack());
        return lqw;
    }

    /**
     * 新增小区管理
     */
    @Override
    public Boolean insertByBo(ZAreaBo bo) {
        ZArea add = BeanUtil.toBean(bo, ZArea.class);
        validEntityBeforeSave(add);
        add.setReleaseIs("001");
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改小区管理
     */
    @Override
    public Boolean updateByBo(ZAreaBo bo) {
        ZArea update = BeanUtil.toBean(bo, ZArea.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ZArea entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除小区管理
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if (isValid) {

        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
