package com.ruoyi.zdsz.service.impl;

import cn.hutool.core.bean.BeanUtil;
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 lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.zdsz.domain.bo.ZHouseBo;
import com.ruoyi.zdsz.domain.vo.ZHouseVo;
import com.ruoyi.zdsz.domain.ZHouse;
import com.ruoyi.zdsz.mapper.ZHouseMapper;
import com.ruoyi.zdsz.service.IZHouseService;
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 ZHouseServiceImpl implements IZHouseService {

    @Resource
    private final ZHouseMapper baseMapper;

    /**
     * 查询房间管理
     */
    @Override
    public ZHouseVo queryById(String id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询房间管理列表
     */
    @Override
    public TableDataInfo<ZHouseVo> queryPageList(ZHouseBo bo, PageQuery pageQuery) {
//        LambdaQueryWrapper<ZHouse> lqw = buildQueryWrapper(bo);
//        Page<ZHouseVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
//        return TableDataInfo.build(result);
        Page<ZHouseBo> page = new Page<>(pageQuery.getPageNum(), pageQuery.getPageSize());
        Page<ZHouseVo> list = baseMapper.getPageList(bo,page);
        return PageUtils.buildDataInfo(list);
    }

    /**
     * 查询房间管理列表
     */
    @Override
    public List<ZHouseVo> queryList(ZHouseBo bo) {
        LambdaQueryWrapper<ZHouse> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

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

    /**
     * 新增房间管理
     */
    @Override
    public Boolean insertByBo(ZHouseBo bo) {
        ZHouse add = BeanUtil.toBean(bo, ZHouse.class);
        validEntityBeforeSave(add);
        add.setSource("0");
        ZHouse byName = baseMapper.getByName(add.getName(), add.getUnitId());
        if (!ObjectUtils.isEmpty(byName)){
            return false;
        }
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改房间管理
     */
    @Override
    public Boolean updateByBo(ZHouseBo bo) {
        ZHouse update = BeanUtil.toBean(bo, ZHouse.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }
    @Override
    public Boolean updateById(String id ,String newPhone) {
        ZHouse update = new ZHouse();
        update.setId(id);
        update.setNewPhone(newPhone);
        return baseMapper.updateById(update) > 0;
    }



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

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

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

    @Override
    public List<ZHouseVo> getHouses(String unitId) {
        return baseMapper.selectListByunitId(unitId);
    }

    @Override
    public ZHouseVo getHousesByIdCard(String idCard) {
        return baseMapper.getHousesByIdCard(idCard);
    }

    @Override
    public ZHouse getHousesMessage(ZHouseBo zHouseBo) {
        LambdaQueryWrapper<ZHouse> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(zHouseBo.getId()), ZHouse::getId,zHouseBo.getId());
        lqw.eq(StringUtils.isNotBlank(zHouseBo.getIdCard()), ZHouse::getIdCard, zHouseBo.getBuildingId());
        return baseMapper.selectOne(lqw);
    }
}
