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.domain.ZHouse;
import com.ruoyi.zdsz.domain.bo.ZBuildingBo;
import com.ruoyi.zdsz.domain.vo.ZBuildingVo;
import com.ruoyi.zdsz.mapper.ZHouseMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.zdsz.domain.bo.ZUnitBo;
import com.ruoyi.zdsz.domain.vo.ZUnitVo;
import com.ruoyi.zdsz.domain.ZUnit;
import com.ruoyi.zdsz.mapper.ZUnitMapper;
import com.ruoyi.zdsz.service.IZUnitService;
import org.springframework.util.ObjectUtils;

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 ZUnitServiceImpl implements IZUnitService {

    private final ZUnitMapper baseMapper;

    @Resource
    private ZHouseMapper zHouseMapper;

    @Override
    public List<ZUnitVo> getUnitList(String buildingId) {
        return baseMapper.getUnitList(buildingId);
    }

    /**
     * 查询单元管理
     */
    @Override
    public ZUnitVo queryById(String id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询单元管理列表
     */
    @Override
    public TableDataInfo<ZUnitVo> getPageList(ZUnitBo bo, PageQuery pageQuery) {
        Page<ZUnitBo> page = new Page<>(pageQuery.getPageNum(), pageQuery.getPageSize());
        Page<ZUnitVo> result = baseMapper.getPageList(bo,page);
        for (ZUnitVo vo:result.getRecords()) {
            vo.setSource(zHouseMapper.getAreaSourceString("unit_id",vo.getId()));
        }
        return TableDataInfo.build(result);
    }

    /**
     * 查询单元管理列表
     */
    @Override
    public List<ZUnitVo> queryList(ZUnitBo bo) {
        LambdaQueryWrapper<ZUnit> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

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

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

    /**
     * 修改单元管理
     */
    @Override
    public Boolean updateByBo(ZUnitBo bo) {
        ZUnit update = BeanUtil.toBean(bo, ZUnit.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

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

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