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

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import com.bonus.canteen.core.alloc.domain.AllocStall;
import com.bonus.canteen.core.alloc.domain.AllocStallMealtime;
import com.bonus.canteen.core.alloc.mapper.AllocStallMapper;
import com.bonus.canteen.core.alloc.service.IAllocStallService;
import com.bonus.canteen.core.utils.BnsConstants;
import com.bonus.common.core.exception.ServiceException;
import com.bonus.common.core.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

/**
 * 档口信息Service业务层处理
 * 
 * @author xsheng
 * @date 2025-04-03
 */
@Service
public class AllocStallServiceImpl implements IAllocStallService {
    @Autowired
    private AllocStallMapper allocStallMapper;

    /**
     * 查询档口信息
     * 
     * @param stallId 档口信息主键
     * @return 档口信息
     */
    @Override
    public AllocStall selectAllocStallByStallId(Long stallId) {
        List<AllocStallMealtime> allocStallMealtimes = allocStallMapper.selectMealtimeByStallId(stallId);
        AllocStall allocStall = allocStallMapper.selectAllocStallByStallId(stallId);
        allocStall.setAllocStallMealtimeList(allocStallMealtimes);
        return allocStall;
    }

    /**
     * 查询档口信息列表
     * 
     * @param allocStall 档口信息
     * @return 档口信息
     */
    @Override
    public List<AllocStall> selectAllocStallList(AllocStall allocStall) {
        return allocStallMapper.selectAllocStallList(allocStall);
    }

    /**
     * 新增档口信息
     * 
     * @param allocStall 档口信息
     * @return 结果
     */
    @Override
    public int insertAllocStall(AllocStall allocStall) {
        allocStall.setCreateTime(DateUtils.getNowDate());
        allocStall.setIfReserve(BnsConstants.COMMON_YES); //默认开启预订餐
        try {
            AllocStall checkResult = allocStallMapper.selectAllocStallByStallName(allocStall);
            if (Objects.nonNull(checkResult)) {
                throw new ServiceException("该食堂档口名称已存在");
            }
            int stallCount = allocStallMapper.insertAllocStall(allocStall);
            if (stallCount > 0 && !CollectionUtils.isEmpty(allocStall.getAllocStallMealtimeList())) {
                allocStallMapper.deleteMealtimeByStallId(allocStall.getStallId());
                List<AllocStallMealtime> mealtimes = allocStall.getAllocStallMealtimeList();
                mealtimes.stream().forEach(o -> o.setStallId(allocStall.getStallId()));
                allocStallMapper.insertAllocStallMealtime(mealtimes);
            }
            return stallCount;
        } catch (Exception e) {
            throw new ServiceException(e.getMessage());
        }
    }

    /**
     * 修改档口信息
     * 
     * @param allocStall 档口信息
     * @return 结果
     */
    @Override
    public int updateAllocStall(AllocStall allocStall) {
        allocStall.setUpdateTime(DateUtils.getNowDate());
        try {
            List<AllocStall> allStallList = allocStallMapper.selectAllocStallList(new AllocStall());
            List<String> otherStallNameList = allStallList.stream().filter(item -> !item.getStallId().equals(allocStall.getStallId()))
                    .filter(item -> item.getCanteenId().equals(allocStall.getCanteenId()))
                    .map(AllocStall::getStallName).collect(Collectors.toList());
            if (otherStallNameList.contains(allocStall.getStallName())) {
                throw new ServiceException("该食堂档口名称已存在");
            }
            int stallCount = allocStallMapper.updateAllocStall(allocStall);
            if (stallCount > 0 && !CollectionUtils.isEmpty(allocStall.getAllocStallMealtimeList())) {
                allocStallMapper.deleteMealtimeByStallId(allocStall.getStallId());
                allocStallMapper.insertAllocStallMealtime(allocStall.getAllocStallMealtimeList());
            }
            return stallCount;
        } catch (Exception e) {
            throw new ServiceException(e.getMessage());
        }
    }

    /**
     * 批量删除档口信息
     * 
     * @param stallIds 需要删除的档口信息主键
     * @return 结果
     */
    @Override
    public int deleteAllocStallByStallIds(Long[] stallIds) {
        for (int i = 0; i < stallIds.length; i++) {
            allocStallMapper.deleteMealtimeByStallId(stallIds[i]);
        }
        return allocStallMapper.deleteAllocStallByStallIds(stallIds);
    }

    /**
     * 删除档口信息信息
     * 
     * @param stallId 档口信息主键
     * @return 结果
     */
    @Override
    public int deleteAllocStallByStallId(Long stallId) {
        allocStallMapper.deleteMealtimeByStallId(stallId);
        return allocStallMapper.deleteAllocStallByStallId(stallId);
    }
}
