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.service.IReportDataProvider;
import com.hotwj.platform.resourceManagement.company.domain.vo.SysCompanyVo;
import com.hotwj.platform.resourceManagement.company.service.ISysCompanyService;
import com.hotwj.platform.securityManagement.serviceQualityComplaint.domain.vo.HotServiceQualityComplaintVo;
import com.hotwj.platform.securityManagement.serviceQualityComplaint.service.IHotServiceQualityComplaintService;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.service.OssService;
import org.dromara.common.core.utils.StringUtils;
import org.springframework.stereotype.Component;

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

@Component
@RequiredArgsConstructor
public class ServiceQualityProvider implements IReportDataProvider {

    private final ISysCompanyService sysCompanyService;
    private final PublicMapper publicMapper;
    private final IHotServiceQualityComplaintService serviceQualityComplaintService;
    private final OssService ossService;

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

    @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)) {
            HotServiceQualityComplaintVo complaint = (HotServiceQualityComplaintVo) params.get("complaint");
            if (complaint != null) {
                ctx.put("complaint", complaint);
                ctx.put("complaintTargetLabel", mapComplaintTarget(complaint.getComplaintTarget()));
                ctx.put("acceptorSignUrl", resolveMaybeOssUrl(complaint.getAcceptorSignImage()));
                ctx.put("handlerSignUrl", resolveMaybeOssUrl(complaint.getHandlerSignImage()));
            }
        }

        return ctx;
    }

    private String mapComplaintTarget(String code) {
        if (StringUtils.isBlank(code)) {
            return "";
        }
        if ("1".equals(code)) {
            return "人员";
        }
        if ("2".equals(code)) {
            return "企业";
        }
        if ("3".equals(code)) {
            return "车辆";
        }
        return code;
    }

    private String resolveMaybeOssUrl(String value) {
        if (StringUtils.isBlank(value)) {
            return null;
        }
        String first = value.split(",")[0].trim();
        if (StringUtils.isBlank(first)) {
            return null;
        }
        if (StringUtils.ishttp(first)) {
            return first;
        }
        if (first.matches("^\\d+$")) {
            String urls = ossService.selectUrlByIds(first);
            if (StringUtils.isBlank(urls)) {
                return null;
            }
            return urls.split(",")[0].trim();
        }
        return first;
    }
}
