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.ZbfProjectSubcontractingQueryBo;
import com.ruoyi.zbf.domain.ZbfProjectSubcontracting;
import com.ruoyi.zbf.mapper.ZbfProjectSubcontractingMapper;
import com.ruoyi.zbf.service.IZbfProjectSubcontractingService;
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 ZbfProjectSubcontractingServiceImpl extends ServicePlusImpl<ZbfProjectSubcontractingMapper, ZbfProjectSubcontracting> implements IZbfProjectSubcontractingService {

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

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

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

    private LambdaQueryWrapper<ZbfProjectSubcontracting> buildQueryWrapper(ZbfProjectSubcontractingQueryBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ZbfProjectSubcontracting> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getProjectId() != null, ZbfProjectSubcontracting::getProjectId, bo.getProjectId());
        lqw.eq(bo.getSectionId() != null, ZbfProjectSubcontracting::getSectionId, bo.getSectionId());
        lqw.like(StrUtil.isNotBlank(bo.getSubName()), ZbfProjectSubcontracting::getSubName, bo.getSubName());
        lqw.eq(StrUtil.isNotBlank(bo.getSubDescribe()), ZbfProjectSubcontracting::getSubDescribe, bo.getSubDescribe());
        lqw.eq(bo.getSubAmount() != null, ZbfProjectSubcontracting::getSubAmount, bo.getSubAmount());
        lqw.eq(StrUtil.isNotBlank(bo.getQualification()), ZbfProjectSubcontracting::getQualification, bo.getQualification());
        lqw.eq(bo.getUserId() != null, ZbfProjectSubcontracting::getUserId, bo.getUserId());
        return lqw;
    }

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

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

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

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