package com.gyee.frame.controller.ticket;

import com.gyee.frame.common.dataSources.DataSource;
import com.gyee.frame.common.dataSources.DataSourceType;
import com.gyee.frame.common.domain.AjaxResult;
import com.gyee.frame.model.ticket.Labor;
import com.gyee.frame.service.ticket.IbsusersService;
import com.gyee.frame.service.ticket.LaborService;
import com.gyee.frame.service.ticket.ShiroService;
import com.gyee.frame.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
public class LoginController {

    @Autowired
    LaborService laborService;
    @Autowired
    IbsusersService ibsusersService;
    @Autowired
    ShiroService shiroService;

    /**
     * 登录入口
     * @param username
     * @param password
     * @return
     */
    @DataSource(value = DataSourceType.TICKET)
    @PostMapping(value = "sys/login")
    public AjaxResult login(@RequestParam("username") String username,
                            @RequestParam("password") String password){
        Labor labor = laborService.getLabor(username, password);
        if (labor == null)
            return AjaxResult.error(4004, "用户名或密码错误");

        Map<String, Object> token = shiroService.createToken(labor, password);

        return AjaxResult.successData(token);
    }


    @PostMapping(value = "sys/logout")
    public AjaxResult logout(@RequestHeader("token")String token){
        shiroService.logout(token);

        return AjaxResult.successData("您已安全退出系统");
    }
}
