package com.engine.htsc.contractmanagement.web;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.engine.common.util.ParamUtil;
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.trip.util.AjaxResult;
import weaver.general.BaseBean;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.*;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.ws.rs.core.Response;

public class ContractExternalinterfaceAction extends BaseBean {


    private ContractService getServices(){
        return ServiceUtil.getService(ContractServiceImpl.class);
    }
    /**
     * 根据条件查询机构
     * custId、id二选一
     * @return
     */
    @GET
    @Path("/orginfo/find")
    @Produces({"text/plain;charset=utf-8"})
    public Response getDataByCustId(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        try {
            Map<String, Object> params = ParamUtil.request2Map(request);
            writeLog("sync fund status starting... params: " + JSON.toJSONString(params));
            Map<String, Object> responseMap = getServices().queryDataByCustId(params);
            return Response.ok(JSONObject.toJSONString(responseMap)).build();
        } catch (Exception e) {
            e.printStackTrace();
            writeLog("sync fund status fail, catch error: ", e);
            return Response.status(Response.Status.BAD_REQUEST).entity(e.getMessage()).build();
        }

    }

    /**
     * 根据条件查询机构证件
     * certType(证件类型，表字段ZJLX)、certNo(证件号码，表字段ZJHM)
     * @return
     */
    @GET
    @Path("/certificate/find")
    @Produces({"text/plain;charset=utf-8"})
    public Response getDataByCertNo(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        try {
            Map<String, Object> params = ParamUtil.request2Map(request);
            writeLog("sync fund status starting... params: " + JSON.toJSONString(params));
            Map<String, Object> responseMap = getServices().queryDataByCertNo(params);

            return Response.ok(JSONObject.toJSONString(responseMap)).build();
        } catch (Exception e) {
            e.printStackTrace();
            writeLog("sync fund status fail, catch error: ", e);
            return Response.status(Response.Status.BAD_REQUEST).entity(e.getMessage()).build();
        }

    }

    /**
     * 新增对手方主表
     * @param params
     * @return
     */
    @POST
    @Path("/orginfo/add")
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public Map<String, Object> addOrginfo(Map<String, Object> params) {
        writeLog("sync fund status starting... params: " + JSON.toJSONString(params));
        try {
            List  dataList = new ArrayList<>();
            Map<String, Object> responseMsg = this.getServices()
                    .addOrginfo(params);
            dataList.add(responseMsg);
            return responseMsg;
        } catch (Exception e) {
            e.printStackTrace();
            writeLog("sync fund status fail, catch error: ", e);
            Map<String,Object> errorMsg = new HashMap<>();
            errorMsg.put("code",500);
            errorMsg.put("message",e.getMessage());
            return errorMsg;
        }
    }
    /**
     * 新增对手方证件信息表
     * @param params
     * @return
     */
    @POST
    @Path("/certificate/add")
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public Map<String, Object> addCertificate(Map<String, Object> params) {
        writeLog("sync fund status starting... params: " + JSON.toJSONString(params));
        try {
            List  dataList = new ArrayList<>();
            Map<String, Object> responseMsg = this.getServices()
                    .addCertificate(params);
            dataList.add(responseMsg);

            return responseMsg;
        } catch (Exception e) {
            e.printStackTrace();
            writeLog("sync fund status fail, catch error: ", e);
            Map<String,Object> errorMsg = new HashMap<>();
            errorMsg.put("code",500);
            errorMsg.put("message",e.getMessage());
            return errorMsg;
        }
    }

    /**
     * 更新机构表信息
     * @param params
     * @return
     */
    @POST
    @Path("/orginfo/update")
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public Map<String, Object> updateOrginfo(Map<String, Object> params) {
        writeLog("sync fund status starting... params: " + JSON.toJSONString(params));
        try {
            List  dataList = new ArrayList<>();
            Map<String, Object> responseMsg = this.getServices()
                    .updateOrginfo(params);
            dataList.add(responseMsg);

            return responseMsg;
        } catch (Exception e) {
            e.printStackTrace();
            writeLog("sync fund status fail, catch error: ", e);
            Map<String,Object> errorMsg = new HashMap<>();
            errorMsg.put("code",500);
            errorMsg.put("message",e.getMessage());
            return errorMsg;
        }
    }
    /**
     * 逻辑删除机构表信息
     * @param params
     * @return
     */
    @POST
    @Path("/orginfo/delete")
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public Map<String, Object> deleteOrginfo(Map<String, Object> params) {
        writeLog("sync fund status starting... params: " + JSON.toJSONString(params));
        try {
            List  dataList = new ArrayList<>();
            Map<String, Object> responseMsg = this.getServices()
                    .deleteOrginfo(params);
            dataList.add(responseMsg);

            return responseMsg;
        } catch (Exception e) {
            e.printStackTrace();
            writeLog("sync fund status fail, catch error: ", e);
            Map<String,Object> errorMsg = new HashMap<>();
            errorMsg.put("code",500);
            errorMsg.put("message",e.getMessage());
            return errorMsg;
        }
    }

    /**
     * 更新机构证件信息
     * @param params
     * @return
     */
    @POST
    @Path("/certificate/update")
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public Map<String, Object> updateCertificate(Map<String, Object> params) {
        writeLog("sync fund status starting... params: " + JSON.toJSONString(params));
        try {
            List  dataList = new ArrayList<>();
            Map<String, Object> responseMsg = this.getServices()
                    .updateCertificate(params);
            dataList.add(responseMsg);

            return responseMsg;
        } catch (Exception e) {
            e.printStackTrace();
            writeLog("sync fund status fail, catch error: ", e);
            Map<String,Object> errorMsg = new HashMap<>();
            errorMsg.put("code",500);
            errorMsg.put("message",e.getMessage());
            return errorMsg;
        }
    }

    /**
     * 删除机构证件信息
     * @param params
     * @return
     */
    @POST
    @Path("/certificate/delete")
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public Map<String, Object> deleteCertificate(Map<String, Object> params) {
        writeLog("sync fund status starting... params: " + JSON.toJSONString(params));
        try {
            List  dataList = new ArrayList<>();
            Map<String, Object> responseMsg = this.getServices()
                    .deleteCertificate(params);
            dataList.add(responseMsg);

            return responseMsg;
        } catch (Exception e) {
            e.printStackTrace();
            writeLog("sync fund status fail, catch error: ", e);
            Map<String,Object> errorMsg = new HashMap<>();
            errorMsg.put("code",500);
            errorMsg.put("message",e.getMessage());
            return errorMsg;
        }
    }

}
