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.securityMeeting.domain.vo.HotSecurityMeetingVo;
import com.hotwj.platform.securityManagement.securityMeeting.service.IHotSecurityMeetingService;
import com.hotwj.platform.securityManagement.securityMeetingAttendee.domain.bo.HotSecurityMeetingAttendeeBo;
import com.hotwj.platform.securityManagement.securityMeetingAttendee.domain.vo.HotSecurityMeetingAttendeeVo;
import com.hotwj.platform.securityManagement.securityMeetingAttendee.service.IHotSecurityMeetingAttendeeService;
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.List;
import java.util.Map;

@Component
@RequiredArgsConstructor
public class SafetyMeetingProvider implements IReportDataProvider {

    private final ISysCompanyService sysCompanyService;
    private final PublicMapper publicMapper;
    private final IHotSecurityMeetingService securityMeetingService;
    private final IHotSecurityMeetingAttendeeService securityMeetingAttendeeService;
    private final OssService ossService;

    @Override
    public String getTemplateCode() {
        return "templates/securityManage/safetyMeetingRecord.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) || "signIn".equals(mode)) {
            Long meetingId = MapUtil.getLong(params, "meetingId");
            if (meetingId != null) {
                HotSecurityMeetingVo meeting = securityMeetingService.queryById(meetingId);
                if (meeting != null) {
                    HotSecurityMeetingAttendeeBo attendeeBo = new HotSecurityMeetingAttendeeBo();
                    attendeeBo.setCompanyId(companyId);
                    attendeeBo.setMeetingId(meetingId);
                    attendeeBo.setIsDeleted(0L);
                    List<HotSecurityMeetingAttendeeVo> attendees = securityMeetingAttendeeService.queryList(attendeeBo);

                    if (attendees != null) {
                        for (HotSecurityMeetingAttendeeVo attendee : attendees) {
                            if (attendee.getAttendeePhotoOssId() != null) {
                                attendee.setAttendeePhotoUrl(resolveMaybeOssUrl(String.valueOf(attendee.getAttendeePhotoOssId())));
                            }
                            if (attendee.getAttendeeSignOssId() != null) {
                                attendee.setAttendeeSignUrl(resolveMaybeOssUrl(String.valueOf(attendee.getAttendeeSignOssId())));
                            }
                        }
                    }

                    ctx.put("meeting", meeting);
                    ctx.put("attendees", attendees);
                }
            }
        }

        return ctx;
    }

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