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 PrejobTrainingCoverProvider implements IReportDataProvider {

    private final ISysCompanyService sysCompanyService;
    private final PublicMapper publicMapper;

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

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

    @Override
    public Map<String, Object> prepareData(Map<String, Object> params) {
        Long companyId = MapUtil.getLong(params, "companyId");
        String coverName = MapUtil.getStr(params, "coverName");
        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("coverName", coverName);
        data.put("regionName", regionName + "道路运输行业");
        // 使用 &nbsp; 实体来增加空格的显示宽度，确保在 HTML 中正确渲染
        // DateUtil.format 使用 SimpleDateFormat，它会尝试解析所有字母。'n', 'b', 's', 'p' 等被误认为是格式化指令。
        // 需要将非日期格式化字符用单引号括起来
        data.put("printDate", DateUtil.format(new Date(), "yyyy' 年 &nbsp;&nbsp;&nbsp; 月'"));

        return data;
    }
}
