package com.saye.hospitalgd.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.saye.hospitalgd.service.OperatorService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;

/**
 * @author thuang
 * @version 1.0
 * @description: 操作员
 * @date 2021/9/29 14:51
 */
@Controller
@RequestMapping("/operator")
@Api(tags = "操作员相关接口")
public class OperatorController {

    @Autowired
    private OperatorService operatorService;

    @RequestMapping("/toOperator")
    public String toOperator(ModelMap modelMap) {

        return "operator/operator";
    }

    /**
     * @description: 查询操作员
     * @author thuang
     * @date 2021/9/17 8:55
     * @version 1.0
     */
    @RequestMapping("/findOperator")
    @ResponseBody
    @ApiOperation("查询操作员")
    public HashMap<Object, Object> findOperator(String likeFiled, String is_active, int page, int limit) {
        HashMap<Object, Object> responseMap = new HashMap<>();

        try {
            HashMap<Object, Object> map = new HashMap<Object, Object>();
            map.put("likeFiled", likeFiled);
            map.put("is_active", is_active);

            PageHelper.startPage(page, limit);
            PageInfo<HashMap<Object, Object>> pageInfo = new PageInfo<HashMap<Object, Object>>(operatorService.findAllOperator(map));

            responseMap.put("code", 0);
            responseMap.put("msg", "OK");
            responseMap.put("count", pageInfo.getTotal());
            responseMap.put("data", pageInfo.getList());
        } catch (Exception e) {
            e.printStackTrace();
            String msg = e.getMessage();
            responseMap.put("code", 1);
            responseMap.put("msg", "查询失败,原因：" + msg);
        }
        return responseMap;
    }

    /**
     * @description: 添加操作员 根据操作员号如果已有 更新，并且设置启用
     * @author thuang
     * @date 2021/9/30 10:17
     * @version 1.0
     */
    @RequestMapping("/addOperator")
    @ResponseBody
    @ApiOperation(value = "添加操作员 根据操作员号如果已有 更新，并且设置启用")
    public HashMap<Object, Object> addOperator(@RequestBody HashMap<Object, Object> map) {
        HashMap<Object, Object> responseMap = new HashMap<>();
        String errCode = "0";
        String errMsg = "";

        try {
            operatorService.addOperator(map);

        } catch (Exception e) {
            e.printStackTrace();
            errCode = "999";
            errMsg = "添加操作员失败，原因：" + e.getMessage();
        }

        responseMap.put("errCode", errCode);
        responseMap.put("errMsg", errMsg);

        return responseMap;
    }

    /**
     * @description: 修改操作员
     * @author thuang
     * @date 2021/9/30 10:44
     * @version 1.0
     */
    @RequestMapping("/modifyOperator")
    @ResponseBody
    @ApiOperation(value = "修改操作员")
    public HashMap<Object, Object> modifyOperator(@RequestBody HashMap<Object, Object> map) {
        HashMap<Object, Object> responseMap = new HashMap<>();
        String errCode = "0";
        String errMsg = "";

        try {
            operatorService.modifyOperator(map);

        } catch (Exception e) {
            e.printStackTrace();
            errCode = "999";
            errMsg = "修改操作员失败，原因：" + e.getMessage();
        }

        responseMap.put("errCode", errCode);
        responseMap.put("errMsg", errMsg);

        return responseMap;
    }

    /**
     * @description: 删除操作员
     * @author thuang
     * @date 2021/9/30 10:44
     * @version 1.0
     */
    @RequestMapping("/deleteOperator")
    @ResponseBody
    @ApiOperation(value = "删除操作员")
    public HashMap<Object, Object> deleteOperator(@RequestBody HashMap<Object, Object> map) {
        HashMap<Object, Object> responseMap = new HashMap<>();
        String errCode = "0";
        String errMsg = "";

        try {
            operatorService.deleteOperator(map);

        } catch (Exception e) {
            e.printStackTrace();
            errCode = "999";
            errMsg = "删除操作员失败，原因：" + e.getMessage();
        }

        responseMap.put("errCode", errCode);
        responseMap.put("errMsg", errMsg);

        return responseMap;
    }

}
