package com.rongwei.sfcommon.sys.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rongwe.scentity.domian.AspCompanyCertificationsDo;
import com.rongwe.scentity.vo.UserMailOrgVo;
import com.rongwei.rwadmincommon.system.domain.SysDictDo;
import com.rongwei.rwadmincommon.system.service.SysDictService;
import com.rongwei.rwcommon.base.R;
import com.rongwei.rwcommon.utils.StringUtils;
import com.rongwei.safecommon.utils.JSCommonUtils;
import com.rongwei.sfcommon.sys.dao.AspCompanyCertificationsDao;
import com.rongwei.sfcommon.sys.dao.SaveCheckCommonDao;
import com.rongwei.sfcommon.sys.service.AspCompanyCertificationsService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static com.rongwei.sfcommon.utils.MlConstants.SAFETY_MANAGER_CODE;

/**
 *
 */
@Service
public class AspCompanyCertificationsServiceImpl extends ServiceImpl<AspCompanyCertificationsDao, AspCompanyCertificationsDo>
        implements AspCompanyCertificationsService {

    public static final String QUALIFICATION_CERTIFICATE_NOTIFY_STATUS = "qualificationCertificate";
    public static final String QUALIFICATION_CERTIFICATE_REMIND_TEMP = "如下资质证照即将过期:【%s】 \n";
    public static final String QUALIFICATION_CERTIFICATE_REMIND_ITEM = "证书编号:【%s】拥有者:【%s】证书类别:【%s】下次复审日期：【%tF】\n";
    private final Logger log = LoggerFactory.getLogger(this.getClass().getName());
    @Autowired
    private SaveCheckCommonDao saveCheckCommonDao;
    @Autowired
    private SysDictService dictService;

    @Override
    public R createRemind(String id) {

        JSCommonUtils.parameterCheck(() -> StringUtils.isBlank(id), "参数异常", "参数异常");
        List<AspCompanyCertificationsDo> aspCompanyCertificationsDos = this.baseMapper.selectBatchIds(Collections.singleton(id));
        this.sendRemind(aspCompanyCertificationsDos);
        return R.ok();
    }

    public void sendRemind(List<AspCompanyCertificationsDo> aspCompanyCertificationsDos) {
        if (aspCompanyCertificationsDos == null || aspCompanyCertificationsDos.isEmpty()) {
            log.debug("暂无需要发送通知的资质证照信息");
            return;
        }
        log.debug("需要发送通知提醒的资质证照信息为:{}", aspCompanyCertificationsDos);
        // 对数据按照tenantId 进行分组
        Map<String, List<AspCompanyCertificationsDo>> collect = aspCompanyCertificationsDos.stream().collect(Collectors.groupingBy(AspCompanyCertificationsDo::getTenantid));
        // 获取对应的提醒人信息
        List<String> roleCodes = Collections.singletonList(SAFETY_MANAGER_CODE);
        List<SysDictDo> dictsByType = dictService.getDictsByType("zslb");
        collect.forEach((k, v) -> {
            List<UserMailOrgVo> userInfoByTenantIdAndRoleCode = saveCheckCommonDao.getUserInfoByTenantIdAndRoleCode(k, roleCodes);
            String str = v.stream()
                    .map(data -> {
                        SysDictDo sysDictDo = dictsByType.stream().filter(dict -> dict.getValue().equals(data.getCertificatecategory())).findFirst().orElse(null);
                        return String.format(QUALIFICATION_CERTIFICATE_REMIND_ITEM, data.getCertificatenumber(),
                                data.getCertificateowner(), sysDictDo == null ? data.getCertificatecategory() : sysDictDo.getName(), data.getNextreviewdate());
                    })
                    .collect(Collectors.joining(""));
            JSCommonUtils.sendNotify("资质证照复审提醒", String.format(QUALIFICATION_CERTIFICATE_REMIND_TEMP, str), null,
                    userInfoByTenantIdAndRoleCode.stream().map(UserMailOrgVo::getId).collect(Collectors.toList()),
                    v.stream().map(AspCompanyCertificationsDo::getId).collect(Collectors.joining(",")),
                    QUALIFICATION_CERTIFICATE_NOTIFY_STATUS);
        });
    }
}




