package com.xmomen.module.system.controller;

import com.xmomen.framework.web.exceptions.ArgumentValidException;
import com.xmomen.module.account.web.controller.vo.CreateUserGroup;
import com.xmomen.module.account.web.controller.vo.UpdateUserGroup;
import com.xmomen.module.logger.Log;
import com.xmomen.module.system.entity.SysOrganization;
import com.xmomen.module.system.model.CreateOrganization;
import com.xmomen.module.system.model.OrganizationModel;
import com.xmomen.module.system.service.OrganizationService;
import com.xmomen.module.user.entity.SysRoles;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * Created by Jeng on 16/3/25.
 */
@RestController
public class OrganizationController {

    // 注入OrganizationService
    @Autowired
    OrganizationService organizationService;

    /**
     * 查询组织机构信息
     * @param id
     * @return
     */
    // 使用@RequestMapping注解，指定请求路径为/organization，请求方法为GET
    @RequestMapping(value = "/organization", method = RequestMethod.GET)
    // 使用@Log注解，记录操作日志，操作名称为查询组织机构信息
    @Log(actionName = "查询组织机构信息")
    // 使用@RequestParam注解，指定请求参数名为id，非必需
    public List<OrganizationModel> getOrganizationTree(@RequestParam(value = "id", required = false) Integer id){
        // 调用organizationService的getOrganizationTree方法，传入id参数，返回组织机构信息列表
        return organizationService.getOrganizationTree(id);
    }

    /**
     * 新增机构组织
     * @param createOrganization
     * @param bindingResult
     * @throws ArgumentValidException
     */
    // 定义一个POST请求，请求路径为/organization
    @RequestMapping(value = "/organization", method = RequestMethod.POST)
    // 记录日志，操作名称为新增组织机构
    @Log(actionName = "新增组织机构")
    // 接收一个CreateOrganization对象，并进行验证
    public void createOrganization(@RequestBody @Valid CreateOrganization createOrganization, BindingResult bindingResult) throws ArgumentValidException {
        // 如果验证结果不为空且存在错误，则抛出ArgumentValidException异常
        if(bindingResult != null && bindingResult.hasErrors()){
            throw new ArgumentValidException(bindingResult);
        }
        // 创建一个SysOrganization对象
        SysOrganization sysOrganization = new SysOrganization();
        // 设置SysOrganization对象的属性
        sysOrganization.setName(createOrganization.getName());
        sysOrganization.setDescription(createOrganization.getDescription());
        sysOrganization.setParentId(createOrganization.getParentId());
        // 调用organizationService的createOrganization方法，创建组织机构
        organizationService.createOrganization(sysOrganization);
    }

    /**
     *  删除机构组织
     * @param id
     */
    @RequestMapping(value = "/organization/{id}", method = RequestMethod.DELETE)
    @Log(actionName = "删除组织机构")
    public void deleteOrganization(@PathVariable(value = "id") Integer id){
        organizationService.delete(id);
    }

    /**
     *  修改机构组织
     * @param id
     */
    @RequestMapping(value = "/organization/{id}", method = RequestMethod.PUT)
    // 记录日志，操作名称为“修改组织机构”
    @Log(actionName = "修改组织机构")
    public void updateOrganization(@PathVariable(value = "id") Integer id,
                                // 请求体，验证CreateOrganization对象
                                @RequestBody @Valid CreateOrganization createOrganization, BindingResult bindingResult) throws ArgumentValidException {
        // 如果验证结果不为空且存在错误，抛出ArgumentValidException异常
        if(bindingResult != null && bindingResult.hasErrors()){
            throw new ArgumentValidException(bindingResult);
        }
        // 创建SysOrganization对象
        SysOrganization sysOrganization = new SysOrganization();
        // 设置id
        sysOrganization.setId(id);
        // 设置名称
        sysOrganization.setName(createOrganization.getName());
        // 设置描述
        sysOrganization.setDescription(createOrganization.getDescription());
        // 设置父id
        sysOrganization.setParentId(createOrganization.getParentId());
        // 调用organizationService的updateOrganization方法更新组织机构
        organizationService.updateOrganization(sysOrganization);
    }

    /**
     * 新增机构组织用户
     * @param id
     * @param userIds
     */
    @RequestMapping(value = "/organization/{id}/user", method = RequestMethod.POST)
    // 添加日志记录，操作名称为“新增组织机构用户”
    @Log(actionName = "新增组织机构用户")
    public void bindOrganizationUser(@PathVariable(value = "id") Integer id,
                                   @RequestParam(value = "userIds") Integer[] userIds) {

        // 调用organizationService的bindOrganizationUser方法，传入id和userIds
        organizationService.bindOrganizationUser(id, userIds);
    }

    /**
     * 删除机构组织用户
     * @param id
     * @param userIds
     */
    @RequestMapping(value = "/organization/{id}/user", method = RequestMethod.DELETE)
    @Log(actionName = "删除组织机构用户")
    public void updateOrganization(@PathVariable(value = "id") Integer id,
                                   @RequestParam(value = "userIds") Integer[] userIds) {
        organizationService.unBindOrganizationUser(id, userIds);
    }
}
