package com.ruoyi.zhdd.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.common.core.page.PagePlus;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.stereotype.Service;
import com.ruoyi.common.core.mybatisplus.core.ServicePlusImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.zhdd.domain.bo.ResourceDetailBo;
import com.ruoyi.zhdd.domain.vo.ResourceDetailVo;
import com.ruoyi.zhdd.domain.ResourceDetail;
import com.ruoyi.zhdd.mapper.ResourceDetailMapper;
import com.ruoyi.zhdd.service.IResourceDetailService;

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

/**
 * 应急资源明细Service业务层处理
 *
 * @author xintong
 * @date 2021-10-27
 */
@Service
public class ResourceDetailServiceImpl extends ServicePlusImpl<ResourceDetailMapper, ResourceDetail, ResourceDetailVo> implements IResourceDetailService {

    @Override
    public ResourceDetailVo queryById(String id){
        return getVoById(id);
    }

    @Override
    public TableDataInfo<ResourceDetailVo> queryPageList(ResourceDetailBo bo) {
        PagePlus<ResourceDetail, ResourceDetailVo> result = pageVo(PageUtils.buildPagePlus(), buildQueryWrapper(bo));
        return PageUtils.buildDataInfo(result);
    }

    @Override
    public List<ResourceDetailVo> queryList(ResourceDetailBo bo) {
        return listVo(buildQueryWrapper(bo));
    }

    private LambdaQueryWrapper<ResourceDetail> buildQueryWrapper(ResourceDetailBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ResourceDetail> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getResourceId() != null, ResourceDetail::getResourceId, bo.getResourceId());
        lqw.like(StringUtils.isNotBlank(bo.getName()), ResourceDetail::getName, bo.getName());
        lqw.eq(StringUtils.isNotBlank(bo.getModel()), ResourceDetail::getModel, bo.getModel());
        lqw.eq(StringUtils.isNotBlank(bo.getSize()), ResourceDetail::getSize, bo.getSize());
        lqw.eq(bo.getNum() != null, ResourceDetail::getNum, bo.getNum());
        lqw.eq(StringUtils.isNotBlank(bo.getUnit()), ResourceDetail::getUnit, bo.getUnit());
        return lqw;
    }

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

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

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

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