package com.ruoyi.zdsz.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.obs.FileUtil;
import com.ruoyi.system.domain.SysConfig;
import com.ruoyi.system.mapper.SysDictDataMapper;
import com.ruoyi.zdsz.domain.*;
import com.ruoyi.zdsz.domain.bo.ZAreaBo;
import com.ruoyi.zdsz.domain.bo.ZEnginNotInstalledBo;
import com.ruoyi.zdsz.domain.vo.*;
import com.ruoyi.zdsz.mapper.ZEnginNotInstalledMapper;
import com.ruoyi.zdsz.mapper.ZBuildingMapper;
import com.ruoyi.zdsz.mapper.ZHouseMapper;
import com.ruoyi.zdsz.mapper.ZUnitMapper;
import com.ruoyi.zdsz.service.IZEnginNotInstalledService;
import com.ruoyi.zdsz.service.IZEnginNotInstalledService;
import lombok.RequiredArgsConstructor;
import org.mybatis.logging.Logger;
import org.mybatis.logging.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.*;

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

    private static final Logger logger = LoggerFactory.getLogger(ZEnginNotInstalledServiceImpl.class);

    @Resource
    private ZEnginNotInstalledMapper baseMapper;

    @Override
    public ZEnginNotInstalledVo queryById(String id) {
        return null;
    }

    @Override
    public TableDataInfo<ZEnginNotInstalledVo> queryPageList(ZEnginNotInstalledBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ZEnginNotInstalled> lqw = buildQueryWrapper(bo);
        Page<ZEnginNotInstalledVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }
    @Override
    public List<ZEnginNotInstalledVo> queryList(ZEnginNotInstalledBo bo) {
        LambdaQueryWrapper<ZEnginNotInstalled> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }
    private LambdaQueryWrapper<ZEnginNotInstalled> buildQueryWrapper(ZEnginNotInstalledBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ZEnginNotInstalled> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(ZEnginNotInstalled::getType);
        lqw.eq(StringUtils.isNotBlank(bo.getDistrict()), ZEnginNotInstalled::getDistrict, bo.getDistrict());
        lqw.eq(StringUtils.isNotBlank(bo.getAreaId()), ZEnginNotInstalled::getAreaId, bo.getAreaId());
        lqw.eq(StringUtils.isNotBlank(bo.getBuildingId()), ZEnginNotInstalled::getBuildingId, bo.getBuildingId());
        lqw.eq(StringUtils.isNotBlank(bo.getUnitId()), ZEnginNotInstalled::getUnitId, bo.getUnitId());
        lqw.eq(StringUtils.isNotBlank(bo.getHouseId()), ZEnginNotInstalled::getHouseId, bo.getHouseId());
        lqw.eq(StringUtils.isNotBlank(bo.getType()), ZEnginNotInstalled::getType, bo.getType());
        lqw.eq(StringUtils.isNotBlank(bo.getEnginType()), ZEnginNotInstalled::getEnginType, bo.getEnginType());
        lqw.like(StringUtils.isNotBlank(bo.getRemark()), ZEnginNotInstalled::getRemark, bo.getRemark());
        return lqw;
    }
    @Override
    public Boolean insertByBo(ZEnginNotInstalledBo bo) {
        ZEnginNotInstalled add = BeanUtil.toBean(bo, ZEnginNotInstalled.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    @Override
    public Boolean updateByBo(ZEnginNotInstalledBo bo) {
        ZEnginNotInstalled update = BeanUtil.toBean(bo, ZEnginNotInstalled.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

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