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

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.storediningplates.vo.StoreDiningPlatesPageReqVO;
import cn.iocoder.yudao.module.member.controller.admin.storediningplates.vo.StoreDiningPlatesSaveReqVO;
import cn.iocoder.yudao.module.member.dal.dataobject.storediningplates.StoreDiningPlatesDO;
import cn.iocoder.yudao.module.member.dal.mysql.storediningplates.StoreDiningPlatesMapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

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


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


/**
 * 门店餐盘设置 Service 实现类
 *
 * @author 我是秦俊旗
 */
@Service
@Validated
public class StoreDiningPlatesServiceImpl implements StoreDiningPlatesService {

    @Resource
    private StoreDiningPlatesMapper storeDiningPlatesMapper;

    @Override
    public Long createStoreDiningPlates(StoreDiningPlatesSaveReqVO createReqVO) {
        // 插入
        StoreDiningPlatesDO storeDiningPlates = BeanUtils.toBean(createReqVO, StoreDiningPlatesDO.class);
        storeDiningPlatesMapper.insert(storeDiningPlates);
        // 返回
        return storeDiningPlates.getId();
    }

    @Override
    public void updateStoreDiningPlates(StoreDiningPlatesSaveReqVO updateReqVO) {
        // 校验存在
        validateStoreDiningPlatesExists(updateReqVO.getId());
        // 更新
        StoreDiningPlatesDO updateObj = BeanUtils.toBean(updateReqVO, StoreDiningPlatesDO.class);
        storeDiningPlatesMapper.updateById(updateObj);
    }

    @Override
    public void deleteStoreDiningPlates(Long id) {
        // 校验存在
        validateStoreDiningPlatesExists(id);
        // 删除
        storeDiningPlatesMapper.deleteById(id);
    }

    private void validateStoreDiningPlatesExists(Long id) {
        if (storeDiningPlatesMapper.selectById(id) == null) {
            throw exception(STORE_DINING_PLATES_NOT_EXISTS);
        }
    }

    @Override
    public StoreDiningPlatesDO getStoreDiningPlates(Long id) {
        return storeDiningPlatesMapper.selectById(id);
    }

    @Override
    public PageResult<StoreDiningPlatesDO> getStoreDiningPlatesPage(StoreDiningPlatesPageReqVO pageReqVO) {
        return storeDiningPlatesMapper.selectPage(pageReqVO);
    }

    @Override
    public StoreDiningPlatesDO getByAmount(BigDecimal amount, Long storeId, boolean applyToCard) {

        List<StoreDiningPlatesDO> storeDiningPlatesDOS = storeDiningPlatesMapper.selectList(Wrappers.<StoreDiningPlatesDO>lambdaQuery().lt(StoreDiningPlatesDO::getAmountStart, amount)
                .ge(StoreDiningPlatesDO::getAmountEnd, amount).eq(StoreDiningPlatesDO::getStoreId, storeId)
                .eq(StoreDiningPlatesDO::getApplyToCard, applyToCard));
        if(CollectionUtil.isNotEmpty(storeDiningPlatesDOS)){
            return storeDiningPlatesDOS.get(0);
        }
        return null;
    }
}