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.sequence.param.SequenceSearchParam;
import com.engine.organization.entity.sequence.po.SequencePO;
import com.engine.organization.enums.LogModuleNameEnum;
import com.engine.organization.enums.OperateTypeEnum;
import com.engine.organization.mapper.sequence.SequenceMapper;
import com.engine.organization.service.SequenceService;
import com.engine.organization.service.impl.SequenceServiceImpl;
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/12
 * @version: 1.0
 */
public class SequenceWrapper extends OrganizationWrapper {
    private SequenceService getSequenceService(User user) {
        return ServiceUtil.getService(SequenceServiceImpl.class, user);
    }

    private SequenceMapper getSequenceMapper() {
        return MapperProxyFactory.getProxy(SequenceMapper.class);
    }

    /**
     * 岗位列表
     *
     * @param params
     * @return
     */
    public Map<String, Object> listPage(Map<String, Object> params) {
        return getSequenceService(user).listPage(params);
    }

    /**
     * 新增岗位
     *
     * @param param
     * @return
     */
    @Log(operateType = OperateTypeEnum.ADD, operateModule = LogModuleNameEnum.SEQUENCE, operateDesc = "新增岗位序列")
    public int saveSequence(SequenceSearchParam param) {
        int saveSequence = getSequenceService(user).saveSequence(param);
        writeOperateLog(new Object() {
        }.getClass(), param.getSequenceName(), JSON.toJSONString(param), "新增岗位序列");
        return saveSequence;
    }

    /**
     * 更新岗位
     *
     * @param param
     * @return
     */
    @Log(operateType = OperateTypeEnum.UPDATE, operateModule = LogModuleNameEnum.SEQUENCE, operateDesc = "更新岗位序列")
    public int updateSequence(SequenceSearchParam param) {
        SequencePO sequenceByID = getSequenceMapper().getSequenceByID(param.getId());
        int updateSequence = getSequenceService(user).updateSequence(param);
        writeOperateLog(new Object() {
        }.getClass(), sequenceByID.getSequenceName(), JSON.toJSONString(param), sequenceByID, getSequenceMapper().getSequenceByID(param.getId()));
        return updateSequence;
    }

    /**
     * 更新禁用标记
     *
     * @param param
     */
    @Log(operateType = OperateTypeEnum.UPDATE, operateModule = LogModuleNameEnum.SEQUENCE, operateDesc = "更新岗位序列禁用标识")
    public int updateForbiddenTagById(SequenceSearchParam param) {
        SequencePO sequenceByID = getSequenceMapper().getSequenceByID(param.getId());
        int updateForbiddenTagById = getSequenceService(user).updateForbiddenTagById(param);
        writeOperateLog(new Object() {
        }.getClass(), sequenceByID.getSequenceName(), JSON.toJSONString(param), sequenceByID, getSequenceMapper().getSequenceByID(param.getId()));
        return updateForbiddenTagById;
    }

    /**
     * 根据ID批量删除
     *
     * @param ids
     */
    @Log(operateType = OperateTypeEnum.DELETE, operateModule = LogModuleNameEnum.SEQUENCE, operateDesc = "删除岗位序列")
    public int deleteByIds( Collection<Long> ids) {
        List<SequencePO> sequencePOS = getSequenceMapper().getSequencesByIds(ids);
        int deleteByIds = getSequenceService(user).deleteByIds(ids);
        for (SequencePO sequencePO : sequencePOS) {
            writeOperateLog(new Object() {
            }.getClass(), sequencePO.getSequenceName(), JSON.toJSONString(ids), "删除岗位序列");
        }
        return deleteByIds;
    }

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


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

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

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


    public Map<String, Object> getSchemeInfo(Map<String, Object> map) {
        return getSequenceService(user).getSchemeInfo(map);
    }
}
