package org.dromara.inspection.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.exception.ServiceException;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.inspection.domain.ArPoint;
import org.dromara.inspection.domain.bo.ArPointBo;
import org.dromara.inspection.domain.vo.ArPointVo;
import org.dromara.inspection.mapper.ArPointMapper;
import org.dromara.inspection.service.IArPointService;
import org.springframework.stereotype.Service;

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

/**
 * 巡检点位Service业务层处理
 *
 * @author Lion Li
 * @date 2025-01-13
 */
@RequiredArgsConstructor
@Service
public class ArPointServiceImpl implements IArPointService {

    private final ArPointMapper baseMapper;

    @Override
    public ArPointVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }

    @Override
    public TableDataInfo<ArPointVo> queryPageList(ArPointBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ArPoint> lqw = buildQueryWrapper(bo);
        Page<ArPointVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    @Override
    public List<ArPointVo> queryList(ArPointBo bo) {
        return baseMapper.selectVoList(buildQueryWrapper(bo));
    }

    private LambdaQueryWrapper<ArPoint> buildQueryWrapper(ArPointBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ArPoint> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getRegionId() != null, ArPoint::getRegionId, bo.getRegionId());
        lqw.like(StringUtils.isNotBlank(bo.getPointName()), ArPoint::getPointName, bo.getPointName());
        lqw.eq(StringUtils.isNotBlank(bo.getPointCode()), ArPoint::getPointCode, bo.getPointCode());
        lqw.between(params.get("beginCreateTime") != null && params.get("endCreateTime") != null,
            ArPoint::getCreateTime, params.get("beginCreateTime"), params.get("endCreateTime"));
        lqw.orderByDesc(ArPoint::getCreateTime);
        return lqw;
    }

    @Override
    public Boolean insertByBo(ArPointBo bo) {
        ArPoint add = MapstructUtils.convert(bo, ArPoint.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    @Override
    public Boolean updateByBo(ArPointBo bo) {
        ArPoint update = MapstructUtils.convert(bo, ArPoint.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(ArPoint entity) {
        // 校验点位编码唯一性（同一区域内）
        if (StringUtils.isNotBlank(entity.getPointCode())) {
            LambdaQueryWrapper<ArPoint> lqw = Wrappers.lambdaQuery();
            lqw.eq(ArPoint::getRegionId, entity.getRegionId());
            lqw.eq(ArPoint::getPointCode, entity.getPointCode());
            if (entity.getId() != null) {
                lqw.ne(ArPoint::getId, entity.getId());
            }
            long count = baseMapper.selectCount(lqw);
            if (count > 0) {
                throw new ServiceException("该区域内点位编码已存在!");
            }
        }
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            // 校验是否有权限删除
            List<ArPoint> list = baseMapper.selectByIds(ids);
            if (list.size() != ids.size()) {
                throw new ServiceException("您没有删除权限!");
            }
        }
        return baseMapper.deleteByIds(ids) > 0;
    }
}
