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.staff.param.StaffPlanSearchParam;
import com.engine.organization.entity.staff.po.StaffPlanPO;
import com.engine.organization.enums.LogModuleNameEnum;
import com.engine.organization.enums.OperateTypeEnum;
import com.engine.organization.mapper.staff.StaffPlanMapper;
import com.engine.organization.service.StaffPlanService;
import com.engine.organization.service.impl.StaffPlanServiceImpl;
import com.engine.organization.util.MenuBtn;
import com.engine.organization.util.OrganizationWrapper;
import com.engine.organization.util.db.MapperProxyFactory;
import org.apache.ibatis.annotations.Param;
import weaver.hrm.User;

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

/**
 * @description:
 * @author:dxfeng
 * @createTime: 2022/05/25
 * @version: 1.0
 */
public class StaffPlanWrapper extends OrganizationWrapper {
    private StaffPlanService getStaffPlanService(User user) {
        return ServiceUtil.getService(StaffPlanServiceImpl.class, user);
    }

    private StaffPlanMapper getStaffPlanMapper() {
        return MapperProxyFactory.getProxy(StaffPlanMapper.class);
    }

    /**
     * 编制方案列表
     *
     * @param params
     * @return
     */
    public Map<String, Object> listPage(StaffPlanSearchParam params) {
        return getStaffPlanService(user).listPage(params);
    }

    /**
     * 新增编制方案
     *
     * @param param
     * @return
     */
    @Log(operateType = OperateTypeEnum.ADD, operateModule = LogModuleNameEnum.STAFFPLAN, operateDesc = "新增编制方案")
    public int saveStaffPlan(StaffPlanSearchParam param) {
        int saveStaffPlan = getStaffPlanService(user).saveStaffPlan(param);
        writeOperateLog(new Object() {
        }.getClass(), param.getPlanName(), JSON.toJSONString(param), "新增编制方案");
        return saveStaffPlan;
    }

    /**
     * 更新编制方案
     *
     * @param param
     * @return
     */
    @Log(operateType = OperateTypeEnum.UPDATE, operateModule = LogModuleNameEnum.STAFFPLAN, operateDesc = "更新编制方案")
    public int updateStaffPlan(StaffPlanSearchParam param) {
        StaffPlanPO staffPlanByID = getStaffPlanMapper().getStaffPlanByID(param.getId());
        int updateStaffPlan = getStaffPlanService(user).updateStaffPlan(param);
        writeOperateLog(new Object() {
        }.getClass(), staffPlanByID.getPlanName(), JSON.toJSONString(param), staffPlanByID, getStaffPlanMapper().getStaffPlanByID(param.getId()));
        return updateStaffPlan;
    }

    /**
     * 更新禁用标记
     *
     * @param param
     */
    @Log(operateType = OperateTypeEnum.UPDATE, operateModule = LogModuleNameEnum.STAFFPLAN, operateDesc = "更新编制方案禁用标识")
    public int updateForbiddenTagById(StaffPlanSearchParam param) {
        StaffPlanPO staffPlanByID = getStaffPlanMapper().getStaffPlanByID(param.getId());
        int updateForbiddenTagById = getStaffPlanService(user).updateForbiddenTagById(param);
        writeOperateLog(new Object() {
        }.getClass(), staffPlanByID.getPlanName(), JSON.toJSONString(param), staffPlanByID, getStaffPlanMapper().getStaffPlanByID(param.getId()));
        return updateForbiddenTagById;
    }

    /**
     * 根据ID批量删除
     *
     * @param ids
     */
    @Log(operateType = OperateTypeEnum.DELETE, operateModule = LogModuleNameEnum.STAFFPLAN, operateDesc = "删除编制方案")
    public int deleteByIds(@Param("ids") Collection<Long> ids) {
        List<StaffPlanPO> staffPlanPOS = getStaffPlanMapper().getStaffPlansByIds(ids);
        int deleteByIds = getStaffPlanService(user).deleteByIds(ids);
        for (StaffPlanPO staffPlanPO : staffPlanPOS) {
            writeOperateLog(new Object() {
            }.getClass(), staffPlanPO.getPlanName(), JSON.toJSONString(ids), "删除编制方案");
        }
        return deleteByIds;
    }

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


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

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