package com.weaver.seconddev.meishi.controller;

import com.weaver.common.authority.annotation.WeaPermission;
import com.weaver.common.base.entity.result.WeaResult;
import com.weaver.ebuilder.datasource.api.entity.SqlParamEntity;
import com.weaver.seconddev.meishi.constant.Constants;
import com.weaver.seconddev.meishi.service.PerformanceSubmitService;
import com.weaver.seconddev.meishi.util.CommonUtils;
import com.weaver.seconddev.meishi.util.DatabaseUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/secondev/lamex/ceshi")
public class PerformanceTestController {
    private final Logger log = LoggerFactory.getLogger(PerformanceTestController.class);

    @Autowired
    public PerformanceSubmitService performanceSubmitService ;

    @Autowired
    public CommonUtils CommonUtils;

    @Autowired
    private DatabaseUtils databaseUtils;

    @GetMapping("/testWorkflow")
    @WeaPermission(publicPermission = true)
    public WeaResult<Object> testWorkflow(HttpServletRequest request){
        Map<String, Object> recordMap = new HashMap<String,Object>();
        log.error("testWorkflow");
        recordMap.put("test","test");
        return WeaResult.success(recordMap);
    }



    @GetMapping("/testWorkflow2")
    @WeaPermission(publicPermission = true)
    public WeaResult<Object> testWorkflow2(HttpServletRequest request){
        String groupId = "weaver-ebuilder-form-service";
        String sourceType = "LOGIC";
        Map<String, Object> fieldMap = new HashMap<String, Object>();
        try{
            String dataSql =" select id,form_id,title,data_key " +
                            " from form_field " +
                            " where form_id='1090784675255934977' \n" +
                            " and data_key in('tjr','tjrbm','tbnf','zq','bmlx','sfzdcf')\n" +
                            " and sub_form_id is null " +
                            " and tenant_key='tt4ysjaz41' \n" +
                            " and delete_type=0" ;

            log.error("queryFromTableField--dataSql:" + dataSql);
            Map<String, Object> result = databaseUtils.execute(sourceType, groupId, dataSql);
            List<Map<String, Object>> recordList = databaseUtils.getDataSourceList(result);
            log.error("queryFromTableField:"+recordList.size());
            for(Map<String,Object> recordMap : recordList ) {
                String data_key = String.valueOf(recordMap.get("data_key"));
                String id = String.valueOf(recordMap.get("id"));
                fieldMap.put(data_key,id);
            }
        }catch (Exception e){
            e.printStackTrace();
            log.error("queryFromTableField:e:" + e);
        }
        return WeaResult.success(fieldMap);
    }


    @GetMapping("/testWorkflow3")
    @WeaPermission(publicPermission = true)
    public WeaResult<Object> testWorkflow3(HttpServletRequest request){
        String groupId = "weaver-ebuilder-form-service";
        String sourceType = "LOGIC";
        Map<String, Object> fieldMap = new HashMap<String, Object>();
        try{
            String dataSql =" select id,form_id,title,data_key " +
                            " from form_field " +
                            " where form_id='1090784675255934977' \n" +
                            " and data_key in('kt')\n" +
                            " and sub_form_id in('1090792062725513218')\n"+
                            " and tenant_key='tt4ysjaz41' \n" +
                            " and delete_type=0" ;

            log.error("queryFromTableField--dataSql:" + dataSql);
            Map<String, Object> result = databaseUtils.execute(sourceType, groupId, dataSql);
            List<Map<String, Object>> recordList = databaseUtils.getDataSourceList(result);
            log.error("queryDetailTableField:"+recordList.size());
            for(Map<String,Object> recordMap : recordList ) {
                String data_key = String.valueOf(recordMap.get("data_key"));
                String id = String.valueOf(recordMap.get("id"));
                fieldMap.put(data_key,id);
            }
        }catch (Exception e){
            e.printStackTrace();
            log.error("queryFromTableField:e:" + e);
        }
        return WeaResult.success(fieldMap);
    }


    @GetMapping("/testWorkflow4")
    @WeaPermission(publicPermission = true)
    public WeaResult<Object> testWorkflow4(HttpServletRequest request){

        String groupId = "weaver-ebuilder-form-service";
        String sourceType = "LOGIC";
        List<Map<String, Object>> recordList = new ArrayList<Map<String, Object>>();

        try{
            String dataSql =" select h.id,h.username,h.department,k.name as deptname\n" +
                    " from eteams.employee h \n" +
                    " left join eteams.department k on k.id = h.department\n" +
                    " where h.id in(" +
                    "   select bmfzr " +
                    "   from ft_1090502818035523594 \n" +
                    "   where bmcj = 0 "  +
                    "   and tenant_key='tt4ysjaz41' \n" +
                    "   and delete_type=0 \n" +
                    " )  " +
                    " and h.tenant_key='tt4ysjaz41' \n" +
                    " and h.delete_type=0 \n" +
                    " and k.type='department' "+
                    " and k.tenant_key='tt4ysjaz41' \n" +
                    " and k.delete_type=0 \n" ;

            log.error("querykhfl--dataSql:" + dataSql);

            Map<String, Object> result = databaseUtils.execute(sourceType, groupId, dataSql);
            recordList = databaseUtils.getDataSourceList(result);
        }catch (Exception e){
            e.printStackTrace();
            log.error("querykhfl--hiredate:" + e);
        }

        return WeaResult.success(recordList);
    }


    @GetMapping("/createWorkflowV1")
    @WeaPermission(publicPermission = true)
    public WeaResult<Object> createWorkflowV1(HttpServletRequest request){
        log.error("createWorkflowV1");
        Map<String,Object> requestMap = CommonUtils.requestToMap(request);
        log.error("TENANT_KEY:"+Constants.TENANT_KEY);
        Map<String, Object> recordMap = performanceSubmitService.createWorkflowV1(Constants.TENANT_KEY,requestMap);
        return WeaResult.success(recordMap);
    }

}
