package cn.ibizlab.assetmanagement.assetapi.rest;

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 lombok.extern.slf4j.Slf4j;
import com.alibaba.fastjson.JSONObject;
import javax.servlet.ServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.beans.BeanCopier;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.http.HttpStatus;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.util.StringUtils;
import org.springframework.context.annotation.Lazy;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.access.prepost.PostAuthorize;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import cn.ibizlab.assetmanagement.assetapi.dto.*;
import cn.ibizlab.assetmanagement.assetapi.mapping.*;
import cn.ibizlab.assetmanagement.core.location.domain.EAMLocationStateModel;
import cn.ibizlab.assetmanagement.core.location.service.IEAMLocationStateModelService;
import cn.ibizlab.assetmanagement.core.location.filter.EAMLocationStateModelSearchContext;
import cn.ibizlab.assetmanagement.util.annotation.VersionCheck;

@Slf4j
@Api(tags = {"功能位置状态模型" })
@RestController("AssetApi-eamlocationstatemodel")
@RequestMapping("")
public class EAMLocationStateModelResource {

    @Autowired
    public IEAMLocationStateModelService eamlocationstatemodelService;

    @Autowired
    @Lazy
    public EAMLocationStateModelMapping eamlocationstatemodelMapping;

    @PreAuthorize("hasPermission(this.eamlocationstatemodelMapping.toDomain(#eamlocationstatemodeldto),'iBizAssetManagement-EAMLocationStateModel-Create')")
    @ApiOperation(value = "新建功能位置状态模型", tags = {"功能位置状态模型" },  notes = "新建功能位置状态模型")
	@RequestMapping(method = RequestMethod.POST, value = "/eamlocationstatemodels")
    @Transactional
    public ResponseEntity<EAMLocationStateModelDTO> create(@RequestBody EAMLocationStateModelDTO eamlocationstatemodeldto) {
        EAMLocationStateModel domain = eamlocationstatemodelMapping.toDomain(eamlocationstatemodeldto);
		eamlocationstatemodelService.create(domain);
        EAMLocationStateModelDTO dto = eamlocationstatemodelMapping.toDto(domain);
		return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @PreAuthorize("hasPermission(this.eamlocationstatemodelMapping.toDomain(#eamlocationstatemodeldtos),'iBizAssetManagement-EAMLocationStateModel-Create')")
    @ApiOperation(value = "批量新建功能位置状态模型", tags = {"功能位置状态模型" },  notes = "批量新建功能位置状态模型")
	@RequestMapping(method = RequestMethod.POST, value = "/eamlocationstatemodels/batch")
    public ResponseEntity<Boolean> createBatch(@RequestBody List<EAMLocationStateModelDTO> eamlocationstatemodeldtos) {
        eamlocationstatemodelService.createBatch(eamlocationstatemodelMapping.toDomain(eamlocationstatemodeldtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @VersionCheck(entity = "eamlocationstatemodel" , versionfield = "updatedate")
    @PreAuthorize("hasPermission(this.eamlocationstatemodelService.get(#eamlocationstatemodel_id),'iBizAssetManagement-EAMLocationStateModel-Update')")
    @ApiOperation(value = "更新功能位置状态模型", tags = {"功能位置状态模型" },  notes = "更新功能位置状态模型")
	@RequestMapping(method = RequestMethod.PUT, value = "/eamlocationstatemodels/{eamlocationstatemodel_id}")
    @Transactional
    public ResponseEntity<EAMLocationStateModelDTO> update(@PathVariable("eamlocationstatemodel_id") String eamlocationstatemodel_id, @RequestBody EAMLocationStateModelDTO eamlocationstatemodeldto) {
		EAMLocationStateModel domain  = eamlocationstatemodelMapping.toDomain(eamlocationstatemodeldto);
        domain .setEamlocationstatemodelid(eamlocationstatemodel_id);
		eamlocationstatemodelService.update(domain );
		EAMLocationStateModelDTO dto = eamlocationstatemodelMapping.toDto(domain );
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @PreAuthorize("hasPermission(this.eamlocationstatemodelService.getEamlocationstatemodelByEntities(this.eamlocationstatemodelMapping.toDomain(#eamlocationstatemodeldtos)),'iBizAssetManagement-EAMLocationStateModel-Update')")
    @ApiOperation(value = "批量更新功能位置状态模型", tags = {"功能位置状态模型" },  notes = "批量更新功能位置状态模型")
	@RequestMapping(method = RequestMethod.PUT, value = "/eamlocationstatemodels/batch")
    public ResponseEntity<Boolean> updateBatch(@RequestBody List<EAMLocationStateModelDTO> eamlocationstatemodeldtos) {
        eamlocationstatemodelService.updateBatch(eamlocationstatemodelMapping.toDomain(eamlocationstatemodeldtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @PreAuthorize("hasPermission(this.eamlocationstatemodelService.get(#eamlocationstatemodel_id),'iBizAssetManagement-EAMLocationStateModel-Remove')")
    @ApiOperation(value = "删除功能位置状态模型", tags = {"功能位置状态模型" },  notes = "删除功能位置状态模型")
	@RequestMapping(method = RequestMethod.DELETE, value = "/eamlocationstatemodels/{eamlocationstatemodel_id}")
    @Transactional
    public ResponseEntity<Boolean> remove(@PathVariable("eamlocationstatemodel_id") String eamlocationstatemodel_id) {
         return ResponseEntity.status(HttpStatus.OK).body(eamlocationstatemodelService.remove(eamlocationstatemodel_id));
    }

    @PreAuthorize("hasPermission(this.eamlocationstatemodelService.getEamlocationstatemodelByIds(#ids),'iBizAssetManagement-EAMLocationStateModel-Remove')")
    @ApiOperation(value = "批量删除功能位置状态模型", tags = {"功能位置状态模型" },  notes = "批量删除功能位置状态模型")
	@RequestMapping(method = RequestMethod.DELETE, value = "/eamlocationstatemodels/batch")
    public ResponseEntity<Boolean> removeBatch(@RequestBody List<String> ids) {
        eamlocationstatemodelService.removeBatch(ids);
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @PostAuthorize("hasPermission(this.eamlocationstatemodelMapping.toDomain(returnObject.body),'iBizAssetManagement-EAMLocationStateModel-Get')")
    @ApiOperation(value = "获取功能位置状态模型", tags = {"功能位置状态模型" },  notes = "获取功能位置状态模型")
	@RequestMapping(method = RequestMethod.GET, value = "/eamlocationstatemodels/{eamlocationstatemodel_id}")
    public ResponseEntity<EAMLocationStateModelDTO> get(@PathVariable("eamlocationstatemodel_id") String eamlocationstatemodel_id) {
        EAMLocationStateModel domain = eamlocationstatemodelService.get(eamlocationstatemodel_id);
        EAMLocationStateModelDTO dto = eamlocationstatemodelMapping.toDto(domain);
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @ApiOperation(value = "获取功能位置状态模型草稿", tags = {"功能位置状态模型" },  notes = "获取功能位置状态模型草稿")
	@RequestMapping(method = RequestMethod.GET, value = "/eamlocationstatemodels/getdraft")
    public ResponseEntity<EAMLocationStateModelDTO> getDraft() {
        return ResponseEntity.status(HttpStatus.OK).body(eamlocationstatemodelMapping.toDto(eamlocationstatemodelService.getDraft(new EAMLocationStateModel())));
    }

    @ApiOperation(value = "检查功能位置状态模型", tags = {"功能位置状态模型" },  notes = "检查功能位置状态模型")
	@RequestMapping(method = RequestMethod.POST, value = "/eamlocationstatemodels/checkkey")
    public ResponseEntity<Boolean> checkKey(@RequestBody EAMLocationStateModelDTO eamlocationstatemodeldto) {
        return  ResponseEntity.status(HttpStatus.OK).body(eamlocationstatemodelService.checkKey(eamlocationstatemodelMapping.toDomain(eamlocationstatemodeldto)));
    }

    @PreAuthorize("hasPermission(this.eamlocationstatemodelMapping.toDomain(#eamlocationstatemodeldto),'iBizAssetManagement-EAMLocationStateModel-Save')")
    @ApiOperation(value = "保存功能位置状态模型", tags = {"功能位置状态模型" },  notes = "保存功能位置状态模型")
	@RequestMapping(method = RequestMethod.POST, value = "/eamlocationstatemodels/save")
    public ResponseEntity<Boolean> save(@RequestBody EAMLocationStateModelDTO eamlocationstatemodeldto) {
        return ResponseEntity.status(HttpStatus.OK).body(eamlocationstatemodelService.save(eamlocationstatemodelMapping.toDomain(eamlocationstatemodeldto)));
    }

    @PreAuthorize("hasPermission(this.eamlocationstatemodelMapping.toDomain(#eamlocationstatemodeldtos),'iBizAssetManagement-EAMLocationStateModel-Save')")
    @ApiOperation(value = "批量保存功能位置状态模型", tags = {"功能位置状态模型" },  notes = "批量保存功能位置状态模型")
	@RequestMapping(method = RequestMethod.POST, value = "/eamlocationstatemodels/savebatch")
    public ResponseEntity<Boolean> saveBatch(@RequestBody List<EAMLocationStateModelDTO> eamlocationstatemodeldtos) {
        eamlocationstatemodelService.saveBatch(eamlocationstatemodelMapping.toDomain(eamlocationstatemodeldtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','iBizAssetManagement-EAMLocationStateModel-searchDefault-all') and hasPermission(#context,'iBizAssetManagement-EAMLocationStateModel-Get')")
	@ApiOperation(value = "获取DEFAULT", tags = {"功能位置状态模型" } ,notes = "获取DEFAULT")
    @RequestMapping(method= RequestMethod.GET , value="/eamlocationstatemodels/fetchdefault")
	public ResponseEntity<List<EAMLocationStateModelDTO>> fetchDefault(EAMLocationStateModelSearchContext context) {
        Page<EAMLocationStateModel> domains = eamlocationstatemodelService.searchDefault(context) ;
        List<EAMLocationStateModelDTO> list = eamlocationstatemodelMapping.toDto(domains.getContent());
        return ResponseEntity.status(HttpStatus.OK)
                .header("x-page", String.valueOf(context.getPageable().getPageNumber()))
                .header("x-per-page", String.valueOf(context.getPageable().getPageSize()))
                .header("x-total", String.valueOf(domains.getTotalElements()))
                .body(list);
	}

    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','iBizAssetManagement-EAMLocationStateModel-searchDefault-all') and hasPermission(#context,'iBizAssetManagement-EAMLocationStateModel-Get')")
	@ApiOperation(value = "查询DEFAULT", tags = {"功能位置状态模型" } ,notes = "查询DEFAULT")
    @RequestMapping(method= RequestMethod.POST , value="/eamlocationstatemodels/searchdefault")
	public ResponseEntity<Page<EAMLocationStateModelDTO>> searchDefault(@RequestBody EAMLocationStateModelSearchContext context) {
        Page<EAMLocationStateModel> domains = eamlocationstatemodelService.searchDefault(context) ;
	    return ResponseEntity.status(HttpStatus.OK)
                .body(new PageImpl(eamlocationstatemodelMapping.toDto(domains.getContent()), context.getPageable(), domains.getTotalElements()));
	}
}

