package cn.iocoder.yudao.module.member.service.activityadddish;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.member.controller.admin.activityadddish.vo.ActivityAddDishPageReqVO;
import cn.iocoder.yudao.module.member.controller.admin.activityadddish.vo.ActivityAddDishSaveBatchVO;
import cn.iocoder.yudao.module.member.controller.admin.activityadddish.vo.ActivityAddDishSaveReqVO;
import cn.iocoder.yudao.module.member.dal.dataobject.activityadddish.ActivityAddDishDO;
import cn.iocoder.yudao.module.member.dal.dataobject.activitytabledish.ActivityTableDishDO;
import cn.iocoder.yudao.module.member.dal.mysql.activityadddish.ActivityAddDishMapper;
import cn.iocoder.yudao.module.member.service.activitytabledish.ActivityTableDishService;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

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

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.member.enums.ErrorCodeConstants.ACTIVITY_ADD_DISH_NOT_CHANGE;
import static cn.iocoder.yudao.module.member.enums.ErrorCodeConstants.ACTIVITY_ADD_DISH_NOT_EXISTS;

/**
 * 活动用餐桌子加菜 Service 实现类
 *
 * @author 我是秦俊旗
 */
@Service
@Validated
public class ActivityAddDishServiceImpl implements ActivityAddDishService {

    @Resource
    private ActivityAddDishMapper activityAddDishMapper;
    @Resource
    private ActivityTableDishService activityTableDishService;

    @Override
    public Long createActivityAddDish(ActivityAddDishSaveReqVO createReqVO) {
        // 插入
        ActivityAddDishDO activityAddDish = BeanUtils.toBean(createReqVO, ActivityAddDishDO.class);
        activityAddDishMapper.insert(activityAddDish);
        // 返回
        return activityAddDish.getId();
    }

    @Override
    public void updateActivityAddDish(ActivityAddDishSaveReqVO updateReqVO) {
        // 校验存在
        validateActivityAddDishExists(updateReqVO.getId());
        // 更新
        ActivityAddDishDO updateObj = BeanUtils.toBean(updateReqVO, ActivityAddDishDO.class);
        activityAddDishMapper.updateById(updateObj);
    }

    @Override
    public void deleteActivityAddDish(Long id) {
        // 校验存在
        validateActivityAddDishExists(id);
        // 删除
        activityAddDishMapper.deleteById(id);
    }

    private void validateActivityAddDishExists(Long id) {
        if (activityAddDishMapper.selectById(id) == null) {
            throw exception(ACTIVITY_ADD_DISH_NOT_EXISTS);
        }
    }

    @Override
    public ActivityAddDishDO getActivityAddDish(Long id) {
        return activityAddDishMapper.selectById(id);
    }

    @Override
    public PageResult<ActivityAddDishDO> getActivityAddDishPage(ActivityAddDishPageReqVO pageReqVO) {
        return activityAddDishMapper.selectPage(pageReqVO);
    }

    @Override
    public void createActivityAddDishBatch(ActivityAddDishSaveBatchVO activityAddDishSaveBatchVO) {

        List<Long> tableDishIds = activityAddDishSaveBatchVO.getTableDishIds();
        if(tableDishIds == null){
            throw exception(ACTIVITY_ADD_DISH_NOT_CHANGE);
        }
        List<ActivityTableDishDO> listByIds = activityTableDishService.getListByIds(tableDishIds);
        Map<Long, Integer> collect = listByIds.stream().collect(Collectors.toMap(ActivityTableDishDO::getId, ActivityTableDishDO::getSerialNumber));


        List<ActivityAddDishDO> list = new ArrayList<>();
        for (Long tableDishId : tableDishIds){
            ActivityAddDishDO activityAddDishDO = new ActivityAddDishDO();
            activityAddDishDO.setTableDishId(tableDishId);
            activityAddDishDO.setDishName(activityAddDishSaveBatchVO.getDishName());
            activityAddDishDO.setDishPrice(activityAddDishSaveBatchVO.getDishPrice());
            activityAddDishDO.setDishImg(activityAddDishSaveBatchVO.getDishImg());
            activityAddDishDO.setSerialNumber(collect.get(tableDishId));
            list.add(activityAddDishDO);
        }
        activityAddDishMapper.insertBatch(list);
    }

}