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.postion.po.PostPO;
import com.engine.organization.enums.LogModuleNameEnum;
import com.engine.organization.enums.OperateTypeEnum;
import com.engine.organization.mapper.post.PostMapper;
import com.engine.organization.service.PostService;
import com.engine.organization.service.impl.PostServiceImpl;
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/13
 * @version: 1.0
 */
public class PostWrapper extends OrganizationWrapper {
    private PostService getPostService(User user) {
        return ServiceUtil.getService(PostServiceImpl.class, user);
    }

    private PostMapper getPostMapper() {
        return MapperProxyFactory.getProxy(PostMapper.class);
    }

    /**
     * 新增职务分类
     *
     * @param postPO
     * @return
     */
    @Log(operateType = OperateTypeEnum.ADD, operateModule = LogModuleNameEnum.POSTINFO, operateDesc = "新增职务分类")
    public int savePost(PostPO postPO) {
        int savePost = getPostService(user).savePost(postPO);
        writeOperateLog(new Object() {
        }.getClass(), postPO.getPostName(), JSON.toJSONString(postPO), "新增职务分类");
        return savePost;
    }

    /**
     * 更新职务分类
     *
     * @param postPO
     * @return
     */
    @Log(operateType = OperateTypeEnum.UPDATE, operateModule = LogModuleNameEnum.POSTINFO, operateDesc = "更新职务分类")
    public int updatePost(PostPO postPO) {
        PostPO postByID = getPostMapper().getPostByID(postPO.getId());
        int updatePost = getPostService(user).updatePost(postPO);
        writeOperateLog(new Object() {
        }.getClass(), postByID.getPostName(), JSON.toJSONString(postPO), postByID, getPostMapper().getPostByID(postPO.getId()));
        return updatePost;
    }

    /**
     * 根据ID批量删除
     *
     * @param ids
     */
    @Log(operateType = OperateTypeEnum.DELETE, operateModule = LogModuleNameEnum.POSTINFO, operateDesc = "删除职务分类")
    public int deleteByIds(Collection<Long> ids) {
        List<PostPO> postPOS = getPostMapper().getPostsByIds(ids);
        int deleteByIds = getPostService(user).deleteByIds(ids);
        for (PostPO postPO : postPOS) {
            writeOperateLog(new Object() {
            }.getClass(), postPO.getPostName(), JSON.toJSONString(ids), "删除职务分类");
        }
        return deleteByIds;
    }

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

    /**
     * 获取左侧树
     *
     * @return
     */
    public TreeData getTreeData() {
        return getPostService(user).getTreeData();
    }

}
