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.scheme.param.GradeSearchParam;
import com.engine.organization.entity.scheme.po.GradePO;
import com.engine.organization.enums.LogModuleNameEnum;
import com.engine.organization.enums.OperateTypeEnum;
import com.engine.organization.mapper.scheme.GradeMapper;
import com.engine.organization.service.GradeService;
import com.engine.organization.service.impl.GradeServiceImpl;
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/11
 * @version: 1.0
 */
public class GradeWrapper extends OrganizationWrapper {


    private GradeService getGradeService(User user) {
        return ServiceUtil.getService(GradeServiceImpl.class, user);
    }

    private GradeMapper getGradeMapper() {
        return MapperProxyFactory.getProxy(GradeMapper.class);
    }


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

    /**
     * 新增职级
     *
     * @param param
     * @return
     */
    @Log(operateType = OperateTypeEnum.ADD, operateModule = LogModuleNameEnum.GRADE, operateDesc = "新增职级")
    public int saveGrade(GradeSearchParam param) {
        int saveGrade = getGradeService(user).saveGrade(param);
        writeOperateLog(new Object() {
        }.getClass(), param.getGradeName(), JSON.toJSONString(param), "新增职级");
        return saveGrade;
    }

    /**
     * 更新职级
     *
     * @param param
     * @return
     */
    @Log(operateType = OperateTypeEnum.UPDATE, operateModule = LogModuleNameEnum.GRADE, operateDesc = "更新职级")
    public int updateGrade(GradeSearchParam param) {
        GradePO original =  getGradeMapper().getGradeByID(param.getId());
        int updateGrade = getGradeService(user).updateGrade(param);
        writeOperateLog(new Object() {
        }.getClass(), original.getGradeName(), JSON.toJSONString(param), original,  getGradeMapper().getGradeByID(param.getId()));
        return updateGrade;
    }

    /**
     * 更新禁用标记
     *
     * @param params
     */
    @Log(operateType = OperateTypeEnum.UPDATE, operateModule = LogModuleNameEnum.GRADE, operateDesc = "更新职级禁用标识")
    public int updateForbiddenTagById(GradeSearchParam params) {
        GradePO original =  getGradeMapper().getGradeByID(params.getId());
        int updateForbiddenTagById = getGradeService(user).updateForbiddenTagById(params);
        writeOperateLog(new Object() {
        }.getClass(), original.getGradeName(), JSON.toJSONString(params), original,  getGradeMapper().getGradeByID(params.getId()));
        return updateForbiddenTagById;
    }

    /**
     * 根据ID批量删除
     *
     * @param ids
     */
    @Log(operateType = OperateTypeEnum.DELETE, operateModule = LogModuleNameEnum.GRADE, operateDesc = "删除职级")
    public int deleteByIds(Collection<Long> ids) {
        List<String> gradeNameByIds =  getGradeMapper().getGradeNameByIds(ids);
        int deleteByIds = getGradeService(user).deleteByIds(ids);
        for (String gradeName : gradeNameByIds) {
            writeOperateLog(new Object() {
            }.getClass(), gradeName, JSON.toJSONString(ids), "删除职级");
        }
        return deleteByIds;
    }

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


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

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

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


    public String getLevelId(Long id) {
        return getGradeService(user).getLevelId(id);
    }
}
