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.ZbfProjectQueryBo;
import com.ruoyi.zbf.domain.ZbfProject;
import com.ruoyi.zbf.mapper.ZbfProjectMapper;
import com.ruoyi.zbf.service.IZbfProjectService;
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 ZbfProjectServiceImpl extends ServicePlusImpl<ZbfProjectMapper, ZbfProject> implements IZbfProjectService {

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

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

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

    private LambdaQueryWrapper<ZbfProject> buildQueryWrapper(ZbfProjectQueryBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ZbfProject> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getUserId() != null, ZbfProject::getUserId, bo.getUserId());
        lqw.like(StrUtil.isNotBlank(bo.getUnitName()), ZbfProject::getUnitName, bo.getUnitName());
        lqw.eq(StrUtil.isNotBlank(bo.getCreditCode()), ZbfProject::getCreditCode, bo.getCreditCode());
        lqw.like(StrUtil.isNotBlank(bo.getProjectName()), ZbfProject::getProjectName, bo.getProjectName());
        lqw.eq(StrUtil.isNotBlank(bo.getProjectImg()), ZbfProject::getProjectImg, bo.getProjectImg());
        lqw.eq(StrUtil.isNotBlank(bo.getProjectAddress()), ZbfProject::getProjectAddress, bo.getProjectAddress());
        lqw.eq(StrUtil.isNotBlank(bo.getContactPerson()), ZbfProject::getContactPerson, bo.getContactPerson());
        lqw.eq(StrUtil.isNotBlank(bo.getContactPhone()), ZbfProject::getContactPhone, bo.getContactPhone());
        return lqw;
    }

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

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

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

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