package com.weaver.seconddev.meishi.service.impl;

import com.weaver.seconddev.meishi.constant.Constants;
import com.weaver.seconddev.meishi.esb.dao.CreatePerformanceDao;
import com.weaver.seconddev.meishi.esb.util.CreatePerformanceUtil;
import com.weaver.seconddev.meishi.service.CreatePerformanceService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CreatePerformanceServiceImpl implements CreatePerformanceService {

    private final Logger log = LoggerFactory.getLogger(CreatePerformanceServiceImpl.class);


    @Autowired
    CreatePerformanceUtil createPerformanceUtil;

    @Autowired
    CreatePerformanceDao createPerformanceDao;

    @Override
    public Map<String, Object> createWorkflowV1(String tenant_key,Map<String,Object> params) {
        Map<String, Object> dataMap = new HashMap<String,Object>();

        Long form_id = Long.valueOf(String.valueOf(params.get("form_id")));
        Long workflowid = Long.valueOf(String.valueOf(params.get("workflowid")));
        String form_data_id = String.valueOf(params.get("form_data_id"));
        Long userid = Long.valueOf(String.valueOf(params.get("userid")));

        log.error("createWorkflowV1:form_id:"+form_id);
        log.error("createWorkflowV1:workflowid:"+workflowid);
        log.error("createWorkflowV1:form_data_id:"+form_data_id);
        log.error("createWorkflowV1:userid:"+userid);

        String requestids = "";
        int errcount = 0;
        try{
            Map<String,Object> mainData = createPerformanceDao.queryMainDataByFormDataId(Constants.TENANT_KEY,form_data_id);
            List<Map<String,Object>> dt1List = createPerformanceDao.queryDt1DataByFormDataId(Constants.TENANT_KEY,form_data_id);
            log.error("createWorkflowV1:dt1List:"+dt1List.size());

            for(int i=0;i<dt1List.size();i++){
                Map<String,Object> createrData = dt1List.get(i);
                String dt1id =  String.valueOf(createrData.get("id"));
                log.error("createWorkflowV1:dt1id:"+dt1id);
                Map<String,Object> reslutMap = createPerformanceUtil.doCreateWorkflowByUserData(Constants.TENANT_KEY,form_id,mainData,createrData,form_data_id,workflowid,userid);
                if (reslutMap.containsKey("code")) {
                    String code = String.valueOf(reslutMap.get("code"));
                    String msg = String.valueOf(reslutMap.get("msg"));
                    if ("200".equals(code)) {
                        String requestid = String.valueOf(reslutMap.get("requestid"));

                        log.error("createWorkflowV1:requestid:"+requestid);
                        log.error("createWorkflowV1:form_data_id:"+form_data_id);

                        createPerformanceDao.updateRequestid2FormTable(Constants.TENANT_KEY,requestid,dt1id,"");
                        requestids += StringUtils.isBlank(requestids) ? requestid :","+requestid ;
                    } else {
                        createPerformanceDao.updateRequestid2FormTable(Constants.TENANT_KEY,"",dt1id,msg);
                        // return WeaResult.fail(500,"部分流程创建失败");
                        errcount++;
                    }
                }
            }
        }catch (Exception e){
        }
        dataMap.put("errcount",errcount);
        dataMap.put("requestids",requestids);
        return dataMap;
    }

}
