package com.nmgs.AppController;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nmgs.config.ResultData;
import com.nmgs.entity.ServiceAreaOperators;
import com.nmgs.mapper.OperatorsMapper;
import com.nmgs.service.IOperatorsService;
import com.nmgs.util.TokenUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Controller
@RequestMapping("/ServiceAreaOperators")
@Api(tags="APP用户信息")
@CrossOrigin(origins = "*")
public class ServiceAreaOperatorsController {

    public static Logger logger = LoggerFactory.getLogger(ServiceAreaOperatorsController.class);

    @Autowired
    private IOperatorsService OperatorsService;

    @Autowired
    private OperatorsMapper operatorsMapper;


    @PostMapping("/getOperatorsList")
    @ResponseBody
    @ApiOperation(value="获取用户列表",httpMethod = "POST")
    public Object getOperatorsList(@RequestBody Map<String, Object> params){

        Page<ServiceAreaOperators> page = OperatorsService.getOperatorsList(params);
        //判断如果当前页大于总页数，就重新查询
        if((Integer)params.get("pageNum") > page.getPages()){
            page =  OperatorsService.getOperatorsList(params);
        }
        return page;
    }


    @PostMapping("/getFiterData")
    @ResponseBody
    @ApiOperation(value="用户过滤查询",httpMethod = "POST")
    public Object getFiterData(@RequestBody Map<String, Object> params) {
        System.out.println(params);
        Map<String, Object> resultMap = new HashMap<>();
        if("N".equals(params.get("isChange"))){
            resultMap.put("roleList", operatorsMapper.selectRoleListList());
            resultMap.put("serviceAreaList", operatorsMapper.selectServiceAreaList());

        }
        return resultMap;
    }

    @RequestMapping(value="/addOperators",method={RequestMethod.POST})
    @ResponseBody
    @ApiOperation(value="新增用户",httpMethod = "POST")
    public Object addOperators(@RequestBody ServiceAreaOperators operators) {
        Map<String, Object> resultMap = new HashMap<>();
        try {
            int res = OperatorsService.addOperators(operators);
            if(res >= 0){
                resultMap.put("msg","保存成功");
                resultMap.put("data","");
                resultMap.put("result",1);
            }else if(res == -2){
                resultMap.put("msg","工号已经存在，请重新输入工号");
                resultMap.put("data","");
                resultMap.put("result",-2);
            }else{
                resultMap.put("msg","保存失败");
                resultMap.put("data","");
                resultMap.put("result",-1);
            }
            return resultMap;
        } catch (Exception e) {
            logger.error("com.nmgs.controller.OperatorsController.addOperators: 保存数据失败   "+e);
            resultMap.put("msg","保存失败");
            resultMap.put("data","");
            resultMap.put("result",-1);
            return resultMap;
        }
    }


    @PostMapping("/updateOperators")
    @ResponseBody
    @ApiOperation(value="更新用户信息",httpMethod = "POST")
    public Object updateOperators(@RequestBody ServiceAreaOperators operators) {
        Map<String, Object> resultMap = new HashMap<>();
        try {
            int res = OperatorsService.updateOperators(operators);
            if(res >= 0){
                resultMap.put("msg","修改成功");
                resultMap.put("data","");
                resultMap.put("result",1);
            }else if(res == -2){
                resultMap.put("msg","工号已经存在，请重新输入工号");
                resultMap.put("data","");
                resultMap.put("result",-2);
            }else{
                resultMap.put("msg","修改失败");
                resultMap.put("data","");
                resultMap.put("result",-1);
            }
            return resultMap;
        } catch (Exception e) {
            logger.error("com.nmgs.controller.OperatorsController.updateOperators: 修改数据失败   "+e);
            resultMap.put("msg","保存失败");
            resultMap.put("data","");
            resultMap.put("result",-1);
            return resultMap;
        }
    }


    @PostMapping("/deleteOperators")
    @ResponseBody
    @ApiOperation(value="删除用户信息",httpMethod = "POST")
    public Object deleteOperators(@RequestBody List<ServiceAreaOperators> Operators) {
        Map<String, Object> resultMap = new HashMap<>();
        try {
            int res = OperatorsService.deleteOperators(Operators);
            if(res >= 0){
                resultMap.put("msg","删除成功");
                resultMap.put("data","");
                resultMap.put("result",1);
            }else{
                resultMap.put("msg","删除失败");
                resultMap.put("data","");
                resultMap.put("result",-1);
            }
            return resultMap;
        } catch (Exception e) {
            logger.error("com.nmgs.controller.OperatorsController.deleteOperators: 删除失败   "+e);
            resultMap.put("msg","删除失败");
            resultMap.put("data","");
            resultMap.put("result",-1);
            return resultMap;
        }
    }


    @PostMapping("/login")
    @ResponseBody
    @ApiOperation(value="用户登录验证",httpMethod = "POST")
    public Object login(@RequestBody Map<String, Object> params){
        Map<String, Object> resultMap = new HashMap<>();
        ServiceAreaOperators login = OperatorsService.login(params);
        if(null == login){
            resultMap.put("msg","用户名或密码错误");
            resultMap.put("data","");
            resultMap.put("result",-1);
        }else{
            //生成令牌，返回token
            String token = TokenUtil.token(login.getJobNumber());
            login.setToken(token);
            resultMap.put("msg","登录成功");
            resultMap.put("data",login);
            resultMap.put("result",1);
        }
        return resultMap;
    }

    @RequestMapping(value="/updatePassword", method = {RequestMethod.POST})
    @ResponseBody
    @ApiOperation(value="用户密码修改",httpMethod = "POST")
    public Object updatePassword(@RequestBody Map<String, Object> params){
        int ret = OperatorsService.updatePassword(params);
        if(ret<0){
          return  ResultData.fail("用户未找到");
        }
        return ResultData.success(1,"修改成功");
    }
}