package com.engine.htsc.cube.web;

import com.alibaba.fastjson.JSONObject;
import com.engine.common.util.ServiceUtil;
import com.engine.htsc.cube.service.CollaborationSaveFormService;
import com.engine.htsc.cube.service.impl.CollaborationSaveFormServiceImpl;
import com.engine.workflow.util.CommonUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import weaver.conn.RecordSetTrans;
import weaver.formmode.virtualform.VirtualFormHandler;
import weaver.general.Util;
import weaver.hrm.User;
import weaver.workflow.workflow.WorkflowBillComInfo;

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.core.Context;
import javax.ws.rs.core.MediaType;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CollaborationSaveFormAction {

    private Log log = LogFactory.getLog(this.getClass());
    private CollaborationSaveFormService getService(HttpServletRequest req, HttpServletResponse resp){
        User user = CommonUtil.getUserByRequest(req, resp);
        return (CollaborationSaveFormService) ServiceUtil.getService(CollaborationSaveFormServiceImpl.class,user);
    }

    /**
     * 协作页面提交保存接口
     * @param request
     * @param response
     * @return
     */
    @GET
    @Path("/doSave")
    @Produces({MediaType.APPLICATION_JSON})
    public Map<String,Object> doSave(@Context HttpServletRequest request,
                                              @Context HttpServletResponse response){
        Map<String,Object> map = new HashMap<>();
        RecordSetTrans rst = new RecordSetTrans();
        try{
            rst.setAutoCommit(false);
            WorkflowBillComInfo workflowBillComInfo = new WorkflowBillComInfo();
            Integer billid = Util.getIntValue(request.getParameter("billid"));
            String formId = Util.null2String(request.getParameter("formId"));
            String JSONStr = Util.null2String(request.getParameter("JSONStr"));
            //1.获取表名
            String tableName = VirtualFormHandler.getRealFromName(Util.null2String(workflowBillComInfo.getTablename(formId)));
            log.info("当前表名--"+tableName);
            //2.解析表单数据
            JSONObject dataObject = JSONObject.parseObject(JSONStr);
            log.info("JSONStr=="+dataObject);
            //2.1 获取真实对应数据
            List<String> list = new ArrayList<>();
            if (dataObject != null && !dataObject.isEmpty()){
                for (Object str : dataObject.keySet()){
                    //2.2 截取field12345
                    String field = str.toString();
                    String id = field.substring(5);
                    rst.executeQuery("select fieldname from workflow_billfield where id = ?",new Object[]{id});
                    if (rst.next()){
                        String fieldname = Util.null2String(rst.getString("fieldname"));
                        String value = fieldname +" = '"+ dataObject.get(str)+"'";
                        list.add(value);
                    }
                }
                String datas = StringUtils.join(list, ",");
                log.info("拼接更新数据=="+datas);
                String updateFormSql = "update "+tableName+" set "+datas+" where id = ?";
                log.info("更新语句=="+updateFormSql);
                rst.executeUpdate(updateFormSql,billid);
            }
            map.put("status",1);
            map.put("msg","success");
            rst.commit();
        }catch (Exception e){
            rst.rollback();
            map.put("status",-1);
            map.put("msg","fail");
            e.printStackTrace();

        }
        return map;
    }
}
