package com.th.demo.service.impl.process;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.th.demo.mapping.develop.DevBussinessTableMapper;
import com.th.demo.mapping.develop.DevTablePropertyMapper;
import com.th.demo.mapping.process.BussinessMapper;
import com.th.demo.model.develop.DevBussinessTable;
import com.th.demo.model.develop.DevTableProperty;
import com.th.demo.model.system.Type;
import com.th.demo.service.process.BussinessService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by dell on 2019-01-09.
 */
@Service("BussinessService")
public class BussinessServiceImpl implements BussinessService {
    @Autowired
    DevTablePropertyMapper devTablePropertyMapper;
    @Autowired
    DevBussinessTableMapper devBussinessTableMapper;
    @Autowired
    BussinessMapper bussinessMapper;


    String result = "";

    @Override
    public String queryBussiness(String processInstanceId, String processDefkey) {
        result = Type.FAIL;
        String sql = "";
        String sqlTableName = "";
        StringBuffer sqlDBCol = new StringBuffer("");
        List<DevBussinessTable> listBussinessTable = devBussinessTableMapper.selectByProcessName(processDefkey);
        if (listBussinessTable != null && listBussinessTable.size() == 1) {
            sqlTableName = listBussinessTable.get(0).getDbTableName();
            List<DevTableProperty> listTableProperty = devTablePropertyMapper.selectByTableName(listBussinessTable.get(0).getDbTableName());
            if (listTableProperty != null && listTableProperty.size() > 0) {
                sqlDBCol.append("'{'");
                for (int j = 0; j < listTableProperty.size(); j++) {
                    sqlDBCol.append("||'\"");
                    sqlDBCol.append(listTableProperty.get(j).getTableProp());
                    sqlDBCol.append("\":\"'||");
                    sqlDBCol.append(listTableProperty.get(j).getTableField());
                    sqlDBCol.append("||'\"");
                    if (j < listTableProperty.size() - 1) {
                        sqlDBCol.append(",'");
                    } else {
                        sqlDBCol.append("'");
                    }
                }
                sqlDBCol.append("||'}'");
                sql = "select " + sqlDBCol.toString() + " from " + sqlTableName + " where process_instance_id1 = '" + processInstanceId + "'";
                System.out.println("-----------------" + sql);
                String sqlResult = bussinessMapper.select(sql);
                System.out.println("-----------------" + sqlResult);
                JSONObject jsonObject = JSON.parseObject(sqlResult);
                System.out.println("-----------------" + jsonObject.get("id"));
                System.out.println("-----------------" + jsonObject.get("title"));
                System.out.println("-----------------" + jsonObject.get("content"));
                JSONArray array = new JSONArray();

                for (int j = 0; j < listTableProperty.size(); j++) {
                    JSONObject obj = new JSONObject();
                    obj.put("code",listTableProperty.get(j).getTableLabel());
                    obj.put("name",jsonObject.get(listTableProperty.get(j).getTableProp()));
                    array.add(obj);
                }

                result = JSON.toJSONString(array);
                System.out.println("-----------------" + result);
            } else {
                result = Type.FAIL + "未匹配到正确的业务数据！请检查DevTableProperty！";
            }
        } else {
            result = Type.FAIL + "未匹配到正确的业务数据！请检查DevBussinessTable！";
        }
        return result;
    }
}
