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

import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.ObjUtil;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.system.controller.admin.dishes.vo.DishesPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.dishes.vo.DishesRespVO;
import cn.iocoder.yudao.module.system.controller.admin.dishes.vo.DishesSaveReqVO;
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.controller.admin.dishesraw.vo.DishesRawPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.dishesraw.vo.DishesRawSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.dishes.DishesDO;
import cn.iocoder.yudao.module.system.dal.dataobject.dishesnutrition.DishesNutritionDO;
import cn.iocoder.yudao.module.system.dal.dataobject.dishesraw.DishesRawDO;
import cn.iocoder.yudao.module.system.dal.mysql.dishes.DishesMapper;
import cn.iocoder.yudao.module.system.enums.ErrorCodeConstants;
import cn.iocoder.yudao.module.system.service.dishesnutrition.DishesNutritionService;
import cn.iocoder.yudao.module.system.service.dishesraw.DishesRawService;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.SneakyThrows;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

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

/**
 * 菜品管理 Service 实现类
 *
 * @author 开发账号
 */
@Service
@Validated
public class DishesServiceImpl implements DishesService {

    @Resource
    private DishesMapper dishesMapper;
    @Resource
    private DishesNutritionService dishesNutritionService;
    @Resource
    private DishesRawService dishesRawService;
    @Override
    @SneakyThrows
    public Long createDishes(DishesSaveReqVO createReqVO) {
        // 插入
        DishesDO dishes = BeanUtils.toBean(createReqVO, DishesDO.class);
        dishes.setDeleted(Boolean.FALSE);
        //计算每g多少钱
        BigDecimal dishesSumPrice = createReqVO.getDishesSumPrice();
        BigDecimal dishesNumber = createReqVO.getDishesNumber();
        BigDecimal div = NumberUtil.div(dishesSumPrice, dishesNumber, 2);
        //避免计算出每克价格约 为0
        if(div.floatValue() <= 0.00f){
            dishes.setDishesBasePrice(new BigDecimal("0.01"));
        }else{
            dishes.setDishesBasePrice(div);
        }
        dishesMapper.insert(dishes);
        List<DishesNutritionSaveReqVO> list = createReqVO.getDishesNutritionList();
        List<DishesRawSaveReqVO> dishesRawList = createReqVO.getDishesRawList();
        if(ObjUtil.isNotEmpty(list)){
            list.forEach(f-> f.setDishesId(dishes.getId()));
            dishesNutritionService.createDishesNutritionList(list);
        }
        if(ObjUtil.isNotEmpty(dishesRawList)){
            dishesRawList.forEach(f-> f.setDishesId(dishes.getId()));
            dishesRawService.createDishesRawList(dishesRawList);
        }
        // 返回
        return dishes.getId();
    }
    @Override
    @SneakyThrows
    public void updateDishes(DishesSaveReqVO updateReqVO) {
        // 校验存在
        validateDishesExists(updateReqVO.getId());
        // 更新
        DishesDO updateObj = BeanUtils.toBean(updateReqVO, DishesDO.class);

        //计算每g多少钱
        BigDecimal dishesSumPrice = updateObj.getDishesSumPrice();
        BigDecimal dishesNumber = updateObj.getDishesNumber();
        BigDecimal div = NumberUtil.div(dishesSumPrice, dishesNumber, 2);
        //避免计算出每克价格约 为0
        if(div.floatValue() <= 0.00f){
            updateObj.setDishesBasePrice(new BigDecimal("0.01"));
        }else{
            updateObj.setDishesBasePrice(div);
        }
        dishesMapper.updateById(updateObj);
        List<DishesNutritionSaveReqVO> list = updateReqVO.getDishesNutritionList();
        List<DishesRawSaveReqVO> dishesRawList = updateReqVO.getDishesRawList();
        if(ObjUtil.isNotEmpty(list)){
            list.forEach(f-> f.setDishesId(updateReqVO.getId()));
            dishesNutritionService.updateDishesNutritionList(list);
        }
        if(ObjUtil.isNotEmpty(dishesRawList)){
            dishesRawList.forEach(f-> f.setDishesId(updateReqVO.getId()));
            dishesRawService.updateDishesRawList(dishesRawList);
        }
    }

    @Override
    public void deleteDishes(Long id) {
        // 校验存在
        validateDishesExists(id);
        // 删除
        dishesMapper.deleteById(id);
    }

    private void validateDishesExists(Long id) {
        if (dishesMapper.selectById(id) == null) {
            throw exception(ErrorCodeConstants.DISHES_NOT_EXISTS);
        }
    }

    /**
     * 获得菜品管理
     *
     * @param id 编号
     * @return 菜品管理
     */
    @Override
    public DishesDO getDishes(Long id) {
        return dishesMapper.selectById(id);
    }

    @Override
    public List<DishesDO> getDishesList(List<Long> ids) {
        return dishesMapper.selectList(Wrappers.<DishesDO>lambdaQuery()
                .in(DishesDO::getId,ids));
    }

    @Override
    public DishesRespVO getDishes1(Long id) {
        DishesRespVO bean = BeanUtils.toBean(dishesMapper.selectById(id), DishesRespVO.class);
        List<DishesNutritionDO> dishesNutritionList = dishesNutritionService.getDishesNutritionList(new DishesNutritionPageReqVO().setDishesId(id));
        DishesRawPageReqVO dishesRawPageReqVO = new DishesRawPageReqVO();
        dishesRawPageReqVO.setDishesId(id);
        dishesRawPageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<DishesRawDO> dishesRawList = dishesRawService.getDishesRawPage(dishesRawPageReqVO).getList();
        bean.setDishesNutritionList(dishesNutritionList);
        bean.setDishesRawList(dishesRawList);
        return bean;
    }

    @Override
    public PageResult<DishesDO> getDishesPage(DishesPageReqVO pageReqVO) {
        return dishesMapper.selectPage(pageReqVO);
    }

    @Override
    public Map<Long, String> getDishName(List<Long> ids) {
        List<DishesDO> dishesDOS = dishesMapper.selectList(Wrappers.<DishesDO>lambdaQuery().in(DishesDO::getId, ids));
        Map<Long, String> collect = dishesDOS.stream().collect(Collectors.toMap(DishesDO::getId, DishesDO::getDishesName));
        return collect;
    }
}