package com.malk.gewu.controller;

/**
 * 错误抛出与拦截详见 CatchException
 */

import com.alibaba.fastjson.JSON;
import com.malk.gewu.service.GWService;
import com.malk.server.aliwork.YDConf;
import com.malk.server.aliwork.YDParam;
import com.malk.server.common.McR;
import com.malk.service.aliwork.YDClient;
import com.malk.utils.UtilDateTime;
import com.malk.utils.UtilMap;
import com.malk.utils.UtilServlet;
import lombok.Synchronized;
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.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping
public class GWController {
    
    @Autowired
    private GWService gwService;

    /**
     * 同步花名册信息
     */
    @Synchronized
    @PostMapping("sync")
    McR syncRoster() {

        gwService.syncRoster();
        return McR.success();
    }

    @Autowired
    private YDClient ydClient;

    /**
     * 拆分培训记录表
     */
    @Synchronized
    @PostMapping("copy")
    McR copyTrain(HttpServletRequest request) {

        Map data = UtilServlet.getParamMap(request);
        log.info("拆分培训记录表, {}", data);
        Map formData = (Map) JSON.parse(String.valueOf(data.get("formData")));
        List<String> userIds = (List<String>) JSON.parse(String.valueOf(formData.get("employeeField_limyct1y")));
        for (String userId : userIds) {
            Map form = UtilMap.putAll(UtilMap.empty(), formData);
            form.put("employeeField_limyct1y", Arrays.asList(userId));
            ydClient.operateData(YDParam.builder()
                    .formUuid("FORM-AC666081OYZE6LMAELILADKXFZO8231E9GLNL5")
                    .formDataJson(JSON.toJSONString(form))
                    .build(), YDConf.FORM_OPERATION.create);
        }

        return McR.success();
    }

    /**
     * 创建提醒日程
     */
    @PostMapping("schedule")
    McR schedule(HttpServletRequest request) {

        Map data = UtilServlet.getParamMap(request);
        log.info("创建提醒日程, {}", data);

        List<String> userIds = (List<String>) JSON.parse(UtilMap.getString(data, "userIds"));
        Date sTime = UtilDateTime.parseDateTime(UtilDateTime.formatDate(new Date(UtilMap.getLong(data, "sTime"))) + " 09:00:000");
        Date eTime = UtilDateTime.parseDateTime(UtilDateTime.formatDate(new Date(UtilMap.getLong(data, "eTime"))) + " 18:00:000");
        String organizer = UtilMap.getString(data, "organizer");

        gwService.createSchedule(UtilMap.getString(data, "summary"), UtilMap.getString(data, "description"), userIds, sTime, eTime, organizer);
        return McR.success();
    }


    /**
     * test
     */
    @PostMapping("test")
    McR test() {

//        String path = JSONUtil.class.getClassLoader().getResource("templates/personnel").getPath();
//        log.info("xxx, {}", path);
//        String json = UtilFile.readJsonStringFromFile(path);
//
//        return McR.success(JSON.parse(json));

//        return McR.success(UtilFile.readJsonObjectFromResource("templates/personnel"));
//        return McR.success(UtilFile.readJsonObjectFromResource("static/json/personnel.json"));

        gwService.test();
        return McR.success();
    }


}
