package cn.ibizlab.r7.runtime.runtimemgr.r7rt_base.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import com.alibaba.fastjson.JSONObject;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.PageRequest;
import cn.ibizlab.r7.runtime.runtimemgr.r7rt_base.service.LoginAccountService;
import cn.ibizlab.r7.runtime.runtimemgr.r7rt_base.domain.LoginAccount;
import cn.ibizlab.r7.runtime.runtimemgr.r7rt_base.filter.*;

@RestController
@RequestMapping(value = "")
public class LoginAccountController {
	@Autowired
    LoginAccountService loginaccountservice;


	@RequestMapping(method = RequestMethod.GET, value = "/loginaccounts/{loginaccount_id}")
    @PreAuthorize("@loginaccount_pms.check(#loginaccount_id,'READ')")
    public ResponseEntity<LoginAccount> get(@PathVariable("loginaccount_id") String loginaccount_id) {
        LoginAccount loginaccount = loginaccountservice.get( loginaccount_id);
        return ResponseEntity.status(HttpStatus.OK).body(loginaccount);
    }

	@RequestMapping(method = RequestMethod.POST, value = "/loginaccounts/checkkey")
    @PreAuthorize("@loginaccount_pms.check('')")
    public ResponseEntity<Boolean> checkKey(@RequestBody LoginAccount loginaccount) {
        boolean b = loginaccountservice.checkKey(loginaccount);
        return ResponseEntity.status(HttpStatus.OK).body(b);
    }

	@RequestMapping(method = RequestMethod.PUT, value = "/loginaccounts/{loginaccount_id}/updatepwd")
    @PreAuthorize("@loginaccount_pms.check(#loginaccount_id,'UPDATE')")
    public ResponseEntity<LoginAccount> updatePWD(@PathVariable("loginaccount_id") String loginaccount_id, @RequestBody LoginAccount loginaccount) {
        LoginAccount loginaccount2 = loginaccountservice.updatePWD(loginaccount_id, loginaccount);
        return ResponseEntity.status(HttpStatus.OK).body(loginaccount2);
    }

	@RequestMapping(method = RequestMethod.POST, value = "/loginaccounts/{loginaccount_id}/save")
    @PreAuthorize("@loginaccount_pms.check(#loginaccount_id,'')")
    public ResponseEntity<LoginAccount> save(@PathVariable("loginaccount_id") String loginaccount_id, @RequestBody LoginAccount loginaccount) {
        LoginAccount loginaccount2 = loginaccountservice.save(loginaccount_id, loginaccount);
        return ResponseEntity.status(HttpStatus.OK).body(loginaccount2);
    }

	@RequestMapping(method = RequestMethod.DELETE, value = "/loginaccounts/{loginaccount_id}")
    @PreAuthorize("@loginaccount_pms.check(#loginaccount_id,'DELETE')")
    public ResponseEntity<Boolean> remove(@PathVariable("loginaccount_id") String loginaccount_id) {
        boolean b = loginaccountservice.remove( loginaccount_id);
        return ResponseEntity.status(HttpStatus.OK).body(b);
    }

	@RequestMapping(method = RequestMethod.POST, value = "/loginaccounts")
    @PreAuthorize("@loginaccount_pms.check('CREATE')")
    public ResponseEntity<LoginAccount> create(@RequestBody LoginAccount loginaccount) {
        LoginAccount loginaccount2 = loginaccountservice.create(loginaccount);
        return ResponseEntity.status(HttpStatus.OK).body(loginaccount2);
    }

	@RequestMapping(method = RequestMethod.GET, value = "/loginaccounts/getdraft")
    @PreAuthorize("@loginaccount_pms.check('CREATE')")
    public ResponseEntity<LoginAccount> getDraft() {
        //LoginAccount loginaccount = loginaccountservice.getDraft( loginaccount_id);
        return ResponseEntity.status(HttpStatus.OK).body(new LoginAccount());
    }

	@RequestMapping(method = RequestMethod.PUT, value = "/loginaccounts/{loginaccount_id}")
    @PreAuthorize("@loginaccount_pms.check(#loginaccount_id,'UPDATE')")
    public ResponseEntity<LoginAccount> update(@PathVariable("loginaccount_id") String loginaccount_id, @RequestBody LoginAccount loginaccount) {
        LoginAccount loginaccount2 = loginaccountservice.update(loginaccount_id, loginaccount);
        return ResponseEntity.status(HttpStatus.OK).body(loginaccount2);
    }

	@RequestMapping(method = RequestMethod.PUT, value = "/api/loginaccounts/{loginaccount_id}")
    @PreAuthorize("@loginaccount_pms.check(#loginaccount_id,'UPDATE')")
    public ResponseEntity<LoginAccount> api_update(@PathVariable("loginaccount_id") String loginaccount_id, @RequestBody LoginAccount loginaccount) {
        LoginAccount loginaccount2 = loginaccountservice.update(loginaccount_id, loginaccount);
        return ResponseEntity.status(HttpStatus.OK).body(loginaccount2);
    }

    @RequestMapping(method= RequestMethod.GET , value="/loginaccounts/fetchgetloginaccount")
    @PreAuthorize("@loginaccount_pms.check('READ')")
	public ResponseEntity<List<LoginAccount>> fetchGetLoginaccount(LoginAccountSearchContext searchContext,Pageable pageable) {
        
        Page<LoginAccount> page = loginaccountservice.fetchGetLoginaccount(searchContext);
        return ResponseEntity.status(HttpStatus.OK)
                .header("x-page", String.valueOf(pageable.getPageNumber()))
        		.header("x-per-page", String.valueOf(pageable.getPageSize()))
        		.header("x-total", String.valueOf(page.getTotalElements()))
        		.body(page.getContent());
    }

    @RequestMapping(method= RequestMethod.GET , value="/loginaccounts/fetchdefault")
    @PreAuthorize("@loginaccount_pms.check('READ')")
	public ResponseEntity<List<LoginAccount>> fetchDefault(LoginAccountSearchContext searchContext,Pageable pageable) {
        
        Page<LoginAccount> page = loginaccountservice.fetchDefault(searchContext);
        return ResponseEntity.status(HttpStatus.OK)
                .header("x-page", String.valueOf(pageable.getPageNumber()))
        		.header("x-per-page", String.valueOf(pageable.getPageSize()))
        		.header("x-total", String.valueOf(page.getTotalElements()))
        		.body(page.getContent());
    }


	@RequestMapping(method = RequestMethod.GET, value = "/users/{user_id}/loginaccounts/{loginaccount_id}")
    @PreAuthorize("@loginaccount_pms.checkByUser(#user_id,#loginaccount_id,'READ')")
    public ResponseEntity<LoginAccount> getByUser(@PathVariable("user_id") String user_id, @PathVariable("loginaccount_id") String loginaccount_id) {
        LoginAccount loginaccount = loginaccountservice.getByUser(user_id,  loginaccount_id);
        return ResponseEntity.status(HttpStatus.OK).body(loginaccount);
    }

	@RequestMapping(method = RequestMethod.POST, value = "/users/{user_id}/loginaccounts/checkkey")
    @PreAuthorize("@loginaccount_pms.checkByUser(#user_id,'')")
    public ResponseEntity<Boolean> checkKeyByUser(@PathVariable("user_id") String user_id, @RequestBody LoginAccount loginaccount) {
        boolean b = loginaccountservice.checkKeyByUser(user_id, loginaccount);
        return ResponseEntity.status(HttpStatus.OK).body(b);
    }

	@RequestMapping(method = RequestMethod.PUT, value = "/users/{user_id}/loginaccounts/{loginaccount_id}/updatepwd")
    @PreAuthorize("@loginaccount_pms.checkByUser(#user_id,#loginaccount_id,'UPDATE')")
    public ResponseEntity<LoginAccount> updatePWDByUser(@PathVariable("user_id") String user_id, @PathVariable("loginaccount_id") String loginaccount_id, @RequestBody LoginAccount loginaccount) {
        LoginAccount loginaccount2 = loginaccountservice.updatePWDByUser(user_id, loginaccount_id, loginaccount);
        return ResponseEntity.status(HttpStatus.OK).body(loginaccount2);
    }

	@RequestMapping(method = RequestMethod.POST, value = "/users/{user_id}/loginaccounts/{loginaccount_id}/save")
    @PreAuthorize("@loginaccount_pms.checkByUser(#user_id,#loginaccount_id,'')")
    public ResponseEntity<LoginAccount> saveByUser(@PathVariable("user_id") String user_id, @PathVariable("loginaccount_id") String loginaccount_id, @RequestBody LoginAccount loginaccount) {
        LoginAccount loginaccount2 = loginaccountservice.saveByUser(user_id, loginaccount_id, loginaccount);
        return ResponseEntity.status(HttpStatus.OK).body(loginaccount2);
    }

	@RequestMapping(method = RequestMethod.DELETE, value = "/users/{user_id}/loginaccounts/{loginaccount_id}")
    @PreAuthorize("@loginaccount_pms.checkByUser(#user_id,#loginaccount_id,'DELETE')")
    public ResponseEntity<Boolean> removeByUser(@PathVariable("user_id") String user_id, @PathVariable("loginaccount_id") String loginaccount_id) {
        boolean b = loginaccountservice.removeByUser(user_id,  loginaccount_id);
        return ResponseEntity.status(HttpStatus.OK).body(b);
    }

	@RequestMapping(method = RequestMethod.POST, value = "/users/{user_id}/loginaccounts")
    @PreAuthorize("@loginaccount_pms.checkByUser(#user_id,'CREATE')")
    public ResponseEntity<LoginAccount> createByUser(@PathVariable("user_id") String user_id, @RequestBody LoginAccount loginaccount) {
        LoginAccount loginaccount2 = loginaccountservice.createByUser(user_id, loginaccount);
        return ResponseEntity.status(HttpStatus.OK).body(loginaccount2);
    }

	@RequestMapping(method = RequestMethod.GET, value = "/users/{user_id}/loginaccounts/getdraft")
    @PreAuthorize("@loginaccount_pms.checkByUser(#user_id,'CREATE')")
    public ResponseEntity<LoginAccount> getDraftByUser() {
        //LoginAccount loginaccount = loginaccountservice.getDraftByUser(user_id,  loginaccount_id);
        return ResponseEntity.status(HttpStatus.OK).body(new LoginAccount());
    }

	@RequestMapping(method = RequestMethod.PUT, value = "/users/{user_id}/loginaccounts/{loginaccount_id}")
    @PreAuthorize("@loginaccount_pms.checkByUser(#user_id,#loginaccount_id,'UPDATE')")
    public ResponseEntity<LoginAccount> updateByUser(@PathVariable("user_id") String user_id, @PathVariable("loginaccount_id") String loginaccount_id, @RequestBody LoginAccount loginaccount) {
        LoginAccount loginaccount2 = loginaccountservice.updateByUser(user_id, loginaccount_id, loginaccount);
        return ResponseEntity.status(HttpStatus.OK).body(loginaccount2);
    }

    @RequestMapping(method = RequestMethod.PUT, value = "/api/users/{user_id}/loginaccounts/{loginaccount_id}")
    @PreAuthorize("@loginaccount_pms.checkByUser(#user_id,#loginaccount_id,'UPDATE')")
    public ResponseEntity<LoginAccount> api_updateByUser(@PathVariable("user_id") String user_id, @PathVariable("loginaccount_id") String loginaccount_id, @RequestBody LoginAccount loginaccount) {
        LoginAccount loginaccount2 = loginaccountservice.updateByUser(user_id, loginaccount_id, loginaccount);
        return ResponseEntity.status(HttpStatus.OK).body(loginaccount2);
    }

    @RequestMapping(method= RequestMethod.GET , value="/users/{user_id}/loginaccounts/fetchgetloginaccount")
    @PreAuthorize("@loginaccount_pms.checkByUser(#user_id,'READ')")
	public ResponseEntity<List<LoginAccount>> fetchGetLoginaccountByUser(@PathVariable("user_id") String user_id,LoginAccountSearchContext searchContext,Pageable pageable) {
        searchContext.setN_userid_eq(user_id);
        Page<LoginAccount> page = loginaccountservice.fetchGetLoginaccountByUser(user_id , searchContext);
        return ResponseEntity.status(HttpStatus.OK)
                .header("x-page", String.valueOf(pageable.getPageNumber()))
        		.header("x-per-page", String.valueOf(pageable.getPageSize()))
        		.header("x-total", String.valueOf(page.getTotalElements()))
        		.body(page.getContent());
    }

    @RequestMapping(method= RequestMethod.GET , value="/users/{user_id}/loginaccounts/fetchdefault")
    @PreAuthorize("@loginaccount_pms.checkByUser(#user_id,'READ')")
	public ResponseEntity<List<LoginAccount>> fetchDefaultByUser(@PathVariable("user_id") String user_id,LoginAccountSearchContext searchContext,Pageable pageable) {
        searchContext.setN_userid_eq(user_id);
        Page<LoginAccount> page = loginaccountservice.fetchDefaultByUser(user_id , searchContext);
        return ResponseEntity.status(HttpStatus.OK)
                .header("x-page", String.valueOf(pageable.getPageNumber()))
        		.header("x-per-page", String.valueOf(pageable.getPageSize()))
        		.header("x-total", String.valueOf(page.getTotalElements()))
        		.body(page.getContent());
    }


}
