package com.unis.flowable.server.controller;

import com.unis.common.utils.ResponseWrapper;
import com.unis.flowable.common.vo.GroupVO;
import com.unis.flowable.common.vo.MembershipVO;
import com.unis.flowable.common.vo.SaveGroupVO;
import com.unis.flowable.common.vo.UserVO;
import com.unis.flowable.server.service.FlowGroupService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.flowable.idm.api.Group;
import org.flowable.idm.api.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

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

/**
 * @Auther:张新亮
 * @Date: 2019/7/24 0024 11:12
 * @Description:
 */
@RestController
@RequestMapping("/flowgroup")
@Api(value= "FlowGroupController", description = "用户角色")
public class FlowGroupController {

    @Autowired
    private FlowGroupService flowGroupService;


    /**
     * 添加用户角色
     * @param groupVO
     * @return
     */
    @SuppressWarnings({ "unchecked", "rawtypes" })
	@RequestMapping(value = "save", method = RequestMethod.POST)
    @ApiOperation(value="添加用户角色", notes = "添加用户角色")
    public ResponseWrapper save(
            @RequestBody List<SaveGroupVO> groupVO
    ) {

        SaveGroupVO saveGroupVO=new SaveGroupVO();
        GroupVO group=new GroupVO();
        group.setId("县粮食局安全发展处");
        group.setName("测试");
        saveGroupVO.setGroupVO(group);

        List<UserVO> userVOList=new ArrayList();
        UserVO userVO=new UserVO();
        userVO.setId("123456");
        userVO.setFirstName("张三");
        userVO.setLastName("530000");
        userVOList.add(userVO);
        userVO=new UserVO();
        userVO.setId("234567");
        userVO.setFirstName("李四");
        userVO.setLastName("530000");
        userVOList.add(userVO);
        saveGroupVO.setUserVOList(userVOList);
        groupVO=new ArrayList();
        groupVO.add(saveGroupVO);
        int result= flowGroupService.save(groupVO);
        if(result==0) {
            return ResponseWrapper.error(500, "添加用户角色---添加失败",null);
        } else {
            return ResponseWrapper.success(200, "添加用户角色---添加成功",null);
        }
    }



    /**
     * 根据名称查询角色
     * @param name
     * @return
     */
    @RequestMapping(value = "findByGroupId", method = RequestMethod.GET)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "角色名称", paramType = "query")
    })
    @ApiOperation(value="根据名称查询角色", notes = "根据名称查询角色")
    public ResponseWrapper findByGroupId(
           String name
    ) {
        int result= flowGroupService.findByGroupId(name);
        if(result==0) {
            return ResponseWrapper.error(504, "用户角色---不存在",null);
        } else {
            return ResponseWrapper.success(200, "添加用户角色---添加成功",null);
        }
    }



    /**
     * 根据名称查询用户
     * @param userId
     * @return
     */
    @RequestMapping(value = "findByUserName", method = RequestMethod.GET)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户ID", paramType = "query")
    })
    @ApiOperation(value="根据名称查询用户", notes = "根据名称查询用户")
    public ResponseWrapper findByUserId(
            String userId
    ) {
        String title="用户管理";
        int result= flowGroupService.findByUserId(userId);
        if(result==0) {
            return ResponseWrapper.error(504, title + "---不存在",null);
        } else {
            return ResponseWrapper.success(200, title+"---添加成功",null);
        }
    }



    /**
     * 根据用户ID查询所属组
     * @param userId
     * @return
     */
    @RequestMapping(value = "findByUserGroupList", method = RequestMethod.GET)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户ID", paramType = "query")
    })
    @ApiOperation(value="根据用户ID查询所属组", notes = "根据用户ID查询所属组")
    public ResponseWrapper findByUserGroupList(
            String userId
    ) {
        List<Group> groupList= flowGroupService.findByUserGroupList(userId);
        return ResponseWrapper.success(200, "查询成功", groupList);
    }



    @RequestMapping(value = "/group", method = RequestMethod.POST)
    @ApiOperation(value="添加角色", notes = "添加角色")
    public ResponseWrapper group(
            @RequestBody List<GroupVO> group) {
    	for(GroupVO group2:group) {
    		flowGroupService.addGroup(group2);
    	}
		return ResponseWrapper.success("处理成功");
    	
        
    }
    
    @RequestMapping(value = "/user", method = RequestMethod.POST)
    @ApiOperation(value="添加用户", notes = "添加用户")
    public ResponseWrapper user(
            @RequestBody List<UserVO> user) {
				
				
				for(UserVO group2:user) {
		    		flowGroupService.addUser(group2);
		    	}
				return ResponseWrapper.success("处理成功");
    	
        
    }
    
    
    @RequestMapping(value = "/member", method = RequestMethod.POST)
    @ApiOperation(value="添加用户角色", notes = "添加用户角色")
    public ResponseWrapper member(
            @RequestBody List<MembershipVO> membershipVO) {
				
				if(membershipVO.size()>0) {
					MembershipVO membershipVO2 = new MembershipVO();
					membershipVO2.setUserId(membershipVO.get(0).getUserId());
					flowGroupService.deleteMember(membershipVO2);
					for(MembershipVO group2:membershipVO) {
			    		flowGroupService.addMember(group2);
			    	}
				}
				
				return ResponseWrapper.success("处理成功");
				
        
    }
    
    @RequestMapping(value = "/deletemember", method = RequestMethod.POST)
    @ApiOperation(value="刪除用户角色", notes = "刪除用户角色，參數用戶id")
    public ResponseWrapper deletemember(
            @RequestBody MembershipVO membershipVO) {

		    		//flowGroupService.deleteMember(membershipVO);
				return ResponseWrapper.success("处理成功");
				
        
    }
}