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

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
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.dishimage.vo.DishImagePageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.dishimage.vo.DishImageSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.dishimage.DishImageDO;
import cn.iocoder.yudao.module.system.dal.mysql.dishimage.DishImageMapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.*;


/**
 * 本周菜单 Service 实现类
 *
 * @author 管理员
 */
@Service
@Validated
public class DishImageServiceImpl implements DishImageService {

    @Resource
    private DishImageMapper dishImageMapper;

    @Override
    public Integer createDishImage(DishImageSaveReqVO createReqVO) {
        // 插入
        DishImageDO dishImage = BeanUtils.toBean(createReqVO, DishImageDO.class);
        validateDishImageExists(dishImage);
        dishImageMapper.insert(dishImage);
        // 返回
        return dishImage.getId();
    }

    @Override
    public void updateDishImage(DishImageSaveReqVO updateReqVO) {
        // 校验存在
        validateDishImageExists(updateReqVO.getId());
        // 更新
        DishImageDO updateObj = BeanUtils.toBean(updateReqVO, DishImageDO.class);
        validateDishImageExists(updateObj);
        dishImageMapper.updateById(updateObj);
    }

    @Override
    public void deleteDishImage(Integer id) {
        // 校验存在
        validateDishImageExists(id);
        // 删除
        dishImageMapper.deleteById(id);
    }

    private void validateDishImageExists(Integer id) {
        if (dishImageMapper.selectById(id) == null) {
            throw exception(DISH_IMAGE_NOT_EXISTS);
        }
    }
    private void validateDishImageExists(DishImageDO dishImage) {
        List<DishImageDO> dishImageDOS = dishImageMapper.selectList(Wrappers.<DishImageDO>lambdaQuery()
                .eq(DishImageDO::getCarteenId, dishImage.getCarteenId())
                .le(DishImageDO::getStartDate, dishImage.getStartDate())
                .ge(DishImageDO::getEndDate, dishImage.getStartDate())
                .ne(dishImage.getId()!=null,DishImageDO::getId, dishImage.getId())
        );
        if(CollectionUtil.isNotEmpty(dishImageDOS)){
            throw exception(START_DATE_EXISTS);
        }
        List<DishImageDO> dishImageDOList = dishImageMapper.selectList(Wrappers.<DishImageDO>lambdaQuery()
                .eq(DishImageDO::getCarteenId, dishImage.getCarteenId())
                .le(DishImageDO::getStartDate, dishImage.getEndDate())
                .ge(DishImageDO::getEndDate, dishImage.getEndDate())
                .ne(dishImage.getId()!=null,DishImageDO::getId, dishImage.getId())
        );
        if(CollectionUtil.isNotEmpty(dishImageDOList)){
            throw exception(END_DATE_EXISTS);
        }
    }

    @Override
    public DishImageDO getDishImage(Integer id) {
        return dishImageMapper.selectById(id);
    }

    @Override
    public PageResult<DishImageDO> getDishImagePage(DishImagePageReqVO pageReqVO) {
        return dishImageMapper.selectPage(pageReqVO);
    }

    @Override
    public DishImageDO getThisWeekDishImage(Long carteenId,String time) {
        // 获取今天的日期
        LocalDate today = LocalDate.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        String startTime =  today.format(formatter);
        if(StrUtil.isNotBlank(time)){
            startTime= time;
        }
//        // 获取本周的开始日期（周一）
//        LocalDate weekStart = today.with(DayOfWeek.MONDAY);
//
//        // 获取本周的结束日期（周日）
//        LocalDate weekEnd = today.with(DayOfWeek.SUNDAY);
//
//        // 格式化日期（年-月-日）
//        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");

//        String startTime =  weekStart.format(formatter);
//        String endTime =  weekEnd.format(formatter);
        List<DishImageDO> dishImageDOS = dishImageMapper.selectList(Wrappers.<DishImageDO>lambdaQuery()
                .le(DishImageDO::getStartDate, startTime)
                .ge(DishImageDO::getEndDate, startTime)
                .eq(DishImageDO::getCarteenId, carteenId));
        if(CollectionUtil.isNotEmpty(dishImageDOS)){
            return dishImageDOS.get(0);
        }
        return new DishImageDO();
    }
}