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

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
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.time.LocalDateTime;
import java.time.LocalTime;
import java.time.YearMonth;
import java.time.format.DateTimeFormatter;
import java.util.*;
import cn.iocoder.yudao.module.server.controller.admin.harmfulinforeport.vo.*;
import cn.iocoder.yudao.module.server.dal.dataobject.harmfulinforeport.HarmfulInfoReportDO;
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.harmfulinforeport.HarmfulInfoReportMapper;

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 HarmfulInfoReportServiceImpl implements HarmfulInfoReportService {

    @Resource
    private HarmfulInfoReportMapper harmfulInfoReportMapper;

    @Override
    @LogRecord(
            type = SYSTEM_HARMFUL_TYPE,
            subType = SYSTEM_HARMFUL_CREATE_SUB_TYPE,
            bizNo = "200",
            success = SYSTEM_HARMFUL_CREATE_SUCCESS
    )
    public Long createHarmfulInfoReport(HarmfulInfoReportSaveReqVO createReqVO) {
        // 插入
        HarmfulInfoReportDO harmfulInfoReport = BeanUtils.toBean(createReqVO, HarmfulInfoReportDO.class);
        harmfulInfoReportMapper.insert(harmfulInfoReport);
        // 返回
        return harmfulInfoReport.getId();
    }

    @Override
    @LogRecord(
            type = SYSTEM_HARMFUL_TYPE,
            subType = SYSTEM_HARMFUL_UPDATE_SUB_TYPE,
            bizNo = "200",
            success = SYSTEM_HARMFUL_UPDATE_SUCCESS
    )
    public void updateHarmfulInfoReport(HarmfulInfoReportSaveReqVO updateReqVO) {
        // 校验存在
        validateHarmfulInfoReportExists(updateReqVO.getId());
        // 更新
        HarmfulInfoReportDO updateObj = BeanUtils.toBean(updateReqVO, HarmfulInfoReportDO.class);
        harmfulInfoReportMapper.updateById(updateObj);
    }
    @LogRecord(
            type = SYSTEM_HARMFUL_TYPE,
            subType = SYSTEM_HARMFUL_UPDATEOBJ_SUB_TYPE,
            bizNo = "200",
            success = SYSTEM_HARMFUL_UPDATEOBJ_SUCCESS
    )
    public void updateHarmfulInfoReportObj(HarmfulInfoReportDO updateObj) {
        // 更新
        harmfulInfoReportMapper.updateById(updateObj);
    }

    @Override
    @LogRecord(
            type = SYSTEM_HARMFUL_TYPE,
            subType = SYSTEM_HARMFUL_DELETE_SUB_TYPE,
            bizNo = "200",
            success = SYSTEM_HARMFUL_DELETE_SUCCESS
    )
    public void deleteHarmfulInfoReport(Long id) {
        // 校验存在
        validateHarmfulInfoReportExists(id);
        // 删除
        harmfulInfoReportMapper.deleteById(id);
    }

    private void validateHarmfulInfoReportExists(Long id) {
        if (harmfulInfoReportMapper.selectById(id) == null) {
            throw exception(HARMFUL_INFO_REPORT_NOT_EXISTS);
        }
    }

    @Override
    public HarmfulInfoReportDO getHarmfulInfoReport(Long id) {
        return harmfulInfoReportMapper.selectById(id);
    }

    @Override
    public PageResult<HarmfulInfoReportDO> getHarmfulInfoReportPage(HarmfulInfoReportPageReqVO pageReqVO) {
        return harmfulInfoReportMapper.selectPage(pageReqVO);
    }

}