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.asset.domain.EAMAssetStateModel;
import cn.ibizlab.assetmanagement.core.asset.service.IEAMAssetStateModelService;
import cn.ibizlab.assetmanagement.core.asset.filter.EAMAssetStateModelSearchContext;
import cn.ibizlab.assetmanagement.util.annotation.VersionCheck;

@Slf4j
@Api(tags = {"资产状态模型" })
@RestController("AssetApi-eamassetstatemodel")
@RequestMapping("")
public class EAMAssetStateModelResource {

    @Autowired
    public IEAMAssetStateModelService eamassetstatemodelService;

    @Autowired
    @Lazy
    public EAMAssetStateModelMapping eamassetstatemodelMapping;

    @PreAuthorize("hasPermission(this.eamassetstatemodelMapping.toDomain(#eamassetstatemodeldto),'iBizAssetManagement-EAMAssetStateModel-Create')")
    @ApiOperation(value = "新建资产状态模型", tags = {"资产状态模型" },  notes = "新建资产状态模型")
	@RequestMapping(method = RequestMethod.POST, value = "/eamassetstatemodels")
    @Transactional
    public ResponseEntity<EAMAssetStateModelDTO> create(@RequestBody EAMAssetStateModelDTO eamassetstatemodeldto) {
        EAMAssetStateModel domain = eamassetstatemodelMapping.toDomain(eamassetstatemodeldto);
		eamassetstatemodelService.create(domain);
        EAMAssetStateModelDTO dto = eamassetstatemodelMapping.toDto(domain);
		return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @PreAuthorize("hasPermission(this.eamassetstatemodelMapping.toDomain(#eamassetstatemodeldtos),'iBizAssetManagement-EAMAssetStateModel-Create')")
    @ApiOperation(value = "批量新建资产状态模型", tags = {"资产状态模型" },  notes = "批量新建资产状态模型")
	@RequestMapping(method = RequestMethod.POST, value = "/eamassetstatemodels/batch")
    public ResponseEntity<Boolean> createBatch(@RequestBody List<EAMAssetStateModelDTO> eamassetstatemodeldtos) {
        eamassetstatemodelService.createBatch(eamassetstatemodelMapping.toDomain(eamassetstatemodeldtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @VersionCheck(entity = "eamassetstatemodel" , versionfield = "updatedate")
    @PreAuthorize("hasPermission(this.eamassetstatemodelService.get(#eamassetstatemodel_id),'iBizAssetManagement-EAMAssetStateModel-Update')")
    @ApiOperation(value = "更新资产状态模型", tags = {"资产状态模型" },  notes = "更新资产状态模型")
	@RequestMapping(method = RequestMethod.PUT, value = "/eamassetstatemodels/{eamassetstatemodel_id}")
    @Transactional
    public ResponseEntity<EAMAssetStateModelDTO> update(@PathVariable("eamassetstatemodel_id") String eamassetstatemodel_id, @RequestBody EAMAssetStateModelDTO eamassetstatemodeldto) {
		EAMAssetStateModel domain  = eamassetstatemodelMapping.toDomain(eamassetstatemodeldto);
        domain .setEamassetstatemodelid(eamassetstatemodel_id);
		eamassetstatemodelService.update(domain );
		EAMAssetStateModelDTO dto = eamassetstatemodelMapping.toDto(domain );
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @PreAuthorize("hasPermission(this.eamassetstatemodelService.getEamassetstatemodelByEntities(this.eamassetstatemodelMapping.toDomain(#eamassetstatemodeldtos)),'iBizAssetManagement-EAMAssetStateModel-Update')")
    @ApiOperation(value = "批量更新资产状态模型", tags = {"资产状态模型" },  notes = "批量更新资产状态模型")
	@RequestMapping(method = RequestMethod.PUT, value = "/eamassetstatemodels/batch")
    public ResponseEntity<Boolean> updateBatch(@RequestBody List<EAMAssetStateModelDTO> eamassetstatemodeldtos) {
        eamassetstatemodelService.updateBatch(eamassetstatemodelMapping.toDomain(eamassetstatemodeldtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @PreAuthorize("hasPermission(this.eamassetstatemodelService.get(#eamassetstatemodel_id),'iBizAssetManagement-EAMAssetStateModel-Remove')")
    @ApiOperation(value = "删除资产状态模型", tags = {"资产状态模型" },  notes = "删除资产状态模型")
	@RequestMapping(method = RequestMethod.DELETE, value = "/eamassetstatemodels/{eamassetstatemodel_id}")
    @Transactional
    public ResponseEntity<Boolean> remove(@PathVariable("eamassetstatemodel_id") String eamassetstatemodel_id) {
         return ResponseEntity.status(HttpStatus.OK).body(eamassetstatemodelService.remove(eamassetstatemodel_id));
    }

    @PreAuthorize("hasPermission(this.eamassetstatemodelService.getEamassetstatemodelByIds(#ids),'iBizAssetManagement-EAMAssetStateModel-Remove')")
    @ApiOperation(value = "批量删除资产状态模型", tags = {"资产状态模型" },  notes = "批量删除资产状态模型")
	@RequestMapping(method = RequestMethod.DELETE, value = "/eamassetstatemodels/batch")
    public ResponseEntity<Boolean> removeBatch(@RequestBody List<String> ids) {
        eamassetstatemodelService.removeBatch(ids);
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @PostAuthorize("hasPermission(this.eamassetstatemodelMapping.toDomain(returnObject.body),'iBizAssetManagement-EAMAssetStateModel-Get')")
    @ApiOperation(value = "获取资产状态模型", tags = {"资产状态模型" },  notes = "获取资产状态模型")
	@RequestMapping(method = RequestMethod.GET, value = "/eamassetstatemodels/{eamassetstatemodel_id}")
    public ResponseEntity<EAMAssetStateModelDTO> get(@PathVariable("eamassetstatemodel_id") String eamassetstatemodel_id) {
        EAMAssetStateModel domain = eamassetstatemodelService.get(eamassetstatemodel_id);
        EAMAssetStateModelDTO dto = eamassetstatemodelMapping.toDto(domain);
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @ApiOperation(value = "获取资产状态模型草稿", tags = {"资产状态模型" },  notes = "获取资产状态模型草稿")
	@RequestMapping(method = RequestMethod.GET, value = "/eamassetstatemodels/getdraft")
    public ResponseEntity<EAMAssetStateModelDTO> getDraft() {
        return ResponseEntity.status(HttpStatus.OK).body(eamassetstatemodelMapping.toDto(eamassetstatemodelService.getDraft(new EAMAssetStateModel())));
    }

    @ApiOperation(value = "检查资产状态模型", tags = {"资产状态模型" },  notes = "检查资产状态模型")
	@RequestMapping(method = RequestMethod.POST, value = "/eamassetstatemodels/checkkey")
    public ResponseEntity<Boolean> checkKey(@RequestBody EAMAssetStateModelDTO eamassetstatemodeldto) {
        return  ResponseEntity.status(HttpStatus.OK).body(eamassetstatemodelService.checkKey(eamassetstatemodelMapping.toDomain(eamassetstatemodeldto)));
    }

    @PreAuthorize("hasPermission(this.eamassetstatemodelMapping.toDomain(#eamassetstatemodeldto),'iBizAssetManagement-EAMAssetStateModel-Save')")
    @ApiOperation(value = "保存资产状态模型", tags = {"资产状态模型" },  notes = "保存资产状态模型")
	@RequestMapping(method = RequestMethod.POST, value = "/eamassetstatemodels/save")
    public ResponseEntity<Boolean> save(@RequestBody EAMAssetStateModelDTO eamassetstatemodeldto) {
        return ResponseEntity.status(HttpStatus.OK).body(eamassetstatemodelService.save(eamassetstatemodelMapping.toDomain(eamassetstatemodeldto)));
    }

    @PreAuthorize("hasPermission(this.eamassetstatemodelMapping.toDomain(#eamassetstatemodeldtos),'iBizAssetManagement-EAMAssetStateModel-Save')")
    @ApiOperation(value = "批量保存资产状态模型", tags = {"资产状态模型" },  notes = "批量保存资产状态模型")
	@RequestMapping(method = RequestMethod.POST, value = "/eamassetstatemodels/savebatch")
    public ResponseEntity<Boolean> saveBatch(@RequestBody List<EAMAssetStateModelDTO> eamassetstatemodeldtos) {
        eamassetstatemodelService.saveBatch(eamassetstatemodelMapping.toDomain(eamassetstatemodeldtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','iBizAssetManagement-EAMAssetStateModel-searchDefault-all') and hasPermission(#context,'iBizAssetManagement-EAMAssetStateModel-Get')")
	@ApiOperation(value = "获取DEFAULT", tags = {"资产状态模型" } ,notes = "获取DEFAULT")
    @RequestMapping(method= RequestMethod.GET , value="/eamassetstatemodels/fetchdefault")
	public ResponseEntity<List<EAMAssetStateModelDTO>> fetchDefault(EAMAssetStateModelSearchContext context) {
        Page<EAMAssetStateModel> domains = eamassetstatemodelService.searchDefault(context) ;
        List<EAMAssetStateModelDTO> list = eamassetstatemodelMapping.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-EAMAssetStateModel-searchDefault-all') and hasPermission(#context,'iBizAssetManagement-EAMAssetStateModel-Get')")
	@ApiOperation(value = "查询DEFAULT", tags = {"资产状态模型" } ,notes = "查询DEFAULT")
    @RequestMapping(method= RequestMethod.POST , value="/eamassetstatemodels/searchdefault")
	public ResponseEntity<Page<EAMAssetStateModelDTO>> searchDefault(@RequestBody EAMAssetStateModelSearchContext context) {
        Page<EAMAssetStateModel> domains = eamassetstatemodelService.searchDefault(context) ;
	    return ResponseEntity.status(HttpStatus.OK)
                .body(new PageImpl(eamassetstatemodelMapping.toDto(domains.getContent()), context.getPageable(), domains.getTotalElements()));
	}
}

