package com.mortals.xhx.modules.information.task;

import cn.hutool.http.HttpUtil;
import com.alibaba.excel.util.CollectionUtils;
import com.alibaba.fastjson.JSONObject;
import com.mortals.framework.exception.AppException;
import com.mortals.framework.service.ITask;
import com.mortals.framework.service.ITaskExcuteService;
import com.mortals.xhx.common.code.NodeTypeEnum;
import com.mortals.xhx.modules.arameter.model.ArameterEntity;
import com.mortals.xhx.modules.arameter.service.ArameterService;
import com.mortals.xhx.modules.information.model.InformationEntity;
import com.mortals.xhx.modules.information.service.InformationService;
import com.mortals.xhx.modules.service.QueryInformationServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author shenxin
 * @Date: 2021/11/17-16:28
 * @Description:办件数据推送蓉易办(办件环节)
 */
@Slf4j
@Component("rybSubmitLinkDataInformationTaskImpl")
public class RybSubmitLinkDataInformationTaskImpl implements ITaskExcuteService {

    @Autowired
    private InformationService informationService;

    @Autowired
    private ArameterService arameterService;

    @Autowired
    private QueryInformationServiceImpl queryInformationService;

    @Override
    public void excuteTask(ITask task) throws AppException {
        //1.查询上月的表是否存在未推送的数据
        List<InformationEntity> informationEntities =
                queryInformationService.queryDataThatHasNotBeenPushed(NodeTypeEnum.蓉易办环节.getCode(), "1", "1", task.getExcuteParam());
        //办件环节数据推送蓉易办
        this.submitLinkData(informationEntities);
    }

    private void submitLinkData(List<InformationEntity> informationEntities) {
        log.info("开始推送办件环节数据");
        if(CollectionUtils.isEmpty(informationEntities)){
            log.info("没有需要推送办件环节数据");
            return;
        }
        List<InformationEntity> collect = informationEntities.parallelStream().map(e -> {
            ArameterEntity extCache = new ArameterEntity();
            extCache.setSysCode(e.getSystemCode());
            extCache = arameterService.find(extCache).get(0);
            //组装参数
            HashMap<String, Object> stringObjectHashMap = assemblyParametersLink(e);
            Map<String, Object> params = new HashMap<>();
            params.put("jsonData", JSONObject.toJSONString(stringObjectHashMap));
            String appKey = HttpUtil
                    .createPost(extCache.getDecideAdress()).form(params)//蓉易办
                    .header("AppKey", extCache.getSysCode())
                    .header("Content-Type", "application/x-www-form-urlencoded;charset=utf-8")
                    .execute()
                    .charset("utf-8")
                    .body();
            JSONObject jsonObject = JSONObject.parseObject(appKey);
            e.setBizStatus(NodeTypeEnum.已提交.getDesc());
            e.setNodeType(NodeTypeEnum.已提交.getCode());
            if ("200".equals(jsonObject.getString("code"))) {
                log.info("提交成功:" + jsonObject);
            } else {
                log.info("提交失败:" + jsonObject);
            }
            e.setProcessStatusName(NodeTypeEnum.蓉易办环节.getValue());
            return e;
        }).collect(Collectors.toList());
        log.info("办件环节推送完成");
        informationService.update(collect, null);
    }

    private HashMap<String, Object> assemblyParametersLink(InformationEntity e) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        HashMap<String, Object> dataMap = new HashMap<>(16);
        dataMap.put("token", "Epoint_WebSerivce_**##0601");
        HashMap<String, Object> baseInfoMap = new HashMap<>(16);
        baseInfoMap.put("flowsn", e.getOfficeCode());
        baseInfoMap.put("operatetype", "80");
        baseInfoMap.put("startdate", format.format(e.getGenTime()));
        baseInfoMap.put("enddate", format.format(e.getCompleteTime()));
        baseInfoMap.put("operateusername", e.getAcceptName());
        baseInfoMap.put("operateuserphone", e.getAcceptPhone());
        baseInfoMap.put("operateuseridcard", e.getAcceptId());
        baseInfoMap.put("areacode", e.getAreaCode());
        baseInfoMap.put("handleTime", format.format(e.getCreateTime()));
        baseInfoMap.put("nodeStatus", "1");
        dataMap.put("params", baseInfoMap);
        return dataMap;
    }


    @Override
    public void stopTask(ITask task) throws AppException {

    }

}
