package com.zcxk.process.service.impl;

import com.alibaba.fastjson.JSON;

import com.google.common.collect.Sets;
import com.zcxk.client.UserCenterClient;
import com.zcxk.process.dao.UserActionDescMapper;
import com.zcxk.process.dao.UserFormMapper;
import com.zcxk.process.dto.TaskUserDTO;
import com.zcxk.process.entity.UserActionDesc;
import com.zcxk.process.entity.UserForm;
import com.zcxk.process.service.UserFormService;
import com.zcxk.process.service.WorkFlowService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * 用户表单服务实现类
 *
 * @author wilian.peng
 * @Description
 * @date 2019年3月27日 上午10:19:34
 */
@Slf4j
@Service
public class UserFormServiceImpl implements UserFormService {
    @Autowired
    UserFormMapper userFormMapper;

    @Autowired
    UserActionDescMapper userActionDescMapper;

    @Autowired
    WorkFlowService workFlowService;
    @Autowired
    UserCenterClient userCenterClient;

    @Override
    public List<UserForm> getUserFormList() {
        log.info("begin UserFormService getUserFormList !");
        List<UserForm> list = userFormMapper.list(new UserForm());
        log.info("end UserFormService getUserFormList ,list.size = " + list.size());
        return list;
    }

    @Override
    public UserForm getUserFormDetail(String formKey, Integer channelId) {
        log.info("begin UserFormService getUserFormDetail ,formKey = " + formKey);
        UserForm result = null;
        UserForm param = new UserForm();
        param.setFormKey(formKey);
        List<UserForm> list = userFormMapper.list(param);
        if (list.size() > 0) {
            result = list.get(0);
            List<UserActionDesc> actionList = userActionDescMapper.queryUserActionWithForm(result.getId(), channelId);
            result.setUserActionList(actionList);
        }
        log.info("end UserFormService getUserFormDetail ,result = " + JSON.toJSONString(result));
        return result;
    }

    @Override
    public UserForm getTaskUserForm(String taskId) {
        UserForm form = null;
        String taskFormkey = workFlowService.getTaskFormkey(taskId);
        if (taskFormkey != null) {
            UserForm queryObj = new UserForm();
            queryObj.setFormKey(taskFormkey);
            form = userFormMapper.singleQuery(queryObj);
        }
        return form;
    }

    @Override
    public Set<Integer> getTaskUsers(TaskUserDTO taskUserDTO) {
        List<Integer>userIds=new ArrayList<>();
        List<Integer> userIdsParam = taskUserDTO.getUserIds();
        if(userIdsParam!=null&&userIdsParam.size()>0){
            userIdsParam.forEach(id->{
                userIds.add(id);
            });
        }
        List<Integer> orgIds = taskUserDTO.getOrgIds();
        if(orgIds!=null&&orgIds.size()>0){
            List<Integer> orgUserIds = userCenterClient.getUserIdsByOrgId(JSON.toJSONString(orgIds));
            userIds.addAll(orgUserIds);
        }

        return Sets.newHashSet(userIds);
    }

}
