package com.engine.htsc.contractmanagement.cmd;

import com.alibaba.fastjson.JSON;
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 weaver.conn.RecordSet;

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

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

    public CheckRepeatDataCmd(Map<String, Object> params){
        this.params = params;
    }
    @Override
    public BizLogContext getLogContext() {
        return null;
    }

    @Override
    public Map<String, Object> execute(CommandContext commandContext) {
        Map<String,Object> resultMap = new HashMap();
        try {
            JSONObject jsonObjectstr = JSONObject.parseObject(JSON.toJSONString(params));
            List dataList = jsonObjectstr.getJSONArray("data");
            String deletesql = "select  count(*) num from uf_dsfgl_dt1 a left join uf_dsfgl b on a.mainid=b.id  where a.zjlx=? and a.zjhm=? and b.scbs=1";
            RecordSet rs = new RecordSet();
            for (Object map:dataList){
                rs.executeQuery(deletesql,((Map)map).get("zjlx"),((Map)map).get("zjhm")); //确认数据是否已被删除
                if (rs.next()){
                    int num = rs.getInt("num");
                    if (num > 0){
                        resultMap.put("num",0);
                        resultMap.put("code",200);
                        return resultMap;
                    }
                }
                String sql = "select  count(*) num from uf_dsfgl_dt1 a left join uf_dsfgl b on a.mainid=b.id  where a.zjlx=? and a.zjhm=? and b.scbs!=1";
                rs.executeQuery(sql,((Map)map).get("zjlx"),((Map)map).get("zjhm"));//查看是否已存在数据
                if (rs.next()){
                    int num = rs.getInt("num");
                    if (num > 0){
                        resultMap.put("num",num);
                    }
                }
            }
            if (resultMap.get("num") == null){
                resultMap.put("num","0");
            }
            resultMap.put("code",200);
        }catch (Exception e){
            e.printStackTrace();
            resultMap.put("message","query error");
            resultMap.put("code",500);
            return resultMap;
        }
        return resultMap;
    }

}
