package com.engine.htsc.cube.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.engine.core.impl.Service;
import com.engine.htsc.cube.service.FeedBackService;
import org.apache.commons.lang3.StringUtils;
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;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by K1810001 on 2020/10/19.
 */
public class FeedBackServiceImpl extends Service implements FeedBackService {

    @Override
    public JSONObject getDataList(Integer pagesize, Integer current, Integer billid, Integer modeid) {
        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();

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

            String xzsymodecubeuuiud = propBean.getPropName("UF_XZSY_CUBEUUIUD");
            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 ="/htoa/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";


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


    @Override
    public JSONObject getTasikDetailList(Integer billid) {

        RecordSet rs = new RecordSet();
        BaseBean bs = new BaseBean();
        RecordSet rwRs = new RecordSet();
        RecordSet nameRs = new RecordSet();
        JSONObject datas = new JSONObject();
        try{
            ResourceComInfo ResourceComInfo = new ResourceComInfo();

            //1. 获取任务详情列表数量
            Integer count = 0;
            String countSql = "select count(1) as count from uf_rw where xzsy = "+billid+" and cjzt=1";
            rs.execute(countSql);
            if (rs.next()){
                count = Util.getIntValue(rs.getString("count"));
            }

            datas.put("count",count);

            //2.获取任务详情列表
            String dataSql = "select id,rwbt,rwbh,jbr,rwzt,rwjslc from uf_rw where xzsy = "+billid+" and cjzt=1 ORDER by id desc";
            rs.execute(dataSql);
            JSONArray dataArray = new JSONArray();
            while(rs.next()){
                JSONObject dataList = new JSONObject();
                String id = Util.null2String(rs.getString("id"));
                String rwbt = Util.null2String(rs.getString("rwbt"));
                String rwbh = Util.null2String(rs.getString("rwbh"));
                String jbr = Util.null2String(rs.getString("jbr"));
                Integer rwzt = Util.getIntValue(rs.getString("rwzt"));
                Integer rwjslc = Util.getIntValue(rs.getString("rwjslc"));
                dataList.put("id",id);
                dataList.put("rwbt",rwbt);
                dataList.put("rwbh",rwbh);
                dataList.put("rwztnum",rwzt);

                String[] split = jbr.split(",");
                List<String> jbrList = new ArrayList<>();
                for (String jbrid:split){
                    String name = ResourceComInfo.getLastname(jbrid);
                    jbrList.add(name);
                }
                String lastName = StringUtils.join(jbrList, ",");
                dataList.put("jbr",lastName);

                String rwbtValue = "";

                PropBean propBean = new PropBean();
                String rwztfieldid = propBean.getPropName("TASK_RWZTFIELDID");

//                String rwztfieldid = bs.getPropValue("HTTASK","RWZTFIELDID");

                String rwztSql = "select selectname from workflow_selectitem where fieldid = "+rwztfieldid+" and selectvalue ="+rwzt;
                rwRs.executeQuery(rwztSql);

                if (rwRs.next()){
                    rwbtValue = Util.null2String(rwRs.getString("selectname"));
                }
                dataList.put("rwzt",rwbtValue);



                String requestname = "";
                String reqNameSql = "select requestname from workflow_requestbase where requestid ="+rwjslc;
                nameRs.execute(reqNameSql);
                if (nameRs.next()){
                    requestname = Util.null2String(nameRs.getString("requestname"));
                }
                dataList.put("rwjslc",requestname);

                //3.操作按钮
                dataArray.add(dataList);
            }

            datas.put("data",dataArray);


        }catch (Exception e){
            e.printStackTrace();
            datas.put("status","数据请求失败");
        }

        return datas;
    }

}
