package weaver.interfaces.htsc.itcglc.action;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import weaver.conn.RecordSet;
import weaver.docs.docs.DocComInfo;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.hrm.company.DepartmentComInfo;
import weaver.hrm.resource.ResourceComInfo;
import weaver.interfaces.htsc.comInfo.PropBean;
import weaver.interfaces.htsc.cus.HTUtil;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;

import java.util.HashMap;
import java.util.Map;

/**
 * It采购流程状态改变接口
 * field配置
 */
public class ItProjectProcurementAction extends BaseBean implements Action {
    private Log log = LogFactory.getLog(this.getClass());

    private String proStatus;

    @Override
    public String execute(RequestInfo requestInfo) {
        try{
            HTUtil htUtil = new HTUtil();
            RecordSet rs = new RecordSet();
            ResourceComInfo resourceComInfo = new ResourceComInfo();
            DepartmentComInfo departmentComInfo = new DepartmentComInfo();
            String tablename = requestInfo.getRequestManager().getBillTableName();
            String src = requestInfo.getRequestManager().getSrc();//流程状态
            String dt5 = tablename+"_dt5";
            String requestid = requestInfo.getRequestid();
            JSONObject datas = new JSONObject();
            rs.executeQuery("select requestid,ngr,xmfzr,xmbh,lcbh,lcbt,ngrq,sbwj,fj " +
                    " from "+tablename+" where requestid = ?",requestid);
            log.info("tablename:"+tablename+"==requestid"+requestid);
            log.info("流程状态："+src);
            log.info("proStatus:"+proStatus);
            //主表
            String sbwj = "";
            String fj = "";
            if (rs.next()){
                String ngr = Util.null2String(rs.getString("ngr"));
                String ngrLoginID = resourceComInfo.getWorkcode(ngr);
                String xmfzr = Util.null2String(rs.getString("xmfzr"));
                String fzrLoginID = resourceComInfo.getWorkcode(xmfzr);
                String xmbh = Util.null2String(rs.getString("xmbh"));
                String lcbh = Util.null2String(rs.getString("lcbh"));
                String lcbt = Util.null2String(rs.getString("lcbt"));
                String ngrq = Util.null2String(rs.getString("ngrq"));
                ngrq = ngrq + ":00";
                sbwj = Util.null2String(rs.getString("sbwj"));
                fj = Util.null2String(rs.getString("fj"));
                datas.put("badge",ngrLoginID);
                datas.put("projectDirector",fzrLoginID);
                datas.put("projectCode",xmbh);
                datas.put("workFlowNum",lcbh);
                //流程状态
                datas.put("proStatus",proStatus);
                datas.put("requestId",requestid);
                datas.put("title",lcbt);
                datas.put("draftDate",ngrq);
            }

            log.info("申报文件："+sbwj+"==附件:"+fj);

            //正文和附件
            DocComInfo docComInfo =new DocComInfo();
            JSONArray filesArray = new JSONArray();
            JSONObject fileObject = new JSONObject();
            String docname = docComInfo.getDocname(sbwj);
            rs.executeQuery("select b.fileid from docimagefile a, imagefile b " +
                    " where a.imagefileid = b.imagefileid and docid = ?",sbwj);
            if (rs.next()){
                sbwj = Util.null2String(rs.getString("fileid"));
            }
            fileObject.put("field",sbwj);
            fileObject.put("fieldname",docname);
            fileObject.put("flag",0);
            filesArray.add(fileObject);



            if (!"".equals(fj)){
                String[] split = fj.split(",");
                for (String value : split){
                    JSONObject attachObject = new JSONObject();
                    String attachname = docComInfo.getDocname(value);
                    rs.executeQuery("select b.fileid from docimagefile a, imagefile b " +
                            " where a.imagefileid = b.imagefileid and docid = ?",value);
                    if (rs.next()){
                        value = Util.null2String(rs.getString("fileid"));
                    }
                    attachObject.put("field",value);
                    attachObject.put("fieldname",attachname);
                    attachObject.put("flag",1);
                    filesArray.add(attachObject);
                }
            }

            datas.put("attachments",filesArray);

            //明细表5
            rs.executeQuery("select b.appid,b.cglx,b.ysje,b.sl,b.cgfs,b.ysbm,b.cgnr,b.cgzd," +
                    " b.ejcgzd,b.gys,b.bz,b.cgdbh,b.cgfzr from "+tablename+" a , "+dt5+" b where " +
                    " a.id = b.mainid and requestid = ?",requestid);
            JSONArray dt5Array = new JSONArray();
            PropBean propBean = new PropBean();
            String itProjectApply = propBean.getPropName("ItProjectApply"); //测试环境ip地址
            String ItProjectProcurementType = propBean.getPropName("ItProjectProcurementType"); //It采购流程采购类型field
            String ItProjectProcurementWay = propBean.getPropName("ItProjectProcurementWay"); //It采购流程采购方式
            RecordSet rsdetail = new RecordSet();
            while (rs.next()){
                JSONObject dt5Object = new JSONObject();
                String appid = Util.null2String(rs.getString("appid"));
                String cglx = Util.null2String(rs.getString("cglx"));
                rsdetail.executeQuery("select selectname from workflow_selectitem " +
                        "where fieldid = ? and selectvalue = ?",ItProjectProcurementType,cglx);
                if (rsdetail.next()){
                    cglx = Util.null2String(rsdetail.getString("selectname"));
                }
                String ysje = Util.null2String(rs.getString("ysje"));
                String sl = Util.null2String(rs.getString("sl"));
                String cgfs = Util.null2String(rs.getString("cgfs"));
                rsdetail.executeQuery("select selectname from workflow_selectitem " +
                        "where fieldid = ? and selectvalue = ?",ItProjectProcurementWay,cgfs);
                if (rsdetail.next()){
                    cgfs = Util.null2String(rsdetail.getString("selectname"));
                }
                String ysbm = Util.null2String(rs.getString("ysbm")); //部门名称和部门编码
                String departmentName = departmentComInfo.getDepartmentName(ysbm);
                String departmentCode = departmentComInfo.getDepartmentCode(ysbm);
                String cgnr = Util.null2String(rs.getString("cgnr"));
                String cgzd = Util.null2String(rs.getString("cgzd"));
                String ejcgzd = Util.null2String(rs.getString("ejcgzd"));
                String gys = Util.null2String(rs.getString("gys"));
                String bz = Util.null2String(rs.getString("bz"));
                String cgdbh = Util.null2String(rs.getString("cgdbh"));
                String cgfzr = Util.null2String(rs.getString("cgfzr")); //采购人工号和名字
                String cgfzrLoginID = resourceComInfo.getWorkcode(cgfzr);
                String cgfzrLastname = resourceComInfo.getLastname(cgfzr);


                dt5Object.put("app_id",appid);
                dt5Object.put("purchase_type",cglx);
                dt5Object.put("c_money",ysje);
                dt5Object.put("number_units",sl);
                dt5Object.put("buy_type",cgfs);
                dt5Object.put("y_department",departmentName);
                dt5Object.put("y_department_id",departmentCode);
                dt5Object.put("c_content",cgnr);
                dt5Object.put("purchase_system",cgzd);
                dt5Object.put("purchase_terms",ejcgzd);
                dt5Object.put("team_id",gys);
                dt5Object.put("c_remark",bz);
                dt5Object.put("purchaseNum",cgdbh);
                dt5Object.put("caigoufuzerenid",cgfzrLoginID);
                dt5Object.put("caigoufuzeren",cgfzrLastname);
                dt5Array.add(dt5Object);

            }
            datas.put("budgetData",dt5Array);
            log.info("参数详情"+requestid+"==="+datas.toString());

            String url = itProjectApply + "/jtxt/ITProjects/updateITPurchase";
            String encode = "utf-8";
            Map<String, String> headers = new HashMap<>();
            log.info("url:"+url);
            String content = htUtil.httpPostRaw(url, datas.toString(), headers, encode);
            log.info("content结果集："+ content.toString());


        }catch (Exception e){
            e.printStackTrace();
            requestInfo.getRequestManager().setMessagecontent("It采购更新状态数据失败");
        }

        return Action.SUCCESS;
    }

    public void setProStatus(String proStatus) {
        this.proStatus = proStatus;
    }

    public String getProStatus() {
        return proStatus;
    }
}
