package com.ktg.iscs.service.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ktg.common.utils.StringUtils;
import com.ktg.iscs.domain.IsMaterialsProperty;
import com.ktg.iscs.domain.IsMaterialsPropertyValue;
import com.ktg.iscs.mapper.IsMaterialsPropertyMapper;
import com.ktg.iscs.service.IIsMaterialsPropertyService;
import com.ktg.iscs.service.IIsMaterialsPropertyValueService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * 物资属性项Service业务层处理
 *
 * @author cgj
 * @date 2025-02-05
 */
@Service
public class IsMaterialsPropertyServiceImpl extends ServiceImpl<IsMaterialsPropertyMapper, IsMaterialsProperty> implements IIsMaterialsPropertyService {

    @Autowired
    private IsMaterialsPropertyMapper isMaterialsPropertyMapper;
    @Autowired
    private IIsMaterialsPropertyValueService iIsMaterialsPropertyValueService;

    @Override
    public Page<IsMaterialsProperty> getIsMaterialsPropertyPage(Page<IsMaterialsProperty> page, IsMaterialsProperty isMaterialsProperty) {
        Page<IsMaterialsProperty> result = page(page, Wrappers.<IsMaterialsProperty>lambdaQuery()
                .like(StringUtils.isNotBlank(isMaterialsProperty.getPropertyName()), IsMaterialsProperty::getPropertyName, isMaterialsProperty.getPropertyName())
                .eq(isMaterialsProperty.getPropertyId() != null, IsMaterialsProperty::getPropertyId, isMaterialsProperty.getPropertyId())
                .le(StringUtils.isNotBlank(isMaterialsProperty.getStartTime()), IsMaterialsProperty::getCreateTime, isMaterialsProperty.getStartTime())
                .ge(StringUtils.isNotBlank(isMaterialsProperty.getEndTime()), IsMaterialsProperty::getCreateTime, isMaterialsProperty.getEndTime())
                .orderByDesc(IsMaterialsProperty::getPropertyId));
        return result;
    }

    @Override
    public Boolean deleteIsMaterialsPropertyByPropertyIds(String propertyIds) {
        Assert.notBlank(propertyIds, "请选择需要删除的数据!");
        Long[] longIds = Convert.toLongArray(propertyIds);
        // 检查下面有没有属性值，有的话先删属性值
        List<IsMaterialsPropertyValue> list = iIsMaterialsPropertyValueService.list(Wrappers.<IsMaterialsPropertyValue>lambdaQuery()
                .in(IsMaterialsPropertyValue::getPropertyId, longIds));
        Assert.isTrue(list.isEmpty(), "删除属性项下存在属性值，请先删除属性值!");
        removeBatchByIds(Arrays.asList(longIds));
        return true;
    }

    @Override
    public Boolean insertIsMaterialsProperty(IsMaterialsProperty isMaterialsProperty) {
        List<IsMaterialsProperty> list = list(Wrappers.<IsMaterialsProperty>lambdaQuery()
                .eq(IsMaterialsProperty::getPropertyName, isMaterialsProperty.getPropertyName()));
        Assert.isTrue(list.isEmpty(), "该名称已被使用!");
        return save(isMaterialsProperty);
    }

    @Override
    public Boolean updateIsMaterialsProperty(IsMaterialsProperty isMaterialsProperty) {
        List<IsMaterialsProperty> list = list(Wrappers.<IsMaterialsProperty>lambdaQuery()
                .eq(IsMaterialsProperty::getPropertyName, isMaterialsProperty.getPropertyName())
                .ne(IsMaterialsProperty::getPropertyId, isMaterialsProperty.getPropertyId()));
        Assert.isTrue(list.isEmpty(), "该名称已被使用!");
        return updateById(isMaterialsProperty);
    }

}
