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

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.storediscount.vo.StoreDiscountPageReqVO;
import cn.iocoder.yudao.module.member.controller.admin.storediscount.vo.StoreDiscountSaveReqVO;
import cn.iocoder.yudao.module.member.dal.dataobject.storediscount.StoreDiscountDO;
import cn.iocoder.yudao.module.member.dal.mysql.storediscount.StoreDiscountMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;

import java.time.LocalDateTime;

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

/**
 * 门店超市折扣 Service 实现类
 *
 * @author 我是秦俊旗
 */
@Service
@Validated
public class StoreDiscountServiceImpl implements StoreDiscountService {

    @Resource
    private StoreDiscountMapper storeDiscountMapper;

    @Override
    public Long createStoreDiscount(StoreDiscountSaveReqVO createReqVO) {
        // 插入
        StoreDiscountDO storeDiscount = BeanUtils.toBean(createReqVO, StoreDiscountDO.class);
        storeDiscountMapper.insert(storeDiscount);
        // 返回
        return storeDiscount.getId();
    }

    @Override
    public void updateStoreDiscount(StoreDiscountSaveReqVO updateReqVO) {
        // 校验存在
        validateStoreDiscountExists(updateReqVO.getId());
        // 更新
        StoreDiscountDO updateObj = BeanUtils.toBean(updateReqVO, StoreDiscountDO.class);
        storeDiscountMapper.updateById(updateObj);
    }

    @Override
    public void deleteStoreDiscount(Long id) {
        // 校验存在
        validateStoreDiscountExists(id);
        // 删除
        storeDiscountMapper.deleteById(id);
    }

    private void validateStoreDiscountExists(Long id) {
        if (storeDiscountMapper.selectById(id) == null) {
            throw exception(STORE_DISCOUNT_NOT_EXISTS);
        }
    }

    @Override
    public StoreDiscountDO getStoreDiscount(Long id) {
        return storeDiscountMapper.selectById(id);
    }

    @Override
    public PageResult<StoreDiscountDO> getStoreDiscountPage(StoreDiscountPageReqVO pageReqVO) {
        return storeDiscountMapper.selectPage(pageReqVO);
    }

    @Override
    public StoreDiscountDO getStoreDiscountByStoreId(Long storeId) {
        LocalDateTime now = LocalDateTime.now();
        LambdaQueryWrapper<StoreDiscountDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StoreDiscountDO::getStoreId, storeId);
        wrapper.eq(StoreDiscountDO::getStatus, "1");
        wrapper.ge(StoreDiscountDO::getEndTime, now);
        wrapper.le(StoreDiscountDO::getStartTime, now);
        wrapper.orderByDesc(StoreDiscountDO::getCreateTime);
        wrapper.last("limit 1");
        return storeDiscountMapper.selectOne(wrapper);
    }
}