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

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.dishesraw.vo.DishesRawPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.dishesraw.vo.DishesRawSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.dishesraw.DishesRawDO;
import cn.iocoder.yudao.module.system.dal.mysql.dishesraw.DishesRawMapper;
import cn.iocoder.yudao.module.system.enums.ErrorCodeConstants;
import lombok.SneakyThrows;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

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

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

/**
 * 菜品原材料 Service 实现类
 *
 * @author 开发账号
 */
@Service
@Validated
public class DishesRawServiceImpl implements DishesRawService {

    @Resource
    private DishesRawMapper dishesRawMapper;

    @Override
    public Long createDishesRaw(DishesRawSaveReqVO createReqVO) {
        // 插入
        DishesRawDO dishesRaw = BeanUtils.toBean(createReqVO, DishesRawDO.class);
        dishesRawMapper.insert(dishesRaw);
        // 返回
        return dishesRaw.getId();
    }

    @Override
    public void updateDishesRaw(DishesRawSaveReqVO updateReqVO) {
        try {
            // 校验存在
            validateDishesRawExists(updateReqVO.getId());
        } catch (Exception e) {
            this.createDishesRaw(updateReqVO);
        }
        // 更新
        DishesRawDO updateObj = BeanUtils.toBean(updateReqVO, DishesRawDO.class);
        dishesRawMapper.updateById(updateObj);
    }

    @Override
    public void updateDishesRawList(List<DishesRawSaveReqVO> updateReqVO) {
        updateReqVO.forEach(this::updateDishesRaw);
    }

    @Override
    public void deleteDishesRaw(Long id) {
        // 校验存在
        validateDishesRawExists(id);
        // 删除
        dishesRawMapper.deleteById(id);
    }

    private void validateDishesRawExists(Long id) {
        if (dishesRawMapper.selectById(id) == null) {
            throw exception(ErrorCodeConstants.DISHES_RAW_NOT_EXISTS);
        }
    }

    @Override
    public DishesRawDO getDishesRaw(Long id) {
        return dishesRawMapper.selectById(id);
    }

    @Override
    public PageResult<DishesRawDO> getDishesRawPage(DishesRawPageReqVO pageReqVO) {
        return dishesRawMapper.selectPage(pageReqVO);
    }

    @Override
    @SneakyThrows
    public Long createDishesRawList(List<DishesRawSaveReqVO> createReqVO) {
        for (DishesRawSaveReqVO reqVO : createReqVO) {
//            String enCode=null;
//            if(ObjUtil.isNotEmpty(reqVO.getRawImgUrl())){
//                enCode= Base64.getEncoder().encodeToString(reqVO.getRawImgUrl().getBytes());
//            }
            // 插入
            DishesRawDO dishesRaw = BeanUtils.toBean(reqVO, DishesRawDO.class);
//            dishesRaw.setRawImgUrl(enCode);
            dishesRawMapper.insert(dishesRaw);
        }
        return 1L;
    }
}