package com.qqchen.deploy.backend.schedule.converter;

import com.qqchen.deploy.backend.framework.converter.BaseConverter;
import com.qqchen.deploy.backend.schedule.dto.ScheduleJobDTO;
import com.qqchen.deploy.backend.schedule.entity.ScheduleJob;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.MappingTarget;
import org.mapstruct.Mappings;

/**
 * 定时任务Converter
 * 
 * 说明：
 * 1. executeCount/successCount/failCount/lastExecuteTime 是后台自动管理的统计字段
 * 2. 创建时必须ignore，让Entity的默认值(=0)生效
 * 3. 更新时必须ignore，防止前端覆盖这些字段
 * 
 * @author qichen
 */
@Mapper(config = BaseConverter.class)
public interface ScheduleJobConverter extends BaseConverter<ScheduleJob, ScheduleJobDTO> {

    /**
     * 覆盖父类的toEntity方法，忽略后台管理的统计字段
     * 让Entity的默认值(executeCount=0等)正常生效
     */
    @Override
    @Mappings({
        @Mapping(target = "id", source = "id"),
        @Mapping(target = "createTime", source = "createTime"),
        @Mapping(target = "createBy", source = "createBy"),
        @Mapping(target = "updateTime", source = "updateTime"),
        @Mapping(target = "updateBy", source = "updateBy"),
        @Mapping(target = "version", source = "version"),
        @Mapping(target = "deleted", source = "deleted"),
        @Mapping(target = "executeCount", ignore = true),
        @Mapping(target = "successCount", ignore = true),
        @Mapping(target = "failCount", ignore = true),
        @Mapping(target = "lastExecuteTime", ignore = true)
    })
    ScheduleJob toEntity(ScheduleJobDTO dto);

    /**
     * 覆盖父类的updateEntity方法，忽略所有不应该被前端修改的字段
     * 注意：覆盖父类方法时，父类的@Mapping注解会失效，必须重新声明
     */
    @Override
    @Mappings({
        @Mapping(target = "id", ignore = true),
        @Mapping(target = "createTime", ignore = true),
        @Mapping(target = "createBy", ignore = true),
        @Mapping(target = "updateTime", ignore = true),
        @Mapping(target = "updateBy", ignore = true),
        @Mapping(target = "version", ignore = true),
        @Mapping(target = "deleted", ignore = true),
        @Mapping(target = "executeCount", ignore = true),
        @Mapping(target = "successCount", ignore = true),
        @Mapping(target = "failCount", ignore = true),
        @Mapping(target = "lastExecuteTime", ignore = true)
    })
    void updateEntity(@MappingTarget ScheduleJob entity, ScheduleJobDTO dto);
}

