package com.engine.organization.service.impl;

import com.api.browser.bean.SearchConditionGroup;
import com.api.browser.bean.SearchConditionItem;
import com.cloudstore.eccom.result.WeaResultMsg;
import com.engine.core.impl.Service;
import com.engine.organization.component.OrganizationWeaTable;
import com.engine.organization.entity.logview.param.LogViewSearchParam;
import com.engine.organization.entity.logview.vo.LogViewVO;
import com.engine.organization.service.LogViewService;
import com.engine.organization.util.OrganizationFormItemUtil;
import org.apache.commons.lang.StringUtils;
import weaver.general.TimeUtil;

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

/**
 * @author:dxfeng
 * @createTime: 2022/07/05
 * @version: 1.0
 */
public class LogViewServiceImpl extends Service implements LogViewService {

    @Override
    public Map<String, Object> listPage(LogViewSearchParam param) {
        Map<String, Object> resultMap = new HashMap<>();
        OrganizationWeaTable<LogViewVO> table = new OrganizationWeaTable<>(user, LogViewVO.class);
        String sqlWhere = buildSqlWhere(param);
        table.setSqlwhere(sqlWhere);
        WeaResultMsg result = new WeaResultMsg(false);
        result.putAll(table.makeDataResult());
        result.success();
        resultMap.putAll(result.getResultMap());
        return resultMap;
    }

    @Override
    public Map<String, Object> getSearchCondition() {
        Map<String, Object> apiDatas = new HashMap<>();
        List<SearchConditionGroup> addGroups = new ArrayList<>();
        List<SearchConditionItem> conditionItems = new ArrayList<>();

        // 操作者
        SearchConditionItem operatorId = OrganizationFormItemUtil.browserItem(user, 2, 16, 2, false, "操作者", "1", "operatorId", "");
        //操作时间
        SearchConditionItem dateItem = OrganizationFormItemUtil.dateItem(user,2,16,true,2,"操作时间","operateDate");

        // 操作者部门
        SearchConditionItem departmentId = OrganizationFormItemUtil.browserItem(user, 2, 16, 2, false, "操作者部门", "4", "departmentId", "");
        // 操作者分部
        SearchConditionItem companyId = OrganizationFormItemUtil.browserItem(user, 2, 16, 2, false, "操作者分部", "164", "companyId", "");

        conditionItems.add(operatorId);
        conditionItems.add(dateItem);
        conditionItems.add(departmentId);
        conditionItems.add(companyId);

        addGroups.add(new SearchConditionGroup("高级搜索条件", true, conditionItems));
        apiDatas.put("conditions", addGroups);
        return apiDatas;
    }

    @Override
    public String showDetailById(Long id) {
        return null;
    }

    static class DateGroupData {
        private String name;
        private String value;

        public DateGroupData(String name, String value) {
            this.name = name;
            this.value = value;
        }

        public String getName() {
            return name;
        }

        public String getValue() {
            return value;
        }
    }
    private String buildSqlWhere(LogViewSearchParam param) {
        if (null == param) {
            return "";
        }
        String sqlWhere = " where delete_type = 0 ";
        if (StringUtils.isNotBlank(param.getModuleType())) {
            sqlWhere += " and operate_module = '" + param.getModuleType() + "'";
        }
        if (null != param.getOperatorId()) {
            sqlWhere += " and operator_id = '" + param.getOperatorId() + "'";
        }
        if (null != param.getCompanyId()) {
            sqlWhere += " and operator_id in (select id from hrmresource where subcompanyid1 = '" + param.getCompanyId() + "')";
        }
        if (null != param.getDepartmentId()) {
            sqlWhere += " and operator_id in (select id from hrmresource where departmentid = '" + param.getDepartmentId() + "')";
        }
        if (StringUtils.isNotBlank(param.getOperateDate()) && !"-1".equals(param.getOperateDate()) && !"0,,".equals(param.getOperateDate())) {
            if (!"6".equals(param.getOperateDate())) {
                sqlWhere += " and create_time >= '" + TimeUtil.getDateByOption(param.getOperateDate(), "0") + " 00:00:00'";
                sqlWhere += " and create_time <= '" + TimeUtil.getDateByOption(param.getOperateDate(), "") + " 23:59:59'";
            } else {
                if (StringUtils.isNotBlank(param.getStartDate())) {
                    sqlWhere += " and create_time >= '" + param.getStartDate() + "'";
                }
                if (StringUtils.isNotBlank(param.getEndDate())) {
                    sqlWhere += " and create_time <= '" + param.getEndDate() + "'";
                }
            }
        }
        return sqlWhere;
    }
}
