package com.ruoyi.zdsz.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.utils.PageUtils;
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.domain.ZArea;
import com.ruoyi.zdsz.domain.vo.ZAreaVo;
import com.ruoyi.zdsz.mapper.ZHouseMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.zdsz.domain.bo.ZBuildingBo;
import com.ruoyi.zdsz.domain.vo.ZBuildingVo;
import com.ruoyi.zdsz.domain.ZBuilding;
import com.ruoyi.zdsz.mapper.ZBuildingMapper;
import com.ruoyi.zdsz.service.IZBuildingService;
import org.springframework.util.ObjectUtils;

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

/**
 * 楼栋管理Service业务层处理
 *
 * @author ruoyi
 * @date 2023-12-27
 */
@RequiredArgsConstructor
@Service
public class ZBuildingServiceImpl implements IZBuildingService {

    private final ZBuildingMapper baseMapper;
    @Resource
    private ZHouseMapper zHouseMapper;

    /**
     * 楼宇下拉
     * */
    @Override
    public List<ZBuildingVo> getBuildingList(String areaId) {
        return baseMapper.getBuildingList(areaId);
    }

    @Override
    public int getBuildingListByAreaId(Collection<String> ids) {
        LambdaQueryWrapper<ZBuilding> lqw = Wrappers.lambdaQuery();
        lqw.orderByDesc(ZBuilding::getCreateTime);
        lqw.in(!ids.isEmpty(), ZBuilding::getAreaId, ids);
        return baseMapper.selectVoList(lqw).size();
    }

    /**
     * 查询楼栋管理列表
     */
    @Override
    public TableDataInfo<ZBuildingVo> getPageList(ZBuildingBo bo, PageQuery pageQuery) {
        Page<ZBuildingBo> page = new Page<>(pageQuery.getPageNum(), pageQuery.getPageSize());
        Page<ZBuildingVo> list = baseMapper.getPageList(bo,page);
        for (ZBuildingVo vo:list.getRecords()) {
            vo.setSource(zHouseMapper.getAreaSourceString("building_id",vo.getId()));
        }
        return TableDataInfo.build(list);
    }

    /**
     * 查询楼栋管理
     */
    @Override
    public ZBuildingVo queryById(String id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询楼栋管理列表
     */
    @Override
    public List<ZBuildingVo> queryList(ZBuildingBo bo) {
        LambdaQueryWrapper<ZBuilding> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ZBuilding> buildQueryWrapper(ZBuildingBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ZBuilding> lqw = Wrappers.lambdaQuery();
        lqw.orderByDesc(ZBuilding::getCreateTime);
        lqw.eq(StringUtils.isNotBlank(bo.getAreaId()), ZBuilding::getAreaId, bo.getAreaId());
        lqw.like(StringUtils.isNotBlank(bo.getName()), ZBuilding::getName, bo.getName());
        return lqw;
    }

    /**
     * 新增楼栋管理
     */
    @Override
    public Boolean insertByBo(ZBuildingBo bo) {
        ZBuilding add = BeanUtil.toBean(bo, ZBuilding.class);
        validEntityBeforeSave(add);
        add.setSource("0");
        if (!ObjectUtils.isEmpty(baseMapper.getByName(add.getName(), add.getAreaId()))){
            return false;
        }
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改楼栋管理
     */
    @Override
    public Boolean updateByBo(ZBuildingBo bo) {
        ZBuilding update = BeanUtil.toBean(bo, ZBuilding.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

    /**
     * 批量删除楼栋管理
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
