package com.ruoyi.zbf.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.mybatisplus.core.ServicePlusImpl;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.zbf.bo.ZbfProjectSectionQueryBo;
import com.ruoyi.zbf.domain.ZbfProjectSection;
import com.ruoyi.zbf.mapper.ZbfProjectSectionMapper;
import com.ruoyi.zbf.service.IZbfProjectSectionService;
import org.springframework.stereotype.Service;

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

/**
 * 总包方项目标段Service业务层处理
 *
 * @author ruoyi
 * @date 2025-02-17
 */
@Service
public class ZbfProjectSectionServiceImpl extends ServicePlusImpl<ZbfProjectSectionMapper, ZbfProjectSection> implements IZbfProjectSectionService {

    @Override
    public ZbfProjectSection queryById(Long id){
        return getById(id);
    }

    @Override
    public TableDataInfo<ZbfProjectSection> queryPageList(ZbfProjectSectionQueryBo bo) {
        Page<ZbfProjectSection> result = page(PageUtils.buildPage(), buildQueryWrapper(bo));
        return PageUtils.buildDataInfo(result);
    }

    @Override
    public List<ZbfProjectSection> queryList(ZbfProjectSectionQueryBo bo) {
        return list(buildQueryWrapper(bo));
    }

    private LambdaQueryWrapper<ZbfProjectSection> buildQueryWrapper(ZbfProjectSectionQueryBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ZbfProjectSection> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getProjectId() != null, ZbfProjectSection::getProjectId, bo.getProjectId());
        lqw.like(StrUtil.isNotBlank(bo.getSectionName()), ZbfProjectSection::getSectionName, bo.getSectionName());
        lqw.eq(StrUtil.isNotBlank(bo.getSectionDescribe()), ZbfProjectSection::getSectionDescribe, bo.getSectionDescribe());
        return lqw;
    }

    @Override
    public Boolean insert(ZbfProjectSection bo) {
        ZbfProjectSection add = BeanUtil.toBean(bo, ZbfProjectSection.class);
        validEntityBeforeSave(add);
        return save(add);
    }

    @Override
    public Boolean update(ZbfProjectSection bo) {
        ZbfProjectSection update = BeanUtil.toBean(bo, ZbfProjectSection.class);
        validEntityBeforeSave(update);
        return updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(ZbfProjectSection entity){
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return removeByIds(ids);
    }
}
