package com.engine.htsc.duban.cmd;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.engine.common.biz.AbstractCommonCommand;
import com.engine.common.entity.BizLogContext;
import com.engine.core.interceptor.CommandContext;
import org.apache.commons.lang3.StringUtils;
import weaver.conn.RecordSet;
import weaver.formmode.setup.ModeRightInfo;
import weaver.general.BaseBean;
import weaver.general.GCONST;
import weaver.general.TimeUtil;
import weaver.general.Util;
import weaver.hrm.User;
import weaver.hrm.resource.ResourceComInfo;
import weaver.interfaces.htsc.comInfo.PropBean;
import weaver.interfaces.htsc.util.HT_Util;

import java.util.*;

public class DbrwFkCmd extends AbstractCommonCommand<Map<String, Object>> {
    @Override
    public BizLogContext getLogContext() {
        return null;
    }

    public DbrwFkCmd(Map<String, Object> params, User user) {
        this.params = params;
        this.user = user;
    }
    @Override
    public Map<String, Object> execute(CommandContext commandContext) {
        RecordSet rs = new RecordSet();
        RecordSet imageNameRs = new RecordSet();
        RecordSet rwValueRs = new RecordSet();
        JSONObject feedObj = new JSONObject();
        String ecologyContentPath = GCONST.getContextPath();
        Integer pagesize = Util.getIntValue(Util.null2String(params.get("pagesize")));
        Integer current = Util.getIntValue(Util.null2String(params.get("current")));
        Integer billid = Util.getIntValue(Util.null2String(params.get("billid")));
        Integer modeid = Util.getIntValue(Util.null2String(params.get("modeid")));
        try{
            ResourceComInfo ResourceComInfo = new ResourceComInfo();

            //1.根据billid获取任务id
            //取建模表配置
            PropBean propBean = new PropBean();
            String rwmodecubeuuiud = propBean.getPropName("UF_DBRW_CUBEUUID");
            JSONObject jsonObject_rw =  propBean.getModeInfoByUUID(rwmodecubeuuiud);
            String rwmodeid = jsonObject_rw.getString("modeid");

            String xzsymodecubeuuiud = propBean.getPropName("UF_DBXZ_CUBEUUID");
            JSONObject jsonObject_xz =  propBean.getModeInfoByUUID(xzsymodecubeuuiud);
            String xzsymodeid = jsonObject_xz.getString("modeid");

            String rwIdStr = "";
            if (modeid == Integer.parseInt(rwmodeid)){
                rwIdStr = String.valueOf(billid);
            }else if (modeid == Integer.parseInt(xzsymodeid)){
                List<Integer> rwList = new ArrayList<>();
                String rwIdSql = "select id from uf_rw where xzsy = "+billid;
                rs.execute(rwIdSql);
                while (rs.next()){
                    int id = Util.getIntValue(rs.getString("id"));
                    rwList.add(id);
                }
                rwIdStr = StringUtils.join(rwList,",");
            }

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

            //获取反馈数据
            JSONArray dataArray = new JSONArray();
            String dataCountSql = "select * from (select rownum r,e.* from " +
                    " (select  * from uf_rwfk t inner join uf_rw l on l.id = t.rw where t.rw in ("+rwIdStr+") order by l.id,t.modedatacreatedate||t.modedatacreatetime desc)" +
                    " e where rownum<="+pagesize+"*"+current+")" +
                    " where r > "+pagesize+"*"+current+"-"+pagesize+"";
            rs.execute(dataCountSql);
            String lastName = "";
            while (rs.next()) {
                JSONObject dataObj = new JSONObject();
                int fkid = Util.getIntValue(rs.getString("id"));
                dataObj.put("fkid",fkid);
                int rw = Util.getIntValue(rs.getString("rw"));
                int fkzt = Util.getIntValue(rs.getString("fkzt"));
                //获取任务状态真实值
                String rwztValue = "";
                String rwbt = "";
//                String rwztfieldid = bs.getPropValue("HTTASK","RWZTFIELDID");
//                String fkztfieldid = bs.getPropValue("HTTASK","FKZTFIELDID");

                String rwztfieldid = propBean.getPropName("TASK_RWZTFIELDID");
                String fkztfieldid = propBean.getPropName("TASK_FKZTFIELDID");

                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);
                //获取反馈状态真实值
                RecordSet fkRs = new RecordSet();
                String fkztValue = "";
                String fkztSql = "select selectname from  workflow_selectitem  where fieldid = ? and  selectvalue = ?";
                fkRs.executeQuery(fkztSql,new Object[]{fkztfieldid,fkzt});
                if (fkRs.next()){
                    fkztValue = Util.null2String(fkRs.getString("selectname"));
                }
                dataObj.put("fkztValue",fkztValue);

                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";
                        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;
    }
}
