package com.engine.organization.wrapper;

import com.alibaba.fastjson.JSON;
import com.api.browser.bean.SearchConditionGroup;
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.hrmresource.po.ResourceItemClassPO;
import com.engine.organization.entity.hrmresource.po.ResourceItemPO;
import com.engine.organization.enums.DeleteTypeEnum;
import com.engine.organization.enums.LogModuleNameEnum;
import com.engine.organization.enums.OperateTypeEnum;
import com.engine.organization.mapper.hrmresource.ResourceBasicInfoMapper;
import com.engine.organization.service.ResourceBasicInfoService;
import com.engine.organization.service.impl.ResourceBasicInfoServiceImpl;
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.*;

/**
 * @Author weaver_cl
 * @Description:
 * @Date 2022/8/23
 * @Version V1.0
 **/
public class ResourceBasicInfoWrapper extends OrganizationWrapper {

    private ResourceBasicInfoService getResourceBasicInfoService(User user) {
        return ServiceUtil.getService(ResourceBasicInfoServiceImpl.class, user);
    }

    private ResourceBasicInfoMapper getResourceBasicInfoMapper() {
        return MapperProxyFactory.getProxy(ResourceBasicInfoMapper.class);
    }

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

    public List<TypeTreeVO> getTreeData() {
        return getResourceBasicInfoService(user).getTreeData();
    }


    public List<SearchConditionGroup> getForm(Map<String, Object> params) {
        return getResourceBasicInfoService(user).getForm(params);
    }

    public Map<String, Object> getHasRight() {
        Map<String, Object> btnDatas = new HashMap<>();
        ArrayList<MenuBtn> topMenuList = new ArrayList<>();
        ArrayList<MenuBtn> rightMenuList = new ArrayList<>();
        // 新增
        topMenuList.add(MenuBtn.builder().isBatch("0").isTop("1").menuFun("addItemClass").menuIcon("icon-coms-New-Flow").menuName("新增类型").type("BTN_AddItemClass").build());
        topMenuList.add(MenuBtn.builder().isBatch("0").isTop("1").menuFun("editItemClass").menuIcon("icon-coms-edit").menuName("修改类型").type("BTN_EditItemClass").build());
        topMenuList.add(MenuBtn.builder().isBatch("0").isTop("1").menuFun("delItemClass").menuIcon("icon-coms-Delete").menuName("删除类型").type("BTN_DelItemClass").build());
        topMenuList.add(MenuBtn.topMenu_addNew());
        // 批量删除
        topMenuList.add(MenuBtn.topMenu_batchDelete());
        btnDatas.put("topMenu", topMenuList);
        // 新增
        rightMenuList.add(MenuBtn.rightMenu_addNew());
        // 日志
        rightMenuList.add(MenuBtn.rightMenu_btnLog());
        // 显示列定制
        rightMenuList.add(MenuBtn.rightMenu_btnColumn());
        btnDatas.put("rightMenu", rightMenuList);
        return btnDatas;
    }

    @Log(operateType = OperateTypeEnum.DELETE, operateModule = LogModuleNameEnum.RESOURCE, operateDesc = "删除人员信息资料")
    public int deleteByIds(Collection<Long> ids) {
        List<ResourceItemPO> resourceItemPOS = getResourceBasicInfoMapper().selectItemByIds(ids);
        int result = getResourceBasicInfoService(user).deletByIds(ids);
        for (ResourceItemPO resourceItemPO : resourceItemPOS) {
            writeOperateLog(new Object() {
            }.getClass(), resourceItemPO.getFname(), JSON.toJSONString(ids), "删除人员信息资料");
        }
        return result;
    }


    @Log(operateType = OperateTypeEnum.RECOVER, operateModule = LogModuleNameEnum.RESOURCE, operateDesc = "恢复人员信息资料")
    public int recoverByIds(Collection<Long> ids) {
        List<ResourceItemPO> resourceItemPOS = getResourceBasicInfoMapper().selectItemByIds(ids);
        int result = getResourceBasicInfoService(user).recoverByIds(ids);
        for (ResourceItemPO resourceItemPO : resourceItemPOS) {
            writeOperateLog(new Object() {
            }.getClass(), resourceItemPO.getFname(), JSON.toJSONString(ids), "恢复人员信息资料");
        }
        return result;
    }

    @Log(operateType = OperateTypeEnum.ADD, operateModule = LogModuleNameEnum.RESOURCE, operateDesc = "新增人员信息资料")
    public int save(ResourceItemPO resourceItemPO) {
        resourceItemPO.setCreateTime(new Date());
        resourceItemPO.setCreator((long) user.getUID());
        resourceItemPO.setFdelete(DeleteTypeEnum.NOT_DELETED.getValue());
        int result = getResourceBasicInfoService(user).save(resourceItemPO);
        writeOperateLog(new Object() {
        }.getClass(), resourceItemPO.getFname(), JSON.toJSONString(resourceItemPO), "新增人员信息资料");
        return result;
    }

    @Log(operateType = OperateTypeEnum.UPDATE, operateModule = LogModuleNameEnum.RESOURCE, operateDesc = "更新人员信息资料")
    public int update(ResourceItemPO resourceItemPO) {
        resourceItemPO.setUpdateTime(new Date());
        ResourceItemPO resourceItem = getResourceBasicInfoService(user).selectById(resourceItemPO.getFid());
        int result = getResourceBasicInfoService(user).update(resourceItemPO);
        writeOperateLog(new Object() {
                }.getClass(), resourceItem.getFname(), JSON.toJSONString(resourceItemPO), resourceItem,
                getResourceBasicInfoService(user).selectById(resourceItemPO.getFid()));
        return result;
    }

    public List<SearchConditionGroup> getItemClassForm(Map<String, Object> params) {
        return getResourceBasicInfoService(user).getItemClassForm(params);
    }

    @Log(operateType = OperateTypeEnum.ADD, operateModule = LogModuleNameEnum.RESOURCE, operateDesc = "新增人员信息分类")
    public int saveResourceItemClass(ResourceItemClassPO resourceItemClassPO) {
        int result = getResourceBasicInfoService(user).saveResourceItemClass(resourceItemClassPO);
        writeOperateLog(new Object() {
        }.getClass(), resourceItemClassPO.getFname(), JSON.toJSONString(resourceItemClassPO), "新增人员信息分类");
        return result;
    }

    @Log(operateType = OperateTypeEnum.UPDATE, operateModule = LogModuleNameEnum.RESOURCE, operateDesc = "更新人员信息分类")
    public int updateResourceItemClass(ResourceItemClassPO resourceItemClassPO) {
        ResourceItemClassPO resourceItemClass = getResourceBasicInfoMapper().getResourceItemClassByFid(resourceItemClassPO.getFid());
        int result = getResourceBasicInfoService(user).updateResourceItemClass(resourceItemClassPO);
        writeOperateLog(new Object() {
                }.getClass(), resourceItemClassPO.getFname(), JSON.toJSONString(resourceItemClassPO), resourceItemClass,
                getResourceBasicInfoMapper().getResourceItemClassByFid(resourceItemClassPO.getFid()));
        return result;
    }

    @Log(operateType = OperateTypeEnum.DELETE, operateModule = LogModuleNameEnum.RESOURCE, operateDesc = "删除人员信息分类")
    public int deleteResourceItemClass(Integer fid) {
        ResourceItemClassPO resourceItemClass = getResourceBasicInfoMapper().getResourceItemClassByFid(fid);
        int result = getResourceBasicInfoService(user).deleteResourceItemClass(fid);
        writeOperateLog(new Object() {
        }.getClass(), resourceItemClass.getFname(), JSON.toJSONString(fid), "删除人员信息分类");

        return result;
    }

    public List<SearchConditionGroup> getSearchCondition() {
        return getResourceBasicInfoService(user).getSearchCondition();
    }
}
