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

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.member.controller.admin.business.vo.BusinessPageReqVO;
import cn.iocoder.yudao.module.member.controller.admin.business.vo.BusinessSaveReqVO;
import cn.iocoder.yudao.module.member.dal.dataobject.business.BusinessDO;
import cn.iocoder.yudao.module.member.dal.mysql.business.BusinessMapper;
import cn.iocoder.yudao.module.member.dal.mysql.card.CardMapper;
import cn.iocoder.yudao.module.member.dal.mysql.order.DishOrderMapper;
import cn.iocoder.yudao.module.member.dal.mysql.orderdetail.OrderDetailMapper;
import cn.iocoder.yudao.module.system.enums.ErrorCodeConstants;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;



import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;

/**
 * 营业统计 Service 实现类
 *
 * @author 开发账号
 */
@Service
@Validated
public class BusinessServiceImpl implements BusinessService {

    @Resource
    private BusinessMapper businessMapper;
    @Resource
    private DishOrderMapper dishOrderMapper;
    @Resource
    private OrderDetailMapper orderDetailMapper;
    @Resource
    private CardMapper cardMapper;
    @Override
    public Long createBusiness(BusinessSaveReqVO createReqVO) {
        // 插入
        BusinessDO business = BeanUtils.toBean(createReqVO, BusinessDO.class);
        businessMapper.insert(business);
        // 返回
        return business.getId();
    }

    @Override
    public void updateBusiness(BusinessSaveReqVO updateReqVO) {
        // 校验存在
        validateBusinessExists(updateReqVO.getId());
        // 更新
        BusinessDO updateObj = BeanUtils.toBean(updateReqVO, BusinessDO.class);
        businessMapper.updateById(updateObj);
    }

    @Override
    public void deleteBusiness(Long id) {
        // 校验存在
        validateBusinessExists(id);
        // 删除
        businessMapper.deleteById(id);
    }

    private void validateBusinessExists(Long id) {
        if (businessMapper.selectById(id) == null) {
            throw exception(ErrorCodeConstants.BUSINESS_NOT_EXISTS);
        }
    }

    @Override
    public BusinessDO getBusiness(Long id) {
        return businessMapper.selectById(id);
    }

    @Override
    public PageResult<BusinessDO> getBusinessPage(BusinessPageReqVO pageReqVO) {
        return businessMapper.selectPage(pageReqVO);
    }


}