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.ArRegion;
import org.dromara.inspection.domain.bo.ArRegionBo;
import org.dromara.inspection.domain.vo.ArRegionVo;
import org.dromara.inspection.mapper.ArRegionMapper;
import org.dromara.inspection.service.IArRegionService;
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 ArRegionServiceImpl implements IArRegionService {

    private final ArRegionMapper baseMapper;

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

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

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

    private LambdaQueryWrapper<ArRegion> buildQueryWrapper(ArRegionBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ArRegion> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getRegionName()), ArRegion::getRegionName, bo.getRegionName());
        lqw.eq(StringUtils.isNotBlank(bo.getRegionCode()), ArRegion::getRegionCode, bo.getRegionCode());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), ArRegion::getStatus, bo.getStatus());
        lqw.between(params.get("beginCreateTime") != null && params.get("endCreateTime") != null,
            ArRegion::getCreateTime, params.get("beginCreateTime"), params.get("endCreateTime"));
        lqw.orderByDesc(ArRegion::getCreateTime);
        return lqw;
    }

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

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

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(ArRegion entity) {
        // 校验区域编码唯一性
        if (StringUtils.isNotBlank(entity.getRegionCode())) {
            LambdaQueryWrapper<ArRegion> lqw = Wrappers.lambdaQuery();
            lqw.eq(ArRegion::getRegionCode, entity.getRegionCode());
            if (entity.getId() != null) {
                lqw.ne(ArRegion::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<ArRegion> list = baseMapper.selectByIds(ids);
            if (list.size() != ids.size()) {
                throw new ServiceException("您没有删除权限!");
            }
        }
        return baseMapper.deleteByIds(ids) > 0;
    }
}
