package com.bonus.canteen.core.supermarket.service.impl;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import com.bonus.canteen.core.supermarket.mapper.SupermarketMaterialMapper;
import com.bonus.common.core.exception.ServiceException;
import com.bonus.common.core.utils.DateUtils;
import com.bonus.common.security.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.bonus.canteen.core.supermarket.mapper.SupermarketMaterialTypeMapper;
import com.bonus.canteen.core.supermarket.domain.SupermarketMaterialType;
import com.bonus.canteen.core.supermarket.service.ISupermarketMaterialTypeService;

/**
 * 商品类别Service业务层处理
 * 
 * @author xsheng
 * @date 2025-05-25
 */
@Service
public class SupermarketMaterialTypeServiceImpl implements ISupermarketMaterialTypeService {
    @Autowired
    private SupermarketMaterialTypeMapper supermarketMaterialTypeMapper;
    @Autowired
    private SupermarketMaterialMapper supermarketMaterialMapper;

    /**
     * 查询商品类别
     * 
     * @param materialTypeId 商品类别主键
     * @return 商品类别
     */
    @Override
    public SupermarketMaterialType selectSupermarketMaterialTypeByMaterialTypeId(Long materialTypeId) {
        return supermarketMaterialTypeMapper.selectSupermarketMaterialTypeByMaterialTypeId(materialTypeId);
    }

    /**
     * 查询商品类别列表
     * 
     * @param supermarketMaterialType 商品类别
     * @return 商品类别
     */
    @Override
    public List<SupermarketMaterialType> selectSupermarketMaterialTypeList(SupermarketMaterialType supermarketMaterialType) {
        return supermarketMaterialTypeMapper.selectSupermarketMaterialTypeList(supermarketMaterialType);
    }

    /**
     * 新增商品类别
     * 
     * @param supermarketMaterialType 商品类别
     * @return 结果
     */
    @Override
    public int insertSupermarketMaterialType(SupermarketMaterialType supermarketMaterialType) {
        supermarketMaterialType.setCreateTime(DateUtils.getNowDate());
        supermarketMaterialType.setCreateBy(SecurityUtils.getUsername());
        try {
            SupermarketMaterialType checkResult = supermarketMaterialTypeMapper.selectSupermarketMaterialTypeByMaterialTypeName(supermarketMaterialType);
            if (Objects.nonNull(checkResult)) {
                throw new ServiceException("该超市商品类别名已存在");
            }
            return supermarketMaterialTypeMapper.insertSupermarketMaterialType(supermarketMaterialType);
        } catch (Exception e) {
            throw new ServiceException(e.getMessage());
        }
    }

    /**
     * 修改商品类别
     * 
     * @param supermarketMaterialType 商品类别
     * @return 结果
     */
    @Override
    public int updateSupermarketMaterialType(SupermarketMaterialType supermarketMaterialType) {
        supermarketMaterialType.setUpdateTime(DateUtils.getNowDate());
        supermarketMaterialType.setUpdateBy(SecurityUtils.getUsername());
        try {
            List<SupermarketMaterialType> allSupermarketMaterialTypeList = supermarketMaterialTypeMapper.selectSupermarketMaterialTypeList(new SupermarketMaterialType());
            List<String> otherSupermarketMaterialTypeList = allSupermarketMaterialTypeList.stream().filter(item -> !item.getMaterialTypeId().equals(supermarketMaterialType.getMaterialTypeId()))
                    .map(SupermarketMaterialType::getMaterialTypeName).collect(Collectors.toList());
            if (otherSupermarketMaterialTypeList.contains(supermarketMaterialType.getMaterialTypeName())) {
                throw new ServiceException("该超市商品类别名已存在");
            }
            return supermarketMaterialTypeMapper.updateSupermarketMaterialType(supermarketMaterialType);
        } catch (Exception e) {
            throw new ServiceException(e.getMessage());
        }
    }

    /**
     * 批量删除商品类别
     * 
     * @param materialTypeIds 需要删除的商品类别主键
     * @return 结果
     */
    @Override
    public int deleteSupermarketMaterialTypeByMaterialTypeIds(Long[] materialTypeIds) {
        int count1 = supermarketMaterialMapper.getSupermarketMaterialCountByMaterialTypes(materialTypeIds);
        if (count1 > 0) {
            throw new ServiceException("该超市商品类型含有商品信息，不能删除");
        }
        int count2 = supermarketMaterialTypeMapper.getSupermarketMaterialTypeChildCountByMaterialTypes(materialTypeIds);
        if (count2 > 0) {
            throw new ServiceException("该超市商品类型含有子类型，不能删除");
        }
        return supermarketMaterialTypeMapper.deleteSupermarketMaterialTypeByMaterialTypeIds(materialTypeIds);
    }

    /**
     * 删除商品类别信息
     * 
     * @param materialTypeId 商品类别主键
     * @return 结果
     */
    @Override
    public int deleteSupermarketMaterialTypeByMaterialTypeId(Long materialTypeId) {
        return supermarketMaterialTypeMapper.deleteSupermarketMaterialTypeByMaterialTypeId(materialTypeId);
    }
}
