package org.dromara.elevator.service.impl;

import cn.hutool.core.util.ObjectUtil;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StreamUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.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.dromara.common.mybatis.helper.DataBaseHelper;
import org.dromara.system.domain.SysDept;
import org.dromara.system.mapper.SysDeptMapper;
import org.springframework.stereotype.Service;
import org.dromara.elevator.domain.bo.EVidiconInfoBo;
import org.dromara.elevator.domain.vo.EVidiconInfoVo;
import org.dromara.elevator.domain.EVidiconInfo;
import org.dromara.elevator.mapper.EVidiconInfoMapper;
import org.dromara.elevator.service.IEVidiconInfoService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 网络摄像机信息;Service业务层处理
 *
 * @author zy
 * @date 2024-07-29
 */
@RequiredArgsConstructor
@Service
public class EVidiconInfoServiceImpl implements IEVidiconInfoService {

    private final EVidiconInfoMapper baseMapper;
    private final SysDeptMapper deptMapper;

    /**
     * 查询网络摄像机信息;
     *
     * @param id 主键
     * @return 网络摄像机信息;
     */
    @Override
    public EVidiconInfoVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询网络摄像机信息;列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 网络摄像机信息;分页列表
     */
    @Override
    public TableDataInfo<EVidiconInfoVo> queryPageList(EVidiconInfoBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<EVidiconInfo> lqw = buildQueryWrapper(bo);
        Page<EVidiconInfoVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的网络摄像机信息;列表
     *
     * @param bo 查询条件
     * @return 网络摄像机信息;列表
     */
    @Override
    public List<EVidiconInfoVo> queryList(EVidiconInfoBo bo) {
        LambdaQueryWrapper<EVidiconInfo> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<EVidiconInfo> buildQueryWrapper(EVidiconInfoBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<EVidiconInfo> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getProjectId()), EVidiconInfo::getProjectId, bo.getProjectId());
        lqw.eq(StringUtils.isNotBlank(bo.getDeviceCode()), EVidiconInfo::getDeviceCode, bo.getDeviceCode());
        lqw.like(StringUtils.isNotBlank(bo.getDeviceName()), EVidiconInfo::getDeviceName, bo.getDeviceName());
        lqw.eq(StringUtils.isNotBlank(bo.getDeviceSerial()), EVidiconInfo::getDeviceSerial, bo.getDeviceSerial());
        lqw.eq(StringUtils.isNotBlank(bo.getRegisterCode()), EVidiconInfo::getRegisterCode, bo.getRegisterCode());
        lqw.eq(StringUtils.isNotBlank(bo.getChannelCode()), EVidiconInfo::getChannelCode, bo.getChannelCode());
        lqw.eq(StringUtils.isNotBlank(bo.getChannelNum()), EVidiconInfo::getChannelNum, bo.getChannelNum());
        lqw.like(StringUtils.isNotBlank(bo.getChannelName()), EVidiconInfo::getChannelName, bo.getChannelName());
        lqw.eq(bo.getIsLine() != null, EVidiconInfo::getIsLine, bo.getIsLine());
        lqw.eq(bo.getArmingState() != null, EVidiconInfo::getArmingState, bo.getArmingState());
        lqw.eq(StringUtils.isNotBlank(bo.getShareState()), EVidiconInfo::getShareState, bo.getShareState());
        lqw.eq(StringUtils.isNotBlank(bo.getCoverAddress()), EVidiconInfo::getCoverAddress, bo.getCoverAddress());
        lqw.eq(bo.getIsEncrypt() != null, EVidiconInfo::getIsEncrypt, bo.getIsEncrypt());
        lqw.eq(StringUtils.isNotBlank(bo.getRelevancyElevator()), EVidiconInfo::getRelevancyElevator, bo.getRelevancyElevator());
        lqw.eq(StringUtils.isNotBlank(bo.getLiveAddress()), EVidiconInfo::getLiveAddress, bo.getLiveAddress());
        lqw.eq(StringUtils.isNotBlank(bo.getSecurityCode()), EVidiconInfo::getSecurityCode, bo.getSecurityCode());
        lqw.eq(StringUtils.isNotBlank(bo.getWhiteList()), EVidiconInfo::getWhiteList, bo.getWhiteList());
        lqw.eq(bo.getDetectionType() != null, EVidiconInfo::getDetectionType, bo.getDetectionType());
        lqw.eq(bo.getVoiceBroadcast() != null, EVidiconInfo::getVoiceBroadcast, bo.getVoiceBroadcast());
        lqw.eq(bo.getPlatformType() != null, EVidiconInfo::getPlatformType, bo.getPlatformType());
        lqw.eq(bo.getVideoType() != null, EVidiconInfo::getVideoType, bo.getVideoType());

        lqw.and(ObjectUtil.isNotNull(bo.getDeptId()), w -> {
            List<SysDept> deptList = deptMapper.selectList(new LambdaQueryWrapper<SysDept>()
                .select(SysDept::getDeptId)
                .apply(DataBaseHelper.findInSet(bo.getDeptId(), "ancestors")));
            List<Long> ids = StreamUtils.toList(deptList, SysDept::getDeptId);
            ids.add(bo.getDeptId());
            w.in(EVidiconInfo::getDeptId, ids);
        }).orderByDesc(EVidiconInfo::getCreateTime);

        return lqw;
    }

    /**
     * 新增网络摄像机信息;
     *
     * @param bo 网络摄像机信息;
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(EVidiconInfoBo bo) {
        EVidiconInfo add = MapstructUtils.convert(bo, EVidiconInfo.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改网络摄像机信息;
     *
     * @param bo 网络摄像机信息;
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(EVidiconInfoBo bo) {
        EVidiconInfo update = MapstructUtils.convert(bo, EVidiconInfo.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

    /**
     * 校验并批量删除网络摄像机信息;信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
