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.EAMAssetType;
import cn.ibizlab.assetmanagement.core.asset.service.IEAMAssetTypeService;
import cn.ibizlab.assetmanagement.core.asset.filter.EAMAssetTypeSearchContext;
import cn.ibizlab.assetmanagement.util.annotation.VersionCheck;

@Slf4j
@Api(tags = {"资产类型" })
@RestController("AssetApi-eamassettype")
@RequestMapping("")
public class EAMAssetTypeResource {

    @Autowired
    public IEAMAssetTypeService eamassettypeService;

    @Autowired
    @Lazy
    public EAMAssetTypeMapping eamassettypeMapping;

    @PreAuthorize("hasPermission(this.eamassettypeMapping.toDomain(#eamassettypedto),'iBizAssetManagement-EAMAssetType-Create')")
    @ApiOperation(value = "新建资产类型", tags = {"资产类型" },  notes = "新建资产类型")
	@RequestMapping(method = RequestMethod.POST, value = "/eamassettypes")
    @Transactional
    public ResponseEntity<EAMAssetTypeDTO> create(@RequestBody EAMAssetTypeDTO eamassettypedto) {
        EAMAssetType domain = eamassettypeMapping.toDomain(eamassettypedto);
		eamassettypeService.create(domain);
        EAMAssetTypeDTO dto = eamassettypeMapping.toDto(domain);
		return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @PreAuthorize("hasPermission(this.eamassettypeMapping.toDomain(#eamassettypedtos),'iBizAssetManagement-EAMAssetType-Create')")
    @ApiOperation(value = "批量新建资产类型", tags = {"资产类型" },  notes = "批量新建资产类型")
	@RequestMapping(method = RequestMethod.POST, value = "/eamassettypes/batch")
    public ResponseEntity<Boolean> createBatch(@RequestBody List<EAMAssetTypeDTO> eamassettypedtos) {
        eamassettypeService.createBatch(eamassettypeMapping.toDomain(eamassettypedtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @VersionCheck(entity = "eamassettype" , versionfield = "updatedate")
    @PreAuthorize("hasPermission(this.eamassettypeService.get(#eamassettype_id),'iBizAssetManagement-EAMAssetType-Update')")
    @ApiOperation(value = "更新资产类型", tags = {"资产类型" },  notes = "更新资产类型")
	@RequestMapping(method = RequestMethod.PUT, value = "/eamassettypes/{eamassettype_id}")
    @Transactional
    public ResponseEntity<EAMAssetTypeDTO> update(@PathVariable("eamassettype_id") String eamassettype_id, @RequestBody EAMAssetTypeDTO eamassettypedto) {
		EAMAssetType domain  = eamassettypeMapping.toDomain(eamassettypedto);
        domain .setEamassettypeid(eamassettype_id);
		eamassettypeService.update(domain );
		EAMAssetTypeDTO dto = eamassettypeMapping.toDto(domain );
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @PreAuthorize("hasPermission(this.eamassettypeService.getEamassettypeByEntities(this.eamassettypeMapping.toDomain(#eamassettypedtos)),'iBizAssetManagement-EAMAssetType-Update')")
    @ApiOperation(value = "批量更新资产类型", tags = {"资产类型" },  notes = "批量更新资产类型")
	@RequestMapping(method = RequestMethod.PUT, value = "/eamassettypes/batch")
    public ResponseEntity<Boolean> updateBatch(@RequestBody List<EAMAssetTypeDTO> eamassettypedtos) {
        eamassettypeService.updateBatch(eamassettypeMapping.toDomain(eamassettypedtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

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

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

    @PostAuthorize("hasPermission(this.eamassettypeMapping.toDomain(returnObject.body),'iBizAssetManagement-EAMAssetType-Get')")
    @ApiOperation(value = "获取资产类型", tags = {"资产类型" },  notes = "获取资产类型")
	@RequestMapping(method = RequestMethod.GET, value = "/eamassettypes/{eamassettype_id}")
    public ResponseEntity<EAMAssetTypeDTO> get(@PathVariable("eamassettype_id") String eamassettype_id) {
        EAMAssetType domain = eamassettypeService.get(eamassettype_id);
        EAMAssetTypeDTO dto = eamassettypeMapping.toDto(domain);
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @ApiOperation(value = "获取资产类型草稿", tags = {"资产类型" },  notes = "获取资产类型草稿")
	@RequestMapping(method = RequestMethod.GET, value = "/eamassettypes/getdraft")
    public ResponseEntity<EAMAssetTypeDTO> getDraft() {
        return ResponseEntity.status(HttpStatus.OK).body(eamassettypeMapping.toDto(eamassettypeService.getDraft(new EAMAssetType())));
    }

    @ApiOperation(value = "检查资产类型", tags = {"资产类型" },  notes = "检查资产类型")
	@RequestMapping(method = RequestMethod.POST, value = "/eamassettypes/checkkey")
    public ResponseEntity<Boolean> checkKey(@RequestBody EAMAssetTypeDTO eamassettypedto) {
        return  ResponseEntity.status(HttpStatus.OK).body(eamassettypeService.checkKey(eamassettypeMapping.toDomain(eamassettypedto)));
    }

    @PreAuthorize("hasPermission(this.eamassettypeMapping.toDomain(#eamassettypedto),'iBizAssetManagement-EAMAssetType-Save')")
    @ApiOperation(value = "保存资产类型", tags = {"资产类型" },  notes = "保存资产类型")
	@RequestMapping(method = RequestMethod.POST, value = "/eamassettypes/save")
    public ResponseEntity<Boolean> save(@RequestBody EAMAssetTypeDTO eamassettypedto) {
        return ResponseEntity.status(HttpStatus.OK).body(eamassettypeService.save(eamassettypeMapping.toDomain(eamassettypedto)));
    }

    @PreAuthorize("hasPermission(this.eamassettypeMapping.toDomain(#eamassettypedtos),'iBizAssetManagement-EAMAssetType-Save')")
    @ApiOperation(value = "批量保存资产类型", tags = {"资产类型" },  notes = "批量保存资产类型")
	@RequestMapping(method = RequestMethod.POST, value = "/eamassettypes/savebatch")
    public ResponseEntity<Boolean> saveBatch(@RequestBody List<EAMAssetTypeDTO> eamassettypedtos) {
        eamassettypeService.saveBatch(eamassettypeMapping.toDomain(eamassettypedtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

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

