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.apply.domain.EAMApplyType;
import cn.ibizlab.assetmanagement.core.apply.service.IEAMApplyTypeService;
import cn.ibizlab.assetmanagement.core.apply.filter.EAMApplyTypeSearchContext;
import cn.ibizlab.assetmanagement.util.annotation.VersionCheck;

@Slf4j
@Api(tags = {"维护申请类型" })
@RestController("AssetApi-eamapplytype")
@RequestMapping("")
public class EAMApplyTypeResource {

    @Autowired
    public IEAMApplyTypeService eamapplytypeService;

    @Autowired
    @Lazy
    public EAMApplyTypeMapping eamapplytypeMapping;

    @PreAuthorize("hasPermission(this.eamapplytypeMapping.toDomain(#eamapplytypedto),'iBizAssetManagement-EAMApplyType-Create')")
    @ApiOperation(value = "新建维护申请类型", tags = {"维护申请类型" },  notes = "新建维护申请类型")
	@RequestMapping(method = RequestMethod.POST, value = "/eamapplytypes")
    @Transactional
    public ResponseEntity<EAMApplyTypeDTO> create(@RequestBody EAMApplyTypeDTO eamapplytypedto) {
        EAMApplyType domain = eamapplytypeMapping.toDomain(eamapplytypedto);
		eamapplytypeService.create(domain);
        EAMApplyTypeDTO dto = eamapplytypeMapping.toDto(domain);
		return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @PreAuthorize("hasPermission(this.eamapplytypeMapping.toDomain(#eamapplytypedtos),'iBizAssetManagement-EAMApplyType-Create')")
    @ApiOperation(value = "批量新建维护申请类型", tags = {"维护申请类型" },  notes = "批量新建维护申请类型")
	@RequestMapping(method = RequestMethod.POST, value = "/eamapplytypes/batch")
    public ResponseEntity<Boolean> createBatch(@RequestBody List<EAMApplyTypeDTO> eamapplytypedtos) {
        eamapplytypeService.createBatch(eamapplytypeMapping.toDomain(eamapplytypedtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @VersionCheck(entity = "eamapplytype" , versionfield = "updatedate")
    @PreAuthorize("hasPermission(this.eamapplytypeService.get(#eamapplytype_id),'iBizAssetManagement-EAMApplyType-Update')")
    @ApiOperation(value = "更新维护申请类型", tags = {"维护申请类型" },  notes = "更新维护申请类型")
	@RequestMapping(method = RequestMethod.PUT, value = "/eamapplytypes/{eamapplytype_id}")
    @Transactional
    public ResponseEntity<EAMApplyTypeDTO> update(@PathVariable("eamapplytype_id") String eamapplytype_id, @RequestBody EAMApplyTypeDTO eamapplytypedto) {
		EAMApplyType domain  = eamapplytypeMapping.toDomain(eamapplytypedto);
        domain .setEamapplytypeid(eamapplytype_id);
		eamapplytypeService.update(domain );
		EAMApplyTypeDTO dto = eamapplytypeMapping.toDto(domain );
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @PreAuthorize("hasPermission(this.eamapplytypeService.getEamapplytypeByEntities(this.eamapplytypeMapping.toDomain(#eamapplytypedtos)),'iBizAssetManagement-EAMApplyType-Update')")
    @ApiOperation(value = "批量更新维护申请类型", tags = {"维护申请类型" },  notes = "批量更新维护申请类型")
	@RequestMapping(method = RequestMethod.PUT, value = "/eamapplytypes/batch")
    public ResponseEntity<Boolean> updateBatch(@RequestBody List<EAMApplyTypeDTO> eamapplytypedtos) {
        eamapplytypeService.updateBatch(eamapplytypeMapping.toDomain(eamapplytypedtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @PreAuthorize("hasPermission(this.eamapplytypeService.get(#eamapplytype_id),'iBizAssetManagement-EAMApplyType-Remove')")
    @ApiOperation(value = "删除维护申请类型", tags = {"维护申请类型" },  notes = "删除维护申请类型")
	@RequestMapping(method = RequestMethod.DELETE, value = "/eamapplytypes/{eamapplytype_id}")
    @Transactional
    public ResponseEntity<Boolean> remove(@PathVariable("eamapplytype_id") String eamapplytype_id) {
         return ResponseEntity.status(HttpStatus.OK).body(eamapplytypeService.remove(eamapplytype_id));
    }

    @PreAuthorize("hasPermission(this.eamapplytypeService.getEamapplytypeByIds(#ids),'iBizAssetManagement-EAMApplyType-Remove')")
    @ApiOperation(value = "批量删除维护申请类型", tags = {"维护申请类型" },  notes = "批量删除维护申请类型")
	@RequestMapping(method = RequestMethod.DELETE, value = "/eamapplytypes/batch")
    public ResponseEntity<Boolean> removeBatch(@RequestBody List<String> ids) {
        eamapplytypeService.removeBatch(ids);
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @PostAuthorize("hasPermission(this.eamapplytypeMapping.toDomain(returnObject.body),'iBizAssetManagement-EAMApplyType-Get')")
    @ApiOperation(value = "获取维护申请类型", tags = {"维护申请类型" },  notes = "获取维护申请类型")
	@RequestMapping(method = RequestMethod.GET, value = "/eamapplytypes/{eamapplytype_id}")
    public ResponseEntity<EAMApplyTypeDTO> get(@PathVariable("eamapplytype_id") String eamapplytype_id) {
        EAMApplyType domain = eamapplytypeService.get(eamapplytype_id);
        EAMApplyTypeDTO dto = eamapplytypeMapping.toDto(domain);
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @ApiOperation(value = "获取维护申请类型草稿", tags = {"维护申请类型" },  notes = "获取维护申请类型草稿")
	@RequestMapping(method = RequestMethod.GET, value = "/eamapplytypes/getdraft")
    public ResponseEntity<EAMApplyTypeDTO> getDraft() {
        return ResponseEntity.status(HttpStatus.OK).body(eamapplytypeMapping.toDto(eamapplytypeService.getDraft(new EAMApplyType())));
    }

    @ApiOperation(value = "检查维护申请类型", tags = {"维护申请类型" },  notes = "检查维护申请类型")
	@RequestMapping(method = RequestMethod.POST, value = "/eamapplytypes/checkkey")
    public ResponseEntity<Boolean> checkKey(@RequestBody EAMApplyTypeDTO eamapplytypedto) {
        return  ResponseEntity.status(HttpStatus.OK).body(eamapplytypeService.checkKey(eamapplytypeMapping.toDomain(eamapplytypedto)));
    }

    @PreAuthorize("hasPermission(this.eamapplytypeMapping.toDomain(#eamapplytypedto),'iBizAssetManagement-EAMApplyType-Save')")
    @ApiOperation(value = "保存维护申请类型", tags = {"维护申请类型" },  notes = "保存维护申请类型")
	@RequestMapping(method = RequestMethod.POST, value = "/eamapplytypes/save")
    public ResponseEntity<Boolean> save(@RequestBody EAMApplyTypeDTO eamapplytypedto) {
        return ResponseEntity.status(HttpStatus.OK).body(eamapplytypeService.save(eamapplytypeMapping.toDomain(eamapplytypedto)));
    }

    @PreAuthorize("hasPermission(this.eamapplytypeMapping.toDomain(#eamapplytypedtos),'iBizAssetManagement-EAMApplyType-Save')")
    @ApiOperation(value = "批量保存维护申请类型", tags = {"维护申请类型" },  notes = "批量保存维护申请类型")
	@RequestMapping(method = RequestMethod.POST, value = "/eamapplytypes/savebatch")
    public ResponseEntity<Boolean> saveBatch(@RequestBody List<EAMApplyTypeDTO> eamapplytypedtos) {
        eamapplytypeService.saveBatch(eamapplytypeMapping.toDomain(eamapplytypedtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','iBizAssetManagement-EAMApplyType-searchDefault-all') and hasPermission(#context,'iBizAssetManagement-EAMApplyType-Get')")
	@ApiOperation(value = "获取DEFAULT", tags = {"维护申请类型" } ,notes = "获取DEFAULT")
    @RequestMapping(method= RequestMethod.GET , value="/eamapplytypes/fetchdefault")
	public ResponseEntity<List<EAMApplyTypeDTO>> fetchDefault(EAMApplyTypeSearchContext context) {
        Page<EAMApplyType> domains = eamapplytypeService.searchDefault(context) ;
        List<EAMApplyTypeDTO> list = eamapplytypeMapping.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-EAMApplyType-searchDefault-all') and hasPermission(#context,'iBizAssetManagement-EAMApplyType-Get')")
	@ApiOperation(value = "查询DEFAULT", tags = {"维护申请类型" } ,notes = "查询DEFAULT")
    @RequestMapping(method= RequestMethod.POST , value="/eamapplytypes/searchdefault")
	public ResponseEntity<Page<EAMApplyTypeDTO>> searchDefault(@RequestBody EAMApplyTypeSearchContext context) {
        Page<EAMApplyType> domains = eamapplytypeService.searchDefault(context) ;
	    return ResponseEntity.status(HttpStatus.OK)
                .body(new PageImpl(eamapplytypeMapping.toDto(domains.getContent()), context.getPageable(), domains.getTotalElements()));
	}
}

