package com.malk.pake.controller;

import com.alibaba.fastjson.JSONObject;
import com.malk.server.common.McR;
import com.malk.service.dingtalk.DDClient;
import com.malk.service.dingtalk.DDClient_Alibtrip;
import com.malk.service.dingtalk.DDClient_Workflow;
import com.malk.utils.PublicUtil;
import com.malk.utils.UtilMap;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

@RestController
@Slf4j
@RequestMapping("/alibtrip")
public class DingAlibtripController {

    @Autowired
    private DDClient ddClient;
    @Autowired
    private DDClient_Alibtrip ddClient_alibtrip;
    @Autowired
    private DDClient_Workflow ddClient_workflow;
    private String CODE="PROC-27EE84F7-EA7E-460C-B9F4-21D54DFD11AA";

    //商机跟进表单通过后同步创建商机档案表的页签明细
    @PostMapping("/project/add")
    public McR addProject(@RequestBody JSONObject param){
        log.info("商旅&OA新增项目接口,参数：{}",param);
        if(PublicUtil.isNull(param,"projectId","projectName")) return McR.errorNullPointer();
        String result="";
        try {
            result=ddClient_alibtrip.addProject(ddClient.getAccessToken(),param.getString("projectName"),param.getString("projectId"));
        }catch (Exception e){
            e.printStackTrace();
        }
        create(param.getString("projectName"),param.getString("projectId"),String.valueOf(param.getJSONArray("userId")));
        return McR.success(result);
    }

    private void create(String name,String id,String userId){
        // 用户编号修改为 王晓磊
        ddClient_workflow.doProcessInstances(ddClient.getAccessToken(),"011329445929268386",CODE, Arrays.asList(
                dataName(name),dataId(id),dataPeople(userId)
                ),
                UtilMap.map("dept_id","-1"));
    }

    private Map dataName(String name){
        return UtilMap.map("name, value","项目名称",name);
    }

    private Map dataId(String id){
        return UtilMap.map("name, value","项目编号",id);
    }

    private Map dataPeople(String userId){
        return UtilMap.map("name, value","项目经理",userId);
    }

}
