package com.api.browser.service.impl;

import com.api.browser.bean.SearchConditionItem;
import com.api.browser.service.BrowserService;
import com.api.browser.util.BrowserConstant;
import com.api.hrm.bean.TreeNode;
import com.cloudstore.eccom.result.WeaResultMsg;
import com.engine.organization.component.OrganizationWeaTable;
import com.engine.organization.entity.browser.bo.CusBowserTreeBO;
import com.engine.organization.entity.browser.enums.TreeNodeTypeEnum;
import com.engine.organization.entity.browser.po.CusBrowserTree;
import com.engine.organization.entity.job.vo.JobBrowserVO;
import com.engine.organization.entity.searchtree.SearchTree;
import com.engine.organization.entity.searchtree.SearchTreeParams;
import com.engine.organization.service.impl.JobServiceImpl;
import com.engine.organization.util.OrganizationFormItemUtil;
import com.engine.organization.util.db.DBType;
import org.apache.commons.lang.StringUtils;
import weaver.conn.RecordSet;
import weaver.general.Util;

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

/**
 * @author:dxfeng
 * @createTime: 2022/07/15
 * @version: 1.0
 */
public class JobBrowserService extends BrowserService {


    @Override
    public Map<String, Object> getBrowserData(Map<String, Object> params) {

        Map<String, Object> resultMap = new HashMap<>();
        String datatype = Util.null2String(params.get("datatype"));
        if ("tree".equals(datatype)) {
            List<TreeNode> nodeData = new ArrayList<>();
            String id = Util.null2String(params.get("id"));
            SearchTreeParams searchTreeParams = new SearchTreeParams();
            if (StringUtils.isBlank(id)) {
                Map<String, Object> searchTree = new JobServiceImpl().getSearchTree(searchTreeParams);
                Map<String, Object> datas = (Map<String, Object>) searchTree.get("datas");
                // 集团
                SearchTree rootCompany = (SearchTree) datas.get("rootCompany");
                nodeData.add(rootCompany);
            } else if ("0".equals(id)) {
                Map<String, Object> searchTree = new JobServiceImpl().getSearchTree(searchTreeParams);
                Map<String, Object> datas = (Map<String, Object>) searchTree.get("datas");
                SearchTree rootCompany = (SearchTree) datas.get("rootCompany");
                nodeData.addAll(rootCompany.getSubs());
            } else {
                String[] idArray = id.split("_");
                if (idArray.length == 2) {
                    if (TreeNodeTypeEnum.TYPE_COMP.getName().equals(idArray[0])) {
                        searchTreeParams.setType(TreeNodeTypeEnum.TYPE_COMP.getValue());
                    } else if (TreeNodeTypeEnum.TYPE_DEPT.getName().equals(idArray[0])) {
                        searchTreeParams.setType(TreeNodeTypeEnum.TYPE_DEPT.getValue());
                    }
                    searchTreeParams.setId(idArray[1]);
                    Map<String, Object> searchTree = new JobServiceImpl().getSearchTree(searchTreeParams);
                    List<TreeNode> datas = (List<TreeNode>) searchTree.get("datas");
                    nodeData.addAll(datas.get(0).getSubs());
                }
            }
            List<CusBrowserTree> cusBrowserTrees = CusBowserTreeBO.convertSearchTreeToBorwserTree(nodeData);
            resultMap.put("datas", cusBrowserTrees);
        } else {
            OrganizationWeaTable<JobBrowserVO> table = new OrganizationWeaTable<>(user, JobBrowserVO.class);
            String sqlWhere = buildSqlWhere(params);
            table.setSqlwhere(sqlWhere);
            WeaResultMsg result = new WeaResultMsg(false);
            result.putAll(table.makeDataResult());
            result.success();
            resultMap.putAll(result.getResultMap());
        }
        return resultMap;
    }

    @Override
    public Map getBrowserConditionInfo(Map<String, Object> map) {
        Map<String, List<SearchConditionItem>> apiDatas = new HashMap<>();
        List<SearchConditionItem> conditions = new ArrayList<>();
        // 编号
        SearchConditionItem jobNoItem = OrganizationFormItemUtil.inputItem(user, 2, 16, 2, 50, "编号", "jobNo");
        // 名称
        SearchConditionItem jobNameItem = OrganizationFormItemUtil.inputItem(user, 2, 16, 2, 50, "名称", "jobName");
        // 所属分部
        SearchConditionItem parentCompBrowserItem = OrganizationFormItemUtil.browserItem(user, 2, 16, 2, false, "所属分部", "164", "ecCompany", "");
        // 所属部门
        SearchConditionItem parentDeptBrowserItem = OrganizationFormItemUtil.browserItem(user, 2, 16, 2, false, "所属部门", "4", "ecDepartment", "");

        conditions.add(jobNoItem);
        conditions.add(jobNameItem);
        conditions.add(parentCompBrowserItem);
        conditions.add(parentDeptBrowserItem);
        apiDatas.put(BrowserConstant.BROWSER_RESULT_CONDITIONS, conditions);
        return apiDatas;
    }

    /**
     * 查询条件
     *
     * @param params
     * @return
     */
    private String buildSqlWhere(Map<String, Object> params) {
        DBType dbType = DBType.get(new RecordSet().getDBType());
        String sqlWhere = " where t.delete_type ='0' ";
        String jobNo = Util.null2String(params.get("jobNo"));
        if (StringUtils.isNotBlank(jobNo)) {
            sqlWhere += " AND t.job_no " + dbType.like(jobNo);
        }
        String jobName = Util.null2String(params.get("jobName"));
        if (StringUtils.isNotBlank(jobName)) {
            sqlWhere += " AND t.job_name " + dbType.like(jobName);
        }
        String ecCompany = Util.null2String(params.get("ecCompany"));
        if (StringUtils.isNotBlank(ecCompany)) {
            sqlWhere += " AND t.ec_company = '" + Long.parseLong(ecCompany) + "'";
        }
        String ecDepartment = Util.null2String(params.get("ecDepartment"));
        if (StringUtils.isNotBlank(ecDepartment)) {
            sqlWhere += " AND t.ec_department = '" + Long.parseLong(ecDepartment) + "'";
        }
        String treeKey = Util.null2String(params.get("treeKey"));
        if (StringUtils.isNotBlank(treeKey)) {
            String[] keyArray = treeKey.split("_");
            if (keyArray.length == 2) {
                if (TreeNodeTypeEnum.TYPE_COMP.getName().equals(keyArray[0])) {
                    sqlWhere += " AND t.parent_comp = '" + Long.parseLong(keyArray[1]) + "'";
                } else if (TreeNodeTypeEnum.TYPE_DEPT.getName().equals(keyArray[0])) {
                    sqlWhere += " AND t.parent_dept = '" + Long.parseLong(keyArray[1]) + "'";
                }
            }
        }
        return sqlWhere;
    }
}
