package net.ferrum.business.service.impl;

import cn.hutool.core.bean.BeanUtil;
import net.ferrum.common.utils.StringUtils;
import net.ferrum.common.core.page.TableDataInfo;
import net.ferrum.common.core.domain.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.springframework.stereotype.Service;
import net.ferrum.business.domain.bo.QsProjectBo;
import net.ferrum.business.domain.vo.QsProjectVo;
import net.ferrum.business.domain.QsProject;
import net.ferrum.business.mapper.QsProjectMapper;
import net.ferrum.business.service.IQsProjectService;

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

/**
 * 模板信息Service业务层处理
 *
 * @author 26554
 * @date 2025-04-21
 */
@RequiredArgsConstructor
@Service
public class QsProjectServiceImpl implements IQsProjectService {

    private final QsProjectMapper baseMapper;

    /**
     * 查询模板信息
     */
    @Override
    public QsProjectVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询模板信息列表
     */
    @Override
    public TableDataInfo<QsProjectVo> queryPageList(QsProjectBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<QsProject> lqw = buildQueryWrapper(bo);
        Page<QsProjectVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询模板信息列表
     */
    @Override
    public List<QsProjectVo> queryList(QsProjectBo bo) {
        LambdaQueryWrapper<QsProject> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<QsProject> buildQueryWrapper(QsProjectBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<QsProject> lqw = Wrappers.lambdaQuery();
        lqw.like(bo.getDockerUserId() != null, QsProject::getDockerUserId, bo.getDockerUserId());
        lqw.eq(StringUtils.isNotBlank(bo.getAdoptFlag()), QsProject::getAdoptFlag, bo.getAdoptFlag());
        lqw.between(params.get("beginDesignAmount") != null && params.get("endDesignAmount") != null,
            QsProject::getDesignAmount ,params.get("beginDesignAmount"), params.get("endDesignAmount"));
        return lqw;
    }

    /**
     * 新增模板信息
     */
    @Override
    public Boolean insertByBo(QsProjectBo bo) {
        QsProject add = BeanUtil.toBean(bo, QsProject.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改模板信息
     */
    @Override
    public Boolean updateByBo(QsProjectBo bo) {
        QsProject update = BeanUtil.toBean(bo, QsProject.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

    /**
     * 批量删除模板信息
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
