package com.izouma.awesomeadmin.web;

import com.izouma.awesomeadmin.constant.AppConstant;
import com.izouma.awesomeadmin.dto.Page;
import com.izouma.awesomeadmin.dto.Result;
import com.izouma.awesomeadmin.model.UsersSession;
import com.izouma.awesomeadmin.service.UsersSessionService;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

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


/**
 * The type Users session controller.
 */
@Controller
@RequestMapping("/usersSession")
@RequiresAuthentication
public class UsersSessionController {

    @Autowired
    private UsersSessionService usersSessionService;


    /**
     * <p>获取全部记录。</p>
     *
     * @return result
     */
    @RequestMapping(value = "/all", method = RequestMethod.GET)
    @ResponseBody
    public Result all(UsersSession usersSession) {

        List<UsersSession> pp = usersSessionService.getUsersSessionList(usersSession);
        return new Result(true, pp);
    }


    /**
     * <p>根据Id。</p>
     *
     * @param id the id
     * @return users session
     */
    @RequestMapping(value = "/getUsersSession", method = RequestMethod.GET)
    @ResponseBody
    public Result getUsersSession(@RequestParam(required = false, value = "id") String id) {

        UsersSession data = usersSessionService.getUsersSessionById(id);
        return new Result(true, data);
    }


    /**
     * <p>根据条件获取。</p>
     *
     * @param usersSession the users session
     * @return one
     */
    @RequestMapping(value = "/getOne", method = RequestMethod.GET)
    @ResponseBody
    public Result getOne(UsersSession usersSession) {

        UsersSession data = usersSessionService.getUsersSession(usersSession);
        return new Result(true, data);
    }


    /**
     * Page result.
     *
     * @param page         the page
     * @param usersSession the users session
     * @return the result
     */
    @RequestMapping(value = "/page", method = RequestMethod.GET)
    @ResponseBody
    public Result page(Page page, UsersSession usersSession) {

        Map<String, Object> result = new HashMap<>();

        List<UsersSession> pp = usersSessionService.getUsersSessionByPage(page, usersSession);

        result.put(AppConstant.PAGE, page);
        result.put("pp", pp);
        return new Result(true, result);
    }


    /**
     * <p>后台获取。</p>
     *
     * @param page         the page
     * @param usersSession the users session
     * @return result
     */
    @RequestMapping(value = "/pageAll", method = RequestMethod.GET)
    @ResponseBody
    public Result pageAll(Page page, UsersSession usersSession) {

        Map<String, Object> result = new HashMap<>();

        List<UsersSession> pp = usersSessionService.queryAllUsersSessionByPage(page, usersSession);
        result.put(AppConstant.PAGE, page);
        result.put("pp", pp);
        return new Result(true, result);
    }


    /**
     * <p>保存。</p>
     *
     * @param usersSession the users session
     * @return result
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ResponseBody
    public Result save(UsersSession usersSession) {

        boolean num = usersSessionService.createUsersSession(usersSession);
        if (num) {
            return new Result(true, "保存成功");
        }
        return new Result(false, "保存异常");

    }


    /**
     * <p>更新信息。</p>
     *
     * @param usersSession the users session
     * @return result
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseBody
    public Result updateUsersSession(UsersSession usersSession) {

        boolean num = usersSessionService.updateUsersSession(usersSession);
        if (num) {
            return new Result(true, "保存成功");
        }
        return new Result(false, "保存异常");
    }


    /**
     * <p>删除。</p>
     *
     * @param id the id
     * @return result
     */
    @RequestMapping(value = "/del", method = RequestMethod.POST)
    @ResponseBody
    public Result deleteUsersSessionId(@RequestParam(required = true, value = "id") String id) {

        boolean num = usersSessionService.deleteUsersSession(id);
        if (num) {
            return new Result(true, "删除成功");
        }
        return new Result(false, "删除异常");
    }

}
