package com.usoftchina.uas.office.qywx.controller;

import com.usoftchina.uas.office.entity.DataCenter;
import com.usoftchina.uas.office.jdbc.DataSourceHolder;
import com.usoftchina.uas.office.qywx.service.UasAnalysisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

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

/**
 * @author yingp
 * @date 2020/2/19
 */
@RestController
public class QywxAnalysisController {
    @Autowired
    private UasAnalysisService uasAnalysisService;
    DataCenter dataCenter = DataCenter.INSTANCE;

    /**
     * 获取部门总结
     *
     * @param instanceid
     * @return
     */
    @PostMapping(value = "/api/analysis/getAnalysisByWeek")
    public Map<String,Object>  getAnalysisByWeek(int instanceid) {
        Map<String,Object> res = new HashMap<>();
        List<Map<String,Object>> resList  = new ArrayList<Map<String,Object>>();
        DataCenter dataCenter = DataCenter.INSTANCE;
        if (null == dataCenter) {
            res.put("success", false);
            res.put("data", resList);
            return res;
        }
        try {
            if (null != dataCenter.getUsername() && null != dataCenter.getPassword() && null != dataCenter.getUrl()) {
                DataSourceHolder.set(dataCenter);
                resList = uasAnalysisService.getAnalysisByWeek(instanceid);
            }
        } finally {
            DataSourceHolder.clear();
        }
        res.put("success", true);
        res.put("data", resList);
        return res;
    }
    /**
     * 获取个人总结
     *
     * @param instanceid
     * @return
     */
    @PostMapping(value = "/api/analysis/getAnalysisByPerson")
    public Map<String,Object>  getAnalysisByPerson(int instanceid,String emCode) {
        Map<String,Object> res = new HashMap<>();
        Map<String,Object> dataRes = new HashMap<>();
        DataCenter dataCenter = DataCenter.INSTANCE;
        if (null == dataCenter) {
            res.put("success", false);
            res.put("data", dataRes);
            return res;
        }
        try {
            if (null != dataCenter.getUsername() && null != dataCenter.getPassword() && null != dataCenter.getUrl()) {
                DataSourceHolder.set(dataCenter);
                dataRes = uasAnalysisService.getAnalysisByPerson(instanceid,emCode);
            }
        } finally {
            DataSourceHolder.clear();
        }
        res.put("success", true);
        res.put("data", dataRes);
        return res;
    }

}
