package com.malk.mc.controller;

import com.alibaba.fastjson.JSONObject;
import com.malk.server.common.McException;
import com.malk.server.common.McR;
import com.malk.server.dingtalk.DDConf;
import com.malk.server.dingtalk.DDR_New;
import com.malk.service.dingtalk.*;
import com.malk.utils.UtilHttp;
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.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("/dd")
public class McDdController {

    @Autowired
    private DDClient_Group ddClientGroup;
    @Autowired
    private DDClient_Report ddClientReport;
    @Autowired
    private DDClient ddClient;

    @Autowired
    private DDClient_Attendance ddClientAttendance;
    @Autowired
    private DDClient_Contacts ddClient_contacts;



    @PostMapping("/group/createGroupByTemp")
    public McR createGroupByTemp(@RequestBody JSONObject param){
        log.info("创建场景群:{}",param);
        McException.assertParamException_Null(param,"title","template_id","owner_user_id","user_ids");
        String id=ddClientGroup.createGroupByTemp(ddClient.getAccessToken(),param.getString("title"),
                param.getString("template_id"),param.getString("owner_user_id"),param.getString("user_ids"));
        return McR.success(id);
    }

    @PostMapping("/report/create")
    public McR createReport(@RequestBody JSONObject param){
        log.info("创建日志:{}",param);
        McException.assertParamException_Null(param,"template_id","to_chat","dd_from","userid","contents");
        JSONObject contents=param.getJSONObject("contents");
        List list=new ArrayList();
        for(String key:contents.keySet()){
            list.add(contents.getJSONObject(key));
        }
        String result=ddClientReport.createReport(ddClient.getAccessToken(),param.getString("to_userids"),param.getString("to_cids")
                ,param.getString("template_id"),param.getBoolean("to_chat"),param.getString("dd_from"),param.getString("userid"),
                list);
        return McR.success(result);
    }

    @PostMapping("/attendence/getAttendenceDetail")
    public McR getAttendenceDetail(@RequestBody JSONObject param) throws ParseException {
        log.info("考勤参数详情:{}",param);
        McException.assertParamException_Null(param,"checkDateFrom","checkDateTo","userIds");
        String accessToken = ddClient.getAccessToken();
        List userIds = (List) param.get("userIds");
        String checkDateFrom = param.getString("checkDateFrom")+" 00:00:00";
//        将字符串修改成时间
        String checkDateTo = param.getString("checkDateTo")+" 23:59:59";
        List<Map> results = ddClientAttendance.listAttendanceRecord(accessToken, userIds, checkDateFrom, checkDateTo);
        log.info("获取考勤详情:{}",results);
        Set<String> addressList = new HashSet<>();
        String address = "";
        for(Map result:results){
            System.out.println("result:"+result.get("userAddress"));
//            需要对result.get("userAddress")进行判断，如果为空则不添加
            if(result.get("userAddress")!=null){
                addressList.add(result.get("userAddress").toString()) ;
            }else{
                continue;
            }
        }
        System.out.println("address:"+addressList) ;
         address = addressList.stream().collect(Collectors.joining(","));
        return McR.success(address);
    }


    @PostMapping("/orgAccount/changeOrgAccount")
    public McR changeEnterpriseAccount(@RequestBody JSONObject param){
        log.info("修改企业账号:{}",param);
        McException.assertParamException_Null(param,"userId","targetUserId");
//        根据传过来的企业账号id查找下面的组织
        String accessToken = ddClient.getAccessToken();
        String  userId = UtilMap.getString(param,"userId");
       UtilHttp.doGet("https://api.dingtalk.com/v1.0/contact/orgAccounts/ownedOrganizations?userId="+userId,DDConf.initTokenHeader(accessToken),param);
        log.info("企业账号id:{}下的组织:{}",userId);



//        没有组织直接给这个企业账号办理离职

//        有组织将企业账号中所有组织转给传过来的企业账号id

        return McR.success();

    }

}
