package org.springblade.system.service.impl;

import jakarta.annotation.Resource;
import org.springblade.common.core.domain.AjaxResult;
import org.springblade.system.config.CronExpressionGenerator;
import org.springblade.system.domain.ArchivedSettingDto;
import org.springblade.system.mapper.ArchivedSettingMapper;
import org.springblade.system.service.ArchivedSettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author：liang.chao
 * @Date：2025/9/15 - 9:58
 */
@Service
public class ArchivedSettingServiceImpl implements ArchivedSettingService {
    @Resource
    private ArchivedSettingMapper archivedSettingMapper;

    @Override
    public List<ArchivedSettingDto> getArchivedSetting(ArchivedSettingDto dto) {
        return archivedSettingMapper.getArchivedSetting(dto);
    }

    @Override
    public AjaxResult addArchivedSetting(ArchivedSettingDto dto) {
        String cronStr = CronExpressionGenerator.generateCron(dto.getArchivedType(), dto.getCycle(), dto.getTime(), dto.getWeekOfDay(), dto.getMonthDay());
        dto.setCronStr(cronStr);
        Integer i = archivedSettingMapper.addArchivedSetting(dto);
        if (i > 0) {
            return AjaxResult.success("配置成功");
        } else {
            return AjaxResult.error("配置失败");
        }
    }

    @Override
    public AjaxResult updateArchivedSetting(List<ArchivedSettingDto> dtos) {
        int num = 0;
        for (ArchivedSettingDto dto : dtos) {
            String cronStr = CronExpressionGenerator.generateCron(dto.getArchivedType(), dto.getCycle(), dto.getTime(), dto.getWeekOfDay(), dto.getMonthDay());
            dto.setCronStr(cronStr);
            Integer i = archivedSettingMapper.updateArchivedSetting(dto);
            num += i;
            archivedSettingMapper.updatecronExpression(dto);
        }
        if (num > 0) {
            return AjaxResult.success("修改成功");
        } else {
            return AjaxResult.error("修改失败");
        }
    }

}
