package com.ruoyi.zdsz.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.common.core.domain.model.LoginUser;
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.ZEnginSpecifications;
import com.ruoyi.zdsz.domain.ZEngineeringMaterialsResets;
import com.ruoyi.zdsz.domain.bo.ZEnginSpecificationsBo;
import com.ruoyi.zdsz.domain.vo.ZEnginSpecificationsVo;
import com.ruoyi.zdsz.mapper.ZEnginSpecificationsMapper;
import com.ruoyi.zdsz.service.IZEnginSpecificationsService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.zdsz.domain.bo.ZEnginMaterialQualityBo;
import com.ruoyi.zdsz.domain.vo.ZEnginMaterialQualityVo;
import com.ruoyi.zdsz.domain.ZEnginMaterialQuality;
import com.ruoyi.zdsz.mapper.ZEnginMaterialQualityMapper;
import com.ruoyi.zdsz.service.IZEnginMaterialQualityService;

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

/**
 * 工程材质Service业务层处理
 *
 * @author ruoyi
 * @date 2023-12-29
 */
@RequiredArgsConstructor
@Service
public class ZEnginMaterialQualityServiceImpl implements IZEnginMaterialQualityService {
    @Resource
    private ZEnginSpecificationsMapper specificationsMapper;
    private final ZEnginMaterialQualityMapper baseMapper;

    /**
     * 查询工程材质
     */
    @Override
    public ZEnginMaterialQualityVo queryById(String id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询工程材质列表
     */
    @Override
    public TableDataInfo<ZEnginMaterialQualityVo> queryPageList(ZEnginMaterialQualityBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ZEnginMaterialQuality> lqw = buildQueryWrapper(bo);
        Page<ZEnginMaterialQualityVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    @Autowired
    private IZEnginSpecificationsService service;

    /**
     * 查询工程材质列表
     */
    @Override
    public List<ZEnginMaterialQualityVo> queryList(ZEnginMaterialQualityBo bo) {
        LambdaQueryWrapper<ZEnginMaterialQuality> lqw = buildQueryWrapper(bo);
        List<ZEnginMaterialQualityVo> vos = baseMapper.selectVoList(lqw);
        vos.forEach(item -> {
            ZEnginSpecificationsBo zEnginSpecificationsBo = new ZEnginSpecificationsBo();
            zEnginSpecificationsBo.setMaterId(item.getId());
            List<ZEnginSpecificationsVo> zEnginSpecificationsVos = service.queryList(zEnginSpecificationsBo);
            StringBuilder it = new StringBuilder();
            for (int i = 0; i < zEnginSpecificationsVos.size(); i++) {
                if (i == 0)
                    it = new StringBuilder(zEnginSpecificationsVos.get(i).getName());
                else
                    it.append(",").append(zEnginSpecificationsVos.get(i).getName());
            }
            item.setSpecificationsName(it.toString());
        });
        return vos;
    }

    private LambdaQueryWrapper<ZEnginMaterialQuality> buildQueryWrapper(ZEnginMaterialQualityBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ZEnginMaterialQuality> lqw = Wrappers.lambdaQuery();
        lqw.orderByDesc(ZEnginMaterialQuality::getCreateTime);
        lqw.like(StringUtils.isNotBlank(bo.getEnginType()), ZEnginMaterialQuality::getEnginType, bo.getEnginType());
        lqw.like(StringUtils.isNotBlank(bo.getName()), ZEnginMaterialQuality::getName, bo.getName());
        lqw.eq(StringUtils.isNotBlank(bo.getEnginSort()), ZEnginMaterialQuality::getEnginSort, bo.getEnginSort());
        lqw.eq(StringUtils.isNotBlank(bo.getEnginNode()), ZEnginMaterialQuality::getEnginNode, bo.getEnginNode());
        lqw.eq(StringUtils.isNotBlank(bo.getEnginStep()), ZEnginMaterialQuality::getEnginStep, bo.getEnginStep());
        lqw.eq(StringUtils.isNotBlank(bo.getNameType()), ZEnginMaterialQuality::getNameType, bo.getNameType());
        lqw.eq(StringUtils.isNotBlank(bo.getMaterialType()), ZEnginMaterialQuality::getMaterialType, bo.getMaterialType());
        return lqw;
    }

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

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

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

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

    @Override
    public List<ZEnginMaterialQualityVo> getEnginMaterialQualityList(ZEnginMaterialQualityBo enginType) {
        return baseMapper.getEnginMaterialQualityList(enginType);
    }
}
