package com.ruoyi.zdsz.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.common.helper.LoginHelper;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.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 com.ruoyi.zdsz.domain.ZEngineeringMaterialsResets;
import com.ruoyi.zdsz.domain.bo.ZBuildingBo;
import com.ruoyi.zdsz.domain.vo.ZBuildingVo;
import com.ruoyi.zdsz.mapper.ZEngineeringMaterialsResetsMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.zdsz.domain.bo.ZEnginSpecificationsBo;
import com.ruoyi.zdsz.domain.vo.ZEnginSpecificationsVo;
import com.ruoyi.zdsz.domain.ZEnginSpecifications;
import com.ruoyi.zdsz.mapper.ZEnginSpecificationsMapper;
import com.ruoyi.zdsz.service.IZEnginSpecificationsService;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Collection;
import java.util.stream.Collectors;

/**
 * 工程材质规格Service业务层处理
 *
 * @author ruoyi
 * @date 2023-12-29
 */
@RequiredArgsConstructor
@Service
public class ZEnginSpecificationsServiceImpl implements IZEnginSpecificationsService {

    private final ZEnginSpecificationsMapper baseMapper;
    @Resource
    private ZEngineeringMaterialsResetsMapper materialsResetsMapper;

    /**
     * 查询工程材质规格
     */
    @Override
    public ZEnginSpecificationsVo queryById(String id){
        return baseMapper.getById(id);
    }

    /**
     * 查询工程材质规格列表
     */
    @Override
    public TableDataInfo<ZEnginSpecificationsVo> queryPageList(ZEnginSpecificationsBo bo, PageQuery pageQuery) {
        Page<ZEnginSpecificationsBo> page = new Page<>(pageQuery.getPageNum(), pageQuery.getPageSize());
        Page<ZEnginSpecificationsVo> result = baseMapper.getPageList(bo,page);
        return TableDataInfo.build(result);
    }

    /**
     * 查询工程材质规格列表
     */
    @Override
    public List<ZEnginSpecificationsVo> queryList(ZEnginSpecificationsBo bo) {
        LambdaQueryWrapper<ZEnginSpecifications> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ZEnginSpecifications> buildQueryWrapper(ZEnginSpecificationsBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ZEnginSpecifications> lqw = Wrappers.lambdaQuery();
        lqw.orderByDesc(ZEnginSpecifications::getCreateTime);
        lqw.eq(StringUtils.isNotBlank(bo.getMaterId()), ZEnginSpecifications::getMaterId, bo.getMaterId());
        lqw.like(StringUtils.isNotBlank(bo.getName()), ZEnginSpecifications::getName, bo.getName());
        return lqw;
    }

    /**
     * 新增工程材质规格
     */
    @Override
    public Boolean insertByBo(ZEnginSpecificationsBo bo) {
        ZEnginSpecifications add = BeanUtil.toBean(bo, ZEnginSpecifications.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改工程材质规格
     */
    @Override
    public Boolean updateByBo(ZEnginSpecificationsBo bo) {
        ZEnginSpecifications update = BeanUtil.toBean(bo, ZEnginSpecifications.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

    /**
     * 批量删除工程材质规格
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if (isValid) {
            if (!ObjectUtil.isEmpty(ids)) {
                    LambdaQueryWrapper<ZEngineeringMaterialsResets> lqw1 = Wrappers.lambdaQuery();
                    Long count = materialsResetsMapper.selectCount(lqw1.in(ZEngineeringMaterialsResets::getEstimatedSpecifications,ids));
                    if (count > 0) {
                        return false;
                    }
            }
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public List<ZEnginSpecificationsVo> getEnginSpecifications(String materId) {
        return baseMapper.getEnginSpecifications(materId);
    }
}
