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.ArStepMedia;
import org.dromara.inspection.domain.bo.ArStepMediaBo;
import org.dromara.inspection.domain.vo.ArStepMediaVo;
import org.dromara.inspection.mapper.ArStepMediaMapper;
import org.dromara.inspection.service.IArStepMediaService;
import org.springframework.stereotype.Service;

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

/**
 * 步骤媒体文件Service业务层处理
 *
 * @author Lion Li
 * @date 2025-01-13
 */
@RequiredArgsConstructor
@Service
public class ArStepMediaServiceImpl implements IArStepMediaService {

    private final ArStepMediaMapper baseMapper;

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

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

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

    private LambdaQueryWrapper<ArStepMedia> buildQueryWrapper(ArStepMediaBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ArStepMedia> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getStepRecordId() != null, ArStepMedia::getStepRecordId, bo.getStepRecordId());
        lqw.eq(StringUtils.isNotBlank(bo.getMediaType()), ArStepMedia::getMediaType, bo.getMediaType());
        lqw.like(StringUtils.isNotBlank(bo.getFileName()), ArStepMedia::getFileName, bo.getFileName());
        lqw.between(params.get("beginUploadTime") != null && params.get("endUploadTime") != null,
            ArStepMedia::getUploadTime, params.get("beginUploadTime"), params.get("endUploadTime"));
        lqw.orderByDesc(ArStepMedia::getUploadTime);
        return lqw;
    }

    @Override
    public Boolean insertByBo(ArStepMediaBo bo) {
        ArStepMedia add = MapstructUtils.convert(bo, ArStepMedia.class);
        validEntityBeforeSave(add);
        // 自动设置上传时间
        if (add.getUploadTime() == null) {
            add.setUploadTime(new Date());
        }
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

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

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(ArStepMedia entity) {
        // TODO: 业务校验
    }

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