package com.engine.htsc.payment.web;


import com.engine.common.util.ParamUtil;
import com.engine.common.util.ServiceUtil;
import com.engine.htsc.payment.pojo.TripPaymentRequest;
import com.engine.htsc.payment.service.PaymentWorkflowService;
import com.engine.htsc.payment.service.impl.PaymentWorkflowServiceImpl;
import java.util.Objects;
import javax.ws.rs.POST;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import weaver.hrm.HrmUserVarify;
import weaver.hrm.User;

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

public class PaymentWorkflowAction {
    private Log log = LogFactory.getLog(this.getClass());
    private PaymentWorkflowService getService(User user){
        return ServiceUtil.getService(PaymentWorkflowServiceImpl.class,user);
    }
    
    private PaymentWorkflowService getService(){
        return ServiceUtil.getService(PaymentWorkflowServiceImpl.class);
    }



    /**
     * 获取所有虚拟部门数据
     * @param request
     * @param response
     * @return
     */
    @GET
    @Path("/getVirtualDept")
    @Produces({MediaType.APPLICATION_JSON})
    public Map<String,Object> getVirtualDept(@Context HttpServletRequest request,
                                             @Context HttpServletResponse response){
        Map<String, Object> apidatas = new HashMap<>();
        try {
            User user = HrmUserVarify.getUser(request, response);
            apidatas = this.getService(user).getVirtualDept();
            //postman测试
            //apidatas = ServiceUtil.getService(PaymentWorkflowServiceImpl.class).getVirtualDept();
            apidatas.put("api_status", 1);
        }catch (Exception e){
            e.printStackTrace();
            apidatas.put("api_status",-1);
            apidatas.put("api_errormsg", "catch exception : " + e.getMessage());
            log.error("isPostion catch exception");
        }
        return apidatas;
    }

    /**
     * 总部智能商旅付款自动构建OA流程
     * @param request
     * @return
     */
    @POST
    @Path("/creatWorkflow")
    @Produces({MediaType.APPLICATION_JSON})
    public Map<String,Object> createBusinessTravelWorkflow(TripPaymentRequest request){
        Map<String, Object> apidatas = new HashMap<>();
        try {
            apidatas = this.getService().createBusinessTravelWorkflow(request);
            apidatas.put("code", 200);
            apidatas.put("message","接口调用成功");
        }catch (Exception e){
            e.printStackTrace();
            apidatas.put("code",-1);
            apidatas.put("message","接口调用失败");
            apidatas.put("api_errormsg", "catch exception : " + e.getMessage());
            log.error("isPostion catch exception");
        }
        return apidatas;
    }
    
    
    /**
     * 总部智能商旅付款修改明细接口,参数中需包含requestid
     * @param request
     * @return
     */
    @POST
    @Path("/updateDetailInfo")
    @Produces({MediaType.APPLICATION_JSON})
    public Map<String,Object> saveDetailInfo(TripPaymentRequest request){
        Map<String, Object> apidatas = new HashMap<>();
        try {
            if(Objects.isNull(request) || Objects.isNull(request.getRequestId()) || request.getRequestId()<=0){
                apidatas.put("code", 400);
                apidatas.put("message","参数非法，request不可为空");
            }
            this.getService().saveDetailInfo(request);
            apidatas.put("code", 200);
            apidatas.put("message","接口调用成功");
        }catch (Exception e){
            e.printStackTrace();
            apidatas.put("code",-1);
            apidatas.put("message","接口调用失败");
            apidatas.put("api_errormsg", "catch exception : " + e.getMessage());
            log.error("isPostion catch exception");
        }
        return apidatas;
    }

    /**
     * 总部通用报销二级部门报销默认费用承担部门为一级部门负责人
     * @param request
     * @param response
     * @return
     */
    @GET
    @Path("/getLevelDept")
    @Produces({MediaType.APPLICATION_JSON})
    public Map<String,Object> getLevelDeparment(@Context HttpServletRequest request,
                                             @Context HttpServletResponse response,
                                                @QueryParam("deptid") String deptid){
        Map<String, Object> apidatas = new HashMap<>();
        try {
            User user = HrmUserVarify.getUser(request, response);
            apidatas = this.getService(user).getLevelDept(deptid);
            apidatas.put("api_status", 1);
        }catch (Exception e){
            e.printStackTrace();
            apidatas.put("api_status",-1);
            apidatas.put("api_errormsg", "catch exception : " + e.getMessage());
        }
        return apidatas;
    }

}
