package com.yunzhupaas.util;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.yunzhupaas.base.ActionResult;
import com.yunzhupaas.base.Pagination;
import com.yunzhupaas.base.entity.DataInterfaceEntity;
import com.yunzhupaas.base.entity.DictionaryDataEntity;
import com.yunzhupaas.base.entity.VisualdevEntity;
import com.yunzhupaas.base.model.flow.FlowFormDataModel;
import com.yunzhupaas.base.service.BillRuleService;
import com.yunzhupaas.base.service.DataInterfaceService;
import com.yunzhupaas.base.service.DictionaryDataService;
import com.yunzhupaas.base.util.SentMessageUtil;
import com.yunzhupaas.base.vo.DownloadVO;
import com.yunzhupaas.config.ConfigValueUtil;
import com.yunzhupaas.emnus.ModuleTypeEnum;
import com.yunzhupaas.engine.model.flowtemplate.FlowExportModel;
import com.yunzhupaas.exception.WorkFlowException;
import com.yunzhupaas.message.model.SentMessageForm;
import com.yunzhupaas.message.service.SendMessageConfigService;
import com.yunzhupaas.permission.entity.*;
import com.yunzhupaas.permission.model.organizeadministrator.OrganizeAdministratorModel;
import com.yunzhupaas.permission.service.*;
import com.yunzhupaas.util.enums.DictionaryDataEnum;
import com.yunzhupaas.visual.service.VisualdevApi;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.*;
import java.util.stream.Collectors;

import static com.yunzhupaas.util.Constants.ADMIN_KEY;

/**
 * @author ：云筑产品开发平台组
 * @version: V3.1.0
 * @copyright 深圳市乐程软件有限公司
 * @date ：2022/4/9 13:28
 */
@Component
@DS("")
public class ServiceAllUtil {


    @Autowired
    private DictionaryDataService dictionaryDataService;
    @Autowired
    private UserRelationService userRelationService;
    @Autowired
    private UserService userService;
    @Autowired
    private RoleService roleService;
    @Autowired
    private OrganizeService organizeService;
    @Autowired
    private OrganizeAdministratorService organizeAdministratorService;
    @Autowired
    private PositionService positionService;
    @Autowired
    private BillRuleService billRuleService;
    @Autowired
    private DataInterfaceService dataInterfaceService;
    @Autowired
    private SentMessageUtil sentMessageUtil;
    @Autowired
    private SendMessageConfigService sendMessageConfigService;
    @Autowired
    private DataFileExport fileExport;
    @Autowired
    private ConfigValueUtil configValueUtil;

    //--------------------------------数据字典------------------------------
    public List<DictionaryDataEntity> getDiList() {
        List<DictionaryDataEntity> dictionList = dictionaryDataService.getListByTypeDataCode(DictionaryDataEnum.FLOWWOEK_ENGINE.getDictionaryTypeId());
        return dictionList;
    }

    public List<DictionaryDataEntity> getDictionName(List<String> id) {
        List<DictionaryDataEntity> dictionList = dictionaryDataService.getDictionName(id);
        return dictionList;
    }

    //--------------------------------用户关系表------------------------------
    public List<UserRelationEntity> getListByUserIdAll(List<String> id) {
        List<UserRelationEntity> list = userRelationService.getListByUserIdAll(id).stream().filter(t -> StringUtil.isNotEmpty(t.getObjectId())).collect(Collectors.toList());
        return list;
    }

    public List<UserRelationEntity> getListByObjectIdAll(List<String> id) {
        List<UserRelationEntity> list = userRelationService.getListByObjectIdAll(id);
        return list;
    }

    public String getAdmin() {
        UserEntity admin = userService.getUserByAccount(ADMIN_KEY);
        return admin.getId();
    }

    //--------------------------------用户------------------------------
    public List<UserEntity> getUserName(List<String> id) {
        List<UserEntity> list = getUserName(id, false);
        return list;
    }

    public List<UserEntity> getListByManagerId(String managerId) {
        List<UserEntity> list = StringUtil.isNotEmpty(managerId) ? userService.getListByManagerId(managerId, null) : new ArrayList<>();
        return list;
    }

    public List<UserEntity> getUserName(List<String> id, boolean enableMark) {
        List<UserEntity> list = userService.getUserName(id);
        if (enableMark) list = list.stream().filter(t -> t.getEnabledMark() != 0).collect(Collectors.toList());
        return list;
    }

    public List<UserEntity> getUserName(List<String> id, Pagination pagination) {
        List<UserEntity> list = userService.getUserName(id, pagination);
        return list;
    }

    public UserEntity getUserInfo(String id) {
        UserEntity entity = null;
        if (StringUtil.isNotEmpty(id)) {
            entity = id.equalsIgnoreCase(ADMIN_KEY) ? userService.getUserByAccount(id) : userService.getInfo(id);
        }
        return entity;
    }

    public List<String> getUserListAll(List<String> idList) {
        List<String> userIdList = userService.getUserIdList(idList, null);
        return userIdList;
    }

    public List<String> getOrganizeUserList(String type) {
        OrganizeAdministratorModel model = organizeAdministratorService.getOrganizeAdministratorList();
        Map<String, List<String>> map = new HashMap<>();
        map.put("select", model.getSelectList());
        map.put("add", model.getAddList());
        map.put("delete", model.getDeleteList());
        map.put("edit", model.getEditList());
        List<String> list = map.get(type) != null ? map.get(type) : new ArrayList<>();
        List<String> userList = userRelationService.getListByObjectIdAll(list).stream().map(UserRelationEntity::getUserId).collect(Collectors.toList());
        return userList;
    }

    //--------------------------------单据规则------------------------------

    public void useBillNumber(String enCode) {
        billRuleService.useBillNumber(enCode);
    }

    //--------------------------------角色------------------------------
    public List<RoleEntity> getListByIds(List<String> id) {
        List<RoleEntity> list = roleService.getListByIds(id, null, false);
        return list;
    }

    //--------------------------------组织------------------------------
    public List<OrganizeEntity> getOrganizeName(List<String> id) {
        List<OrganizeEntity> list = organizeService.getOrganizeName(id);
        return list;
    }

    public OrganizeEntity getOrganizeInfo(String id) {
        OrganizeEntity entity = StringUtil.isNotEmpty(id) ? organizeService.getInfo(id) : null;
        return entity;
    }

    public List<OrganizeEntity> getOrganizeId(String organizeId) {
        List<OrganizeEntity> organizeList = new ArrayList<>();
        organizeService.getOrganizeId(organizeId, organizeList);
        Collections.reverse(organizeList);
        return organizeList;
    }

    public List<OrganizeEntity> getDepartmentAll(String organizeId) {
        List<OrganizeEntity> departmentAll = organizeService.getDepartmentAll(organizeId);
        return departmentAll;
    }

    //--------------------------------岗位------------------------------
    public List<PositionEntity> getPositionName(List<String> id) {
        List<PositionEntity> list = positionService.getPositionName(id, false);
        return list;
    }

    //--------------------------------远端------------------------------
    public ActionResult infoToId(String interId, Map<String, String> parameterMap) {
        return dataInterfaceService.infoToId(interId, null, parameterMap);
    }

    public List<DataInterfaceEntity> getInterfaceList(List<String> id) {
        return dataInterfaceService.getList(id);
    }

    //--------------------------------发送消息------------------------------
    public void sendMessage(List<SentMessageForm> messageListAll) {
        for (SentMessageForm messageForm : messageListAll)
            if (messageForm.isSysMessage()) sentMessageUtil.sendMessage(messageForm);
    }

    public void updateSendConfigUsed(String id, List<String> idList) {
        sendMessageConfigService.updateUsed(id, idList);
    }

    public void sendDelegateMsg(List<SentMessageForm> messageListAll) {
        for (SentMessageForm messageForm : messageListAll) {
            sentMessageUtil.sendDelegateMsg(messageForm);
        }
    }

    //------------------------------导出-------------------------------
    public DownloadVO exportData(FlowExportModel model) {
        DownloadVO downloadVO = fileExport.exportFile(model, configValueUtil.getTemporaryFilePath(), model.getFlowTemplate().getFullName(), ModuleTypeEnum.FLOW_FLOWENGINE.getTableName());
        return downloadVO;
    }

    //------------------------------表单数据-------------------------------
    public void createOrUpdate(FlowFormDataModel flowFormDataModel) throws WorkFlowException {
        //todo 流程调用表单接口
//        formDataService.saveOrUpdate(flowFormDataModel);
    }

    public Map<String, Object> infoData(String formId, String id) throws WorkFlowException {
        //todo 流程调用表单接口
        Map<String, Object> dataAll = new HashMap<>();
//        if (StringUtil.isNotEmpty(formId) && StringUtil.isNotEmpty(id)) {
//            Map<String, Object> info = new HashMap<>();
//            ActionResult result = formDataService.info(formId, id);
//            if (result.getCode() != 200) {
//                throw new WorkFlowException(result.getMsg());
//            }
//            if (result.getData() instanceof Map) {
//                info.putAll((Map) result.getData());
//            }
//            dataAll.putAll(info);
//        }
        return dataAll;
    }

    //------------------------------表单对象-------------------------------
    public VisualdevEntity getForm(String id) {
        //todo 流程调用表单接口
//        FlowFormEntity form = StringUtil.isNotEmpty(id) ? flowFormService.getById(id) : null;
//        return form;
        return null;
    }

    public List<VisualdevEntity> getFlowIdList(String id) {
//        List<FlowFormEntity> list = StringUtil.isNotEmpty(id) ? flowFormService.getFlowIdList(id) : new ArrayList<>();
//        return list;
        return null;
    }

    public void updateForm(VisualdevEntity entity) {
//        flowFormService.updateForm(entity);
    }

    public void formIdList(List<String> formId, String tempJsonId) {
//        flowFormRelationService.saveFlowIdByFormIds(tempJsonId, formId);
    }

    public void deleteFormId(String tempJsonId) {
//        flowFormRelationService.saveFlowIdByFormIds(tempJsonId, new ArrayList<>());
    }

}

