package com.hs.admin.controller;

import com.hs.admin.common.Result;
import com.hs.admin.common.annotations.CurrentUser;
import com.hs.admin.common.annotations.Token;
import com.hs.admin.model.DicOrg;
import com.hs.admin.model.reqmodel.OrgReq;
import com.hs.admin.model.respmodel.DicOrgList;
import com.hs.admin.model.respmodel.SysUserOrgRsList;
import com.hs.admin.service.DicOrgService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

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

@Api(tags = "机构API", produces = "produces", consumes = "consumes", protocols = "protocols")
@RequestMapping("/org")
@RestController
public class OrgController {

    @Autowired
    private DicOrgService dicOrgService;


    @PostMapping("parentall")
    @Token
    @ApiOperation("查询机构列表")
    public Object getParentAll(@RequestBody OrgReq.GetOrgReq org) throws Exception {
        List<DicOrgList> list = dicOrgService.getParentAll(org);
        return Result.success(list);
    }

    @PostMapping("all")
    @Token
    @ApiOperation("查询机构列表")
    public Object getAll() {
        List<DicOrg> list = dicOrgService.getAll();
        return Result.success(list);
    }


    @PostMapping("info")
    @Token
    @ApiOperation("查询机构详细信息")
    public Object getById(@RequestBody OrgReq.InfoOrgReq org) throws Exception {
        DicOrg info = dicOrgService.info(org.getOrgId());
        return Result.success(info);
    }

    @PostMapping("add")
    @Token
    @ApiOperation("添加机构")
    public Object addOrg(@ApiIgnore @CurrentUser Integer userId, @Valid @RequestBody OrgReq.AddOrgReq org) throws Exception {
        DicOrg sysRole = dicOrgService.getByName(org.getOrgName());
        if (sysRole != null) {
            return Result.error("该机构已存在！");
        }
        boolean result = dicOrgService.addOrg(userId, org);
        if (result)
            return Result.success(null);
        else
            return Result.error();
    }

    @PostMapping("delete")
    @Token
    @ApiOperation("删除机构")
    public Object deleteOrg(@RequestBody OrgReq.DeleteOrgReq org) throws Exception {
        boolean result = dicOrgService.deleteOrg(org);
        if (result)
            return Result.success(null);
        else
            return Result.error();
    }

    @PostMapping("update")
    @Token
    @ApiOperation("修改机构")
    public Object updateOrg(@Valid @RequestBody OrgReq.UpdateOrgReq org) throws Exception {
        DicOrg reqDic = dicOrgService.info(org.getOrgId());
        DicOrg nameRole = dicOrgService.getByName(org.getOrgName());
        if (reqDic != null) {
            if (nameRole != null && !nameRole.getOrgName().equals(reqDic.getOrgName())) {
                return Result.error("该机构已存在");
            }
        }
        boolean result = dicOrgService.updateOrg(org);
        if (result)
            return Result.success(null);
        else
            return Result.error();
    }

    @PostMapping("userorg")
    @Token
    @ApiOperation("查询用户机构")
    public Object getUserOrg(@RequestBody OrgReq.GetUserOrgReq org) throws Exception {
        List<SysUserOrgRsList> list = dicOrgService.getUserOrg(org.getUserId());
        return Result.success(list);
    }

    @PostMapping("saveuserorg")
    @Token
    @ApiOperation("保存用户机构")
    public Object saveUserOrg(@ApiIgnore @CurrentUser Integer loginUserId, @RequestBody OrgReq.SaveUserOrgReq org) throws Exception {
        boolean result = dicOrgService.saveUserOrg(Long.valueOf(loginUserId), org.getUserId(), org.getOrg());
        if (result)
            return Result.success(null);
        else
            return Result.error();
    }
}
