package com.fjs.scenic.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.fjs.scenic.entity.system.Manage;
import com.fjs.scenic.entity.user.UserStatistical;
import com.fjs.scenic.service.system.ManageService;
import com.fjs.scenic.service.user.StatisticalPermissionsService;
import com.fjs.scenic.service.user.UserStatisticalService;
import com.fjs.scenic.utils.Cons;
import com.fjs.scenic.utils.Md5Utils;
import com.fjs.scenic.utils.ReturnResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.catalina.Manager;
import org.apache.catalina.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/api")
@Api(description = "领导查询登陆")
public class LoginController {
    @Resource
    private ManageService manageService;
    @Resource
    private StatisticalPermissionsService sp;
    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @GetMapping("/login")
    @ApiOperation(value = "登陆", notes = "登陆")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "account", value = "账户", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "password", value = "密码", required = true, dataType = "String", paramType = "query")
    })
    public ReturnResult login(String account, String password){
        Manage m2 = manageService.getManeger(account, Md5Utils.MD5(password));
        if(null == m2){
            return ReturnResult.error("帐号或密码错误！");
        }else if(m2.getStatus().equals("0")){
            return ReturnResult.error("帐号已被冻结！");
        }else{
            List<HashMap<String, Object>> permissions = sp.selectByUserID(m2.getId());
            String token = Md5Utils.MD5(m2.getPhone()+System.currentTimeMillis());
            redisTemplate.opsForValue().set(token,m2.getSupplierIds(),60 * 60 * 2, TimeUnit.SECONDS);
            JSONObject result = new JSONObject();
            result.put("permissions",permissions);
            result.put("token",token);
            return ReturnResult.ok(result);
        }
    }
    @GetMapping("/logout")
    @ApiOperation(value = "退出", notes = "退出")
    public void logout(HttpServletRequest request,HttpServletResponse response) throws IOException {
        String user_token = request.getHeader(Cons.TOKEN.WECHAT);
        redisTemplate.delete(user_token);
        response.setHeader("Content-type", "text/html;charset=UTF-8");
        response.getWriter().write(JSONObject.toJSONString(ReturnResult.build(200, "退出成功！")));
    }
}
