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.basic.domain.BasicCanteen;
import com.bonus.canteen.core.supermarket.domain.SupermarketMaterial;
import com.bonus.canteen.core.supermarket.domain.SupermarketProduct;
import com.bonus.canteen.core.supermarket.mapper.SupermarketMaterialMapper;
import com.bonus.canteen.core.supermarket.mapper.SupermarketProductMapper;
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.SupermarketInfoMapper;
import com.bonus.canteen.core.supermarket.domain.SupermarketInfo;
import com.bonus.canteen.core.supermarket.service.ISupermarketInfoService;
import org.springframework.util.CollectionUtils;

/**
 * 超市Service业务层处理
 * 
 * @author xsheng
 * @date 2025-05-25
 */
@Service
public class SupermarketInfoServiceImpl implements ISupermarketInfoService {
    @Autowired
    private SupermarketInfoMapper supermarketInfoMapper;
    @Autowired
    private SupermarketProductMapper supermarketProductMapper;

    /**
     * 查询超市
     * 
     * @param supermarketId 超市主键
     * @return 超市
     */
    @Override
    public SupermarketInfo selectSupermarketInfoBySupermarketId(Long supermarketId) {
        return supermarketInfoMapper.selectSupermarketInfoBySupermarketId(supermarketId);
    }

    /**
     * 查询超市列表
     * 
     * @param supermarketInfo 超市
     * @return 超市
     */
    @Override
    public List<SupermarketInfo> selectSupermarketInfoList(SupermarketInfo supermarketInfo) {
        return supermarketInfoMapper.selectSupermarketInfoList(supermarketInfo);
    }

    /**
     * 新增超市
     * 
     * @param supermarketInfo 超市
     * @return 结果
     */
    @Override
    public int insertSupermarketInfo(SupermarketInfo supermarketInfo) {
        supermarketInfo.setCreateTime(DateUtils.getNowDate());
        supermarketInfo.setCreateBy(SecurityUtils.getUsername());
        try {
            try {
                SupermarketInfo checkResult = supermarketInfoMapper.selectSupermarketInfoBySupermarketName(supermarketInfo);
                if (Objects.nonNull(checkResult)) {
                    throw new ServiceException("该区域超市名称已存在");
                }
                return supermarketInfoMapper.insertSupermarketInfo(supermarketInfo);
            } catch (Exception e) {
                throw new ServiceException(e.getMessage());
            }


        } catch (Exception e) {
            throw new ServiceException(e.getMessage());
        }
    }

    /**
     * 修改超市
     * 
     * @param supermarketInfo 超市
     * @return 结果
     */
    @Override
    public int updateSupermarketInfo(SupermarketInfo supermarketInfo) {
        supermarketInfo.setUpdateTime(DateUtils.getNowDate());
        try {
            List<SupermarketInfo> allSupermarketList = supermarketInfoMapper.selectSupermarketInfoList(new SupermarketInfo());
            List<String> otherSupermarketNameList = allSupermarketList.stream().filter(item -> !item.getSupermarketId().equals(supermarketInfo.getSupermarketId()))
                    .filter(item -> item.getAreaId().equals(supermarketInfo.getAreaId()))
                    .map(SupermarketInfo::getSupermarketName).collect(Collectors.toList());
            if (otherSupermarketNameList.contains(supermarketInfo.getSupermarketName())) {
                throw new ServiceException("该区域超市名称已存在");
            }
            return supermarketInfoMapper.updateSupermarketInfo(supermarketInfo);
        } catch (Exception e) {
            throw new ServiceException(e.getMessage());
        }
    }

    /**
     * 批量删除超市
     * 
     * @param supermarketIds 需要删除的超市主键
     * @return 结果
     */
    @Override
    public int deleteSupermarketInfoBySupermarketIds(Long[] supermarketIds) {
        for (int i = 0; i < supermarketIds.length; i++) {
            SupermarketProduct supermarketProduct = new SupermarketProduct();
            supermarketProduct.setSupermarketId(supermarketIds[i]);
            List<SupermarketProduct> list = supermarketProductMapper.selectSupermarketProductList(supermarketProduct);
            if (!CollectionUtils.isEmpty(list)) {
                throw new ServiceException("该超市含有商品，不能删除");
            }
        }
        return supermarketInfoMapper.deleteSupermarketInfoBySupermarketIds(supermarketIds);
    }

    /**
     * 删除超市信息
     * 
     * @param supermarketId 超市主键
     * @return 结果
     */
    @Override
    public int deleteSupermarketInfoBySupermarketId(Long supermarketId) {
        return supermarketInfoMapper.deleteSupermarketInfoBySupermarketId(supermarketId);
    }
}
