package cn.iocoder.yudao.module.server.service.rectifyinforeport;

import com.mzt.logapi.starter.annotation.LogRecord;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.server.controller.admin.rectifyinforeport.vo.*;
import cn.iocoder.yudao.module.server.dal.dataobject.rectifyinforeport.RectifyInfoReportDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.server.dal.mysql.rectifyinforeport.RectifyInfoReportMapper;

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

/**
 * 整改结果 Service 实现类
 *
 * @author TRS
 */
@Service
@Validated
public class RectifyInfoReportServiceImpl implements RectifyInfoReportService {

    @Resource
    private RectifyInfoReportMapper rectifyInfoReportMapper;

    @LogRecord(
            type = SYSTEM_RECTIFY_TYPE,
            subType = SYSTEM_RECTIFY_CREATE_SUB_TYPE,
            bizNo = "210",
            success = SYSTEM_RECTIFY_CREATE_SUCCESS
    )
    public Long createRectifyInfoReport(RectifyInfoReportSaveReqVO createReqVO) {
        // 插入
        RectifyInfoReportDO rectifyInfoReport = BeanUtils.toBean(createReqVO, RectifyInfoReportDO.class);
        rectifyInfoReportMapper.insert(rectifyInfoReport);
        // 返回
        return rectifyInfoReport.getId();
    }

    @LogRecord(
            type = SYSTEM_RECTIFY_TYPE,
            subType = SYSTEM_RECTIFY_UPDATE_SUB_TYPE,
            bizNo = "210",
            success = SYSTEM_RECTIFY_UPDATE_SUCCESS
    )
    public void updateRectifyInfoReport(RectifyInfoReportSaveReqVO updateReqVO) {
        // 校验存在
        validateRectifyInfoReportExists(updateReqVO.getId());
        // 更新
        RectifyInfoReportDO updateObj = BeanUtils.toBean(updateReqVO, RectifyInfoReportDO.class);
        rectifyInfoReportMapper.updateById(updateObj);
    }

    @LogRecord(
            type = SYSTEM_RECTIFY_TYPE,
            subType = SYSTEM_RECTIFY_DELETE_SUB_TYPE,
            bizNo = "210",
            success = SYSTEM_RECTIFY_DELETE_SUCCESS
    )
    public void deleteRectifyInfoReport(Long id) {
        // 校验存在
        validateRectifyInfoReportExists(id);
        // 删除
        rectifyInfoReportMapper.deleteById(id);
    }

    private void validateRectifyInfoReportExists(Long id) {
        if (rectifyInfoReportMapper.selectById(id) == null) {
            throw exception(RECTIFY_INFO_REPORT_NOT_EXISTS);
        }
    }

    @Override
    public RectifyInfoReportDO getRectifyInfoReport(Long id) {
        return rectifyInfoReportMapper.selectById(id);
    }

    @Override
    public PageResult<RectifyInfoReportDO> getRectifyInfoReportPage(RectifyInfoReportPageReqVO pageReqVO) {
        return rectifyInfoReportMapper.selectPage(pageReqVO);
    }
    @LogRecord(
            type = SYSTEM_RECTIFY_TYPE,
            subType = SYSTEM_RECTIFY_UPDATEOBJ_SUB_TYPE,
            bizNo = "200",
            success = SYSTEM_RECTIFY_UPDATEOBJ_SUCCESS
    )
    public void updateRectifyInfoReportObj(RectifyInfoReportDO updateObj) {
        // 更新
        rectifyInfoReportMapper.updateById(updateObj);
    }

}