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.EAMAssetState;
import cn.ibizlab.assetmanagement.core.asset.service.IEAMAssetStateService;
import cn.ibizlab.assetmanagement.core.asset.filter.EAMAssetStateSearchContext;
import cn.ibizlab.assetmanagement.util.annotation.VersionCheck;

@Slf4j
@Api(tags = {"资产状态" })
@RestController("AssetApi-eamassetstate")
@RequestMapping("")
public class EAMAssetStateResource {

    @Autowired
    public IEAMAssetStateService eamassetstateService;

    @Autowired
    @Lazy
    public EAMAssetStateMapping eamassetstateMapping;

    @PreAuthorize("hasPermission(this.eamassetstateMapping.toDomain(#eamassetstatedto),'iBizAssetManagement-EAMAssetState-Create')")
    @ApiOperation(value = "新建资产状态", tags = {"资产状态" },  notes = "新建资产状态")
	@RequestMapping(method = RequestMethod.POST, value = "/eamassetstates")
    @Transactional
    public ResponseEntity<EAMAssetStateDTO> create(@RequestBody EAMAssetStateDTO eamassetstatedto) {
        EAMAssetState domain = eamassetstateMapping.toDomain(eamassetstatedto);
		eamassetstateService.create(domain);
        EAMAssetStateDTO dto = eamassetstateMapping.toDto(domain);
		return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @PreAuthorize("hasPermission(this.eamassetstateMapping.toDomain(#eamassetstatedtos),'iBizAssetManagement-EAMAssetState-Create')")
    @ApiOperation(value = "批量新建资产状态", tags = {"资产状态" },  notes = "批量新建资产状态")
	@RequestMapping(method = RequestMethod.POST, value = "/eamassetstates/batch")
    public ResponseEntity<Boolean> createBatch(@RequestBody List<EAMAssetStateDTO> eamassetstatedtos) {
        eamassetstateService.createBatch(eamassetstateMapping.toDomain(eamassetstatedtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @VersionCheck(entity = "eamassetstate" , versionfield = "updatedate")
    @PreAuthorize("hasPermission(this.eamassetstateService.get(#eamassetstate_id),'iBizAssetManagement-EAMAssetState-Update')")
    @ApiOperation(value = "更新资产状态", tags = {"资产状态" },  notes = "更新资产状态")
	@RequestMapping(method = RequestMethod.PUT, value = "/eamassetstates/{eamassetstate_id}")
    @Transactional
    public ResponseEntity<EAMAssetStateDTO> update(@PathVariable("eamassetstate_id") String eamassetstate_id, @RequestBody EAMAssetStateDTO eamassetstatedto) {
		EAMAssetState domain  = eamassetstateMapping.toDomain(eamassetstatedto);
        domain .setEamassetstateid(eamassetstate_id);
		eamassetstateService.update(domain );
		EAMAssetStateDTO dto = eamassetstateMapping.toDto(domain );
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @PreAuthorize("hasPermission(this.eamassetstateService.getEamassetstateByEntities(this.eamassetstateMapping.toDomain(#eamassetstatedtos)),'iBizAssetManagement-EAMAssetState-Update')")
    @ApiOperation(value = "批量更新资产状态", tags = {"资产状态" },  notes = "批量更新资产状态")
	@RequestMapping(method = RequestMethod.PUT, value = "/eamassetstates/batch")
    public ResponseEntity<Boolean> updateBatch(@RequestBody List<EAMAssetStateDTO> eamassetstatedtos) {
        eamassetstateService.updateBatch(eamassetstateMapping.toDomain(eamassetstatedtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

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

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

    @PostAuthorize("hasPermission(this.eamassetstateMapping.toDomain(returnObject.body),'iBizAssetManagement-EAMAssetState-Get')")
    @ApiOperation(value = "获取资产状态", tags = {"资产状态" },  notes = "获取资产状态")
	@RequestMapping(method = RequestMethod.GET, value = "/eamassetstates/{eamassetstate_id}")
    public ResponseEntity<EAMAssetStateDTO> get(@PathVariable("eamassetstate_id") String eamassetstate_id) {
        EAMAssetState domain = eamassetstateService.get(eamassetstate_id);
        EAMAssetStateDTO dto = eamassetstateMapping.toDto(domain);
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @ApiOperation(value = "获取资产状态草稿", tags = {"资产状态" },  notes = "获取资产状态草稿")
	@RequestMapping(method = RequestMethod.GET, value = "/eamassetstates/getdraft")
    public ResponseEntity<EAMAssetStateDTO> getDraft() {
        return ResponseEntity.status(HttpStatus.OK).body(eamassetstateMapping.toDto(eamassetstateService.getDraft(new EAMAssetState())));
    }

    @ApiOperation(value = "检查资产状态", tags = {"资产状态" },  notes = "检查资产状态")
	@RequestMapping(method = RequestMethod.POST, value = "/eamassetstates/checkkey")
    public ResponseEntity<Boolean> checkKey(@RequestBody EAMAssetStateDTO eamassetstatedto) {
        return  ResponseEntity.status(HttpStatus.OK).body(eamassetstateService.checkKey(eamassetstateMapping.toDomain(eamassetstatedto)));
    }

    @PreAuthorize("hasPermission(this.eamassetstateMapping.toDomain(#eamassetstatedto),'iBizAssetManagement-EAMAssetState-Save')")
    @ApiOperation(value = "保存资产状态", tags = {"资产状态" },  notes = "保存资产状态")
	@RequestMapping(method = RequestMethod.POST, value = "/eamassetstates/save")
    public ResponseEntity<Boolean> save(@RequestBody EAMAssetStateDTO eamassetstatedto) {
        return ResponseEntity.status(HttpStatus.OK).body(eamassetstateService.save(eamassetstateMapping.toDomain(eamassetstatedto)));
    }

    @PreAuthorize("hasPermission(this.eamassetstateMapping.toDomain(#eamassetstatedtos),'iBizAssetManagement-EAMAssetState-Save')")
    @ApiOperation(value = "批量保存资产状态", tags = {"资产状态" },  notes = "批量保存资产状态")
	@RequestMapping(method = RequestMethod.POST, value = "/eamassetstates/savebatch")
    public ResponseEntity<Boolean> saveBatch(@RequestBody List<EAMAssetStateDTO> eamassetstatedtos) {
        eamassetstateService.saveBatch(eamassetstateMapping.toDomain(eamassetstatedtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

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

