package org.dromara.elevator.service.impl;

import cn.hutool.core.util.ObjectUtil;
import org.apache.commons.lang3.StringUtils;
import org.dromara.common.core.exception.ServiceException;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StreamUtils;
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.elevator.domain.EElevatorInfo;
import org.dromara.system.domain.SysDept;
import org.dromara.system.mapper.SysDeptMapper;
import org.springframework.stereotype.Service;
import org.dromara.elevator.domain.bo.EElevatorRelevancyDeviceBo;
import org.dromara.elevator.domain.vo.EElevatorRelevancyDeviceVo;
import org.dromara.elevator.domain.EElevatorRelevancyDevice;
import org.dromara.elevator.mapper.EElevatorRelevancyDeviceMapper;
import org.dromara.elevator.service.IEElevatorRelevancyDeviceService;

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

/**
 * 电梯设备关联;Service业务层处理
 *
 * @author zy
 * @date 2024-09-09
 */
@RequiredArgsConstructor
@Service
public class EElevatorRelevancyDeviceServiceImpl implements IEElevatorRelevancyDeviceService {

    private final EElevatorRelevancyDeviceMapper baseMapper;
    private final SysDeptMapper deptMapper;

    /**
     * 查询电梯设备关联;
     *
     * @param id 主键
     * @return 电梯设备关联;
     */
    @Override
    public EElevatorRelevancyDeviceVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询电梯设备关联;列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 电梯设备关联;分页列表
     */
    @Override
    public TableDataInfo<EElevatorRelevancyDeviceVo> queryPageList(EElevatorRelevancyDeviceBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<EElevatorRelevancyDevice> lqw = buildQueryWrapper(bo);
        Page<EElevatorRelevancyDeviceVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的电梯设备关联;列表
     *
     * @param bo 查询条件
     * @return 电梯设备关联;列表
     */
    @Override
    public List<EElevatorRelevancyDeviceVo> queryList(EElevatorRelevancyDeviceBo bo) {
        LambdaQueryWrapper<EElevatorRelevancyDevice> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<EElevatorRelevancyDevice> buildQueryWrapper(EElevatorRelevancyDeviceBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<EElevatorRelevancyDevice> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getRegisterCode()), EElevatorRelevancyDevice::getRegisterCode, bo.getRegisterCode());
        lqw.eq(StringUtils.isNotBlank(bo.getElevatorNum()), EElevatorRelevancyDevice::getElevatorNum, bo.getElevatorNum());
        lqw.like(StringUtils.isNotBlank(bo.getElevatorName()), EElevatorRelevancyDevice::getElevatorName, bo.getElevatorName());
        lqw.eq(StringUtils.isNotBlank(bo.getElevatorAddress()), EElevatorRelevancyDevice::getElevatorAddress, bo.getElevatorAddress());
        lqw.like(StringUtils.isNotBlank(bo.getTenementName()), EElevatorRelevancyDevice::getTenementName, bo.getTenementName());
        lqw.like(StringUtils.isNotBlank(bo.getMaintenanceName()), EElevatorRelevancyDevice::getMaintenanceName, bo.getMaintenanceName());
        lqw.eq(StringUtils.isNotBlank(bo.getDeviceCode()), EElevatorRelevancyDevice::getDeviceCode, bo.getDeviceCode());
        lqw.eq(bo.getFinallyTime() != null, EElevatorRelevancyDevice::getFinallyTime, bo.getFinallyTime());
        lqw.eq(StringUtils.isNotBlank(bo.getQrCode()), EElevatorRelevancyDevice::getQrCode, bo.getQrCode());
        lqw.eq(StringUtils.isNotBlank(bo.getBlackBoxCode()), EElevatorRelevancyDevice::getBlackBoxCode, bo.getBlackBoxCode());
        lqw.eq(StringUtils.isNotBlank(bo.getCameraCode()), EElevatorRelevancyDevice::getCameraCode, bo.getCameraCode());
        lqw.eq(StringUtils.isNotBlank(bo.getScreenCode()), EElevatorRelevancyDevice::getScreenCode, bo.getScreenCode());
        lqw.eq(bo.getIsBlackBox() != null, EElevatorRelevancyDevice::getIsBlackBox, bo.getIsBlackBox());
        lqw.eq(bo.getIsScreen() != null, EElevatorRelevancyDevice::getIsScreen, bo.getIsScreen());
        lqw.eq(bo.getIsCamera() != null, EElevatorRelevancyDevice::getIsCamera, bo.getIsCamera());
        lqw.eq(bo.getIsTimmy() != null, EElevatorRelevancyDevice::getIsTimmy, bo.getIsTimmy());
        lqw.eq(bo.getIsQrcode() != null, EElevatorRelevancyDevice::getIsQrcode, bo.getIsQrcode());
        lqw.eq(bo.getIsFloor() != null, EElevatorRelevancyDevice::getIsFloor, bo.getIsFloor());
        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(EElevatorRelevancyDevice::getDeptId, ids);
        }).orderByDesc(EElevatorRelevancyDevice::getCreateTime);

        return lqw;
    }

    /**
     * 新增电梯设备关联;
     *
     * @param bo 电梯设备关联;
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(EElevatorRelevancyDeviceBo bo) {
        EElevatorRelevancyDevice add = MapstructUtils.convert(bo, EElevatorRelevancyDevice.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改电梯设备关联;
     *
     * @param bo 电梯设备关联;
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(EElevatorRelevancyDeviceBo bo) {
        EElevatorRelevancyDevice update = MapstructUtils.convert(bo, EElevatorRelevancyDevice.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

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

    /**
     * 电梯设备关联
     * @param bo
     * @return
     */
    @Override
    public boolean relevancy(EElevatorRelevancyDeviceBo bo) {
        if (StringUtils.isBlank(bo.getElevatorNum())) {
            throw new ServiceException("电梯编号不能为空!");
        }
        LambdaQueryWrapper<EElevatorRelevancyDevice> query = new LambdaQueryWrapper<>();
        query.eq(EElevatorRelevancyDevice::getElevatorNum, bo.getElevatorNum());
        EElevatorRelevancyDevice device = baseMapper.selectOne(query);
        if (Objects.isNull(device)) {
            throw new ServiceException("暂未查询到此电梯信息!");
        }
        device.setQrCode(bo.getQrCode());
        device.setIsQrcode(StringUtils.isBlank(bo.getQrCode()) ? 0 : 1);
        device.setBlackBoxCode(bo.getBlackBoxCode());
        device.setIsBlackBox(StringUtils.isBlank(bo.getBlackBoxCode()) ? 0 : 1);
        device.setCameraCode(bo.getCameraCode());
        device.setIsCamera(StringUtils.isBlank(bo.getCameraCode()) ? 0 : 1);
        device.setScreenCode(bo.getScreenCode());
        device.setIsScreen(StringUtils.isBlank(bo.getScreenCode()) ? 0 : 1);

        return baseMapper.updateById(device) > 0;
    }
}
