package com.api.attendance.browser.service.impl;

import com.api.browser.bean.ListHeadBean;
import com.api.browser.bean.SearchConditionItem;
import com.api.browser.bean.SplitTableBean;
import com.api.browser.bean.SplitTableColBean;
import com.api.browser.service.BrowserService;
import com.api.browser.util.*;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.hrm.User;
import weaver.systeminfo.SystemEnv;

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

/**
 * @Author: sy
 * @Description:
 * @Date: 2024/4/17
 **/
public class KqxmBrowserService extends BrowserService {
    BaseBean bs = new BaseBean();
//    User user = new User(Integer.parseInt("1"));
    @Override
    public Map<String, Object> getBrowserData(Map<String, Object> params) throws Exception {
        Map<String, Object> apidatas = new HashMap<String, Object>();

        String kqxmName = Util.null2String(params.get("kqxmName"));

        String sqlWhere = Util.null2String(params.get("sqlWhere"));
        String sqlwhere = " where 1=1 ";
        if(!kqxmName.equals("")){
            sqlwhere += " and mc like '%";
            sqlwhere += Util.fromScreen2(kqxmName,user.getLanguage());
            sqlwhere += "%'";
        }

        if(!sqlWhere.equals("")){
            sqlwhere += " and "+sqlWhere;
        }

        sqlwhere  = SqlUtils.replaceFirstAnd(sqlwhere);
        // 设置好搜索条件
        String backFields = " id ,mc as kqxmName";
        String fromSql = " uf_jcl_kq_kqxm ";

        List<SplitTableColBean> cols = new ArrayList<SplitTableColBean>();
        cols.add(new SplitTableColBean("true","id"));
//        cols.add(new SplitTableColBean("100%", SystemEnv.getHtmlLabelName(546742, user.getLanguage()),"mc","mc").setIsInputCol(BoolAttr.TRUE).setShowType(1));
        cols.add(new SplitTableColBean("100%", SystemEnv.getHtmlLabelName(546742, user.getLanguage()),"kqxmName","kqxmName").setIsInputCol(BoolAttr.TRUE).setShowType(1));

        SplitTableBean tableBean  =  new SplitTableBean(backFields,fromSql,sqlwhere,"id","id","asc",cols);
        bs.writeLog("KqxmBrowserService params : " + params + ", tableBean : " + tableBean);
        apidatas.putAll(SplitTableUtil.makeListDataResult(tableBean));
        return apidatas;
    }

    @Override
    public Map<String, Object> getBrowserConditionInfo(Map<String, Object> params) throws Exception {

        Map<String, Object> apidatas = new HashMap<String, Object>();
        List<SearchConditionItem> conditions = new ArrayList<SearchConditionItem>();
        apidatas.put(BrowserConstant.BROWSER_RESULT_CONDITIONS, conditions);

        ConditionFactory conditionFactory = new ConditionFactory(user);
        conditions.add(conditionFactory.createCondition(ConditionType.INPUT, 546742, "kqxmName", true));

        return apidatas;
    }


    @Override
    public Map<String, Object> getMultBrowserDestData(Map<String, Object> params) throws Exception {
        Map<String, Object> apidatas = new HashMap<String, Object>();
        List<Map<String,Object>> datas  = new ArrayList<Map<String,Object>>();
        String selectids = Util.null2String(params.get(BrowserConstant.BROWSER_MULT_DEST_SELECTIDS));
        if(user==null||"".equals(selectids)){
            return apidatas;
        }
        RecordSet recordSet = new RecordSet();
        String tempSql = " select id,mc from uf_jcl_kq_kqxm where 1=1 and id in ("+selectids+") ";
        recordSet.executeQuery(tempSql);
        while (recordSet.next()){
            Map<String,Object> item = new HashMap<String,Object>();
            item.put("id", recordSet.getString("id"));
            item.put("kqxmName", Util.null2String(recordSet.getString("mc")));
            datas.add(item);
        }
        List<ListHeadBean> tableHeadColumns =  new ArrayList<ListHeadBean>();
        tableHeadColumns.add(new ListHeadBean("id", BoolAttr.TRUE));
        tableHeadColumns.add(new ListHeadBean("kqxmName","",1, BoolAttr.TRUE));

        apidatas.put(BrowserConstant.BROWSER_RESULT_COLUMN, tableHeadColumns);
        apidatas.put(BrowserConstant.BROWSER_RESULT_DATA, datas);
        apidatas.put(BrowserConstant.BROWSER_RESULT_TYPE, BrowserDataType.LIST_ALL_DATA.getTypeid());
        return apidatas;
    }
}
