package com.api.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.Util;
import weaver.systeminfo.SystemEnv;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 数智岗位浏览按钮二开
 *
 * @author:dxfeng
 * @createTime: 2023/05/04
 * @version: 1.0
 */
public class JobTitlesBrowserService extends BrowserService {

    @Override
    public Map<String, Object> getBrowserData(Map<String, Object> params) throws Exception {
        Map<String, Object> apidatas = new HashMap<>(16);
        String jobtitlemark = Util.null2String(params.get("jobtitlemark"));
        String jobtitlename = Util.null2String(params.get("jobtitlename"));
        String jobtitlecode = Util.null2String(params.get("jobtitlecode"));
        String jobactivityname = Util.null2String(params.get("jobactivityname"));
        String sqlwhere = "where 1 = 1 ";
        String backfields;
        String fromSql;
        String orderby;
        List<SplitTableColBean> cols = new ArrayList<>();
        SplitTableBean tableBean;
        if ("278".equals(browserType)) {
            backfields = "c.id,c.jobtitlemark,c.jobtitlename,a.jobgroupname,a.jobgroupremark,b.jobactivitymark";
            fromSql = "from HrmJobTitles c left join HrmJobActivities b on b.id = c.jobactivityid left join HrmJobGroups a on a.id = b.jobgroupid";
            orderby = "c.id";
            sqlwhere = " where (c.canceled is null or c.canceled <> 1)";
            String jobgroupid = Util.null2String(params.get("jobgroupid"));
            String jobactivitieid = Util.null2String(params.get("jobactivitieid"));
            if (!"".equals(jobgroupid)) {
                sqlwhere += " and a.id =" + jobgroupid;
            }
            if (!"".equals(jobactivitieid)) {
                sqlwhere += " and b.id= " + jobactivitieid;
            }
            if (!"".equals(jobtitlemark)) {
                sqlwhere += " and c.jobtitlemark like '%" + Util.fromScreen2(jobtitlemark, user.getLanguage()) + "%' ";
            }
            if (!"".equals(jobtitlename)) {
                sqlwhere += " and c.jobtitlename like '%" + Util.fromScreen2(jobtitlename, user.getLanguage()) + "%' ";
            }
            if (!"".equals(jobtitlecode)) {
                sqlwhere += " and c.jobtitlecode like '%" + Util.fromScreen2(jobtitlecode, user.getLanguage()) + "%' ";
            }

            cols.add(new SplitTableColBean("true", "id"));
            cols.add(new SplitTableColBean("35%", SystemEnv.getHtmlLabelName(6086, user.getLanguage()), "jobtitlemark", "jobtitlemark", 1).setIsInputCol(BoolAttr.TRUE));
            cols.add(new SplitTableColBean("35%", SystemEnv.getHtmlLabelName(1915, user.getLanguage()), "jobactivitymark", "jobactivitymark"));
            cols.add(new SplitTableColBean("30%", SystemEnv.getHtmlLabelName(805, user.getLanguage()), "jobgroupremark", "jobgroupremark"));
            tableBean = new SplitTableBean(backfields, fromSql, sqlwhere, orderby, "c.id", cols);
        } else {
            backfields = " a.id as id,a.jobtitlemark as jobtitlemark,a.jobtitlename as jobtitlename, b.jobactivityname as jobactivityname, b.jobactivitymark as jobactivitymark , a.jobtitlecode as jobtitlecode, c.jobgroupremark as jobgroupremark";
            fromSql = " from HrmJobTitles a left join HrmJobActivities b on a.jobactivityid=b.id left join hrmjobgroups c on b.jobgroupid =c.id";
            orderby = " a.id ";
            sqlwhere += " and (a.canceled is null or a.canceled <> 1)";
            if (!"".equals(jobtitlemark)) {
                sqlwhere += " and a.jobtitlemark like '%" + jobtitlemark + "%'";
            }

            if (!"".equals(jobtitlename)) {
                sqlwhere += " and a.jobtitlename like '%" + jobtitlename + "%'";
            }

            if (!"".equals(jobtitlecode)) {
                sqlwhere += " and a.jobtitlecode like '%" + jobtitlecode + "%'";
            }

            if (!"".equals(jobactivityname)) {
                sqlwhere += " and jobactivityname like '%" + jobactivityname + "%'";
            }
            cols.add(new SplitTableColBean("true", "id"));
            cols.add(new SplitTableColBean("20%", "岗位简称", "jobtitlename", "jobtitlename"));
            cols.add(new SplitTableColBean("20%", "岗位全称", "jobtitlemark", "jobtitlemark").setIsInputCol(BoolAttr.TRUE));
            cols.add(new SplitTableColBean("20%", "岗位编号", "jobtitlecode", "jobtitlecode"));
            cols.add(new SplitTableColBean("20%", "子序列", "jobactivitymark", "jobactivitymark"));
            cols.add(new SplitTableColBean("20%", "序列", "jobgroupremark", "jobgroupremark"));
            tableBean = new SplitTableBean(backfields, fromSql, sqlwhere, orderby, "a.id", cols);
        }

        tableBean.setSqlsortway("ASC");
        tableBean.setSqlisdistinct("true");
        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<>(16);
        List<SearchConditionItem> conditions = new ArrayList<>();
        apidatas.put(BrowserConstant.BROWSER_RESULT_CONDITIONS, conditions);
        ConditionFactory conditionFactory = new ConditionFactory(user);
        if ("278".equals(browserType)) {
            conditions.add(conditionFactory.createCondition(ConditionType.BROWSER, 805, "jobgroupid", "281"));
            conditions.add(conditionFactory.createCondition(ConditionType.BROWSER, 15855, "jobactivitieid", "282"));
        }
        conditions.add(conditionFactory.createCondition(ConditionType.INPUT, 15767, "jobtitlename"));
        conditions.add(conditionFactory.createCondition(ConditionType.INPUT, 399, "jobtitlemark", true));
        conditions.add(conditionFactory.createCondition(ConditionType.INPUT, 524944, "jobtitlecode"));
        return apidatas;
    }

    @Override
    public Map<String, Object> getMultBrowserDestData(Map<String, Object> params) throws Exception {
        Map<String, Object> apidatas = new HashMap<>(16);
        List<Map<String, Object>> datas = new ArrayList<>();
        String selectids = Util.null2String(params.get(BrowserConstant.BROWSER_MULT_DEST_SELECTIDS));
        if (user == null || "".equals(selectids)) {
            return apidatas;
        }
        RecordSet rs = new RecordSet();
        String tempSql = " select c.id,c.jobtitlemark,c.jobtitlename,a.jobgroupname,a.jobgroupremark,b.jobactivitymark " +
                " from HrmJobTitles c left join HrmJobActivities b on b.id = c.jobactivityid left join HrmJobGroups a on a.id = b.jobgroupid " +
                " where c.id in (" + selectids + ")" +
                " order by c.jobtitlemark ";
        rs.execute(tempSql);
        while (rs.next()) {
            Map<String, Object> item = new HashMap<>(16);
            item.put("id", rs.getString("id"));
            item.put("jobtitlemark", Util.null2String(rs.getString("jobtitlemark")));
            item.put("jobactivitymark", Util.null2String(rs.getString("jobactivitymark")));
            item.put("jobgroupremark", Util.null2String(rs.getString("jobgroupremark")));
            datas.add(item);
        }

        List<ListHeadBean> tableHeadColumns = new ArrayList<>();
        tableHeadColumns.add(new ListHeadBean("id", BoolAttr.TRUE));
        tableHeadColumns.add(new ListHeadBean("jobtitlemark", "", 1, BoolAttr.TRUE));
        tableHeadColumns.add(new ListHeadBean("jobactivitymark", ""));
        tableHeadColumns.add(new ListHeadBean("jobgroupremark", ""));

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

    @Override
    public Map<String, Object> browserAutoComplete(HttpServletRequest request, HttpServletResponse response) throws Exception {
        Map<String, Object> apidatas = new HashMap<>(16);
        String q = Util.null2String(request.getParameter("q"));
        List<String> sqlParams = new ArrayList<>();
        String keyword = "";
        if (q.length() > 0) {
            keyword = "%" + q + "%";
        }
        RecordSet rs = new RecordSet();
        String sqlwhere = "where 1 = 1 ";
        String backfields = " a.id as id,a.jobtitlemark as jobtitlemark,a.jobtitlename as jobtitlename,a.jobtitlecode as jobtitlecode,b.jobactivityname as jobactivityname, b.jobactivitymark as jobactivitymark ";
        String fromSql = " from HrmJobTitles a left join HrmJobActivities b on a.jobactivityid=b.id ";
        String orderby = " order by a.id ";
        sqlwhere += " and (a.canceled is null or a.canceled <> 1)";
        if (!"".equals(keyword)) {
            sqlwhere += " and (a.jobtitlemark like ? or a.jobtitlename like ? or a.ecology_pinyin_search like ? )";
            sqlParams.add(keyword);
            sqlParams.add(keyword);
            sqlParams.add(keyword);
        }
        String sql = "select " + backfields + fromSql + sqlwhere + orderby;
        rs.executeQuery(sql, sqlParams);
        List<Map<String, String>> datas = new ArrayList<>();
        while (rs.next()) {
            Map<String, String> item = new HashMap<>();
            item.put("id", Util.null2String(rs.getString("id")));
            String name = Util.null2String(rs.getString("jobtitlemark"));
            String jobtitlecode = Util.null2String(rs.getString("jobtitlecode"));
            String title = name + "|" + jobtitlecode + "|" + Util.null2String(rs.getString("jobactivitymark"));
            item.put("name", name);
            item.put("title", title);
            datas.add(item);
        }
        apidatas.put("datas", datas);
        return apidatas;
    }
}
