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.device.domain.DeviceBind;
import com.bonus.canteen.core.device.mapper.DeviceBindMapper;
import com.bonus.canteen.core.menu.dto.MenuRecipeParamDTO;
import com.bonus.canteen.core.menu.mapper.MenuRecipeMapper;
import com.bonus.canteen.core.menu.vo.MenuRecipeV2VO;
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;
    @Autowired
    DeviceBindMapper deviceBindMapper;
    @Autowired
    private MenuRecipeMapper menuRecipeMapper;

    /**
     * 查询档口信息
     * 
     * @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) {
        int count = 0;
        for (int i = 0; i < stallIds.length; i++) {
            Long stallId = stallIds[i];
            //判断档口是否含有设备
            DeviceBind deviceBind = new DeviceBind();
            deviceBind.setStallId(stallId);
            List<DeviceBind> deviceBindList = deviceBindMapper.selectDeviceBindList(deviceBind);
            if (!CollectionUtils.isEmpty(deviceBindList) && deviceBindList.size() > 0) {
                throw new ServiceException("此档口已绑定设备,不能删除");
            }
            //判断档口是否含有菜谱
            MenuRecipeParamDTO menuRecipeParamDTO = new MenuRecipeParamDTO();
            menuRecipeParamDTO.setStallId(stallId);
            List<MenuRecipeV2VO> menuRecipeList = menuRecipeMapper.getDishesList(menuRecipeParamDTO);
            if (!CollectionUtils.isEmpty(menuRecipeList) && menuRecipeList.size() > 0) {
                throw new ServiceException("此档口含有菜谱,不能删除");
            }
            allocStallMapper.deleteMealtimeByStallId(stallId);
            count += allocStallMapper.deleteAllocStallByStallId(stallId);
        }
        return count;
    }

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