package com.yc.ship.module.marketing.service.couponpackagecode;

import com.yc.ship.framework.common.pojo.PageResult;
import com.yc.ship.framework.common.util.object.BeanUtils;
import com.yc.ship.module.marketing.controller.admin.couponpackagecode.vo.CouponPackageCodePageReqVO;
import com.yc.ship.module.marketing.controller.admin.couponpackagecode.vo.CouponPackageCodeSaveReqVO;
import com.yc.ship.module.marketing.dal.dataobject.couponpackagecode.CouponPackageCodeDO;
import com.yc.ship.module.marketing.dal.mysql.couponpackagecode.CouponPackageCodeMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

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

import static com.yc.ship.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.yc.ship.module.marketing.enums.ErrorCodeConstants.COUPON_PACKAGE_CODE_NOT_EXISTS;

/**
 * 优惠券-券包领取码 Service 实现类
 *
 * @author lfy
 */
@Service
@Validated
public class CouponPackageCodeServiceImpl implements CouponPackageCodeService {

    @Resource
    private CouponPackageCodeMapper couponPackageCodeMapper;

    @Override
    public Long createCouponPackageCode(CouponPackageCodeSaveReqVO createReqVO) {
        // 插入
        CouponPackageCodeDO couponPackageCode = BeanUtils.toBean(createReqVO, CouponPackageCodeDO.class);
        couponPackageCodeMapper.insert(couponPackageCode);
        // 返回
        return couponPackageCode.getId();
    }

    @Override
    public void updateCouponPackageCode(CouponPackageCodeSaveReqVO updateReqVO) {
        // 校验存在
        validateCouponPackageCodeExists(updateReqVO.getId());
        // 更新
        CouponPackageCodeDO updateObj = BeanUtils.toBean(updateReqVO, CouponPackageCodeDO.class);
        couponPackageCodeMapper.updateById(updateObj);
    }

    @Override
    public void deleteCouponPackageCode(Long id) {
        // 校验存在
        validateCouponPackageCodeExists(id);
        // 删除
        couponPackageCodeMapper.deleteById(id);
    }

    private void validateCouponPackageCodeExists(Long id) {
        if (couponPackageCodeMapper.selectById(id) == null) {
            throw exception(COUPON_PACKAGE_CODE_NOT_EXISTS);
        }
    }

    @Override
    public CouponPackageCodeDO getCouponPackageCode(Long id) {
        return couponPackageCodeMapper.selectById(id);
    }

    @Override
    public PageResult<CouponPackageCodeDO> getCouponPackageCodePage(CouponPackageCodePageReqVO pageReqVO) {
        return couponPackageCodeMapper.selectPage(pageReqVO);
    }

    @Override
    public List<CouponPackageCodeDO> getListByPackageId(Long packageId) {
        return couponPackageCodeMapper.selectListByPackageId(packageId) ;
    }

}