/**
 * Copyright 2019 DH
 * All right reserved.
 * 项目名称: 运维系统
 * 创建日期:2022/8/22
 */
package org.springblade.modules.yjxt.business.warn.wrapper;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springblade.core.mp.support.BaseEntityWrapper;
import org.springblade.core.oss.model.BladeFile;
import org.springblade.core.tool.utils.BeanUtil;
import org.springblade.core.tool.utils.Func;
import org.springblade.core.tool.utils.SpringUtil;
import org.springblade.modules.baseinfo.region.whole.entity.BaseRegionInfoEntity;
import org.springblade.modules.baseinfo.region.whole.service.IBaseRegionInfoService;
import org.springblade.modules.baseinfo.rtu.service.IRtuBaseInfoService;
import org.springblade.modules.yjxt.business.resident.dto.DangerAreaResidentInfoDTO;
import org.springblade.modules.yjxt.business.resident.service.IDangerAreaResidentService;
import org.springblade.modules.yjxt.business.transfer.dto.DamageTransferPersonInfoDTO;
import org.springblade.modules.yjxt.business.transfer.service.IDamageTransferPersonService;
import org.springblade.modules.yjxt.business.warn.entity.OriginalWarningInfoEntity;
import org.springblade.modules.yjxt.business.warn.service.IOriginalWarningService;
import org.springblade.modules.yjxt.business.warn.vo.OriginalWarningInfoVO;
import org.springblade.utils.QRCodeUtils;


import java.io.File;
import java.io.FileInputStream;
import java.util.Objects;

/***
 * Date:2022/8/22
 * Title:文件所属模块（必须填写）
 * Description：对本文件的详细描述，原则上不能少于30字
 * @author dylan
 * @version 1.0
 * Remark：认为有必要的其他信息
 */
public class YjWarnInfoWrapper extends BaseEntityWrapper<OriginalWarningInfoEntity, OriginalWarningInfoVO> {
    public static YjWarnInfoWrapper build() {
        return new YjWarnInfoWrapper();
    }

    private static IRtuBaseInfoService rtuBaseInfoService;

    private static IRtuBaseInfoService getRtuBaseInfoService() {
        if (rtuBaseInfoService == null) {
            rtuBaseInfoService = SpringUtil.getBean(IRtuBaseInfoService.class);
        }
        return rtuBaseInfoService;
    }

    private static IDangerAreaResidentService dangerAreaResidentService;

    private static IDangerAreaResidentService getDangerAreaResidentService() {
        if (dangerAreaResidentService == null) {
            dangerAreaResidentService = SpringUtil.getBean(IDangerAreaResidentService.class);
        }
        return dangerAreaResidentService;
    }

    private static IDamageTransferPersonService damageTransferPersonService;

    private static IDamageTransferPersonService getDamageTransferPersonService() {
        if (damageTransferPersonService == null) {
            damageTransferPersonService = SpringUtil.getBean(IDamageTransferPersonService.class);
        }
        return damageTransferPersonService;
    }

    private static IOriginalWarningService originalWarningService;

    private static IOriginalWarningService getOriginalWarningService() {
        if (originalWarningService == null) {
            originalWarningService = SpringUtil.getBean(IOriginalWarningService.class);
        }
        return originalWarningService;
    }

    private static IBaseRegionInfoService baseRegionInfoService;

    private static IBaseRegionInfoService getBaseRegionInfoService() {
        if (baseRegionInfoService == null) {
            baseRegionInfoService = SpringUtil.getBean(IBaseRegionInfoService.class);
        }
        return baseRegionInfoService;
    }

    @Override
    public OriginalWarningInfoVO entityVO(OriginalWarningInfoEntity entity) {
        OriginalWarningInfoVO vo = Objects.requireNonNull(BeanUtil.copy(entity, OriginalWarningInfoVO.class));
        return this.entityVO(vo);
    }

    public OriginalWarningInfoVO entityVO(OriginalWarningInfoVO vo) {

        if (Func.notNull(vo.getWarnAdcd())) {
            LambdaQueryWrapper<BaseRegionInfoEntity> queryWrapper = Wrappers.<BaseRegionInfoEntity>query().lambda();
            queryWrapper.eq(BaseRegionInfoEntity::getAdcd, vo.getWarnAdcd());
            queryWrapper.last("limit 1");
            BaseRegionInfoEntity entity = getBaseRegionInfoService().getOne(queryWrapper);
            if (Func.notNull(entity)) {
                vo.setAdName(entity.getAdnm());

                String ancestores = entity.getAncestors();
                StringBuilder fullCode = new StringBuilder();
                String[] codes = ancestores.split(",");
                for (int i = 2; i < codes.length; i++) {
                    if (fullCode.length() > 0) {
                        fullCode.append("/");
                    }
                    queryWrapper = Wrappers.<BaseRegionInfoEntity>query().lambda();
                    queryWrapper.eq(BaseRegionInfoEntity::getAdcd, codes[i]);
                    queryWrapper.last("limit 1");
                    BaseRegionInfoEntity region = getBaseRegionInfoService().getOne(queryWrapper);
                    fullCode.append(region.getAdnm());
                }
                if (fullCode.length() > 0) {
                    fullCode.append("/");
                }
                if (Func.notNull(vo.getAdName())) {

                    fullCode.append(vo.getAdName());
                }
                vo.setFullAadName(fullCode.toString());
            }

        }

        if (Func.isNull(vo.getWarnQrUrl())) {
            String qrUrl = getOriginalWarningService().createQr(vo.getWarnId());
            if (Func.notNull(qrUrl)) {
                vo.setWarnQrUrl(qrUrl);
                getOriginalWarningService().updateById(vo);
            }
        }
        DangerAreaResidentInfoDTO residentInfoDTO = new DangerAreaResidentInfoDTO();
        residentInfoDTO.setDangerAreaCode(vo.getWarnAdcd());
        long totalTransferPerson = getDangerAreaResidentService().residentCount(residentInfoDTO);

        DamageTransferPersonInfoDTO transferPersonInfoDTO = new DamageTransferPersonInfoDTO();
        transferPersonInfoDTO.setWarnId(Func.toStr(vo.getWarnId()));
        long safetyTransferPerson = getDamageTransferPersonService().transferPersonCount(transferPersonInfoDTO);

        vo.setTotalTransferPerson(totalTransferPerson);
        vo.setSafetyTransferPerson(safetyTransferPerson);

        return vo;
    }
}
