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.DeleteParam;
import com.engine.organization.entity.department.param.*;
import com.engine.organization.entity.department.po.DepartmentPO;
import com.engine.organization.entity.department.vo.SingleDeptTreeVO;
import com.engine.organization.entity.job.vo.SingleJobTreeVO;
import com.engine.organization.entity.searchtree.SearchTreeParams;
import com.engine.organization.enums.LogModuleNameEnum;
import com.engine.organization.enums.OperateTypeEnum;
import com.engine.organization.mapper.department.DepartmentMapper;
import com.engine.organization.service.DepartmentService;
import com.engine.organization.service.impl.DepartmentServiceImpl;
import com.engine.organization.thread.DepartmentTriggerRunnable;
import com.engine.organization.util.MenuBtn;
import com.engine.organization.util.OrganizationWrapper;
import com.engine.organization.util.db.MapperProxyFactory;
import com.engine.organization.util.page.PageInfo;
import com.engine.organization.util.response.ReturnResult;
import weaver.hrm.User;

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

/**
 * @Author weaver_cl
 * @description:
 * @Date 2022/5/20
 * @Version V1.0
 **/
public class DepartmentWrapper extends OrganizationWrapper {

    public DepartmentService getDepartmentService(User user) {
        return ServiceUtil.getService(DepartmentServiceImpl.class, user);
    }

    private DepartmentMapper getDepartmentMapper() {
        return MapperProxyFactory.getProxy(DepartmentMapper.class);
    }

    public ReturnResult getDeptListByPid(QuerySingleDeptListParam param) {
        PageInfo<SingleDeptTreeVO> singleDeptTreeVOS = getDepartmentService(user).getDeptListByPid(param);
        return ReturnResult.successed(singleDeptTreeVOS);
    }

    /**
     * 部门联查岗位
     *
     * @param param
     * @return
     */
    public PageInfo<SingleJobTreeVO> getJobListByPid(QuerySingleDeptListParam param) {
        return getDepartmentService(user).getJobListByPid(param);
    }

    /**
     * 左侧树
     *
     * @param params
     * @return
     */
    public Map<String, Object> getSearchTree(SearchTreeParams params) {
        return getDepartmentService(user).getSearchTree(params);
    }

    /**
     * 列表数据展示
     *
     * @param param
     * @return
     */
    public Map<String, Object> listPage(DeptSearchParam param) {
        return getDepartmentService(user).listPage(param);
    }

    /**
     * 获取保存表单
     *
     * @return
     */
    public Map<String, Object> getSaveForm(DeptSearchParam param) {
        return getDepartmentService(user).getSaveForm(param);
    }

    /**
     * 保存部门基础信息
     *
     * @param params
     * @return
     */
    @Log(operateType = OperateTypeEnum.ADD, operateDesc = "新增部门", operateModule = LogModuleNameEnum.DEPARTMENT)
    public Long saveBaseForm(Map<String, Object> params) {
        Long departmentId = getDepartmentService(user).saveBaseForm(params);
        writeOperateLog(new Object() {
        }.getClass(), params.get("dept_name").toString(), JSON.toJSONString(params), "新增部门");
        new Thread(new DepartmentTriggerRunnable(departmentId)).start();
        return departmentId;
    }


    /**
     * 更新禁用标记
     *
     * @param params
     */
    @Log(operateType = OperateTypeEnum.UPDATE, operateDesc = "更新部门禁用标记", operateModule = LogModuleNameEnum.DEPARTMENT)
    public int updateForbiddenTagById(DeptSearchParam params) {
        DepartmentPO deptById = getDepartmentMapper().getDeptById(params.getId());
        int updateForbiddenTagById = getDepartmentService(user).updateForbiddenTagById(params);
        DepartmentPO newDeptById = getDepartmentMapper().getDeptById(params.getId());
        writeOperateLog(new Object() {
        }.getClass(), deptById.getDeptName(), JSON.toJSONString(params), deptById, newDeptById);
        new Thread(new DepartmentTriggerRunnable(deptById, newDeptById)).start();
        return updateForbiddenTagById;
    }

    /**
     * 更新分部主表、拓展表、明细表
     *
     * @param params
     * @return
     */
    @Log(operateType = OperateTypeEnum.UPDATE, operateDesc = "更新部门", operateModule = LogModuleNameEnum.DEPARTMENT)
    public Long updateForm(Map<String, Object> params) {
        long id = Long.parseLong(params.get("id").toString());
        DepartmentPO deptById = getDepartmentMapper().getDeptById(id);
        Long departmentId = getDepartmentService(user).updateForm(params);
        DepartmentPO newDeptById = getDepartmentMapper().getDeptById(id);
        writeOperateLog(new Object() {
        }.getClass(), deptById.getDeptName(), JSON.toJSONString(params), deptById, newDeptById);
        new Thread(new DepartmentTriggerRunnable(deptById, newDeptById)).start();
        return departmentId;
    }

    /**
     * 根据ID批量删除
     *
     * @param ids
     */
    @Log(operateType = OperateTypeEnum.DELETE, operateDesc = "删除部门", operateModule = LogModuleNameEnum.DEPARTMENT)
    public int deleteByIds(Collection<Long> ids) {
        List<DepartmentPO> departmentPOS = getDepartmentMapper().getDeptsByIds(ids);
        int deleteByIds = getDepartmentService(user).deleteByIds(ids);
        for (DepartmentPO departmentPO : departmentPOS) {
            writeOperateLog(new Object() {
            }.getClass(), departmentPO.getDeptName(), JSON.toJSONString(ids), "删除部门");
            // 更新组织架构图
            new Thread(new DepartmentTriggerRunnable(departmentPO)).start();
        }
        return deleteByIds;
    }

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

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

    /**
     * 获取基本信息表单
     *
     * @param params
     * @return
     */
    public Map<String, Object> getDeptBaseForm(Map<String, Object> params) {
        return getDepartmentService(user).getDeptBaseForm(params);
    }

    /**
     * 复制表单
     *
     * @return
     */
    public List<SearchConditionGroup> getCopyForm() {
        return getDepartmentService(user).getCopyForm();
    }

    /**
     * 复制岗位到指定部门
     *
     * @param copyParam
     * @return
     */
    @Log(operateType = OperateTypeEnum.COPY, operateDesc = "复制部门", operateModule = LogModuleNameEnum.DEPARTMENT)
    public int copyDepartment(DeptCopyParam copyParam) {
        List<DepartmentPO> departmentPOS = getDepartmentMapper().getDeptsByIds(DeleteParam.builder().ids(copyParam.getIds()).build().getIds());
        int copyDepartment = getDepartmentService(user).copyDepartment(copyParam);
        for (DepartmentPO departmentPO : departmentPOS) {
            writeOperateLog(new Object() {
            }.getClass(), departmentPO.getDeptName(), JSON.toJSONString(copyParam), "复制部门[" + departmentPO.getDeptName() + "]");
        }
        return copyDepartment;
    }

    /**
     * 获取合并表单
     *
     * @param id
     * @return
     */
    public List<SearchConditionGroup> getMergeForm(Long id) {
        return getDepartmentService(user).getMergeForm(id);
    }

    /**
     * 合并到指定部门
     *
     * @param mergeParam
     * @return
     */
    @Log(operateType = OperateTypeEnum.MERGE, operateDesc = "合并部门", operateModule = LogModuleNameEnum.DEPARTMENT)
    public int mergeDepartment(DepartmentMergeParam mergeParam) {
        DepartmentPO departmentPO = getDepartmentMapper().getDeptById(mergeParam.getId());
        int mergeDepartment = getDepartmentService(user).mergeDepartment(mergeParam);
        writeOperateLog(new Object() {
        }.getClass(), departmentPO.getDeptName(), JSON.toJSONString(mergeParam), departmentPO, getDepartmentMapper().getDeptById(departmentPO.getId()));
        return mergeDepartment;
    }

    /**
     * 获取转移表单
     *
     * @return
     */
    public List<SearchConditionGroup> getMoveForm() {
        return getDepartmentService(user).getMoveForm();
    }

    /**
     * 转移到指定分部或部门
     *
     * @param moveParam
     * @return
     */
    @Log(operateType = OperateTypeEnum.MOVE, operateDesc = "转移部门", operateModule = LogModuleNameEnum.DEPARTMENT)
    public int moveDepartment(DepartmentMoveParam moveParam) {
        DepartmentPO departmentPO = getDepartmentMapper().getDeptById(moveParam.getId());
        int moveDepartment = getDepartmentService(user).moveDepartment(moveParam);
        writeOperateLog(new Object() {
        }.getClass(), departmentPO.getDeptName(), JSON.toJSONString(moveParam), departmentPO, getDepartmentMapper().getDeptById(departmentPO.getId()));
        return moveDepartment;
    }
}
