package org.agt.module.license.service.license;

import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import org.agt.module.license.controller.admin.license.vo.*;
import org.agt.module.license.dal.dataobject.license.LicenseDO;
import org.agt.framework.common.pojo.PageResult;
import org.agt.framework.common.pojo.PageParam;
import org.agt.framework.common.util.object.BeanUtils;

import org.agt.module.license.dal.mysql.license.LicenseMapper;

import static org.agt.framework.common.exception.util.ServiceExceptionUtil.exception;
import static org.agt.module.license.enums.ErrorCodeConstants.*;

/**
 * License Service 实现类
 *
 * @author 管理员
 */
@Service
@Validated
public class LicenseServiceImpl implements LicenseService {

    @Resource
    private LicenseMapper licenseMapper;

    @Override
    public Long createLicense(LicenseSaveReqVO createReqVO) {
        // 插入
        LicenseDO license = BeanUtils.toBean(createReqVO, LicenseDO.class);
        licenseMapper.insert(license);
        // 返回
        return license.getId();
    }

    @Override
    public void updateLicense(LicenseSaveReqVO updateReqVO) {
        // 校验存在
        validateLicenseExists(updateReqVO.getId());
        // 更新
        LicenseDO updateObj = BeanUtils.toBean(updateReqVO, LicenseDO.class);
        licenseMapper.updateById(updateObj);
    }

    @Override
    public void deleteLicense(Long id) {
        // 校验存在
        validateLicenseExists(id);
        // 删除
        licenseMapper.deleteById(id);
    }

    private void validateLicenseExists(Long id) {
        if (licenseMapper.selectById(id) == null) {
            throw exception(LICENSE_NOT_EXISTS);
        }
    }

    @Override
    public LicenseDO getLicense(Long id) {
        return licenseMapper.selectById(id);
    }

    @Override
    public PageResult<LicenseDO> getLicensePage(LicensePageReqVO pageReqVO) {
        return licenseMapper.selectPage(pageReqVO);
    }

}