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.company.param.CompParam;
import com.engine.organization.entity.company.po.CompPO;
import com.engine.organization.entity.department.param.DepartmentMoveParam;
import com.engine.organization.enums.LogModuleNameEnum;
import com.engine.organization.enums.OperateTypeEnum;
import com.engine.organization.mapper.comp.CompMapper;
import com.engine.organization.service.CompService;
import com.engine.organization.service.impl.CompServiceImpl;
import com.engine.organization.util.MenuBtn;
import com.engine.organization.util.OrganizationWrapper;
import com.engine.organization.util.db.MapperProxyFactory;
import org.apache.commons.lang.StringUtils;
import weaver.hrm.User;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description:
 * @author:dxfeng
 * @createTime: 2022/05/16
 * @version: 1.0
 */
public class CompWrapper extends OrganizationWrapper {
    private CompService getCompService(User user) {
        return ServiceUtil.getService(CompServiceImpl.class, user);
    }

    private CompMapper getCompMapper() {
        return MapperProxyFactory.getProxy(CompMapper.class);
    }

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


    /**
     * 保存公司/分部基础信息
     *
     * @param params
     * @return
     */
    @Log(operateType = OperateTypeEnum.ADD, operateDesc = "新增分部", operateModule = LogModuleNameEnum.COMPANY)
    public Long saveBaseComp(Map<String, Object> params) {
        Long companyId = getCompService(user).saveBaseComp(params);
        writeOperateLog(new Object() {
        }.getClass(), params.get("subcompanyname").toString(), JSON.toJSONString(params), "新增分部");
        //TODO new Thread(new CompanyTriggerRunnable(companyId)).start();
        return companyId;
    }

    /**
     * 更新禁用标记
     *
     * @param params
     */
    @Log(operateType = OperateTypeEnum.UPDATE, operateDesc = "更新分部禁用标识", operateModule = LogModuleNameEnum.COMPANY)
    public int updateForbiddenTagById(CompParam params) {
        CompPO compPO = getCompMapper().listById(params.getId());
        int updateForbiddenTagById = getCompService(user).updateForbiddenTagById(params);
        CompPO newCompPO = getCompMapper().listById(params.getId());
        writeOperateLog(new Object() {
        }.getClass(), compPO.getSubCompanyName(), JSON.toJSONString(params), compPO, newCompPO);
        //TODO new Thread(new CompanyTriggerRunnable(compPO, newCompPO)).start();
        return updateForbiddenTagById;
    }

    /**
     * 更新分部主表、拓展表、明细表
     *
     * @param params
     * @return
     */
    @Log(operateType = OperateTypeEnum.UPDATE, operateDesc = "更新分部信息", operateModule = LogModuleNameEnum.COMPANY)
    public Long updateComp(Map<String, Object> params) {
        Integer id = Integer.parseInt(params.get("id").toString());
        CompPO compPO = getCompMapper().listById(id);
        Long companyId = getCompService(user).updateComp(params);
        CompPO newCompPO = getCompMapper().listById(id);
        writeOperateLog(new Object() {
        }.getClass(), compPO.getSubCompanyName(), JSON.toJSONString(params), compPO, newCompPO);
        //TODO new Thread(new CompanyTriggerRunnable(compPO, newCompPO)).start();
        return companyId;
    }

    /**
     * 根据ID批量删除
     *
     * @param ids
     */
    @Log(operateType = OperateTypeEnum.DELETE, operateDesc = "删除分部信息", operateModule = LogModuleNameEnum.COMPANY)
    public Map<String, Object> deleteByIds(List<Long> ids) {
        Map<String, Object> params = new HashMap<>();
        params.put("id", StringUtils.join(ids, ","));
        List<CompPO> compsByIds = getCompMapper().getCompsByIds(ids);
        Map<String, Object> map = getCompService(user).deleteByIds(params);
        for (CompPO compsById : compsByIds) {
            writeOperateLog(new Object() {
            }.getClass(), compsById.getSubCompanyName(), JSON.toJSONString(ids), "删除分部信息");
            //TODO new CompanyTriggerRunnable(compsById).run();
        }
        return map;
    }

    /**
     * 转移分部
     *
     * @param moveParam
     * @return
     */
    @Log(operateType = OperateTypeEnum.MOVE, operateDesc = "转移分部", operateModule = LogModuleNameEnum.COMPANY)
    public int moveCompany(DepartmentMoveParam moveParam) {
        CompPO compPO = getCompMapper().listById(moveParam.getId().intValue());
        int moveCompany = getCompService(user).moveCompany(moveParam);
        CompPO newCompPO = getCompMapper().listById(moveParam.getId().intValue());
        writeOperateLog(new Object() {
        }.getClass(), compPO.getSubCompanyName(), JSON.toJSONString(moveParam), compPO, newCompPO);
        //TODO new Thread(new CompanyTriggerRunnable(compPO, newCompPO)).start();
        return moveCompany;
    }

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

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

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

    /**
     * 新增表单
     *
     * @return
     */
    public Map<String, Object> getCompSaveForm(Map<String, Object> params) {
        return getCompService(user).getCompSaveForm(params);
    }

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

}
