package cn.stylefeng.workflow.instance.modular.stat.demo.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.roses.kernel.dict.api.DictApi;
import cn.stylefeng.workflow.instance.modular.stat.demo.mapper.DemoMapper;
import cn.stylefeng.workflow.instance.modular.stat.demo.pojo.BaoxiaoStatResult;
import cn.stylefeng.workflow.instance.modular.stat.demo.service.DemoInstanceStatService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class DemoInstanceStatServiceImpl implements DemoInstanceStatService {

    @Resource
    private DemoMapper demoMapper;

    @Resource
    private DictApi dictApi;

    @Override
    public List<BaoxiaoStatResult> getBaoxiaoStat(Integer days) {

        Date currentDate = new Date();

        DateTime beforeDate = DateUtil.offsetDay(currentDate, -days);

        List<BaoxiaoStatResult> baoxiaoStatResults = this.demoMapper.baoxiaoStat(beforeDate, currentDate);

        if (ObjectUtil.isEmpty(baoxiaoStatResults)) {
            return new ArrayList<>();
        }

        for (BaoxiaoStatResult baoxiaoStatResult : baoxiaoStatResults) {
            String baoxiaoType = baoxiaoStatResult.getBaoxiaoType();
            if (ObjectUtil.isNotEmpty(baoxiaoType)) {
                String dictNameByDictId = dictApi.getDictNameByDictId(Long.valueOf(baoxiaoType));
                baoxiaoStatResult.setBaoxiaoType(dictNameByDictId);
            }
        }

        return baoxiaoStatResults;
    }

}
