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.ArTask;
import org.dromara.inspection.domain.bo.ArTaskBo;
import org.dromara.inspection.domain.vo.ArTaskVo;
import org.dromara.inspection.mapper.ArTaskMapper;
import org.dromara.inspection.service.IArTaskService;
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 ArTaskServiceImpl implements IArTaskService {

    private final ArTaskMapper baseMapper;

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

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

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

    private LambdaQueryWrapper<ArTask> buildQueryWrapper(ArTaskBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ArTask> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getTaskName()), ArTask::getTaskName, bo.getTaskName());
        lqw.eq(StringUtils.isNotBlank(bo.getTaskCode()), ArTask::getTaskCode, bo.getTaskCode());
        lqw.eq(bo.getRegionId() != null, ArTask::getRegionId, bo.getRegionId());
        lqw.eq(StringUtils.isNotBlank(bo.getTaskType()), ArTask::getTaskType, bo.getTaskType());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), ArTask::getStatus, bo.getStatus());
        lqw.between(params.get("beginCreateTime") != null && params.get("endCreateTime") != null,
            ArTask::getCreateTime, params.get("beginCreateTime"), params.get("endCreateTime"));
        lqw.orderByDesc(ArTask::getCreateTime);
        return lqw;
    }

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

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

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(ArTask entity) {
        // 校验任务编码唯一性
        if (StringUtils.isNotBlank(entity.getTaskCode())) {
            LambdaQueryWrapper<ArTask> lqw = Wrappers.lambdaQuery();
            lqw.eq(ArTask::getTaskCode, entity.getTaskCode());
            if (entity.getId() != null) {
                lqw.ne(ArTask::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<ArTask> list = baseMapper.selectByIds(ids);
            if (list.size() != ids.size()) {
                throw new ServiceException("您没有删除权限!");
            }
            // TODO: 校验是否有关联的步骤，有则不允许删除
        }
        return baseMapper.deleteByIds(ids) > 0;
    }
}
