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

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import com.bonus.canteen.core.basic.mapper.BasicStallMapper;
import com.bonus.canteen.core.utils.BnsConstants;
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.basic.mapper.BasicCanteenMapper;
import com.bonus.canteen.core.basic.domain.BasicCanteen;
import com.bonus.canteen.core.basic.service.IBasicCanteenService;

/**
 * 食堂信息Service业务层处理
 * 
 * @author xsheng
 * @date 2025-05-25
 */
@Service
public class BasicCanteenServiceImpl implements IBasicCanteenService {
    @Autowired
    private BasicCanteenMapper basicCanteenMapper;
    @Autowired
    private BasicStallMapper basicStallMapper;

    /**
     * 查询食堂信息
     * 
     * @param canteenId 食堂信息主键
     * @return 食堂信息
     */
    @Override
    public BasicCanteen selectBasicCanteenByCanteenId(Long canteenId) {
        return basicCanteenMapper.selectBasicCanteenByCanteenId(canteenId);
    }

    /**
     * 查询食堂信息列表
     * 
     * @param basicCanteen 食堂信息
     * @return 食堂信息
     */
    @Override
    public List<BasicCanteen> selectBasicCanteenList(BasicCanteen basicCanteen) {
        return basicCanteenMapper.selectBasicCanteenList(basicCanteen);
    }

    /**
     * 新增食堂信息
     * 
     * @param basicCanteen 食堂信息
     * @return 结果
     */
    @Override
    public int insertBasicCanteen(BasicCanteen basicCanteen) {
        basicCanteen.setCreateTime(DateUtils.getNowDate());
        basicCanteen.setCreateBy(SecurityUtils.getUsername());
        basicCanteen.setIfReserve(BnsConstants.COMMON_YES); //默认开启预订餐
        try {
            BasicCanteen checkResult = basicCanteenMapper.selectBasicCanteenByCanteenName(basicCanteen);
            if (Objects.nonNull(checkResult)) {
                throw new ServiceException("该区域食堂名称已存在");
            }
            return basicCanteenMapper.insertBasicCanteen(basicCanteen);
        } catch (Exception e) {
            throw new ServiceException(e.getMessage());
        }
    }

    /**
     * 修改食堂信息
     * 
     * @param basicCanteen 食堂信息
     * @return 结果
     */
    @Override
    public int updateBasicCanteen(BasicCanteen basicCanteen) {
        basicCanteen.setUpdateTime(DateUtils.getNowDate());
        basicCanteen.setUpdateBy(SecurityUtils.getUsername());
        try {
            List<BasicCanteen> allCanteenList = basicCanteenMapper.selectBasicCanteenList(new BasicCanteen());
            List<String> otherCanteenNameList = allCanteenList.stream().filter(item -> !item.getCanteenId().equals(basicCanteen.getCanteenId()))
                    .filter(item -> item.getAreaId().equals(basicCanteen.getAreaId()))
                    .map(BasicCanteen::getCanteenName).collect(Collectors.toList());
            if (otherCanteenNameList.contains(basicCanteen.getCanteenName())) {
                throw new ServiceException("该区域食堂名称已存在");
            }
            return basicCanteenMapper.updateBasicCanteen(basicCanteen);
        } catch (Exception e) {
            throw new ServiceException(e.getMessage());
        }
    }

    /**
     * 批量删除食堂信息
     * 
     * @param canteenIds 需要删除的食堂信息主键
     * @return 结果
     */
    @Override
    public int deleteBasicCanteenByCanteenIds(Long[] canteenIds) {
        int count = basicStallMapper.selectBasicStallCountByCanteenIds(canteenIds);
        if (count > 0) {
            throw new ServiceException("食堂含有档口信息，不能删除");
        }
        return basicCanteenMapper.deleteBasicCanteenByCanteenIds(canteenIds);
    }

    /**
     * 删除食堂信息信息
     * 
     * @param canteenId 食堂信息主键
     * @return 结果
     */
    @Override
    public int deleteBasicCanteenByCanteenId(Long canteenId) {
        return basicCanteenMapper.deleteBasicCanteenByCanteenId(canteenId);
    }
}
