package com.fs.hospital.controller.hospital;

import com.fs.common.annotation.Log;
import com.fs.common.constant.UserConstants;
import com.fs.common.core.controller.BaseController;
import com.fs.common.core.domain.AjaxResult;
import com.fs.common.core.page.TableDataInfo;
import com.fs.common.enums.BusinessType;
import com.fs.common.utils.ServletUtils;
import com.fs.common.utils.poi.ExcelUtil;
import com.fs.hospital.domain.HospitalPost;
import com.fs.hospital.service.IHospitalPostService;
import com.fs.framework.security.LoginUser;
import com.fs.framework.security.SecurityUtils;
import com.fs.framework.service.TokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 岗位信息操作处理
 *

 */
@RestController
@RequestMapping("/hospital/post")
public class HospitalPostController extends BaseController
{
    @Autowired
    private IHospitalPostService postService;
    @Autowired
    private TokenService tokenService;
    /**
     * 获取岗位列表
     */
    @PreAuthorize("@ss.hasPermi('hospital:post:list')")
    @GetMapping("/list")
    public TableDataInfo list(HospitalPost post)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        post.setHospitalId(loginUser.getHospital().getHospitalId());
        startPage();
        List<HospitalPost> list = postService.selectHospitalPostList(post);
        return getDataTable(list);
    }

    @Log(title = "岗位管理", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('hospital:post:export')")
    @GetMapping("/export")
    public AjaxResult export(HospitalPost post)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        post.setHospitalId(loginUser.getHospital().getHospitalId());
        List<HospitalPost> list = postService.selectHospitalPostList(post);
        ExcelUtil<HospitalPost> util = new ExcelUtil<HospitalPost>(HospitalPost.class);
        return util.exportExcel(list, "岗位数据");
    }

    /**
     * 根据岗位编号获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('hospital:post:query')")
    @GetMapping(value = "/{postId}")
    public AjaxResult getInfo(@PathVariable Long postId)
    {
        return AjaxResult.success(postService.selectHospitalPostByPostId(postId));
    }

    /**
     * 新增岗位
     */
    @PreAuthorize("@ss.hasPermi('hospital:post:add')")
    @Log(title = "岗位管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody HospitalPost post)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        post.setHospitalId(loginUser.getHospital().getHospitalId());
       if (postService.checkPostCodeUnique(post)>0)
        {
            return AjaxResult.error("新增岗位'" + post.getPostName() + "'失败，岗位编码已存在");
        }
        post.setCreateBy(SecurityUtils.getUsername());
        return toAjax(postService.insertHospitalPost(post));
    }

    /**
     * 修改岗位
     */
    @PreAuthorize("@ss.hasPermi('hospital:post:edit')")
    @Log(title = "岗位管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody HospitalPost post)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        post.setHospitalId(loginUser.getHospital().getHospitalId());
        if (UserConstants.NOT_UNIQUE.equals(postService.checkPostCodeUnique(post)))
        {
            return AjaxResult.error("修改岗位'" + post.getPostName() + "'失败，岗位编码已存在");
        }
        post.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(postService.updateHospitalPost(post));
    }

    /**
     * 删除岗位
     */
    @PreAuthorize("@ss.hasPermi('hospital:post:remove')")
    @Log(title = "岗位管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{postIds}")
    public AjaxResult remove(@PathVariable Long[] postIds)
    {

        return toAjax(postService.deleteHospitalPostByPostIds(postIds));
    }

    /**
     * 获取岗位选择框列表
     */
    @GetMapping("/optionselect")
    public AjaxResult optionselect()
    {
        HospitalPost postMap=new HospitalPost();
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        postMap.setHospitalId(loginUser.getHospital().getHospitalId());
        List<HospitalPost> posts = postService.selectHospitalPostList(postMap);
        return AjaxResult.success(posts);
    }
}
