package com.engine.organization.service.impl;

import com.api.browser.bean.SearchConditionGroup;
import com.api.browser.bean.SearchConditionItem;
import com.engine.core.impl.Service;
import com.engine.organization.entity.TopTab;
import com.engine.organization.service.GroupService;
import com.engine.organization.thread.GroupTriggerRunnable;
import com.engine.organization.util.HasRightUtil;
import com.engine.organization.util.MenuBtn;
import com.engine.organization.util.OrganizationAssert;
import com.engine.organization.util.OrganizationFormItemUtil;
import weaver.conn.RecordSet;
import weaver.general.StringUtil;
import weaver.general.Util;

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

/**
 * @description:
 * @author:dxfeng
 * @createTime: 2022/05/16
 * @version: 1.0
 */
public class GroupServiceImpl extends Service implements GroupService {
    private static final String RIGHT_NAME = "Group:All";

    @Override
    public Map<String, Object> getGroupFormField(Map<String, Object> params) {
        Map<String, Object> apiDatas = new HashMap<>();
        List<SearchConditionItem> selectItems = new ArrayList<>();
        List<SearchConditionGroup> addGroups = new ArrayList<>();
        SearchConditionItem companyNameItem = OrganizationFormItemUtil.inputItem(user, 2, 16, 3, 50, "总部名称", "companyname");
        companyNameItem.setRules("required|string");
        SearchConditionItem companyDescItem = OrganizationFormItemUtil.inputItem(user, 2, 16, 2, 50, "总部全称", "companydesc");
        SearchConditionItem companyWebItem = OrganizationFormItemUtil.textareaItem(user, 2, 16, true, 2, 60, "公司网站", "companyweb");

        // 赋值
        String id = Util.null2String(params.get("id"));
        if (!StringUtil.isEmpty(id)) {
            RecordSet rs = new RecordSet();
            String sql = "select * from HrmCompany where id in(" + id + ")";
            rs.executeQuery(sql);
            int colcount = rs.getColCounts();
            while (rs.next()) {
                Map<String, Object> row = new HashMap<>();
                for (int i = 1; i <= colcount; i++) {
                    row.put(rs.getColumnName(i).toLowerCase(), Util.null2String(rs.getString(i)));
                }
                // 赋值
                companyNameItem.setValue(row.get("companyname"));
                companyDescItem.setValue(row.get("companydesc"));
                companyWebItem.setValue(row.get("companyweb"));
            }
        }


        // 设置编辑属性
        if ("1".equals(Util.null2String(params.get("viewattr")))) {
            companyNameItem.setViewAttr(1);
            companyDescItem.setViewAttr(1);
            companyWebItem.setViewAttr(1);
        }

        selectItems.add(companyNameItem);
        selectItems.add(companyDescItem);
        selectItems.add(companyWebItem);
        addGroups.add(new SearchConditionGroup("基本信息", true, selectItems));
        apiDatas.put("condition", addGroups);
        return apiDatas;
    }

    @Override
    public boolean updateGroup(Map<String, Object> params) {
        HasRightUtil.hasRight(user, RIGHT_NAME, false);
        String id = Util.null2String(params.get("id"));
        OrganizationAssert.notNull(id, "数据有误");
        String companyname = (String) params.get("companyname");
        String companydesc = (String) params.get("companydesc");
        String companyweb = (String) params.get("companyweb");
        RecordSet rs = new RecordSet();
        boolean executeUpdate = rs.executeUpdate("update HrmCompany set COMPANYNAME = ?,COMPANYDESC=?,COMPANYWEB=? where id = ?", companyname, companydesc, companyweb, id);
        new Thread(new GroupTriggerRunnable(params)).start();
        return executeUpdate;

    }

    @Override
    public Map<String, Object> getHasRight() {
        Map<String, Object> btnDatas = new HashMap<>();
        ArrayList<MenuBtn> topMenuList = new ArrayList<>();
        ArrayList<MenuBtn> rightMenuList = new ArrayList<>();
        // 编辑
        topMenuList.add(MenuBtn.builder().isTop("1").menuFun("doEdit").menuIcon("icon-coms-edit").menuName("编辑").type("BTN_EDIT").build());
        btnDatas.put("topMenu", topMenuList);
        // 编辑
        rightMenuList.add(MenuBtn.builder().isTop("1").menuFun("doEdit").menuIcon("icon-coms-edit").menuName("编辑").type("BTN_EDIT").build());
        btnDatas.put("rightMenu", rightMenuList);
        btnDatas.put("hasRight", HasRightUtil.hasRight(user, RIGHT_NAME, true));
        return btnDatas;
    }

    @Override
    public Map<String, Object> getTabInfo() {
        Map<String, Object> apiDatas = new HashMap<>();
        List<TopTab> topTabs = new ArrayList<>();
        topTabs.add(TopTab.builder().title("总部信息").viewCondition("1").build());
        apiDatas.put("topTabs", topTabs);
        return apiDatas;
    }

}
