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

import cn.hutool.core.collection.CollectionUtil;
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.dishstatistics.vo.DishStatisticsPageReqVO;
import cn.iocoder.yudao.module.member.controller.admin.dishstatistics.vo.DishStatisticsSaveReqVO;
import cn.iocoder.yudao.module.member.dal.dataobject.dishstatistics.DishStatisticsDO;
import cn.iocoder.yudao.module.member.dal.mysql.dishstatistics.DishStatisticsMapper;
import cn.iocoder.yudao.module.member.util.MemberConstants;
import cn.iocoder.yudao.module.system.api.dish.DishesApi;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
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.*;
/**
 * 菜品统计 Service 实现类
 *
 * @author 开发账号
 */
@Service
@Validated
public class DishStatisticsServiceImpl implements DishStatisticsService {

    @Resource
    private DishStatisticsMapper dishStatisticsMapper;
    @Resource
    private DishesApi dishesApi;

    @Override
    public Long createDishStatistics(DishStatisticsSaveReqVO createReqVO) {
        // 插入
        DishStatisticsDO dishStatistics = BeanUtils.toBean(createReqVO, DishStatisticsDO.class);
        dishStatisticsMapper.insert(dishStatistics);
        // 返回
        return dishStatistics.getId();
    }

    @Override
    public void updateDishStatistics(DishStatisticsSaveReqVO updateReqVO) {
        // 校验存在
        validateDishStatisticsExists(updateReqVO.getId());
        // 更新
        DishStatisticsDO updateObj = BeanUtils.toBean(updateReqVO, DishStatisticsDO.class);
        dishStatisticsMapper.updateById(updateObj);
    }

    @Override
    public void deleteDishStatistics(Long id) {
        // 校验存在
        validateDishStatisticsExists(id);
        // 删除
        dishStatisticsMapper.deleteById(id);
    }

    private void validateDishStatisticsExists(Long id) {
        if (dishStatisticsMapper.selectById(id) == null) {
            throw exception(DISH_STATISTICS_NOT_EXISTS);
        }
    }

    @Override
    public DishStatisticsDO getDishStatistics(Long id) {
        return dishStatisticsMapper.selectById(id);
    }

    @Override
    public PageResult<DishStatisticsDO> getDishStatisticsPage(DishStatisticsPageReqVO pageReqVO) {
        return dishStatisticsMapper.selectPage(pageReqVO);
    }


    @Override
    public Integer addData() {
        String time = LocalDateTime.now().format(DateTimeFormatter.ofPattern(MemberConstants.DATE_FORMAT));
        List<DishStatisticsDO> saleData = dishStatisticsMapper.getSaleData(time);
        if(CollectionUtil.isNotEmpty(saleData)){
            List<Long> dishIds = saleData.stream().map(DishStatisticsDO::getDishId).collect(Collectors.toList());
            Map<Long, String> dishName = dishesApi.getDishName(dishIds);
            saleData.forEach(add ->{
                add.setDishesName(dishName.get(add.getDishId()));
            });
            dishStatisticsMapper.insertBatch(saleData);
        }
        return saleData.size();

    }
}