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

import java.util.List;

import cn.hutool.core.bean.BeanUtil;
import com.bonus.canteen.core.menu.domain.MenuMaterialNutrition;
import com.bonus.canteen.core.menu.mapper.MenuMaterialNutritionMapper;
import com.bonus.canteen.core.menu.service.IMenuMaterialService;
import com.bonus.common.core.exception.ServiceException;
import com.bonus.common.houqin.utils.id.Id;
import com.bonus.common.security.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import com.bonus.canteen.core.menu.mapper.MenuMaterialMapper;
import com.bonus.canteen.core.menu.domain.MenuMaterial;

import javax.annotation.Resource;

/**
 * 商家食材原料信息Service业务层处理
 *
 * @author xsheng
 * @date 2025-04-03
 */
@Service
public class MenuMaterialServiceImpl implements IMenuMaterialService {
    @Autowired
    private MenuMaterialMapper menuMaterialMapper;

    @Resource
    @Lazy
    private MenuMaterialNutritionMapper menuMaterialNutritionMapper;

    /**
     * 查询商家食材原料信息
     *
     * @param id 商家食材原料信息主键
     * @return 商家食材原料信息
     */
    @Override
    public MenuMaterial selectMenuMaterialById(Long id) {
        return menuMaterialMapper.selectMenuMaterialById(id);
    }

    /**
     * 查询商家食材原料信息列表
     *
     * @param menuMaterial 商家食材原料信息
     * @return 商家食材原料信息
     */
    @Override
    public List<MenuMaterial> selectMenuMaterialList(MenuMaterial menuMaterial) {
        return menuMaterialMapper.selectMenuMaterialList(menuMaterial);
    }

    /**
     * 新增商家食材原料信息
     *
     * @param menuMaterial 商家食材原料信息
     * @return 结果
     */
    @Override
    public int insertMenuMaterial(MenuMaterial menuMaterial) {
        try {
            MenuMaterial one = menuMaterialMapper.getOne(menuMaterial);
            if(one !=null){
                throw new ServiceException("食材原料名称已存在");
            }
            menuMaterial.setMaterialId(Id.next());
            System.err.println("menuMaterial= " + menuMaterial.toString());
            int maxId = menuMaterialMapper.getLastId();
            menuMaterial.setMaterialCode(getCode(maxId + 1));
            menuMaterial.setCertId(SecurityUtils.getUserId());
            int code = menuMaterialMapper.insertMenuMaterial(menuMaterial);
            if (menuMaterial.getMenuMaterialNutritionDTO() != null) {
                MenuMaterialNutrition menuMaterialNutrition = new MenuMaterialNutrition();
                BeanUtil.copyProperties(menuMaterial.getMenuMaterialNutritionDTO(), menuMaterialNutrition, new String[0]);
                menuMaterialNutrition.setMaterialId(menuMaterial.getMaterialId());
                menuMaterialNutrition.setCategoryId(menuMaterial.getCategoryId());
                code =   this.menuMaterialNutritionMapper.insert(menuMaterialNutrition);
            }
            return code;
        } catch (Exception e) {
            System.err.println("menuMaterial= " + menuMaterial.toString());
            throw new ServiceException(""+e.getMessage());
        }
    }

    private String getCode(int num){
        if(num <10){
            return "0000"+num;
        }else if(num <100){
            return "000"+num;
        }else if(num <1000){
            return "00"+num;
        } else {
            return "0"+num;
        }
    }

    /**
     * 修改商家食材原料信息
     *
     * @param menuMaterial 商家食材原料信息
     * @return 结果
     */
    @Override
    public int updateMenuMaterial(MenuMaterial menuMaterial) {
        try {
            MenuMaterial one = menuMaterialMapper.getOne(menuMaterial);
            if(one !=null && !menuMaterial.getMaterialName().equals(one.getMaterialName())){
                throw new ServiceException("食材原料名称已存在");
            }
            return menuMaterialMapper.updateMenuMaterial(menuMaterial);
        } catch (Exception e) {
            throw new ServiceException(e.getMessage());
        }
    }

    /**
     * 批量删除商家食材原料信息
     *
     * @param ids 需要删除的商家食材原料信息主键
     * @return 结果
     */
    @Override
    public int deleteMenuMaterialByIds(Integer[] ids) {
        return menuMaterialMapper.deleteMenuMaterialByIds(ids);
    }

    /**
     * 删除商家食材原料信息信息
     *
     * @param id 商家食材原料信息主键
     * @return 结果
     */
    @Override
    public int deleteMenuMaterialById(Long id) {
        return menuMaterialMapper.deleteMenuMaterialById(id);
    }
}
