package com.zcloud.controller.xgf;

import com.zcloud.controller.base.BaseController;
import com.zcloud.entity.Page;
import com.zcloud.entity.PageData;
import com.zcloud.service.xgf.TrainUsersService;
import com.zcloud.service.xgf.TrainingBatchService;
import com.zcloud.util.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author fangjiakai
 * @date 2023/05/15 11:09
 */
@Controller
@RequestMapping("/flowTrain")
public class FlowTrainController extends BaseController {

    @Value("${preventionxgf.api.url}")
    private String xgfUrl;

    @Resource
    private TrainingBatchService trainingbatchService;

    @Resource
    private TrainUsersService trainUsersService;

    @RequestMapping(value = "/batchList")
    @ResponseBody
    public Object batchList(Page page) throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        PageData pd = this.getPageData();
        pd.put("CORPINFO_ID", Jurisdiction.getCORPINFO_ID());
        String KEYWORDS = pd.getString("KEYWORDS");                        //关键词检索条件
        if (Tools.notEmpty(KEYWORDS)) pd.put("KEYWORDS", KEYWORDS.trim());
        page.setPd(pd);
        List<PageData> varList = trainingbatchService.list(page);    //列出TrainingBatch列表
        map.put("varList", varList);
        map.put("page", page);
        map.put("result", "success");
        return map;
    }

    @RequestMapping(value = "/batchUserList")
    @ResponseBody
    public Object batchUserList(Page page) throws Exception {
        PageData request = this.getPageData();
        PageData condition = new PageData();
        condition.put("TRAINING_BATCH_ID", request.get("TRAINING_BATCH_ID"));
        page.setPd(condition);
        List<PageData> varList = trainingbatchService.batchUserlistPage(page);

        PageData response = new PageData();
        response.put("varList", varList);
        response.put("page", page);
        response.put("total", page.getTotalResult());
        response.put("result", "success");
        return response;
    }

    @RequestMapping(value = "/batchAudit")
    @ResponseBody
    public Object batchAudit(Page page) throws Exception {
        PageData pd = new PageData();
        pd = this.getPageData();
        Map result = HttpClientService.doPost(xgfUrl + "/openApi/trainingbatch/audit", pd);
        return result;
    }

    private String getPageUrl(Page page) {
        return "?showCount=" + page.getShowCount() + "&currentPage=" + page.getUrlCurrentPage();
    }

    /**
     * 分公司端-相关方流动人员培训审核-查看用户信息
     *
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/getUserDetailById")
    @ResponseBody
    public Object getCorpUserList() throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        String errInfo = "success";
        PageData pd = new PageData();
        pd = this.getPageData();
        Map result = HttpClientService.doPost(xgfUrl + "/api/user/getFloatPersonDetailById/", pd);
        return result;
    }

    @RequestMapping(value = "/approveUser")
    @ResponseBody
    public Object approveUser() throws Exception {
        Map<String,Object> map = new HashMap<String,Object>();
        PageData pd = this.getPageData();
        trainUsersService.approveUser(pd);
        map.put("result", "success");
        return map;
    }

    @RequestMapping(value = "/getDetailsById")
    @ResponseBody
    public Object getDetailsById() throws Exception {
        PageData request = this.getPageData();
        PageData condition = new PageData();
        condition.put("TRAINING_BATCH_ID", request.get("TRAINING_BATCH_ID"));
        PageData entity = trainingbatchService.findById(condition);
        switch (entity.getString("STEP_STATUS")) {
            case "0":
                if (entity.getString("TERRITORIALITY_USER_ID").equals(Jurisdiction.getUSER_ID())) {
                    entity.put("step", "TERRITORIALITY_STATE");
                }
                break;
            case "1":
                if (entity.getString("MANAGER_USER_ID").equals(Jurisdiction.getUSER_ID())) {
                    entity.put("step", "MANAGER_STATE");
                }
                break;
            case "2":
                if (entity.getString("SUPERVISION_USER_ID").equals(Jurisdiction.getUSER_ID())) {
                    entity.put("step", "SUPERVISION_STATE");
                }
                break;
            default:
                break;
        }
        PageData response = new PageData();
        response.put("pd", entity);
        response.put("result", "success");
        return response;
    }

    @RequestMapping(value = "/endApproval")
    @ResponseBody
    public Object endApproval() throws Exception {
        Map<String,Object> map = new HashMap<String,Object>();
        PageData pd = this.getPageData();
        trainUsersService.endApproval(pd);
        map.put("result", "success");
        return map;
    }

    @RequestMapping(value = "/oldBatchList")
    @ResponseBody
    public Object oldBatchList(Page page) throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        PageData pd = this.getPageData();
        pd.put("CORPINFO_ID", Jurisdiction.getCORPINFO_ID());
        String KEYWORDS = pd.getString("KEYWORDS");                        //关键词检索条件
        if (Tools.notEmpty(KEYWORDS)) pd.put("KEYWORDS", KEYWORDS.trim());
        page.setPd(pd);
        List<PageData> varList = trainingbatchService.oldBatchList(page);    //列出TrainingBatch列表
        map.put("varList", varList);
        map.put("page", page);
        map.put("result", "success");
        return map;
    }
}