package com.engine.htsc.workflow.service.impl;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.engine.core.impl.Service;
import com.engine.htsc.workflow.service.WorkFlowFeedBackService;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.GCONST;
import weaver.general.Util;
import weaver.hrm.resource.ResourceComInfo;
import weaver.interfaces.htsc.comInfo.PropBean;

/**
 * Created by K1810001 on 2020/10/21.
 */
public class WorkFlowFeedBackServiceImpl extends Service implements WorkFlowFeedBackService {
    @Override
    public JSONObject getDataList(Integer pagesize, Integer current, Integer rwid) {
        RecordSet rs = new RecordSet();
        RecordSet imageNameRs = new RecordSet();
        RecordSet rwValueRs = new RecordSet();
        JSONObject feedObj = new JSONObject();
        BaseBean bs = new BaseBean();
        String ecologyContentPath = GCONST.getContextPath();

        try {
            ResourceComInfo ResourceComInfo = new ResourceComInfo();

            //2.获取总数
            String countSql = "select count(1) as count from uf_rwfk where rw = " + rwid;
            rs.execute(countSql);
            if (rs.next()) {
                int count = Util.getIntValue(rs.getString("count"));
                feedObj.put("count", count);
            }

            PropBean propBean = new PropBean();
            String rwztfieldid = propBean.getPropName("TASK_RWZTFIELDID");
            //获取反馈数据
            JSONArray dataArray = new JSONArray();
            String dataCountSql = "select * from (select rownum r,e.* from (select * from uf_rwfk where rw = " + rwid + " order by id desc) e where rownum<=" + pagesize + "*" + current + ")" +
                    " where r > " + pagesize + "*" + current + "-" + pagesize + "";
            rs.execute(dataCountSql);
            String lastName = "";
            while (rs.next()) {
                JSONObject dataObj = new JSONObject();
                int rw = Util.getIntValue(rs.getString("rw"));
                //获取任务状态真实值
                String rwztValue = "";
                String rwbt = "";
//                String rwztfieldid = bs.getPropValue("HTTASK", "RWZTFIELDID");

                String valueSql = "select a.selectname,b.rwbt from workflow_selectitem a,uf_rw b where a.fieldid = " + rwztfieldid + " and b.id = " + rw + " and  a.selectValue = b.rwzt";
                rwValueRs.execute(valueSql);
                if (rwValueRs.next()) {
                    rwztValue = Util.null2String(rwValueRs.getString("selectname"));
                    rwbt = Util.null2String(rwValueRs.getString("rwbt"));
                }
                dataObj.put("rwztValue", rwztValue);
                dataObj.put("rwbt", rwbt);

                String fkr = Util.null2String(rs.getString("fkr"));
                lastName = ResourceComInfo.getLastname(fkr);
                dataObj.put("lastname", lastName);
                String modedatacreatedate = Util.null2String(rs.getString("modedatacreatedate"));
                String modedatacreatetime = Util.null2String(rs.getString("modedatacreatetime"));
                String dateTime = modedatacreatedate + " " + modedatacreatetime;
                String fkxq = Util.null2String(rs.getString("fkxq"));
                String fj = Util.null2String(rs.getString("fj"));
                dataObj.put("dateTime", dateTime);
                dataObj.put("fkxq", fkxq);

                //附件
                JSONArray fjArray = new JSONArray();
                if (!"".equals(fj)) {
                    String[] fjSplit = fj.split(",");
                    for (int i = 0; i < fjSplit.length; i++) {
                        JSONObject fjObj = new JSONObject();
                        String imageName = "";
                        String imageNameSql = "select imagefilename from DocImageFile where docid = " + fjSplit[i];
                        imageNameRs.execute(imageNameSql);
                        if (imageNameRs.next()) {
                            imageName = Util.null2String(imageNameRs.getString("imagefilename"));
                        }

                        String fjurl = ecologyContentPath+"/spa/document/index.jsp?f_weaver_belongto_userid=" + fkr + "&f_weaver_belongto_usertype=0" +
                                "&id=" + fjSplit[i] + "&formmode_authorize=formmode_authorize&moduleid=formmode&authorizemodeId=4012" +
                                "&authorizefieldid=14594&authorizeformmodebillId=8&router=1#/main/document/detail";

                        fjObj.put("fjurl", fjurl);
                        fjObj.put("fjName", imageName);
                        fjArray.add(fjObj);
                    }
                }

                dataObj.put("fj", fjArray);
                dataArray.add(dataObj);
            }
            feedObj.put("data", dataArray);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return feedObj;
    }
}
