package cn.iocoder.yudao.module.system.service.biz;

/**
 * @Author: jinyd
 * @CreateTime: 2024/8/19  11:42
 * @Description:
 * @Version: 4.0
 */


import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.ManufacturerPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.ManufacturerSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.biz.ManufacturerDO;
import cn.iocoder.yudao.module.system.dal.mysql.biz.ManufacturerMapper;
import com.mzt.logapi.context.LogRecordContext;
import com.mzt.logapi.starter.annotation.LogRecord;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.MANUFACTURER_NOT_EXISTS;
import static cn.iocoder.yudao.module.system.enums.LogRecordConstants.*;

/**
 * 床位变更记录 Service 实现类
 * @author 系统管理员
 */
@Service
@Validated
public class ManufacturerServiceImpl implements ManufacturerService {

    @Resource
    private ManufacturerMapper manufacturerMapper;

    @Override
    @LogRecord(type = DEVICE_MANUFACTURER_TYPE, subType = DEVICE_MANUFACTURER_CREATE_SUB_TYPE, bizNo = "{{#id}}",
            success = DEVICE_MANUFACTURER_CREATE_SUCCESS)
    public Long createManufacturer(ManufacturerSaveReqVO createReqVO) {
        // 插入
        ManufacturerDO manufacturer = BeanUtils.toBean(createReqVO, ManufacturerDO.class);
        manufacturerMapper.insert(manufacturer);
        // 返回
        LogRecordContext.putVariable("id", manufacturer.getId());
        return manufacturer.getId();
    }

    @Override
    @LogRecord(type = DEVICE_MANUFACTURER_TYPE, subType = DEVICE_MANUFACTURER_UPDATE_SUB_TYPE, bizNo = "{{#updateReqVO.id}}",
            success = DEVICE_MANUFACTURER_UPDATE_SUCCESS)
    public void updateManufacturer(ManufacturerSaveReqVO updateReqVO) {
        // 校验存在
        validateManufacturerExists(updateReqVO.getId());
        // 更新
        ManufacturerDO updateObj = BeanUtils.toBean(updateReqVO, ManufacturerDO.class);
        manufacturerMapper.updateById(updateObj);
    }

    @Override
    @LogRecord(type = DEVICE_MANUFACTURER_TYPE, subType = DEVICE_MANUFACTURER_DELETE_SUB_TYPE, bizNo = "{{#id}}",
            success = DEVICE_MANUFACTURER_DELETE_SUCCESS)
    public void deleteManufacturer(Long id) {
        // 校验存在
        validateManufacturerExists(id);
        // 删除
        manufacturerMapper.deleteById(id);
    }

    private void validateManufacturerExists(Long id) {
        ManufacturerDO manufacturerDO = manufacturerMapper.selectById(id);
        if (manufacturerDO == null) {
            throw exception(MANUFACTURER_NOT_EXISTS);
        }
        LogRecordContext.putVariable("name", manufacturerDO.getName());
    }

    @Override
    public ManufacturerDO getManufacturer(Long id) {
        return manufacturerMapper.selectById(id);
    }

    @Override
    public PageResult<ManufacturerDO> getManufacturerPage(ManufacturerPageReqVO pageReqVO) {
        return manufacturerMapper.selectPage(pageReqVO);
    }

}
