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.LoginAccountPwdDTO;
import cn.ibizlab.r7.runtime.core.r7rt_base.domain.LoginAccountPwd;
import cn.ibizlab.r7.runtime.core.r7rt_base.service.ILoginAccountPwdService;
import cn.ibizlab.r7.runtime.util.SearchContext;
import cn.ibizlab.r7.runtime.core.r7rt_base.filter.LoginAccountPwdSearchContext;

@Api(tags = {"LoginAccountPwd" })
@RestController
@RequestMapping("")
public class LoginAccountPwdResource {

    @Autowired
    private ILoginAccountPwdService loginaccountpwdService;

    public ILoginAccountPwdService getLoginaccountpwdService() {
        return this.loginaccountpwdService;
    }

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

    @ApiOperation(value = "Update", tags = {"LoginAccountPwd" },  notes = "Update")
	@RequestMapping(method = RequestMethod.PUT, value = "/runtimemgr/loginaccountpwds/{loginaccountpwd_id}")

    public ResponseEntity<LoginAccountPwdDTO> update(@PathVariable("loginaccountpwd_id") String loginaccountpwd_id, @RequestBody LoginAccountPwdDTO loginaccountpwddto) {
		LoginAccountPwd domain = loginaccountpwddto.toDO();
        domain.setLoginAccountPwdId(loginaccountpwd_id);
		loginaccountpwdService.update(domain);
		LoginAccountPwdDTO dto = new LoginAccountPwdDTO();
		dto.fromDO(domain);
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @ApiOperation(value = "Remove", tags = {"LoginAccountPwd" },  notes = "Remove")
	@RequestMapping(method = RequestMethod.DELETE, value = "/runtimemgr/loginaccountpwds/{loginaccountpwd_id}")

    public ResponseEntity<Boolean> remove(@PathVariable("loginaccountpwd_id") String loginaccountpwd_id) {
        LoginAccountPwdDTO loginaccountpwddto = new LoginAccountPwdDTO();
		LoginAccountPwd domain = new LoginAccountPwd();
		loginaccountpwddto.setLoginAccountPwdId(loginaccountpwd_id);
		domain.setLoginAccountPwdId(loginaccountpwd_id);
        Boolean rst = loginaccountpwdService.remove(domain.getLoginAccountPwdId());
        if(rst){
			return ResponseEntity.status(HttpStatus.OK).body(rst);
		}else{
			return ResponseEntity.status(HttpStatus.OK).body(rst);
		}
    }

    @ApiOperation(value = "Get", tags = {"LoginAccountPwd" },  notes = "Get")
	@RequestMapping(method = RequestMethod.GET, value = "/runtimemgr/loginaccountpwds/{loginaccountpwd_id}")
    public ResponseEntity<LoginAccountPwdDTO> get(@PathVariable("loginaccountpwd_id") String loginaccountpwd_id) {
        LoginAccountPwdDTO dto = new LoginAccountPwdDTO();
        LoginAccountPwd domain = loginaccountpwdService.get(loginaccountpwd_id);
		dto.fromDO(domain);
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @ApiOperation(value = "Save", tags = {"LoginAccountPwd" },  notes = "Save")
	@RequestMapping(method = RequestMethod.POST, value = "/runtimemgr/loginaccountpwds/{loginaccountpwd_id}/save")
    public ResponseEntity<Boolean> save(@RequestBody LoginAccountPwdDTO loginaccountpwddto) {
        LoginAccountPwd loginaccountpwd = loginaccountpwddto.toDO();
    	Boolean b = loginaccountpwdService.save(loginaccountpwd) ;
        return ResponseEntity.status(HttpStatus.OK).body(b);
    }

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

    public ResponseEntity<LoginAccountPwdDTO> create(@RequestBody LoginAccountPwdDTO loginaccountpwddto) {
        LoginAccountPwdDTO dto = new LoginAccountPwdDTO();
        LoginAccountPwd domain = loginaccountpwddto.toDO();
		loginaccountpwdService.create(domain);
        dto.fromDO(domain);
		return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @ApiOperation(value = "GetDraft", tags = {"LoginAccountPwd" },  notes = "GetDraft")
	@RequestMapping(method = RequestMethod.GET, value = "/runtimemgr/loginaccountpwds/{loginaccountpwd_id}/getdraft")

    public ResponseEntity<LoginAccountPwdDTO> getDraft(@PathVariable("loginaccountpwd_id") String loginaccountpwd_id, @RequestBody LoginAccountPwdDTO loginaccountpwddto) {
        LoginAccountPwd loginaccountpwd = loginaccountpwddto.toDO();
    	loginaccountpwd = loginaccountpwdService.getDraft(loginaccountpwd) ;
    	loginaccountpwddto.fromDO(loginaccountpwd);
        return ResponseEntity.status(HttpStatus.OK).body(loginaccountpwddto);
    }

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


}
