package cn.newfeifan.mall.sale.service.shopstatus;

import cn.newfeifan.mall.framework.common.exception.util.ServiceExceptionUtil;
import cn.newfeifan.mall.sale.controller.admin.shopstatus.vo.ShopStatusPageReqVO;
import cn.newfeifan.mall.sale.controller.admin.shopstatus.vo.ShopStatusSaveReqVO;
import cn.newfeifan.mall.sale.dal.dataobject.shopstatus.ShopStatusDO;
import cn.newfeifan.mall.sale.dal.mysql.shopstatus.ShopStatusMapper;
import cn.newfeifan.mall.module.enums.ErrorCodeConstants;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import cn.newfeifan.mall.framework.common.pojo.PageResult;
import cn.newfeifan.mall.framework.common.util.object.BeanUtils;


/**
 * 店铺状态 Service 实现类
 *
 * @author 非繁人
 */
@Service
@Validated
public class ShopStatusServiceImpl implements ShopStatusService {

    @Resource
    private ShopStatusMapper shopStatusMapper;

    @Override
    public Long createShopStatus(ShopStatusSaveReqVO createReqVO) {
        // 插入
        ShopStatusDO shopStatus = BeanUtils.toBean(createReqVO, ShopStatusDO.class);
        shopStatusMapper.insert(shopStatus);
        // 返回
        return shopStatus.getId();
    }

    @Override
    public void updateShopStatus(ShopStatusSaveReqVO updateReqVO) {
        // 校验存在
        validateShopStatusExists(updateReqVO.getId());
        // 更新
        ShopStatusDO updateObj = BeanUtils.toBean(updateReqVO, ShopStatusDO.class);
        shopStatusMapper.updateById(updateObj);
    }

    @Override
    public void deleteShopStatus(Long id) {
        // 校验存在
        validateShopStatusExists(id);
        // 删除
        shopStatusMapper.deleteById(id);
    }

    private void validateShopStatusExists(Long id) {
        if (shopStatusMapper.selectById(id) == null) {
            throw ServiceExceptionUtil.exception(ErrorCodeConstants.SHOP_STATUS_NOT_EXISTS);
        }
    }

    @Override
    public ShopStatusDO getShopStatus(Long id) {
        return shopStatusMapper.selectById(id);
    }

    @Override
    public PageResult<ShopStatusDO> getShopStatusPage(ShopStatusPageReqVO pageReqVO) {
        return shopStatusMapper.selectPage(pageReqVO);
    }

}