package com.engine.htsc.contractmanagement.web;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.engine.common.util.ServiceUtil;

import com.engine.htsc.contractmanagement.service.ContractService;
import com.engine.htsc.contractmanagement.service.impl.ContractServiceImpl;
import com.engine.htsc.payment.util.HttpUtil;
import com.engine.htsc.trip.util.AjaxResult;
import weaver.general.BaseBean;
import weaver.interfaces.htsc.comInfo.PropBean;

import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ContractAction extends BaseBean {

    private ContractService getServices(){
        return ServiceUtil.getService(ContractServiceImpl.class);
    }
    /**
     * 查询变更记录
     * @param params
     * modeid(模块id),billid(数据id),formid(表id)
     * @return
     */
    @POST
    @Path("/getChangeRecord")
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public AjaxResult<List<Map<String, String>>> getChangeRecord(Map<String, Object> params) {
        writeLog("sync fund status starting... params: " + JSON.toJSONString(params));
        try {
            List  dataList = new ArrayList<>();
            Map<String, Object> responseMsg = this.getServices()
                    .getChangeRecord(params);
            dataList.add(responseMsg);

            return AjaxResult.ok(dataList);
        } catch (Exception e) {
            e.printStackTrace();
            writeLog("sync fund status fail, catch error: ", e);
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 客户搜索
     * @param params
     * type：0：(getCustByNameOrCertNo获取证件号码),1：(getCustInfo根据证件号码获取工商信息)
     * @return
     */
    @POST
    @Path("/getCustomInfo")
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public AjaxResult<List<Map<String, Object>>> getCustomInfo(Map<String, String> params) {
        writeLog("sync fund status starting... params: " + JSON.toJSONString(params));
        try {
            List<Map<String, Object>> dataList = new ArrayList<>();
            String type = params.get("type");
            PropBean pb = new PropBean();
            JSONObject jsonObject=new JSONObject();
            jsonObject.putAll(params);
            String oahost = null;
            if (type.equals("0")){
                oahost = pb.getPropName("getCustByNameOrCertNo");
            }else if (type.equals("1")){
                oahost = pb.getPropName("getCustInfo");
            }
            if (oahost != null){
                String responsestr = HttpUtil.postApplicationJson(oahost,jsonObject.toJSONString());
                if (!"".equals(responsestr)){
                    JSONObject jsonObjectstr = JSONObject.parseObject(responsestr);
                    if (!"0".equals(jsonObjectstr.getString("code"))){
                        return AjaxResult.error(jsonObjectstr.getString("msg"));
                    }
                    Map<String,Object> resultData = (Map<String, Object>) jsonObjectstr.get("resultData");
                    dataList.add(resultData);
                }
            }

            return AjaxResult.ok(dataList);
        } catch (Exception e) {
            e.printStackTrace();
            writeLog("sync fund status fail, catch error: ", e);
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 查看是否已存在对手方
     * @param params
     * @return
     */
    @POST
    @Path("/checkRepeatData")
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public AjaxResult<List<Map<String, Object>>> checkRepeatData(Map<String, Object> params) {
        writeLog("sync fund status starting... params: " + JSON.toJSONString(params));
        try {
            List<Map<String, Object>> dataList = new ArrayList<>();
            Map<String, Object> responseMsg = this.getServices().checkRepeatData(params);
            dataList.add(responseMsg);
            if ("200".equals(String.valueOf(responseMsg.get("code")))){
                return AjaxResult.ok(dataList);
            }else {
                return AjaxResult.error("变更失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            writeLog("sync fund status fail, catch error: ", e);
            return AjaxResult.error(e.getMessage());
        }
    }

}
