package com.unis.crk.basic.controller;

import com.unis.crk.basic.entity.UserOrg;
import com.unis.crk.basic.service.IUserOrgService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.unis.crk.utils.ResultUtil;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * <p>
 * 机构授权表 前端控制器
 * </p>
 *
 * @author ghh
 * @since 2021-09-26
 */
@Api(value = "机构授权表",tags = {"机构授权表"})
@RestController
@RequestMapping("userOrg")
@CrossOrigin
public class UserOrgController {

    private final static Logger LOGGER = LoggerFactory.getLogger(UserOrgController.class);


    @Autowired
    private IUserOrgService userOrgService;

    /**
     * 根据用户编号获取列表数据
     * @param userId	用户编号
     * @return List<UserOrg>	用户机构列表
     */
    @ApiOperation(value="根据用户编号获取列表数据", notes="根据用户编号获取列表数据")
    @GetMapping("getByUserId/{userId}")
    public List<UserOrg> getByUserId(@PathVariable("userId")  String userId) {

        return userOrgService.queryByUserId(userId);
    }
    /**
     * 保存用户机构权限
     * @param userOrgVo	用户编号
     * @return

    @RequestMapping(value="/save",method = RequestMethod.GET)
    @ApiOperation(value="保存用户机构权限", notes = "保存用户机构权限")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户id", paramType = "query"),
            @ApiImplicitParam(name = "departIds", value = "部门ID字符串以,隔开", paramType = "query"),
            @ApiImplicitParam(name = "orgId", value = "单位ID", paramType = "query"),

    })
    public Map<String, Object> save(@RequestBody UserOrgVo userOrgVo) {
        String departIds = userOrgVo.getDepartIds();
        String[] orgIdList = departIds.split(",");
        userOrgService.save(userOrgVo.getUserId(), orgIdList, userOrgVo.getOrgId());
        return new HashMap<String, Object>();
    }
     */
    /**
     * 根据ID查询数据
     * @param id
     * @return
     */
    @ApiOperation(value="查询", notes="根据ID查询数据")
    @GetMapping("getOne/{id}")
    public ResultUtil getOne(@PathVariable("id") String id){
        LOGGER.info("入参为:{}",id);
        ResultUtil<UserOrg> data = new ResultUtil<>();
        if (StringUtils.isEmpty(id)){
            data.failed("传入的主键为空");
            return data;
        }
        data.success(this.userOrgService.getById(id));
        return data;
    }


    /**
     * 查询全部数据
     * @return
     */
    @ApiOperation(value="查询", notes="查询全部数据")
    @GetMapping("getAll")
    public ResultUtil getAll(){
        ResultUtil<List<UserOrg>> data = new ResultUtil<>();
        data.success(this.userOrgService.list());
        return data;
    }


    /**
     * 新增一条数据
     * @param param
     * @return
     */
    @ApiOperation(value="新增", notes="新增一条数据")
    @PostMapping("add")
    public ResultUtil add(@RequestBody UserOrg param,@RequestBody String departIds){
        LOGGER.info("入参为:{}", param);
        ResultUtil<Boolean> data = new ResultUtil<>();
        if (ObjectUtils.isEmpty(param)){
            data.failed("所接收对象为null");
            return data;
        }
        data.success(this.userOrgService.save(param));
        return data;
    }



    /**
     * 修改一条数据
     * @param param
     * @return
     */
    @ApiOperation(value="修改", notes="修改一条数据")
    @PatchMapping("edit")
    public ResultUtil edit(@RequestBody UserOrg param){
        LOGGER.info("入参为:{}", param);
        ResultUtil<Boolean> data = new ResultUtil<>();
        if (ObjectUtils.isEmpty(param)){
            data.failed("所接收对象为null");
            return data;
        }
        data.success(this.userOrgService.updateById(param));
        return data;
    }


    /**
     * 根据ID删除数据
     * @param id
     * @return
     */
    @ApiOperation(value="删除", notes="根据ID删除数据")
    @DeleteMapping("delete/{id}")
    public ResultUtil delete(@PathVariable("id") String id){
        LOGGER.info("入参为:{}", id);
        ResultUtil<Boolean> data = new ResultUtil<>();
        if (StringUtils.isEmpty(id)){
            data.failed("所接收id为null");
            return data;
        }
        data.success(this.userOrgService.removeById(id));
        return data;
    }


}

