package com.kcim.web;

import com.kcim.common.util.PageUtils;
import com.kcim.common.util.Result;
import com.kcim.common.util.UserContext;
import com.kcim.dao.model.Department;
import com.kcim.dao.model.dto.DepartmentRequest;
import com.kcim.service.DepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

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


/**
 * 科室表
 *
 * @author KCYG
 * @date 2021-07-27 08:49:37
 */
@RestController
@RequestMapping("department")
@Api(tags = "科室信息管理")

public class DepartmentController {
    @Autowired
    private DepartmentService departmentService;

    /**
     * 列表
     */
    @GetMapping("/list")
    @ApiOperation("分页查询科室信息")
    public Result list(@RequestParam(defaultValue = "1", value = "current") Integer current,
                       @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                       @RequestParam(value = "name",required = false) String name){
//        User user = (User) SecurityUtils.getSubject().getPrincipal();
        Long hospId = UserContext.getHospId();
        PageUtils pageUtils= departmentService.queryList(current,pageSize,hospId,name);
        return Result.ok(pageUtils);
    }


    /**
     * 信息
     */
    @GetMapping("/getDepartment")
    @ApiOperation("根据指定条件查询科室的相关信息")
    public Result getDepartmentId(@RequestParam(name = "id",required = false) Integer id,
                                  @RequestParam(name = "departmentName",required = false) String departmentName,
                                  @RequestParam(name = "departmentCode",required = false) String departmentCode,
                                  @RequestParam(name = "hospId",required = false) Long hospId){
		List<Department> departmentList = departmentService.getByDepartment(id,departmentName,departmentCode,hospId);
        return Result.ok(departmentList);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    @ApiOperation("保存科室相关信息")
    public Result save(@RequestBody @Valid DepartmentRequest departmentRequest){
		departmentService.addDepartment(departmentRequest);
        return Result.ok();
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    @ApiOperation("修改科室信息")
    public Result update(@RequestBody @Valid DepartmentRequest departmentRequest){
		departmentService.updateByDepartment(departmentRequest);
        return Result.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    @ApiOperation("删除科室信息")
    public Result delete(@RequestBody Integer[] ids){
        departmentService.deleteByIds(Arrays.asList(ids));
        return Result.ok();
    }

}
