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.StaffSearchParam;
import com.engine.organization.entity.staff.po.StaffPO;
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.StaffMapper;
import com.engine.organization.mapper.staff.StaffPlanMapper;
import com.engine.organization.service.StaffService;
import com.engine.organization.service.impl.StaffServiceImpl;
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;
import java.util.stream.Collectors;

/**
 * @description:
 * @author:dxfeng
 * @createTime: 2022/05/25
 * @version: 1.0
 */
public class StaffWrapper extends OrganizationWrapper {
    private StaffService getStaffService(User user) {
        return ServiceUtil.getService(StaffServiceImpl.class, user);
    }

    private StaffMapper getStaffMapper() {
        return MapperProxyFactory.getProxy(StaffMapper.class);
    }

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

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

    /**
     * 新增编制
     *
     * @param param
     * @return
     */
    @Log(operateType = OperateTypeEnum.ADD, operateModule = LogModuleNameEnum.STAFF, operateDesc = "新增编制上报")
    public int saveStaff(StaffSearchParam param) {
        int saveStaff = getStaffService(user).saveStaff(param);
        writeOperateLog(new Object() {
        }.getClass(), getStaffPlanMapper().getStaffPlanByID(param.getPlanId()).getPlanName(), JSON.toJSONString(param), "新增编制上报");
        return saveStaff;
    }

    /**
     * 更新编制
     *
     * @param param
     * @return
     */
    @Log(operateType = OperateTypeEnum.UPDATE, operateModule = LogModuleNameEnum.STAFF, operateDesc = "更新编制上报")
    public int updateStaff(StaffSearchParam param) {
        StaffPO staffByID = getStaffMapper().getStaffByID(param.getId());
        int updateStaff = getStaffService(user).updateStaff(param);
        writeOperateLog(new Object() {
        }.getClass(), getStaffPlanMapper().getStaffPlanByID(param.getPlanId()).getPlanName(), JSON.toJSONString(param), staffByID, getStaffMapper().getStaffByID(param.getId()));
        return updateStaff;
    }


    /**
     * 根据ID批量删除
     *
     * @param ids
     */
    @Log(operateType = OperateTypeEnum.DELETE, operateModule = LogModuleNameEnum.STAFF, operateDesc = "删除编制上报")
    public int deleteByIds(Collection<Long> ids) {
        List<StaffPO> staffPOS = getStaffMapper().getStaffsByIds(ids);
        List<StaffPlanPO> staffPlansByIds = getStaffPlanMapper().getStaffPlansByIds(ids);
        Map<Long, String> collect = staffPlansByIds.stream().collect(Collectors.toMap(StaffPlanPO::getId, StaffPlanPO::getPlanName));
        int deleteByIds = getStaffService(user).deleteByIds(ids);
        for (StaffPO staffPO : staffPOS) {
            writeOperateLog(new Object() {
            }.getClass(), collect.get(staffPO.getId()), JSON.toJSONString(ids), "删除编制上报");
        }
        return deleteByIds;
    }

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


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

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