package com.engine.organization.wrapper;

import com.alibaba.fastjson.JSON;
import com.api.browser.bean.SearchConditionGroup;
import com.api.browser.bean.SearchConditionItem;
import com.engine.common.util.ServiceUtil;
import com.engine.organization.annotation.Log;
import com.engine.organization.entity.detach.param.ManagerDetachParam;
import com.engine.organization.entity.detach.po.ManagerDetachPO;
import com.engine.organization.enums.LogModuleNameEnum;
import com.engine.organization.enums.OperateTypeEnum;
import com.engine.organization.mapper.detach.ManagerDetachMapper;
import com.engine.organization.service.ManagerDetachService;
import com.engine.organization.service.impl.ManagerDetachServiceImpl;
import com.engine.organization.util.MenuBtn;
import com.engine.organization.util.OrganizationFormItemUtil;
import com.engine.organization.util.OrganizationWrapper;
import com.engine.organization.util.db.MapperProxyFactory;
import weaver.hrm.User;

import java.util.*;

/**
 * @Author weaver_cl
 * @Description:
 * @Date 2022/10/21
 * @Version V1.0
 **/
public class ManagerDetachWrapper extends OrganizationWrapper {

    public ManagerDetachService getManagerDetachService(User user) {
        return ServiceUtil.getService(ManagerDetachServiceImpl.class,user);
    }

    public Map<String, Object> listPage(Map<String, Object> params) {
        return getManagerDetachService(user).listPage(params);
    }

    public Map<String, List<MenuBtn>> getTableBtn() {
        return MenuBtn.getCommonBtnDatas();
    }

    public Map<String, Object> getSearchCondition() {
        Map<String, Object> apiDatas = new HashMap<>();
        List<SearchConditionGroup> addGroups = new ArrayList<>();
        List<SearchConditionItem> conditionItems = new ArrayList<>();
        SearchConditionItem ecManager = OrganizationFormItemUtil.inputItem(user, 2, 16, 2, 50, "管理员名称", "ecManager");
        SearchConditionItem ecRolelevel = OrganizationFormItemUtil.browserItem(user, 2, 16, 2, false, "可管理组织机构", "164", "ecRolelevel", "");
        conditionItems.add(ecManager);
        conditionItems.add(ecRolelevel);
        addGroups.add(new SearchConditionGroup("高级搜索条件", true, conditionItems));
        apiDatas.put("conditions", addGroups);
        return apiDatas;
    }

    public Map<String, Object> getForm(Integer id) {
        return getManagerDetachService(user).getForm(id);
    }

    @Log(operateType = OperateTypeEnum.DELETE,operateModule = LogModuleNameEnum.DETACH,operateDesc = "删除分权数据")
    public int deleteByIds(Collection<Long> ids) {
        List<ManagerDetachPO> managerDetachPOS = MapperProxyFactory.getProxy(ManagerDetachMapper.class).selectByIds(ids);
        int deleteByIds = getManagerDetachService(user).deleteByIds(ids);
        for (ManagerDetachPO managerDetachPO : managerDetachPOS) {
            writeOperateLog(new Object() {
            }.getClass(), String.valueOf(managerDetachPO.getEcManager()), JSON.toJSONString(ids), "删除等级方案");
        }
        return deleteByIds;
    }


    @Log(operateType = OperateTypeEnum.ADD,operateModule = LogModuleNameEnum.DETACH,operateDesc = "新增分权管理员")
    public int save(ManagerDetachParam param) {
        int save = getManagerDetachService(user).save(param);
        writeOperateLog(new Object() {
        }.getClass(), String.valueOf(param.getEcManager()), JSON.toJSONString(param), "新增分权管理员");
        return save;
    }

    @Log(operateType = OperateTypeEnum.UPDATE, operateModule = LogModuleNameEnum.DETACH, operateDesc = "分权数据更新")
    public int updateDetach(ManagerDetachParam param) {
        ManagerDetachPO managerDetachPO = MapperProxyFactory.getProxy(ManagerDetachMapper.class).getDetachById(param.getId());
        int result = getManagerDetachService(user).updateDetach(param);
        writeOperateLog(new Object() {
        }.getClass(), String.valueOf(managerDetachPO.getEcManager()), JSON.toJSONString(param), managerDetachPO, MapperProxyFactory.getProxy(ManagerDetachMapper.class).getDetachById(param.getId()));
        return result;
    }


    public String doDetach(String isDetach) {
        return getManagerDetachService(user).doDetach(isDetach);
    }
}
