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.EAMAsset;
import cn.ibizlab.assetmanagement.core.asset.service.IEAMAssetService;
import cn.ibizlab.assetmanagement.core.asset.filter.EAMAssetSearchContext;
import cn.ibizlab.assetmanagement.util.annotation.VersionCheck;

@Slf4j
@Api(tags = {"资产" })
@RestController("AssetApi-eamasset")
@RequestMapping("")
public class EAMAssetResource {

    @Autowired
    public IEAMAssetService eamassetService;

    @Autowired
    @Lazy
    public EAMAssetMapping eamassetMapping;

    @PreAuthorize("hasPermission(this.eamassetMapping.toDomain(#eamassetdto),'iBizAssetManagement-EAMAsset-Create')")
    @ApiOperation(value = "新建资产", tags = {"资产" },  notes = "新建资产")
	@RequestMapping(method = RequestMethod.POST, value = "/eamassets")
    @Transactional
    public ResponseEntity<EAMAssetDTO> create(@RequestBody EAMAssetDTO eamassetdto) {
        EAMAsset domain = eamassetMapping.toDomain(eamassetdto);
		eamassetService.create(domain);
        EAMAssetDTO dto = eamassetMapping.toDto(domain);
		return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @PreAuthorize("hasPermission(this.eamassetMapping.toDomain(#eamassetdtos),'iBizAssetManagement-EAMAsset-Create')")
    @ApiOperation(value = "批量新建资产", tags = {"资产" },  notes = "批量新建资产")
	@RequestMapping(method = RequestMethod.POST, value = "/eamassets/batch")
    public ResponseEntity<Boolean> createBatch(@RequestBody List<EAMAssetDTO> eamassetdtos) {
        eamassetService.createBatch(eamassetMapping.toDomain(eamassetdtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @VersionCheck(entity = "eamasset" , versionfield = "updatedate")
    @PreAuthorize("hasPermission(this.eamassetService.get(#eamasset_id),'iBizAssetManagement-EAMAsset-Update')")
    @ApiOperation(value = "更新资产", tags = {"资产" },  notes = "更新资产")
	@RequestMapping(method = RequestMethod.PUT, value = "/eamassets/{eamasset_id}")
    @Transactional
    public ResponseEntity<EAMAssetDTO> update(@PathVariable("eamasset_id") String eamasset_id, @RequestBody EAMAssetDTO eamassetdto) {
		EAMAsset domain  = eamassetMapping.toDomain(eamassetdto);
        domain .setAssetid(eamasset_id);
		eamassetService.update(domain );
		EAMAssetDTO dto = eamassetMapping.toDto(domain );
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @PreAuthorize("hasPermission(this.eamassetService.getEamassetByEntities(this.eamassetMapping.toDomain(#eamassetdtos)),'iBizAssetManagement-EAMAsset-Update')")
    @ApiOperation(value = "批量更新资产", tags = {"资产" },  notes = "批量更新资产")
	@RequestMapping(method = RequestMethod.PUT, value = "/eamassets/batch")
    public ResponseEntity<Boolean> updateBatch(@RequestBody List<EAMAssetDTO> eamassetdtos) {
        eamassetService.updateBatch(eamassetMapping.toDomain(eamassetdtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @PreAuthorize("hasPermission(this.eamassetService.get(#eamasset_id),'iBizAssetManagement-EAMAsset-Remove')")
    @ApiOperation(value = "删除资产", tags = {"资产" },  notes = "删除资产")
	@RequestMapping(method = RequestMethod.DELETE, value = "/eamassets/{eamasset_id}")
    @Transactional
    public ResponseEntity<Boolean> remove(@PathVariable("eamasset_id") String eamasset_id) {
         return ResponseEntity.status(HttpStatus.OK).body(eamassetService.remove(eamasset_id));
    }

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

    @PostAuthorize("hasPermission(this.eamassetMapping.toDomain(returnObject.body),'iBizAssetManagement-EAMAsset-Get')")
    @ApiOperation(value = "获取资产", tags = {"资产" },  notes = "获取资产")
	@RequestMapping(method = RequestMethod.GET, value = "/eamassets/{eamasset_id}")
    public ResponseEntity<EAMAssetDTO> get(@PathVariable("eamasset_id") String eamasset_id) {
        EAMAsset domain = eamassetService.get(eamasset_id);
        EAMAssetDTO dto = eamassetMapping.toDto(domain);
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @ApiOperation(value = "获取资产草稿", tags = {"资产" },  notes = "获取资产草稿")
	@RequestMapping(method = RequestMethod.GET, value = "/eamassets/getdraft")
    public ResponseEntity<EAMAssetDTO> getDraft() {
        return ResponseEntity.status(HttpStatus.OK).body(eamassetMapping.toDto(eamassetService.getDraft(new EAMAsset())));
    }

    @ApiOperation(value = "检查资产", tags = {"资产" },  notes = "检查资产")
	@RequestMapping(method = RequestMethod.POST, value = "/eamassets/checkkey")
    public ResponseEntity<Boolean> checkKey(@RequestBody EAMAssetDTO eamassetdto) {
        return  ResponseEntity.status(HttpStatus.OK).body(eamassetService.checkKey(eamassetMapping.toDomain(eamassetdto)));
    }

    @PreAuthorize("hasPermission(this.eamassetMapping.toDomain(#eamassetdto),'iBizAssetManagement-EAMAsset-Save')")
    @ApiOperation(value = "保存资产", tags = {"资产" },  notes = "保存资产")
	@RequestMapping(method = RequestMethod.POST, value = "/eamassets/save")
    public ResponseEntity<Boolean> save(@RequestBody EAMAssetDTO eamassetdto) {
        return ResponseEntity.status(HttpStatus.OK).body(eamassetService.save(eamassetMapping.toDomain(eamassetdto)));
    }

    @PreAuthorize("hasPermission(this.eamassetMapping.toDomain(#eamassetdtos),'iBizAssetManagement-EAMAsset-Save')")
    @ApiOperation(value = "批量保存资产", tags = {"资产" },  notes = "批量保存资产")
	@RequestMapping(method = RequestMethod.POST, value = "/eamassets/savebatch")
    public ResponseEntity<Boolean> saveBatch(@RequestBody List<EAMAssetDTO> eamassetdtos) {
        eamassetService.saveBatch(eamassetMapping.toDomain(eamassetdtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','iBizAssetManagement-EAMAsset-searchDefault-all') and hasPermission(#context,'iBizAssetManagement-EAMAsset-Get')")
	@ApiOperation(value = "获取DEFAULT", tags = {"资产" } ,notes = "获取DEFAULT")
    @RequestMapping(method= RequestMethod.GET , value="/eamassets/fetchdefault")
	public ResponseEntity<List<EAMAssetDTO>> fetchDefault(EAMAssetSearchContext context) {
        Page<EAMAsset> domains = eamassetService.searchDefault(context) ;
        List<EAMAssetDTO> list = eamassetMapping.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-EAMAsset-searchDefault-all') and hasPermission(#context,'iBizAssetManagement-EAMAsset-Get')")
	@ApiOperation(value = "查询DEFAULT", tags = {"资产" } ,notes = "查询DEFAULT")
    @RequestMapping(method= RequestMethod.POST , value="/eamassets/searchdefault")
	public ResponseEntity<Page<EAMAssetDTO>> searchDefault(@RequestBody EAMAssetSearchContext context) {
        Page<EAMAsset> domains = eamassetService.searchDefault(context) ;
	    return ResponseEntity.status(HttpStatus.OK)
                .body(new PageImpl(eamassetMapping.toDto(domains.getContent()), context.getPageable(), domains.getTotalElements()));
	}
    @PreAuthorize("hasPermission(this.eamassetMapping.toDomain(#eamassetdto),'iBizAssetManagement-EAMAsset-Create')")
    @ApiOperation(value = "根据功能位置建立资产", tags = {"资产" },  notes = "根据功能位置建立资产")
	@RequestMapping(method = RequestMethod.POST, value = "/eamlocations/{eamlocation_id}/eamassets")
    @Transactional
    public ResponseEntity<EAMAssetDTO> createByEAMLocation(@PathVariable("eamlocation_id") String eamlocation_id, @RequestBody EAMAssetDTO eamassetdto) {
        EAMAsset domain = eamassetMapping.toDomain(eamassetdto);
        domain.setEamlocationid(eamlocation_id);
		eamassetService.create(domain);
        EAMAssetDTO dto = eamassetMapping.toDto(domain);
		return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @PreAuthorize("hasPermission(this.eamassetMapping.toDomain(#eamassetdtos),'iBizAssetManagement-EAMAsset-Create')")
    @ApiOperation(value = "根据功能位置批量建立资产", tags = {"资产" },  notes = "根据功能位置批量建立资产")
	@RequestMapping(method = RequestMethod.POST, value = "/eamlocations/{eamlocation_id}/eamassets/batch")
    public ResponseEntity<Boolean> createBatchByEAMLocation(@PathVariable("eamlocation_id") String eamlocation_id, @RequestBody List<EAMAssetDTO> eamassetdtos) {
        List<EAMAsset> domainlist=eamassetMapping.toDomain(eamassetdtos);
        for(EAMAsset domain:domainlist){
            domain.setEamlocationid(eamlocation_id);
        }
        eamassetService.createBatch(domainlist);
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @VersionCheck(entity = "eamasset" , versionfield = "updatedate")
    @PreAuthorize("hasPermission(this.eamassetService.get(#eamasset_id),'iBizAssetManagement-EAMAsset-Update')")
    @ApiOperation(value = "根据功能位置更新资产", tags = {"资产" },  notes = "根据功能位置更新资产")
	@RequestMapping(method = RequestMethod.PUT, value = "/eamlocations/{eamlocation_id}/eamassets/{eamasset_id}")
    @Transactional
    public ResponseEntity<EAMAssetDTO> updateByEAMLocation(@PathVariable("eamlocation_id") String eamlocation_id, @PathVariable("eamasset_id") String eamasset_id, @RequestBody EAMAssetDTO eamassetdto) {
        EAMAsset domain = eamassetMapping.toDomain(eamassetdto);
        domain.setEamlocationid(eamlocation_id);
        domain.setAssetid(eamasset_id);
		eamassetService.update(domain);
        EAMAssetDTO dto = eamassetMapping.toDto(domain);
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @PreAuthorize("hasPermission(this.eamassetService.getEamassetByEntities(this.eamassetMapping.toDomain(#eamassetdtos)),'iBizAssetManagement-EAMAsset-Update')")
    @ApiOperation(value = "根据功能位置批量更新资产", tags = {"资产" },  notes = "根据功能位置批量更新资产")
	@RequestMapping(method = RequestMethod.PUT, value = "/eamlocations/{eamlocation_id}/eamassets/batch")
    public ResponseEntity<Boolean> updateBatchByEAMLocation(@PathVariable("eamlocation_id") String eamlocation_id, @RequestBody List<EAMAssetDTO> eamassetdtos) {
        List<EAMAsset> domainlist=eamassetMapping.toDomain(eamassetdtos);
        for(EAMAsset domain:domainlist){
            domain.setEamlocationid(eamlocation_id);
        }
        eamassetService.updateBatch(domainlist);
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @PreAuthorize("hasPermission(this.eamassetService.get(#eamasset_id),'iBizAssetManagement-EAMAsset-Remove')")
    @ApiOperation(value = "根据功能位置删除资产", tags = {"资产" },  notes = "根据功能位置删除资产")
	@RequestMapping(method = RequestMethod.DELETE, value = "/eamlocations/{eamlocation_id}/eamassets/{eamasset_id}")
    @Transactional
    public ResponseEntity<Boolean> removeByEAMLocation(@PathVariable("eamlocation_id") String eamlocation_id, @PathVariable("eamasset_id") String eamasset_id) {
		return ResponseEntity.status(HttpStatus.OK).body(eamassetService.remove(eamasset_id));
    }

    @PreAuthorize("hasPermission(this.eamassetService.getEamassetByIds(#ids),'iBizAssetManagement-EAMAsset-Remove')")
    @ApiOperation(value = "根据功能位置批量删除资产", tags = {"资产" },  notes = "根据功能位置批量删除资产")
	@RequestMapping(method = RequestMethod.DELETE, value = "/eamlocations/{eamlocation_id}/eamassets/batch")
    public ResponseEntity<Boolean> removeBatchByEAMLocation(@RequestBody List<String> ids) {
        eamassetService.removeBatch(ids);
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @PostAuthorize("hasPermission(this.eamassetMapping.toDomain(returnObject.body),'iBizAssetManagement-EAMAsset-Get')")
    @ApiOperation(value = "根据功能位置获取资产", tags = {"资产" },  notes = "根据功能位置获取资产")
	@RequestMapping(method = RequestMethod.GET, value = "/eamlocations/{eamlocation_id}/eamassets/{eamasset_id}")
    public ResponseEntity<EAMAssetDTO> getByEAMLocation(@PathVariable("eamlocation_id") String eamlocation_id, @PathVariable("eamasset_id") String eamasset_id) {
        EAMAsset domain = eamassetService.get(eamasset_id);
        EAMAssetDTO dto = eamassetMapping.toDto(domain);
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @ApiOperation(value = "根据功能位置获取资产草稿", tags = {"资产" },  notes = "根据功能位置获取资产草稿")
    @RequestMapping(method = RequestMethod.GET, value = "/eamlocations/{eamlocation_id}/eamassets/getdraft")
    public ResponseEntity<EAMAssetDTO> getDraftByEAMLocation(@PathVariable("eamlocation_id") String eamlocation_id) {
        EAMAsset domain = new EAMAsset();
        domain.setEamlocationid(eamlocation_id);
        return ResponseEntity.status(HttpStatus.OK).body(eamassetMapping.toDto(eamassetService.getDraft(domain)));
    }

    @ApiOperation(value = "根据功能位置检查资产", tags = {"资产" },  notes = "根据功能位置检查资产")
	@RequestMapping(method = RequestMethod.POST, value = "/eamlocations/{eamlocation_id}/eamassets/checkkey")
    public ResponseEntity<Boolean> checkKeyByEAMLocation(@PathVariable("eamlocation_id") String eamlocation_id, @RequestBody EAMAssetDTO eamassetdto) {
        return  ResponseEntity.status(HttpStatus.OK).body(eamassetService.checkKey(eamassetMapping.toDomain(eamassetdto)));
    }

    @PreAuthorize("hasPermission(this.eamassetMapping.toDomain(#eamassetdto),'iBizAssetManagement-EAMAsset-Save')")
    @ApiOperation(value = "根据功能位置保存资产", tags = {"资产" },  notes = "根据功能位置保存资产")
	@RequestMapping(method = RequestMethod.POST, value = "/eamlocations/{eamlocation_id}/eamassets/save")
    public ResponseEntity<Boolean> saveByEAMLocation(@PathVariable("eamlocation_id") String eamlocation_id, @RequestBody EAMAssetDTO eamassetdto) {
        EAMAsset domain = eamassetMapping.toDomain(eamassetdto);
        domain.setEamlocationid(eamlocation_id);
        return ResponseEntity.status(HttpStatus.OK).body(eamassetService.save(domain));
    }

    @PreAuthorize("hasPermission(this.eamassetMapping.toDomain(#eamassetdtos),'iBizAssetManagement-EAMAsset-Save')")
    @ApiOperation(value = "根据功能位置批量保存资产", tags = {"资产" },  notes = "根据功能位置批量保存资产")
	@RequestMapping(method = RequestMethod.POST, value = "/eamlocations/{eamlocation_id}/eamassets/savebatch")
    public ResponseEntity<Boolean> saveBatchByEAMLocation(@PathVariable("eamlocation_id") String eamlocation_id, @RequestBody List<EAMAssetDTO> eamassetdtos) {
        List<EAMAsset> domainlist=eamassetMapping.toDomain(eamassetdtos);
        for(EAMAsset domain:domainlist){
             domain.setEamlocationid(eamlocation_id);
        }
        eamassetService.saveBatch(domainlist);
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','iBizAssetManagement-EAMAsset-searchDefault-all') and hasPermission(#context,'iBizAssetManagement-EAMAsset-Get')")
	@ApiOperation(value = "根据功能位置获取DEFAULT", tags = {"资产" } ,notes = "根据功能位置获取DEFAULT")
    @RequestMapping(method= RequestMethod.GET , value="/eamlocations/{eamlocation_id}/eamassets/fetchdefault")
	public ResponseEntity<List<EAMAssetDTO>> fetchEAMAssetDefaultByEAMLocation(@PathVariable("eamlocation_id") String eamlocation_id,EAMAssetSearchContext context) {
        context.setN_eamlocationid_eq(eamlocation_id);
        Page<EAMAsset> domains = eamassetService.searchDefault(context) ;
        List<EAMAssetDTO> list = eamassetMapping.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-EAMAsset-searchDefault-all') and hasPermission(#context,'iBizAssetManagement-EAMAsset-Get')")
	@ApiOperation(value = "根据功能位置查询DEFAULT", tags = {"资产" } ,notes = "根据功能位置查询DEFAULT")
    @RequestMapping(method= RequestMethod.POST , value="/eamlocations/{eamlocation_id}/eamassets/searchdefault")
	public ResponseEntity<Page<EAMAssetDTO>> searchEAMAssetDefaultByEAMLocation(@PathVariable("eamlocation_id") String eamlocation_id, @RequestBody EAMAssetSearchContext context) {
        context.setN_eamlocationid_eq(eamlocation_id);
        Page<EAMAsset> domains = eamassetService.searchDefault(context) ;
	    return ResponseEntity.status(HttpStatus.OK)
                .body(new PageImpl(eamassetMapping.toDto(domains.getContent()), context.getPageable(), domains.getTotalElements()));
	}
}

