package com.ym.mec.web.controller;

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 com.ym.mec.common.entity.HttpResponseResult;
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;
import java.util.Map;

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

    @Autowired
    private OrganizationService organizationService;

    @ApiOperation(value = "分页查询分部列表")
    @GetMapping("/queryPage")
    @PreAuthorize("@pcs.hasPermissions('organization/queryPage')")
    public Object queryPage(OrganizationQueryInfo queryInfo){
        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 = "获取员工所在分部列表（管理员查看所有的分部）")
    @GetMapping("/queryEmployeeOrganByUser")
    @PreAuthorize("@pcs.hasPermissions('organization/queryEmployeeOrganByUser')")
    public Object queryEmployeeOrganByUser() throws Exception {
        return succeed(organizationService.queryEmployeeOrganByUser());
    }

    @ApiOperation(value = "新增分部")
    @PostMapping("/add")
    @PreAuthorize("@pcs.hasPermissions('organization/add')")
    public Object add(Organization organization){
        return succeed(organizationService.add(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.updateBasicInfo(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));
    }

    @ApiOperation(value = "获取用户所在分部的年级列表）")
    @GetMapping("/getGradeList")
    @PreAuthorize("@pcs.hasPermissions('organization/getGradeList')")
    @ApiParam(value = "分部编号", required = true)
    public HttpResponseResult<Map<Integer,String>> getGradeList(Integer id) {
        return succeed(organizationService.getGradeList(id));
    }
}
