package com.hotwj.platform.reportStatistics.provider.cover;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.map.MapUtil;
import com.hotwj.platform.common.mapper.PublicMapper;
import com.hotwj.platform.common.vo.AddressVo;
import com.hotwj.platform.reportStatistics.service.IReportDataProvider;
import com.hotwj.platform.resourceManagement.company.domain.vo.SysCompanyVo;
import com.hotwj.platform.resourceManagement.company.service.ISysCompanyService;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.utils.StringUtils;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 日常安全培训封面
 *
 * @author shihongwei
 */
@Component
@RequiredArgsConstructor
public class DailyTrainingCoverProvider implements IReportDataProvider {

    private final ISysCompanyService sysCompanyService;
    private final PublicMapper publicMapper;

    @Override
    public String getTemplateCode() {
        return "templates/common/dailyTrainingCover.html.vm";
    }

    @Override
    public String getReportType() {
        return "DAILY_TRAINING_COVER";
    }

    @Override
    public Map<String, Object> prepareData(Map<String, Object> params) {
        Long companyId = MapUtil.getLong(params, "companyId");
        Map<String, Object> data = new HashMap<>();

        SysCompanyVo company = sysCompanyService.queryById(companyId);
        String companyName = "";
        String regionName = "";
        if (company != null) {
            companyName = StringUtils.blankToDefault(company.getCompanyName(), "");
            if (StringUtils.isNotBlank(company.getRegionCode())) {
                AddressVo addressVo = publicMapper.selectAddressByCode(company.getRegionCode());
                if (addressVo != null) {
                    regionName = addressVo.getLabel();
                }
            }
        }
        data.put("companyName", companyName);
        data.put("regionName", regionName);
        // DateUtil.format 使用 SimpleDateFormat，它会尝试解析所有字母。'n', 'b', 's', 'p' 等被误认为是格式化指令。
        // 需要将非日期格式化字符用单引号括起来
        data.put("printYear", DateUtil.format(new Date(), "yyyy"));

        return data;
    }
}
