package cn.iocoder.yudao.module.system.service.dishesnutrition;

import cn.hutool.core.util.NumberUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.system.controller.admin.dishesnutrition.vo.DishesNutritionPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.dishesnutrition.vo.DishesNutritionSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.dishesnutrition.DishesNutritionDO;
import cn.iocoder.yudao.module.system.dal.mysql.dishesnutrition.DishesNutritionMapper;
import cn.iocoder.yudao.module.system.enums.ErrorCodeConstants;
import cn.iocoder.yudao.module.system.enums.Nutrition.NutritionEnum;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;

/**
 * 菜品营养 Service 实现类
 *
 * @author 开发账号
 */
@Service
@Validated
public class DishesNutritionServiceImpl implements DishesNutritionService {

    @Resource
    private DishesNutritionMapper dishesNutritionMapper;


    @Override
    public Long createDishesNutrition(DishesNutritionSaveReqVO createReqVO) {
        // 插入
        DishesNutritionDO dishesNutrition = BeanUtils.toBean(createReqVO, DishesNutritionDO.class);
        dishesNutritionMapper.insert(dishesNutrition);
        // 返回
        return dishesNutrition.getId();
    }

    @Override
    public void updateDishesNutrition(DishesNutritionSaveReqVO updateReqVO) {
        // 校验存在
        try {
            validateDishesNutritionExists(updateReqVO.getId());
        } catch (Exception e) {
            this.createDishesNutrition(updateReqVO);
        }
        // 更新
        DishesNutritionDO updateObj = BeanUtils.toBean(updateReqVO, DishesNutritionDO.class);
        dishesNutritionMapper.updateById(updateObj);
    }

    @Override
    public void updateDishesNutritionList(List<DishesNutritionSaveReqVO> updateReqVO) {
        percentage(updateReqVO);
        updateReqVO.forEach(this::updateDishesNutrition);
    }

    @Override
    public void deleteDishesNutrition(Long id) {
        // 校验存在
        validateDishesNutritionExists(id);
        // 删除
        dishesNutritionMapper.deleteById(id);
    }

    private void validateDishesNutritionExists(Long id) {
        if (dishesNutritionMapper.selectById(id) == null) {
            throw exception(ErrorCodeConstants.DISHES_NUTRITION_NOT_EXISTS);
        }
    }

    @Override
    public DishesNutritionDO getDishesNutrition(Long id) {
        return dishesNutritionMapper.selectById(id);
    }

    @Override
    public PageResult<DishesNutritionDO> getDishesNutritionPage(DishesNutritionPageReqVO pageReqVO) {
        return dishesNutritionMapper.selectPage(pageReqVO);
    }

    /**
     * @param pageReqV
     * @return
     * @Description: 获得菜品营养集合
     * @Author: qjq
     * @Date: 2024/4/2 10:28
     */
    @Override
    public List<DishesNutritionDO> getDishesNutritionList(DishesNutritionPageReqVO pageReqV) {
        return dishesNutritionMapper.selectList(new LambdaQueryWrapperX<DishesNutritionDO>()
                .eqIfPresent(DishesNutritionDO::getDishesId,pageReqV.getDishesId()));
    }

    @Override
    public Long createDishesNutritionList(List<DishesNutritionSaveReqVO> createReqVO) {
        this.percentage(createReqVO);
        Long dishesNutrition=0L;
        for (DishesNutritionSaveReqVO reqVO : createReqVO) {
            dishesNutrition+= this.createDishesNutrition(reqVO);
        }
        return dishesNutrition;
    }


    @Override
    public String getDishesNutritionKcal(Long dishesId) {
        DishesNutritionDO dis = dishesNutritionMapper.selectOne(new LambdaQueryWrapperX<DishesNutritionDO>()
                .likeIfPresent(DishesNutritionDO::getNutritionName, "能量")
                .eqIfPresent(DishesNutritionDO::getDishesId,dishesId)
                .last("LIMIT 1"));
        String nutritionNumber = null;
        try {
            nutritionNumber = dis.getNutritionNumber();
            return NumberUtil.div(nutritionNumber, "4.184", 0).toString();
        } catch (Exception e) {
           return "0";
        }
    }
    /**
     * @Description: 计算营养百分比
     * 计算100克的占比
     * @Author: qjq
     * @Date: 2024/4/2 10:35
     * @return
     */
    public void percentage(List<DishesNutritionSaveReqVO> createReqVO){
        createReqVO.forEach(f-> f.setNutritionPer((NutritionEnum.percentage(f.getNutritionName(),new BigDecimal(f.getNutritionNumber())))));
    }
}