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.DeleteParam;
import com.engine.organization.entity.job.param.JobCopyParam;
import com.engine.organization.entity.job.param.JobMergeParam;
import com.engine.organization.entity.job.param.JobSearchParam;
import com.engine.organization.entity.job.po.JobPO;
import com.engine.organization.entity.searchtree.SearchTreeParams;
import com.engine.organization.enums.LogModuleNameEnum;
import com.engine.organization.enums.OperateTypeEnum;
import com.engine.organization.mapper.job.JobMapper;
import com.engine.organization.service.JobService;
import com.engine.organization.service.impl.JobServiceImpl;
import com.engine.organization.thread.JobTriggerRunnable;
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 com.engine.organization.util.response.ReturnResult;
import weaver.hrm.User;

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

/**
 * @description:
 * @author:dxfeng
 * @createTime: 2022/05/27
 * @version: 1.0
 */
public class JobWrapper extends OrganizationWrapper {
    private JobService getJobService(User user) {
        return ServiceUtil.getService(JobServiceImpl.class, user);
    }

    private JobMapper getJobMapper() {
        return MapperProxyFactory.getProxy(JobMapper.class);
    }

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

    /**
     * 列表
     *
     * @param param
     * @return
     */
    public Map<String, Object> listPage(JobSearchParam param) {
        return getJobService(user).listPage(param);
    }

    /**
     * 顶部按钮
     *
     * @return
     */
    public Map<String, List<MenuBtn>> getHasRight() {
        return getJobService(user).getHasRight();
    }

    /**
     * 搜索条件
     *
     * @return
     */
    public Map<String, Object> getSearchCondition() {
        return getJobService(user).getSearchCondition();
    }

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

    /**
     * 获取详细表单
     *
     * @param params
     * @return
     */
    public Map<String, Object> getJobBaseForm(Map<String, Object> params) {
        return getJobService(user).getJobBaseForm(params);

    }

    /**
     * 保存基础信息
     *
     * @param params
     * @return
     */
    @Log(operateType = OperateTypeEnum.ADD, operateModule = LogModuleNameEnum.JOB, operateDesc = "新增岗位")
    public Long saveBaseForm(Map<String, Object> params) {
        Long jobId = getJobService(user).saveBaseForm(params);
        writeOperateLog(new Object() {
        }.getClass(), params.get("job_name").toString(), JSON.toJSONString(params), "新增岗位");
        // 更新组织架构图
        new Thread(new JobTriggerRunnable(jobId)).start();
        return jobId;
    }

    /**
     * 更新主表、拓展表、明细表
     *
     * @param params
     * @return
     */
    @Log(operateType = OperateTypeEnum.UPDATE, operateModule = LogModuleNameEnum.JOB, operateDesc = "更新岗位")
    public Long updateForm(Map<String, Object> params) {
        long id = Long.parseLong(params.get("id").toString());
        JobPO jobById = getJobMapper().getJobById(id);
        Long jobId = getJobService(user).updateForm(params);
        JobPO newJobById = getJobMapper().getJobById(id);
        writeOperateLog(new Object() {
        }.getClass(), jobById.getJobName(), JSON.toJSONString(params), jobById, newJobById);
        // 更新组织架构图
        new Thread(new JobTriggerRunnable(jobById, newJobById)).start();
        return jobId;
    }

    /**
     * 复制岗位到指定部门
     *
     * @param param
     * @return
     */
    @Log(operateType = OperateTypeEnum.COPY, operateModule = LogModuleNameEnum.JOB, operateDesc = "复制岗位")
    public int copyJobItem(JobCopyParam param) {
        List<JobPO> jobPOS = getJobMapper().getJobsByIds(DeleteParam.builder().ids(param.getIds()).build().getIds());
        int copyJobItem = getJobService(user).copyJobItem(param.getIds(), param.getDepartment());
        for (JobPO jobPO : jobPOS) {
            writeOperateLog(new Object() {
            }.getClass(), jobPO.getJobName(), JSON.toJSONString(param), "复制岗位[" + jobPO.getJobName() + "]");
        }
        return copyJobItem;
    }

    /**
     * 更新禁用标识
     *
     * @param params
     * @return
     */
    @Log(operateType = OperateTypeEnum.UPDATE, operateModule = LogModuleNameEnum.JOB, operateDesc = "更新岗位禁用标识")
    public int updateForbiddenTagById(JobSearchParam params) {
        JobPO jobById = getJobMapper().getJobById(params.getId());
        int updateForbiddenTagById = getJobService(user).updateForbiddenTagById(params);
        JobPO newJobById = getJobMapper().getJobById(params.getId());
        writeOperateLog(new Object() {
        }.getClass(), jobById.getJobNo(), JSON.toJSONString(params), jobById, newJobById);
        // 更新组织架构图
        new Thread(new JobTriggerRunnable(jobById, newJobById)).start();
        return updateForbiddenTagById;
    }

    /**
     * 根据ID批量删除
     *
     * @param ids
     * @return
     */
    @Log(operateType = OperateTypeEnum.DELETE, operateModule = LogModuleNameEnum.JOB, operateDesc = "删除岗位")
    public int deleteByIds(Collection<Long> ids) {
        List<JobPO> jobPOS = getJobMapper().getJobsByIds(ids);
        int deleteByIds = getJobService(user).deleteByIds(ids);
        for (JobPO jobPO : jobPOS) {
            writeOperateLog(new Object() {
            }.getClass(), jobPO.getJobName(), JSON.toJSONString(ids), "删除岗位");
            // 更新组织架构图
            new Thread(new JobTriggerRunnable(jobPO)).start();
        }
        return deleteByIds;
    }


    /**
     * 获取复制表单
     *
     * @return
     */
    public ReturnResult getCopyForm() {
        List<SearchConditionGroup> addGroups = new ArrayList<>();
        List<SearchConditionItem> condition = new ArrayList<>();
        SearchConditionItem deptBrowserItem = OrganizationFormItemUtil.browserItem(user, 2, 16, 3, false, "复制到", "4", "department", "deptBrowser");
        deptBrowserItem.setRules("required|string");
        condition.add(deptBrowserItem);
        addGroups.add(new SearchConditionGroup("", true, condition));
        return ReturnResult.successed(addGroups);
    }

    /**
     * 根据岗位获取人员
     *
     * @param jobId
     * @return
     */
    public ReturnResult getHrmListByJobId(Long jobId) {
        Map<String, Object> hrmListByJobId = getJobService(user).getHrmListByJobId(jobId);
        return ReturnResult.successed(hrmListByJobId);
    }

    public List<SearchConditionGroup> getMergeForm(Long id) {
        return getJobService(user).getMergeForm(id);
    }

    @Log(operateType = OperateTypeEnum.MERGE, operateModule = LogModuleNameEnum.JOB, operateDesc = "合并岗位")
    public int mergeJob(JobMergeParam mergeParam) {
        JobPO jobById = getJobMapper().getJobById(mergeParam.getId());
        int mergeJob = getJobService(user).mergeJob(mergeParam);
        writeOperateLog(new Object() {
        }.getClass(), jobById.getJobNo(), JSON.toJSONString(mergeParam), jobById, getJobMapper().getJobById(mergeParam.getId()));

        return mergeJob;
    }
}
