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.Hospital;
import com.kcim.dao.model.dto.HospitalDto;
import com.kcim.dao.model.dto.HospitalSaveDto;
import com.kcim.vo.HospitalAllVO;
import com.kcim.service.HospitalService;
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-26 08:52:56
 */
@RestController
@RequestMapping("hosptail")
@Api(tags = "医院相关操作")
public class HospitalController {
    @Autowired
    private HospitalService hospitalService;

//    /**
//     * 列表
//     */
//    @RequestMapping("/list")
//    public Result list(@RequestParam Map<String, Object> params){
//        PageUtils page = hosptailService.queryPage(params);
//        return CommonResponse.success(page);
//    }

    /**
     * 用户信息列表查询  分页查询
     * @return
     */
    @GetMapping("/list")
    @ApiOperation("分页查询医院信息")
    public Result list(@RequestParam(value = "current", defaultValue = "1") 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 = hospitalService.queryList(current,pageSize,hospId,name);
        return Result.ok(pageUtils);
    }

    /**
     * 信息
     */
    @GetMapping("/getById")
    @ApiOperation("根据Id查询医院信息")
    public Result info(Integer id){
		Hospital hospital = hospitalService.getById(id);
        return Result.ok(hospital);
    }

    @GetMapping("/getAll")
    @ApiOperation("获取全部的医院信息")
    public Result getAll(){
        List<HospitalAllVO> hospitalAllVOList = hospitalService.getAll();
        return Result.ok(hospitalAllVOList);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    @ApiOperation("保存医院信息")
    public Result save(@RequestBody @Valid HospitalSaveDto hospitalSaveDto){
        hospitalService.saveHosptail(hospitalSaveDto);
        return Result.ok();
    }

    /**
     * 修改
     * 修改的操作是作废新增
     */
    @PostMapping("/update")
    @ApiOperation("修改医院信息")
    public Result update(@RequestBody @Valid HospitalDto hospitalDto){
        // 作废操作
        hospitalService.updateByHosptail(hospitalDto);
        return Result.ok();
    }

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

}
