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.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.DeviceCategoryPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.DeviceCategorySaveReqVO;
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.DeviceCategoryDO;
import cn.iocoder.yudao.module.system.dal.dataobject.biz.ManufacturerDO;
import cn.iocoder.yudao.module.system.dal.mysql.biz.DeviceCategoryMapper;
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 java.util.List;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.DEVICE_CATEGORY_NOT_EXISTS;
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 DeviceCategoryServiceImpl implements DeviceCategoryService {

    @Resource
    private DeviceCategoryMapper categoryMapper;

    @Override
    @LogRecord(type = DEVICE_CATEGORY_TYPE, subType = DEVICE_CATEGORY_CREATE_SUB_TYPE, bizNo = "{{#id}}",
            success = DEVICE_CATEGORY_CREATE_SUCCESS)
    public Long createCategory(DeviceCategorySaveReqVO createReqVO) {
        // 插入
        DeviceCategoryDO category = BeanUtils.toBean(createReqVO, DeviceCategoryDO.class);
        categoryMapper.insert(category);
        // 返回
        LogRecordContext.putVariable("id", category.getId());
        return category.getId();
    }

    @Override
    @LogRecord(type = DEVICE_CATEGORY_TYPE, subType = DEVICE_CATEGORY_UPDATE_SUB_TYPE, bizNo = "{{#updateReqVO.id}}",
            success = DEVICE_CATEGORY_UPDATE_SUCCESS)
    public void updateCategory(DeviceCategorySaveReqVO updateReqVO) {
        // 校验存在
        validateCategoryExists(updateReqVO.getId());
        // 更新
        DeviceCategoryDO updateObj = BeanUtils.toBean(updateReqVO, DeviceCategoryDO.class);
        categoryMapper.updateById(updateObj);
    }

    @Override
    @LogRecord(type = DEVICE_CATEGORY_TYPE, subType = DEVICE_CATEGORY_DELETE_SUB_TYPE, bizNo = "{{#id}}",
            success = DEVICE_CATEGORY_DELETE_SUCCESS)
    public void deleteCategory(Long id) {
        // 校验存在
        validateCategoryExists(id);
        // 删除
        categoryMapper.deleteById(id);
    }

    private void validateCategoryExists(Long id) {
        DeviceCategoryDO deviceCategoryDO = categoryMapper.selectById(id);
        if (deviceCategoryDO == null) {
            throw exception(DEVICE_CATEGORY_NOT_EXISTS);
        }
        LogRecordContext.putVariable("name", deviceCategoryDO.getName());
    }

    @Override
    public DeviceCategoryDO getCategory(Long id) {
        return categoryMapper.selectById(id);
    }

    @Override
    public List<DeviceCategoryDO> getList(DeviceCategoryDO deviceCategoryDO) {
        List<DeviceCategoryDO> deviceCategoryDOS = categoryMapper.selectList(
                new LambdaQueryWrapperX<DeviceCategoryDO>().eqIfPresent(DeviceCategoryDO::getName, deviceCategoryDO.getName()));
        return deviceCategoryDOS;
    }
}
