package com.ym.mec.web.controller;

import com.ym.mec.auth.api.client.SysUserFeignService;
import com.ym.mec.auth.api.entity.SysUser;
import com.ym.mec.biz.dal.entity.Organization;
import com.ym.mec.biz.dal.page.OrganizationQueryInfo;
import com.ym.mec.biz.service.OrganizationService;
import com.ym.mec.common.controller.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@RequestMapping("organization")
@Api(tags = "分部服务")
@RestController
public class OrganizationController extends BaseController {

    @Autowired
    private OrganizationService organizationService;
	@Autowired
	private SysUserFeignService sysUserFeignService;

    @ApiOperation(value = "分页查询分部列表")
    @GetMapping("/queryPage")
    @PreAuthorize("@pcs.hasPermissions('organization/queryPage')")
    public Object queryPage(OrganizationQueryInfo queryInfo){
    	SysUser sysUser = sysUserFeignService.queryUserInfo();
		if (sysUser == null) {
			return failed("用户信息获取失败");
		}
        return succeed(organizationService.queryPage(queryInfo));
    }

    @ApiOperation(value = "获取员工所在分部列表")
    @GetMapping("/queryEmployeeOrgan")
    @PreAuthorize("@pcs.hasPermissions('organization/queryEmployeeOrgan')")
    public Object queryEmployeeOrgan() throws Exception {
        return succeed(organizationService.queryEmployeeOrgan());
    }

    @ApiOperation(value = "新增分部")
    @PostMapping("/add")
    @PreAuthorize("@pcs.hasPermissions('organization/add')")
    public Object add(Organization organization){
        return succeed(organizationService.insert(organization));
    }

    @ApiOperation(value = "根据分部编号删除分部")
    @PostMapping("/del/{id}")
    @PreAuthorize("@pcs.hasPermissions('organization/del')")
    public Object del(@ApiParam(value = "分部编号", required = true) @PathVariable("id") Integer id){
        return succeed(organizationService.delete(id));
    }

    @ApiOperation(value = "修改分部信息")
    @PostMapping("/update")
    @PreAuthorize("@pcs.hasPermissions('organization/update')")
    public Object update(Organization organization){
        organization.setUpdateTime(new Date());
        return succeed(organizationService.update(organization));
    }

    @ApiOperation(value = "根据分部编号查询分部详情")
    @GetMapping("/get/{id}")
    @PreAuthorize("@pcs.hasPermissions('organization/get')")
    @ApiParam(value = "分部编号", required = true)
    public Object get( @PathVariable("id") Integer id){
        return succeed(organizationService.get(id));
    }
}
