package com.engine.htsc.receipt.web;

import com.alibaba.fastjson.JSON;
import com.engine.common.util.ParamUtil;
import com.engine.common.util.ServiceUtil;
import com.engine.htsc.receipt.service.ReceiptService;
import weaver.general.BaseBean;
import weaver.general.Util;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import java.util.HashMap;
import java.util.Map;

public class ReceiptAction {

    ReceiptService getService() {
        return ServiceUtil.getService(ReceiptService.class);
    }

    @POST
    @Path("/getCopyDocid")
    @Produces({MediaType.TEXT_PLAIN})
    public String getCopyDocid(@Context HttpServletRequest request, @Context HttpServletResponse response){
        BaseBean bs = new BaseBean();
        Map<String, Object> apidatas = new HashMap<String, Object>();
        bs.writeLog("----ReceiptAction-----");
        try{
            Map<String,Object> param = ParamUtil.request2Map(request);
            String userid = Util.null2String(param.get("userid"));
            String type = Util.null2String(param.get("type"));
            String docids = Util.null2String(param.get("docids"));
            String oldPair = Util.null2String(param.get("oldPair"));

            ReceiptService service = getService();
            apidatas = service.getCopyDocid(userid,docids,oldPair,type);
        }catch (Exception e) {
            e.printStackTrace();
            apidatas.put("api_status", false);
            apidatas.put("api_errormsg", "catch exception : " + e.getMessage());
        }

        return JSON.toJSONString(apidatas);
    }


    @POST
    @Path("/copyAllFile")
    @Produces({MediaType.TEXT_PLAIN})
    public String copyAllFile(@Context HttpServletRequest request, @Context HttpServletResponse response){
        BaseBean bs = new BaseBean();
        Map<String, Object> apidatas = new HashMap<String, Object>();
        bs.writeLog("----ReceiptAction-----");
        try{
            Map<String,Object> param = ParamUtil.request2Map(request);
            String requestids = Util.null2String(param.get("requestids"));

            ReceiptService service = getService();
            apidatas = service.copyAllFile(requestids);
        }catch (Exception e) {
            e.printStackTrace();
            apidatas.put("api_status", false);
            apidatas.put("api_errormsg", "catch exception : " + e.getMessage());
        }

        return JSON.toJSONString(apidatas);
    }

    @POST
    @Path("/refreshInfo")
    @Produces({MediaType.TEXT_PLAIN})
    public String refreshInfo(@Context HttpServletRequest request, @Context HttpServletResponse response){
        BaseBean bs = new BaseBean();
        Map<String, Object> apidatas = new HashMap<String, Object>();
        bs.writeLog("----ReceiptAction-----");
        try{
            Map<String,Object> param = ParamUtil.request2Map(request);
            String requestids = Util.null2String(param.get("requestids"));

            ReceiptService service = getService();
            apidatas = service.refreshInfo(requestids);
        }catch (Exception e) {
            e.printStackTrace();
            apidatas.put("api_status", false);
            apidatas.put("api_errormsg", "catch exception : " + e.getMessage());
        }

        return JSON.toJSONString(apidatas);
    }

    @POST
    @Path("/updateDeptName")
    @Produces({MediaType.TEXT_PLAIN})
    public String updateDeptName(@Context HttpServletRequest request, @Context HttpServletResponse response){
        BaseBean bs = new BaseBean();
        Map<String, Object> apidatas = new HashMap<String, Object>();
        bs.writeLog("----ReceiptAction-----");
        try{
            Map<String,Object> param = ParamUtil.request2Map(request);
            String requestid = Util.null2String(param.get("requestid"));
            String ids = Util.null2String(param.get("ids"));
            String spDeptid = Util.null2String(param.get("spDeptid"));
            String spDeptName = Util.null2String(param.get("spDeptName"));
            String startTime = Util.null2String(param.get("startTime"));

            ReceiptService service = getService();
            apidatas.put("message",service.updateDeptName(requestid,ids,spDeptid,spDeptName,startTime));
        }catch (Exception e) {
            e.printStackTrace();
            apidatas.put("api_status", false);
            apidatas.put("api_errormsg", "catch exception : " + e.getMessage());
        }

        return JSON.toJSONString(apidatas);
    }

}
