package com.engine.htsc.contractmanagement.cmd;

import com.engine.common.biz.AbstractCommonCommand;
import com.engine.common.entity.BizLogContext;
import com.engine.core.interceptor.CommandContext;
import weaver.conn.RecordSet;

import java.util.*;

public class ChangeRecordCmd extends AbstractCommonCommand<Map<String, Object>> {

    public ChangeRecordCmd(Map<String, Object> params){
        this.params = params;
    }

    @Override
    public BizLogContext getLogContext() {
        return null;
    }

    @Override
    public Map<String, Object> execute(CommandContext commandContext) {
        Map<String, Object> dataMap = new HashMap<>();
        List dataList = new ArrayList();
        dataMap.put("data",dataList);
        String modeid = params.get("modeid").toString();//模块id
        String billid = params.get("billid").toString();//数据id
        String formid = params.get("formid").toString();//表id
        Map<String,String> fieldNames = getFieldName(formid);

        String sql = "select a.viewlogid,a.fieldid,a.fieldvalue,a.prefieldvalue,b.operatedate,b.operatetime,b.operatetype,b.operateuserid " +
                "from modelogfielddetail a left join Modeviewlog_"+modeid+" b on a.viewlogid=b.id " +
                "where a.modeid=? and b.relatedid=? order by a.viewlogid";
        //历史版本数据
        String versionSql = "select id,dqbb from uf_dsfgl where fid=? order by dqbb";
        List<String> historyList = new ArrayList<>();
        RecordSet rs = new RecordSet();
        rs.executeQuery(versionSql,billid);
        while (rs.next()){
            historyList.add(rs.getString("id"));
        }
        rs.executeQuery(sql,modeid,billid);
        Map<String,List> resultMap = new TreeMap<>();
        while (rs.next()){
            List<Map<String,String>> resultList = null;
            if (resultMap.get(rs.getString("viewlogid")) == null){
                resultList = new ArrayList();
                resultMap.put(rs.getString("viewlogid"),resultList);
            }else {
                resultList = resultMap.get(rs.getString("viewlogid"));
            }
            Map<String,String> map = new HashMap();
            resultList.add(map);
            map.put("viewlogid",rs.getString("viewlogid"));
            map.put("fieldName",fieldNames.get(rs.getString("fieldid")) == null ? rs.getString("fieldid"):fieldNames.get(rs.getString("fieldid")));
            map.put("fieldvalue",rs.getString("fieldvalue"));
            map.put("prefieldvalue",rs.getString("prefieldvalue"));
            map.put("operatetime",rs.getString("operatedate")+" "+rs.getString("operatetime"));
            map.put("operatetype",rs.getString("operatetype"));
            map.put("operateuserid",rs.getString("operateuserid"));
        }
        int version=0;
        Map<String,String> statusMap = new HashMap<>();
        statusMap.put("5","有效");
        statusMap.put("6","失效");
        statusMap.put("7","草稿");
        for(Map.Entry<String,List> entry:resultMap.entrySet()){
            Map<String,String> recordMap = new HashMap<>();
            List<Map<String,String>> list = entry.getValue();
            StringBuilder changeContent = new StringBuilder();
            for (Map<String,String> map:list){
                recordMap.put("type",map.get("operatetype"));//变更类型
                recordMap.put("operateTime",map.get("operatetime"));//操作时间
                recordMap.put("operateUserId",map.get("operateuserid"));//操作人ID
                changeContent.append(map.get("fieldName"));
                changeContent.append("由");
                if ("状态".equals(map.get("fieldName"))){
                    changeContent.append("\""+statusMap.get(map.get("prefieldvalue"))+"\"");
                    changeContent.append("变为");
                    changeContent.append("\""+statusMap.get(map.get("fieldvalue"))+"\"");
                    changeContent.append(",");
                }else {
                    changeContent.append("\""+map.get("prefieldvalue")+"\"");
                    changeContent.append("变为");
                    changeContent.append("\""+map.get("fieldvalue")+"\"");
                    changeContent.append(",");
                }
                if ("变更描述".equals(map.get("fieldName")) || "原因说明".endsWith(map.get("fieldName"))){
                    recordMap.put("changeRemark",map.get("fieldvalue"));//变更备注
                }
            }
            if (recordMap.get("changeRemark") == null){
                recordMap.put("changeRemark","");
            }
            if (recordMap.get("operateUserId").endsWith("1")){
                recordMap.put("operateUserName","系统管理员");//操作人名称
            }else {
                recordMap.put("operateUserName",getUserName(recordMap.get("operateUserId")));
            }
            recordMap.put("changeContent",changeContent.toString().substring(0,changeContent.length()-1));//变更内容
            if (version<(historyList.size())){
                recordMap.put("historyDataId",historyList.get(version));
            }

            recordMap.put("version",String.valueOf(++version));//历史版本
            dataList.add(recordMap);
        }
        Collections.reverse(dataList);

        return dataMap;
    }

    /**
     * 获取表单字段名称
     * @param formId
     * @return
     */
    public Map<String,String> getFieldName(String formId){
        Map<String,String> map = new HashMap<>();
        RecordSet rs = new RecordSet();
        String sql = "select a.id,b.labelname from workflow_billfield a left join HTMLLABELINFO b on a.fieldlabel=b.indexid where billid=? and b.languageid=7";
        rs.executeQuery(sql,formId);
        while (rs.next()){
            map.put(rs.getString("id"),rs.getString("labelname"));
        }
        return map;
    }
    public String getUserName(String id){
        RecordSet rs = new RecordSet();
        String sql = "select lastname from HRMRESOURCE where id=?";
        rs.executeQuery(sql,id);
        String userName ="";
        if (rs.next()){
            userName = rs.getString("lastname");
        }
        return userName;
    }
}
