package com.engine.organization.wrapper;

import com.alibaba.fastjson.JSON;
import com.engine.common.util.ServiceUtil;
import com.engine.organization.annotation.Log;
import com.engine.organization.entity.TreeData;
import com.engine.organization.entity.scheme.param.LevelSearchParam;
import com.engine.organization.entity.scheme.po.LevelPO;
import com.engine.organization.enums.LogModuleNameEnum;
import com.engine.organization.enums.OperateTypeEnum;
import com.engine.organization.mapper.scheme.LevelMapper;
import com.engine.organization.service.LevelService;
import com.engine.organization.service.impl.LevelServiceImpl;
import com.engine.organization.util.MenuBtn;
import com.engine.organization.util.OrganizationWrapper;
import com.engine.organization.util.db.MapperProxyFactory;
import weaver.hrm.User;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * @description:
 * @author:dxfeng
 * @createTime: 2022/05/10
 * @version: 1.0
 */
public class LevelWrapper extends OrganizationWrapper {
    private LevelService getLevelService(User user) {
        return ServiceUtil.getService(LevelServiceImpl.class, user);
    }

    private LevelMapper getLevelMapper() {
        return MapperProxyFactory.getProxy(LevelMapper.class);
    }

    /**
     * 职等列表
     *
     * @param params
     * @return
     */
    public Map<String, Object> listPage(Map<String, Object> params) {
        return getLevelService(user).listPage(params);
    }

    /**
     * 新增职等
     *
     * @param param
     * @return
     */
    @Log(operateType = OperateTypeEnum.ADD, operateModule = LogModuleNameEnum.LEVEL, operateDesc = "新增职等")
    public int saveLevel(LevelSearchParam param) {
        int saveLevel = getLevelService(user).saveLevel(param);
        writeOperateLog(new Object() {
        }.getClass(), param.getLevelName(), JSON.toJSONString(param), "新增职等");
        return saveLevel;
    }

    /**
     * 更新职等
     *
     * @param param
     * @return
     */
    @Log(operateType = OperateTypeEnum.UPDATE, operateModule = LogModuleNameEnum.LEVEL, operateDesc = "更新职等")
    public int updateLevel(LevelSearchParam param) {
        LevelPO levelByID = getLevelMapper().getLevelByID(param.getId());
        int updateLevel = getLevelService(user).updateLevel(param);
        writeOperateLog(new Object() {
        }.getClass(), levelByID.getLevelName(), JSON.toJSONString(param), levelByID, getLevelMapper().getLevelByID(param.getId()));
        return updateLevel;
    }

    /**
     * 更新禁用标记
     *
     * @param params
     */
    @Log(operateType = OperateTypeEnum.UPDATE, operateModule = LogModuleNameEnum.LEVEL, operateDesc = "更新职等禁用标识")
    public int updateForbiddenTagById(LevelSearchParam params) {
        LevelPO levelByID = getLevelMapper().getLevelByID(params.getId());
        int updateForbiddenTagById = getLevelService(user).updateForbiddenTagById(params);
        writeOperateLog(new Object() {
        }.getClass(), levelByID.getLevelName(), JSON.toJSONString(params), levelByID, getLevelMapper().getLevelByID(params.getId()));
        return updateForbiddenTagById;
    }

    /**
     * 根据ID批量删除
     *
     * @param ids
     */
    @Log(operateType = OperateTypeEnum.DELETE, operateModule = LogModuleNameEnum.LEVEL, operateDesc = "删除职等")
    public int deleteByIds(Collection<Long> ids) {
        List<LevelPO> levelPOS = getLevelMapper().getLevelsByIds(ids);
        int deleteByIds = getLevelService(user).deleteByIds(ids);
        for (LevelPO levelPO : levelPOS) {
            writeOperateLog(new Object() {
            }.getClass(), levelPO.getLevelName(), JSON.toJSONString(ids), "删除职等");
        }
        return deleteByIds;
    }

    /**
     * 获取搜索条件
     *
     * @param params
     * @return
     */
    public Map<String, Object> getSearchCondition(Map<String, Object> params) {
        return getLevelService(user).getSearchCondition(params);
    }


    /**
     * 获取新增表单
     *
     * @param params
     * @return
     */
    public Map<String, Object> getLevelForm(Map<String, Object> params) {
        return getLevelService(user).getLevelForm(params);
    }

    /**
     * 获取列表页面按钮信息
     *
     * @return
     */
    public Map<String, List<MenuBtn>> getTableBtn() {
        return getLevelService(user).getTableBtn();
    }

    /**
     * 获取列表tabs
     *
     * @return
     */
    public Map<String, Object> getTabInfo() {
        return getLevelService(user).getTabInfo();
    }

    public TreeData getTreeData() {
        return getLevelService(user).getTreeData();
    }
}
