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.fieldset.vo.TypeTreeVO;
import com.engine.organization.entity.scheme.param.SchemeSearchParam;
import com.engine.organization.entity.scheme.po.SchemePO;
import com.engine.organization.enums.LogModuleNameEnum;
import com.engine.organization.enums.OperateTypeEnum;
import com.engine.organization.mapper.scheme.SchemeMapper;
import com.engine.organization.service.SchemeService;
import com.engine.organization.service.impl.SchemeServiceImpl;
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;

/**
 * @Author dxfeng
 * @description:
 * @Date 2022/5/9
 * @Version V1.0
 **/
public class SchemeWrapper extends OrganizationWrapper {

    private SchemeService getSchemeService(User user) {
        return ServiceUtil.getService(SchemeServiceImpl.class, user);
    }

    private SchemeMapper getSchemeMapper() {
        return MapperProxyFactory.getProxy(SchemeMapper.class);
    }

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

    /**
     * 新增等级方案
     *
     * @param param
     * @return
     */
    @Log(operateType = OperateTypeEnum.ADD, operateModule = LogModuleNameEnum.SCHEME, operateDesc = "新增等级方案")
    public int save(SchemeSearchParam param) {
        int save = getSchemeService(user).save(param);
        writeOperateLog(new Object() {
        }.getClass(), param.getSchemeName(), JSON.toJSONString(param), "新增等级方案");
        return save;
    }

    /**
     * 更新等级方案信息
     *
     * @param param
     * @return
     */
    @Log(operateType = OperateTypeEnum.UPDATE, operateModule = LogModuleNameEnum.SCHEME, operateDesc = "更新等级方案")
    public int updateScheme(SchemeSearchParam param) {
        SchemePO schemeByID = getSchemeMapper().getSchemeByID(param.getId());
        int updateScheme = getSchemeService(user).updateScheme(param);
        writeOperateLog(new Object() {
        }.getClass(), schemeByID.getSchemeName(), JSON.toJSONString(param), schemeByID, getSchemeMapper().getSchemeByID(param.getId()));
        return updateScheme;
    }

    /**
     * 更新禁用标记
     *
     * @param params
     */
    @Log(operateType = OperateTypeEnum.UPDATE, operateModule = LogModuleNameEnum.SCHEME, operateDesc = "更新等级方案禁用标识")
    public int updateForbiddenTagById(SchemeSearchParam params) {
        SchemePO schemeByID = getSchemeMapper().getSchemeByID(params.getId());
        int updateForbiddenTagById = getSchemeService(user).updateForbiddenTagById(params);
        writeOperateLog(new Object() {
        }.getClass(), schemeByID.getSchemeName(), JSON.toJSONString(params), schemeByID, getSchemeMapper().getSchemeByID(params.getId()));
        return updateForbiddenTagById;
    }

    /**
     * 根据ID批量删除等级方案信息
     *
     * @param ids
     */
    @Log(operateType = OperateTypeEnum.DELETE, operateModule = LogModuleNameEnum.SCHEME, operateDesc = "删除等级方案")
    public int deleteByIds(Collection<Long> ids) {
        List<SchemePO> schemePOS = getSchemeMapper().getSchemesByIds(ids);
        int deleteByIds = getSchemeService(user).deleteByIds(ids);
        for (SchemePO schemePO : schemePOS) {
            writeOperateLog(new Object() {
            }.getClass(), schemePO.getSchemeName(), JSON.toJSONString(ids), "删除等级方案");

        }
        return deleteByIds;
    }


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


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

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


    /**
     * 等级方案树
     * @return
     */
    public List<TypeTreeVO> getTreeData() {
        return getSchemeService(user).getTreeData();
    }
}
