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.domain.BasicStallMealtime;
import com.bonus.canteen.core.basic.mapper.BasicStallMealtimeMapper;
import com.bonus.canteen.core.cook.domain.CookRecipe;
import com.bonus.canteen.core.cook.mapper.CookRecipeMapper;
import com.bonus.canteen.core.device.domain.DeviceBind;
import com.bonus.canteen.core.device.mapper.DeviceBindMapper;
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.BasicStallMapper;
import com.bonus.canteen.core.basic.domain.BasicStall;
import com.bonus.canteen.core.basic.service.IBasicStallService;
import org.springframework.util.CollectionUtils;

/**
 * 档口信息Service业务层处理
 * 
 * @author xsheng
 * @date 2025-05-25
 */
@Service
public class BasicStallServiceImpl implements IBasicStallService {
    @Autowired
    private BasicStallMapper basicStallMapper;
    @Autowired
    private BasicStallMealtimeMapper basicStallMealtimeMapper;
    @Autowired
    private DeviceBindMapper deviceBindMapper;
    @Autowired
    CookRecipeMapper cookRecipeMapper;

    /**
     * 查询档口信息
     * 
     * @param stallId 档口信息主键
     * @return 档口信息
     */
    @Override
    public BasicStall selectBasicStallByStallId(Long stallId) {
        BasicStallMealtime basicStallMealtime = new BasicStallMealtime();
        basicStallMealtime.setStallId(stallId);
        List<BasicStallMealtime> basicStallMealtimes = basicStallMealtimeMapper.selectBasicStallMealtimeList(basicStallMealtime);
        BasicStall basicStall = basicStallMapper.selectBasicStallByStallId(stallId);
        basicStall.setBasicStallMealtimeList(basicStallMealtimes);
        return basicStall;
    }

    /**
     * 查询档口信息列表
     * 
     * @param basicStall 档口信息
     * @return 档口信息
     */
    @Override
    public List<BasicStall> selectBasicStallList(BasicStall basicStall) {
        return basicStallMapper.selectBasicStallList(basicStall);
    }

    /**
     * 新增档口信息
     * 
     * @param basicStall 档口信息
     * @return 结果
     */
    @Override
    public int insertBasicStall(BasicStall basicStall) {
        basicStall.setCreateTime(DateUtils.getNowDate());
        basicStall.setCreateBy(SecurityUtils.getUsername());
        basicStall.setIfReserve(BnsConstants.COMMON_YES); //默认开启预订餐
        try {
            BasicStall checkResult = basicStallMapper.selectBasicStallByStallName(basicStall);
            if (Objects.nonNull(checkResult)) {
                throw new ServiceException("该食堂档口名称已存在");
            }
            int stallCount = basicStallMapper.insertBasicStall(basicStall);
            if (stallCount > 0 && !CollectionUtils.isEmpty(basicStall.getBasicStallMealtimeList())) {
                basicStallMealtimeMapper.deleteBasicStallMealtimeByStallId(basicStall.getStallId());
                List<BasicStallMealtime> mealtimes = basicStall.getBasicStallMealtimeList();
                mealtimes.stream().forEach(o -> o.setStallId(basicStall.getStallId()));
                basicStallMealtimeMapper.batchInsertBasicStallMealtime(mealtimes);
            }
            return stallCount;
        } catch (Exception e) {
            throw new ServiceException(e.getMessage());
        }
    }

    /**
     * 修改档口信息
     * 
     * @param basicStall 档口信息
     * @return 结果
     */
    @Override
    public int updateBasicStall(BasicStall basicStall) {
        basicStall.setUpdateTime(DateUtils.getNowDate());
        basicStall.setUpdateBy(SecurityUtils.getUsername());
        try {
            List<BasicStall> allStallList = basicStallMapper.selectBasicStallList(new BasicStall());
            List<String> otherStallNameList = allStallList.stream().filter(item -> !item.getStallId().equals(basicStall.getStallId()))
                    .filter(item -> item.getCanteenId().equals(basicStall.getCanteenId()))
                    .map(BasicStall::getStallName).collect(Collectors.toList());
            if (otherStallNameList.contains(basicStall.getStallName())) {
                throw new ServiceException("该食堂档口名称已存在");
            }
            int stallCount = basicStallMapper.updateBasicStall(basicStall);
            if (stallCount > 0 && !CollectionUtils.isEmpty(basicStall.getBasicStallMealtimeList())) {
                basicStallMealtimeMapper.deleteBasicStallMealtimeByStallId(basicStall.getStallId());
                basicStallMealtimeMapper.batchInsertBasicStallMealtime(basicStall.getBasicStallMealtimeList());
            }
            return stallCount;
        } catch (Exception e) {
            throw new ServiceException(e.getMessage());
        }
    }

    /**
     * 批量删除档口信息
     * 
     * @param stallIds 需要删除的档口信息主键
     * @return 结果
     */
    @Override
    public int deleteBasicStallByStallIds(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("此档口已绑定设备,不能删除");
            }
            //判断档口是否含有菜谱
            CookRecipe cookRecipe = new CookRecipe();
            cookRecipe.setStallId(stallId);
            List<CookRecipe> cookRecipeList = cookRecipeMapper.selectCookRecipeList(cookRecipe);
            if (!CollectionUtils.isEmpty(cookRecipeList) && cookRecipeList.size() > 0) {
                throw new ServiceException("此档口含有菜谱,不能删除");
            }
            basicStallMealtimeMapper.deleteBasicStallMealtimeByStallId(stallId);
            count += basicStallMapper.deleteBasicStallByStallId(stallId);
        }
        return count;
    }

    /**
     * 删除档口信息信息
     * 
     * @param stallId 档口信息主键
     * @return 结果
     */
    @Override
    public int deleteBasicStallByStallId(Long stallId) {
        basicStallMealtimeMapper.deleteBasicStallMealtimeByStallId(stallId);
        return basicStallMapper.deleteBasicStallByStallId(stallId);
    }
}
