package com.mortals.xhx.thread;

import cn.hutool.core.codec.Base64;
import com.alibaba.fastjson.JSONObject;
import com.mortals.framework.common.Rest;
import com.mortals.framework.service.ICacheService;
import com.mortals.xhx.busiz.req.accept.ComplexAcceptReq;
import com.mortals.xhx.common.code.YesNoEnum;
import com.mortals.xhx.common.utils.BeanUtil;
import com.mortals.xhx.protocol.complex.matter.model.req.*;
import com.mortals.xhx.protocol.complex.matter.model.rsp.AcceptRspInfo;
import com.mortals.xhx.protocol.complex.matter.service.IComplexMatterService;
import com.mortals.xhx.utils.SpringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.util.ObjectUtils;

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

import static com.mortals.xhx.common.key.Constant.REDIS_RESP_TIMEOUT;
import static com.mortals.xhx.common.key.RedisKey.KEY_EVENT_IMPLEMENTATION_RESP;

/**
 * 自助接件服务
 *
 * @author: zxfei
 * @date: 2023/12/3 14:04
 */
@Slf4j
public class ImplementAcceptSendTask implements Runnable {

    private ComplexAcceptReq complexAcceptReq;

    private String deviceLogo;

    private IComplexMatterService complexMatterService;

    private ICacheService cacheService;


    public ImplementAcceptSendTask(String deviceLogo, ComplexAcceptReq complexAcceptReq) {
        this.complexAcceptReq = complexAcceptReq;
        this.deviceLogo = deviceLogo;
        complexMatterService = SpringUtils.getBean(IComplexMatterService.class);
        cacheService = SpringUtils.getBean(ICacheService.class);
    }

    @Override
    public void run() {
        try {
            log.info("自助接件服务");

            //遍历 上传附件
            List<MaterialsItem> materialsItemList = complexAcceptReq.getMaterials().stream().map(item -> {
                MaterialsItem materialsItem = new MaterialsItem();
                //base64
                String materialContent = item.getMaterialContent();
                if (!ObjectUtils.isEmpty(materialContent)) {
                    byte[] bytes = Base64.decode(materialContent);
                    Rest<String> uploadFileRest = complexMatterService.uploadFile(deviceLogo, bytes, item.getUploadFileName());
                    if (uploadFileRest.getCode() == YesNoEnum.YES.getValue()) {
                        item.setMaterialPath(uploadFileRest.getData());
                        item.setMaterialContent("");
                    }
                }
                BeanUtils.copyProperties(item, materialsItem, BeanUtil.getNullPropertyNames(item));
                return materialsItem;
            }).collect(Collectors.toList());


            AcceptReq acceptReq = new AcceptReq();
            BeanUtils.copyProperties(complexAcceptReq, acceptReq, BeanUtil.getNullPropertyNames(complexAcceptReq));
            acceptReq.setMaterials(materialsItemList);

            if (!ObjectUtils.isEmpty(complexAcceptReq.getHandlings())) {
                List<HandlingsItem> handlingsItemList = complexAcceptReq.getHandlings().stream().map(item -> {
                    HandlingsItem handlingsItem = new HandlingsItem();
                    BeanUtils.copyProperties(item, handlingsItem);
                    return handlingsItem;
                }).collect(Collectors.toList());
                acceptReq.setHandlings(handlingsItemList);
            }

            if (!ObjectUtils.isEmpty(complexAcceptReq.getApplicantLegal())) {
                ApplicantLeagal applicantLeagal = new ApplicantLeagal();
                BeanUtils.copyProperties(complexAcceptReq.getApplicantLegal(), applicantLeagal);
                acceptReq.setApplicantLegal(applicantLeagal);
            }
            if (!ObjectUtils.isEmpty(complexAcceptReq.getApplicantPersonal())) {
                ApplicantPersonal applicantPersonal = new ApplicantPersonal();
                BeanUtils.copyProperties(complexAcceptReq.getApplicantPersonal(), applicantPersonal);
                acceptReq.setApplicantPersonal(applicantPersonal);
            }


            Rest<List<AcceptRspInfo>> rest = complexMatterService.accept(deviceLogo, acceptReq);
            cacheService.lpushForTime(KEY_EVENT_IMPLEMENTATION_RESP + complexAcceptReq.getNonce(), REDIS_RESP_TIMEOUT, JSONObject.toJSONString(rest));

/*
            Rest<List<MaterialInfo>> rest = complexMatterService.getMaterialByEvent(deviceLogo, implementationReq.getEventId());
            cacheService.lpushForTime(KEY_EVENT_IMPLEMENTATION_RESP + implementationReq.getNonce(), REDIS_RESP_TIMEOUT, JSONObject.toJSONString(rest));
*/

        } catch (Exception e) {
            log.error("发送异常：" + e);
        }


    }

}