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

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.domain.vo.GovViolationInfoDetailVo;
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.List;
import java.util.Map;

@Component
@RequiredArgsConstructor
public class ViolationInvestigationProvider implements IReportDataProvider {

    private final ISysCompanyService sysCompanyService;
    private final PublicMapper publicMapper;

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

    @Override
    public boolean isLandscape() {
        return true;
    }

    @Override
    public String getReportTitle() {
        return "违法违章信息排查记录";
    }

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

        Map<String, Object> ctx = new HashMap<>();
        ctx.put("mode", mode);

        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();
                }
            }
        }
        ctx.put("companyName", companyName);

        if ("cover".equals(mode)) {
            ctx.put("regionName", regionName);
            ctx.put("printYear", DateUtil.format(new Date(), "yyyy"));
        } else if ("detail".equals(mode)) {
            String managerName = MapUtil.getStr(params, "managerName");
            String investigateDate = MapUtil.getStr(params, "investigateDate");
            List<GovViolationInfoDetailVo> details = (List<GovViolationInfoDetailVo>) params.get("details");

            ctx.put("managerName", StringUtils.blankToDefault(managerName, ""));
            ctx.put("investigateDate", investigateDate);
            ctx.put("details", details);
        }

        return ctx;
    }
}
