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

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import cn.hutool.core.collection.CollUtil;
import com.bonus.canteen.core.menu.domain.MenuDishesEffectRelation;
import com.bonus.canteen.core.menu.domain.MenuSuitDishes;
import com.bonus.canteen.core.menu.dto.MenuNutritionDTO;
import com.bonus.canteen.core.menu.service.IMenuNutritionService;
import com.bonus.canteen.core.utils.BnsUtils;
import com.bonus.common.core.exception.ServiceException;
import com.bonus.common.core.utils.DateUtils;
import com.bonus.common.houqin.constant.DelFlagEnum;
import com.bonus.common.houqin.i18n.I18n;
import com.bonus.common.houqin.utils.LeBeanUtil;
import com.bonus.common.houqin.utils.id.Id;
import com.bonus.common.security.utils.SecurityUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.bonus.canteen.core.menu.mapper.MenuNutritionMapper;
import com.bonus.canteen.core.menu.domain.MenuNutrition;

/**
 * 食材营养基础信息Service业务层处理
 *
 * @author xsheng
 * @date 2025-04-03
 */
@Service
public class MenuNutritionServiceImpl implements IMenuNutritionService {
    @Autowired
    private MenuNutritionMapper menuNutritionMapper;

    /**
     * 查询食材营养基础信息
     *
     * @param id 食材营养基础信息主键
     * @return 食材营养基础信息
     */
    @Override
    public MenuNutrition selectMenuNutritionById(Long id) {
        return menuNutritionMapper.selectMenuNutritionById(id);
    }

    /**
     * 查询食材营养基础信息列表
     *
     * @param menuNutrition 食材营养基础信息
     * @return 食材营养基础信息
     */
    @Override
    public List<MenuNutrition> selectMenuNutritionList(MenuNutrition menuNutrition) {
        return menuNutritionMapper.selectMenuNutritionList(menuNutrition);
    }

    /**
     * 新增食材营养基础信息
     *
     * @param menuNutritionDTO 食材营
     *                         养基础信息
     * @return 结果
     */
    @Override
    public int insertMenuNutrition(MenuNutritionDTO menuNutritionDTO) {
        try {
            MenuNutrition menuNutrition = this.menuNutritionMapper.getOne(menuNutritionDTO);
            String username = SecurityUtils.getUsername();
            List effectIdList;
            if (ObjectUtils.isNotEmpty(menuNutrition)) {
                if (!DelFlagEnum.DEL_TRUE.key().equals(menuNutrition.getDelFlag())) {
                    throw new ServiceException(I18n.getMessage("营养信息已存在", new Object[0]));
                }
                //菜品功效关联
                BnsUtils.copyProperties(menuNutritionDTO, menuNutrition);
                List<Long> suitIdList = menuNutritionDTO.getSuitablePopulationList();

                menuNutritionMapper.delSuitDishesBatch(menuNutritionDTO);
                if (CollUtil.isNotEmpty(suitIdList)) {
                    this.insertSuitDishesBatch(suitIdList, menuNutritionDTO.getNutritionId(), username);
                }
              //事宜人群
                effectIdList = menuNutritionDTO.getEffectList();
                menuNutritionMapper.delDishesEffect(menuNutritionDTO);
                if (CollUtil.isNotEmpty(effectIdList)) {
                    this.insertEffectBatch(effectIdList, menuNutritionDTO.getNutritionId(), username);
                }
                menuNutrition.setUptime(LocalDateTime.now());
                menuNutrition.setDelFlag(DelFlagEnum.DEL_FALSE.key());
               return menuNutritionMapper.updateMenuNutrition(menuNutritionDTO);

                }else{
                if(menuNutrition == null){
                    menuNutrition = new MenuNutrition();
                }
                Long nutritionId = Id.next();
                menuNutrition.setNutritionId(Id.next());
                menuNutrition.setCrby(username);
                menuNutrition.setId(nutritionId);
                BnsUtils.copyProperties(menuNutritionDTO, menuNutrition);
                return menuNutritionMapper.insertMenuNutrition(menuNutrition);
            }
        } catch (Exception e) {
            throw new ServiceException(e.toString());
        }
    }


    private void insertEffectBatch(List<Long> effectIdList, Long nutritionId, String username) {
        List<MenuDishesEffectRelation> effectIdsList = new ArrayList<>();
        Iterator var5 = effectIdList.iterator();

        while(var5.hasNext()) {
            Long effectId = (Long)var5.next();
            MenuDishesEffectRelation menuDishesEffectRelation = new MenuDishesEffectRelation();
            menuDishesEffectRelation.setDishesId(nutritionId);
            menuDishesEffectRelation.setEffectId(effectId);
            menuDishesEffectRelation.setCrby(username);
            effectIdsList.add(menuDishesEffectRelation);
        }
        this.menuNutritionMapper.insertMenuDishesEffect(effectIdsList);

    }
    private void insertSuitDishesBatch(List<Long> suitIdList, Long nutritionId, String username) {
        List<MenuSuitDishes> suitDishesList = new ArrayList<>();
        Iterator var5 = suitIdList.iterator();

        while(var5.hasNext()) {
            Long suitId = (Long)var5.next();
            MenuSuitDishes suitDishes = new MenuSuitDishes();
            suitDishes.setDishesId(nutritionId);
            suitDishes.setSuitId(suitId);
            suitDishes.setCrby(username);
            suitDishesList.add(suitDishes);
        }

        this.menuNutritionMapper.insertMenuSuitDishes(suitDishesList);
    }

    /**
     * 修改食材营养基础信息
     *
     * @param menuNutritionDTO 食材营养基础信息
     * @return 结果
     */
    @Override
    public int updateMenuNutrition(MenuNutritionDTO menuNutritionDTO) {
        try {
            MenuNutrition menuNutrition = this.menuNutritionMapper.getOne(menuNutritionDTO);
            if(menuNutrition == null){
                throw new ServiceException(I18n.getMessage("营养信息不存在", new Object[0]));
            }
            return menuNutritionMapper.updateMenuNutrition(menuNutritionDTO);
        } catch (Exception e) {
            throw new ServiceException(e.toString());
        }
    }

    /**
     * 批量删除食材营养基础信息
     *
     * @param ids 需要删除的食材营养基础信息主键
     * @return 结果
     */
    @Override
    public int deleteMenuNutritionByIds(String[] ids) {
        return menuNutritionMapper.deleteMenuNutritionByIds(ids);
    }

    /**
     * 删除食材营养基础信息信息
     *
     * @param id 食材营养基础信息主键
     * @return 结果
     */
    @Override
    public int deleteMenuNutritionById(Long id) {
        return menuNutritionMapper.deleteMenuNutritionById(id);
    }
}
