package cn.ibizlab.r7.runtime.service.runtimemgr.resource;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.math.BigInteger;
import java.util.HashMap;
import javax.servlet.ServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.beans.BeanCopier;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.http.HttpStatus;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.util.StringUtils;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;

import com.alibaba.fastjson.JSONObject;
import org.springframework.data.domain.PageRequest;
import cn.ibizlab.r7.runtime.service.runtimemgr.dto.LoginAccountDTO;
import cn.ibizlab.r7.runtime.core.r7rt_base.domain.LoginAccount;
import cn.ibizlab.r7.runtime.core.r7rt_base.service.ILoginAccountService;
import cn.ibizlab.r7.runtime.util.SearchContext;
import cn.ibizlab.r7.runtime.core.r7rt_base.filter.LoginAccountSearchContext;

@Api(tags = {"LoginAccount" })
@RestController
@RequestMapping("")
public class LoginAccountResource {

    @Autowired
    private ILoginAccountService loginaccountService;

    public ILoginAccountService getLoginaccountService() {
        return this.loginaccountService;
    }

    @ApiOperation(value = "Get", tags = {"LoginAccount" },  notes = "Get")
	@RequestMapping(method = RequestMethod.GET, value = "/runtimemgr/loginaccounts/{loginaccount_id}")
    public ResponseEntity<LoginAccountDTO> get(@PathVariable("loginaccount_id") String loginaccount_id) {
        LoginAccountDTO dto = new LoginAccountDTO();
        LoginAccount domain = loginaccountService.get(loginaccount_id);
		dto.fromDO(domain);
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @ApiOperation(value = "CheckKey", tags = {"LoginAccount" },  notes = "CheckKey")
	@RequestMapping(method = RequestMethod.POST, value = "/runtimemgr/loginaccounts/checkkey")
    public ResponseEntity<Boolean> checkKey(@RequestBody LoginAccountDTO loginaccountdto) {
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @ApiOperation(value = "更新密码", tags = {"LoginAccount" },  notes = "更新密码")
	@RequestMapping(method = RequestMethod.PUT, value = "/runtimemgr/loginaccounts/{loginaccount_id}/updatepwd")

    public ResponseEntity<LoginAccountDTO> updatePWD(@PathVariable("loginaccount_id") String loginaccount_id, @RequestBody LoginAccountDTO loginaccountdto) {
        LoginAccount loginaccount = loginaccountdto.toDO();
    	loginaccount = loginaccountService.updatePWD(loginaccount) ;
    	loginaccountdto.fromDO(loginaccount);
        return ResponseEntity.status(HttpStatus.OK).body(loginaccountdto);
    }

    @ApiOperation(value = "Save", tags = {"LoginAccount" },  notes = "Save")
	@RequestMapping(method = RequestMethod.POST, value = "/runtimemgr/loginaccounts/{loginaccount_id}/save")
    public ResponseEntity<Boolean> save(@RequestBody LoginAccountDTO loginaccountdto) {
        LoginAccount loginaccount = loginaccountdto.toDO();
    	Boolean b = loginaccountService.save(loginaccount) ;
        return ResponseEntity.status(HttpStatus.OK).body(b);
    }

    @ApiOperation(value = "Remove", tags = {"LoginAccount" },  notes = "Remove")
	@RequestMapping(method = RequestMethod.DELETE, value = "/runtimemgr/loginaccounts/{loginaccount_id}")

    public ResponseEntity<Boolean> remove(@PathVariable("loginaccount_id") String loginaccount_id) {
        LoginAccountDTO loginaccountdto = new LoginAccountDTO();
		LoginAccount domain = new LoginAccount();
		loginaccountdto.setLoginAccountId(loginaccount_id);
		domain.setLoginAccountId(loginaccount_id);
        Boolean rst = loginaccountService.remove(domain.getLoginAccountId());
        if(rst){
			return ResponseEntity.status(HttpStatus.OK).body(rst);
		}else{
			return ResponseEntity.status(HttpStatus.OK).body(rst);
		}
    }

    @ApiOperation(value = "Create", tags = {"LoginAccount" },  notes = "Create")
	@RequestMapping(method = RequestMethod.POST, value = "/runtimemgr/loginaccounts")

    public ResponseEntity<LoginAccountDTO> create(@RequestBody LoginAccountDTO loginaccountdto) {
        LoginAccountDTO dto = new LoginAccountDTO();
        LoginAccount domain = loginaccountdto.toDO();
		loginaccountService.create(domain);
        dto.fromDO(domain);
		return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @ApiOperation(value = "GetDraft", tags = {"LoginAccount" },  notes = "GetDraft")
	@RequestMapping(method = RequestMethod.GET, value = "/runtimemgr/loginaccounts/{loginaccount_id}/getdraft")

    public ResponseEntity<LoginAccountDTO> getDraft(@PathVariable("loginaccount_id") String loginaccount_id, @RequestBody LoginAccountDTO loginaccountdto) {
        LoginAccount loginaccount = loginaccountdto.toDO();
    	loginaccount = loginaccountService.getDraft(loginaccount) ;
    	loginaccountdto.fromDO(loginaccount);
        return ResponseEntity.status(HttpStatus.OK).body(loginaccountdto);
    }

    @ApiOperation(value = "Update", tags = {"LoginAccount" },  notes = "Update")
	@RequestMapping(method = RequestMethod.PUT, value = "/runtimemgr/loginaccounts/{loginaccount_id}")

    public ResponseEntity<LoginAccountDTO> update(@PathVariable("loginaccount_id") String loginaccount_id, @RequestBody LoginAccountDTO loginaccountdto) {
		LoginAccount domain = loginaccountdto.toDO();
        domain.setLoginAccountId(loginaccount_id);
		loginaccountService.update(domain);
		LoginAccountDTO dto = new LoginAccountDTO();
		dto.fromDO(domain);
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

	@ApiOperation(value = "获取getLoginaccount", tags = {"LoginAccount" } ,notes = "获取getLoginaccount")
    @RequestMapping(method= RequestMethod.GET , value="/runtimemgr/loginaccounts/fetchgetloginaccount")
	public ResponseEntity<Page<LoginAccountDTO>> fetchGetLoginaccount(LoginAccountSearchContext context,Pageable pageable ,ServletRequest request) {
        context.setPageable(pageable);
        List<LoginAccountDTO> list = new ArrayList<LoginAccountDTO>();
        
        Page<LoginAccount> domains = loginaccountService.searchGetLoginaccount(context) ;
        for(LoginAccount loginaccount : domains.getContent()){
            LoginAccountDTO dto = new LoginAccountDTO();
            dto.fromDO(loginaccount);
            list.add(dto);
        }
	    return ResponseEntity.status(HttpStatus.OK).body(new PageImpl(list,context.getPageable(),domains.getTotalElements()));
	}

	@ApiOperation(value = "获取DEFAULT", tags = {"LoginAccount" } ,notes = "获取DEFAULT")
    @RequestMapping(method= RequestMethod.GET , value="/runtimemgr/loginaccounts/fetchdefault")
	public ResponseEntity<Page<LoginAccountDTO>> fetchDefault(LoginAccountSearchContext context,Pageable pageable ,ServletRequest request) {
        context.setPageable(pageable);
        List<LoginAccountDTO> list = new ArrayList<LoginAccountDTO>();
        
        Page<LoginAccount> domains = loginaccountService.searchDefault(context) ;
        for(LoginAccount loginaccount : domains.getContent()){
            LoginAccountDTO dto = new LoginAccountDTO();
            dto.fromDO(loginaccount);
            list.add(dto);
        }
	    return ResponseEntity.status(HttpStatus.OK).body(new PageImpl(list,context.getPageable(),domains.getTotalElements()));
	}



    @ApiOperation(value = "Get", tags = {"LoginAccount" },  notes = "Get")
	@RequestMapping(method = RequestMethod.GET, value = "/runtimemgr/users/{user_id}/loginaccounts/{loginaccount_id}")
    public ResponseEntity<LoginAccountDTO> getByUser(@PathVariable("user_id") String user_id, @PathVariable("loginaccount_id") String loginaccount_id) {
        LoginAccountDTO dto = new LoginAccountDTO();
        LoginAccount domain = loginaccountService.get(loginaccount_id);
		dto.fromDO(domain);
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @ApiOperation(value = "CheckKey", tags = {"LoginAccount" },  notes = "CheckKey")
	@RequestMapping(method = RequestMethod.POST, value = "/runtimemgr/users/{user_id}/loginaccounts/checkkey")
    public ResponseEntity<Boolean> checkKeyByUser(@PathVariable("user_id") String user_id, @RequestBody LoginAccountDTO loginaccountdto) {

        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @ApiOperation(value = "更新密码", tags = {"LoginAccount" },  notes = "更新密码")
	@RequestMapping(method = RequestMethod.PUT, value = "/runtimemgr/users/{user_id}/loginaccounts/{loginaccountloginaccountid}/updatepwd")

    public ResponseEntity<LoginAccountDTO> updatePWDByUser(@PathVariable("user_id") String user_id, @PathVariable("loginaccount_id") String loginaccount_id, @RequestBody LoginAccountDTO loginaccountdto) {
        LoginAccount loginaccount = loginaccountdto.toDO();
    	loginaccount = loginaccountService.updatePWD(loginaccount) ;
    	loginaccountdto.fromDO(loginaccount);
        return ResponseEntity.status(HttpStatus.OK).body(loginaccountdto);
    }

    @ApiOperation(value = "Save", tags = {"LoginAccount" },  notes = "Save")
	@RequestMapping(method = RequestMethod.POST, value = "/runtimemgr/users/{user_id}/loginaccounts/save")
    public ResponseEntity<Boolean> saveByUser(@PathVariable("user_id") String user_id, @RequestBody LoginAccountDTO loginaccountdto) {
        LoginAccount domain = loginaccountdto.toDO();
        domain.setUserId(user_id);
    	Boolean b = loginaccountService.save(domain) ;
        return ResponseEntity.status(HttpStatus.OK).body(b);
    }

    @ApiOperation(value = "Remove", tags = {"LoginAccount" },  notes = "Remove")
	@RequestMapping(method = RequestMethod.DELETE, value = "/runtimemgr/users/{user_id}/loginaccounts/{loginaccount_id}")

    public ResponseEntity<Boolean> removeByUser(@PathVariable("user_id") String user_id, @PathVariable("loginaccount_id") String loginaccount_id) {
        LoginAccountDTO loginaccountdto = new LoginAccountDTO();
		LoginAccount domain = new LoginAccount();
		loginaccountdto.setLoginAccountId(loginaccount_id);
		domain.setLoginAccountId(loginaccount_id);
        Boolean rst = loginaccountService.remove(domain.getLoginAccountId());
        if(rst){
			return ResponseEntity.status(HttpStatus.OK).body(rst);
		}else{
			return ResponseEntity.status(HttpStatus.OK).body(rst);
		}
    }

    @ApiOperation(value = "Create", tags = {"LoginAccount" },  notes = "Create")
	@RequestMapping(method = RequestMethod.POST, value = "/runtimemgr/users/{user_id}/loginaccounts")

    public ResponseEntity<LoginAccountDTO> createByUser(@PathVariable("user_id") String user_id, @RequestBody LoginAccountDTO loginaccountdto) {
        LoginAccountDTO dto = new LoginAccountDTO();
        LoginAccount domain = loginaccountdto.toDO();
        domain.setUserId(user_id);
		loginaccountService.create(domain);
        dto.fromDO(domain);
		return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @ApiOperation(value = "GetDraft", tags = {"LoginAccount" },  notes = "GetDraft")
	@RequestMapping(method = RequestMethod.GET, value = "/runtimemgr/users/{user_id}/loginaccounts/{loginaccountloginaccountid}/getdraft")

    public ResponseEntity<LoginAccountDTO> getDraftByUser(@PathVariable("user_id") String user_id, @PathVariable("loginaccount_id") String loginaccount_id, @RequestBody LoginAccountDTO loginaccountdto) {
        LoginAccount loginaccount = loginaccountdto.toDO();
    	loginaccount = loginaccountService.getDraft(loginaccount) ;
    	loginaccountdto.fromDO(loginaccount);
        return ResponseEntity.status(HttpStatus.OK).body(loginaccountdto);
    }

    @ApiOperation(value = "Update", tags = {"LoginAccount" },  notes = "Update")
	@RequestMapping(method = RequestMethod.PUT, value = "/runtimemgr/users/{user_id}/loginaccounts/{loginaccount_id}")

    public ResponseEntity<LoginAccountDTO> updateByUser(@PathVariable("user_id") String user_id, @PathVariable("loginaccount_id") String loginaccount_id, @RequestBody LoginAccountDTO loginaccountdto) {
		LoginAccount domain = loginaccountdto.toDO();
        domain.setUserId(user_id);
        domain.setLoginAccountId(loginaccount_id);
		loginaccountService.update(domain);
		LoginAccountDTO dto = new LoginAccountDTO();
		dto.fromDO(domain);
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

	@ApiOperation(value = "获取getLoginaccount", tags = {"LoginAccount" } ,notes = "获取getLoginaccount")
    @RequestMapping(method= RequestMethod.GET , value="/runtimemgr/users/{user_id}/loginaccounts/fetchgetloginaccount")
	public ResponseEntity<Page<LoginAccountDTO>> fetchLoginAccountGetLoginaccountByUser(@PathVariable("user_id") String user_id,LoginAccountSearchContext context,Pageable pageable ,ServletRequest request) {
        context.setPageable(pageable);
        List<LoginAccountDTO> list = new ArrayList<LoginAccountDTO>();
        context.setN_userid_eq(user_id);
        Page<LoginAccount> domains = loginaccountService.searchGetLoginaccount(context) ;
        for(LoginAccount loginaccount : domains.getContent()){
            LoginAccountDTO dto = new LoginAccountDTO();
            dto.fromDO(loginaccount);
            list.add(dto);
        }
	    return ResponseEntity.status(HttpStatus.OK).body(new PageImpl(list,context.getPageable(),domains.getTotalElements()));
	}

	@ApiOperation(value = "获取DEFAULT", tags = {"LoginAccount" } ,notes = "获取DEFAULT")
    @RequestMapping(method= RequestMethod.GET , value="/runtimemgr/users/{user_id}/loginaccounts/fetchdefault")
	public ResponseEntity<Page<LoginAccountDTO>> fetchLoginAccountDefault(@PathVariable("user_id") String user_id,LoginAccountSearchContext context,Pageable pageable ,ServletRequest request) {
        context.setPageable(pageable);
        List<LoginAccountDTO> list = new ArrayList<LoginAccountDTO>();
        context.setN_userid_eq(user_id);
        Page<LoginAccount> domains = loginaccountService.searchDefault(context) ;
        for(LoginAccount loginaccount : domains.getContent()){
            LoginAccountDTO dto = new LoginAccountDTO();
            dto.fromDO(loginaccount);
            list.add(dto);
        }
	    return ResponseEntity.status(HttpStatus.OK).body(new PageImpl(list,context.getPageable(),domains.getTotalElements()));
	}


}
