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.EAMApply;
import cn.ibizlab.assetmanagement.core.apply.service.IEAMApplyService;
import cn.ibizlab.assetmanagement.core.apply.filter.EAMApplySearchContext;
import cn.ibizlab.assetmanagement.util.annotation.VersionCheck;

@Slf4j
@Api(tags = {"维护申请" })
@RestController("AssetApi-eamapply")
@RequestMapping("")
public class EAMApplyResource {

    @Autowired
    public IEAMApplyService eamapplyService;

    @Autowired
    @Lazy
    public EAMApplyMapping eamapplyMapping;

    @PreAuthorize("hasPermission(this.eamapplyMapping.toDomain(#eamapplydto),'iBizAssetManagement-EAMApply-Create')")
    @ApiOperation(value = "新建维护申请", tags = {"维护申请" },  notes = "新建维护申请")
	@RequestMapping(method = RequestMethod.POST, value = "/eamapplies")
    @Transactional
    public ResponseEntity<EAMApplyDTO> create(@RequestBody EAMApplyDTO eamapplydto) {
        EAMApply domain = eamapplyMapping.toDomain(eamapplydto);
		eamapplyService.create(domain);
        EAMApplyDTO dto = eamapplyMapping.toDto(domain);
		return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @PreAuthorize("hasPermission(this.eamapplyMapping.toDomain(#eamapplydtos),'iBizAssetManagement-EAMApply-Create')")
    @ApiOperation(value = "批量新建维护申请", tags = {"维护申请" },  notes = "批量新建维护申请")
	@RequestMapping(method = RequestMethod.POST, value = "/eamapplies/batch")
    public ResponseEntity<Boolean> createBatch(@RequestBody List<EAMApplyDTO> eamapplydtos) {
        eamapplyService.createBatch(eamapplyMapping.toDomain(eamapplydtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @VersionCheck(entity = "eamapply" , versionfield = "updatedate")
    @PreAuthorize("hasPermission(this.eamapplyService.get(#eamapply_id),'iBizAssetManagement-EAMApply-Update')")
    @ApiOperation(value = "更新维护申请", tags = {"维护申请" },  notes = "更新维护申请")
	@RequestMapping(method = RequestMethod.PUT, value = "/eamapplies/{eamapply_id}")
    @Transactional
    public ResponseEntity<EAMApplyDTO> update(@PathVariable("eamapply_id") String eamapply_id, @RequestBody EAMApplyDTO eamapplydto) {
		EAMApply domain  = eamapplyMapping.toDomain(eamapplydto);
        domain .setEamapplyid(eamapply_id);
		eamapplyService.update(domain );
		EAMApplyDTO dto = eamapplyMapping.toDto(domain );
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @PreAuthorize("hasPermission(this.eamapplyService.getEamapplyByEntities(this.eamapplyMapping.toDomain(#eamapplydtos)),'iBizAssetManagement-EAMApply-Update')")
    @ApiOperation(value = "批量更新维护申请", tags = {"维护申请" },  notes = "批量更新维护申请")
	@RequestMapping(method = RequestMethod.PUT, value = "/eamapplies/batch")
    public ResponseEntity<Boolean> updateBatch(@RequestBody List<EAMApplyDTO> eamapplydtos) {
        eamapplyService.updateBatch(eamapplyMapping.toDomain(eamapplydtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

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

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

    @PostAuthorize("hasPermission(this.eamapplyMapping.toDomain(returnObject.body),'iBizAssetManagement-EAMApply-Get')")
    @ApiOperation(value = "获取维护申请", tags = {"维护申请" },  notes = "获取维护申请")
	@RequestMapping(method = RequestMethod.GET, value = "/eamapplies/{eamapply_id}")
    public ResponseEntity<EAMApplyDTO> get(@PathVariable("eamapply_id") String eamapply_id) {
        EAMApply domain = eamapplyService.get(eamapply_id);
        EAMApplyDTO dto = eamapplyMapping.toDto(domain);
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @ApiOperation(value = "获取维护申请草稿", tags = {"维护申请" },  notes = "获取维护申请草稿")
	@RequestMapping(method = RequestMethod.GET, value = "/eamapplies/getdraft")
    public ResponseEntity<EAMApplyDTO> getDraft() {
        return ResponseEntity.status(HttpStatus.OK).body(eamapplyMapping.toDto(eamapplyService.getDraft(new EAMApply())));
    }

    @ApiOperation(value = "检查维护申请", tags = {"维护申请" },  notes = "检查维护申请")
	@RequestMapping(method = RequestMethod.POST, value = "/eamapplies/checkkey")
    public ResponseEntity<Boolean> checkKey(@RequestBody EAMApplyDTO eamapplydto) {
        return  ResponseEntity.status(HttpStatus.OK).body(eamapplyService.checkKey(eamapplyMapping.toDomain(eamapplydto)));
    }

    @PreAuthorize("hasPermission(this.eamapplyMapping.toDomain(#eamapplydto),'iBizAssetManagement-EAMApply-Save')")
    @ApiOperation(value = "保存维护申请", tags = {"维护申请" },  notes = "保存维护申请")
	@RequestMapping(method = RequestMethod.POST, value = "/eamapplies/save")
    public ResponseEntity<Boolean> save(@RequestBody EAMApplyDTO eamapplydto) {
        return ResponseEntity.status(HttpStatus.OK).body(eamapplyService.save(eamapplyMapping.toDomain(eamapplydto)));
    }

    @PreAuthorize("hasPermission(this.eamapplyMapping.toDomain(#eamapplydtos),'iBizAssetManagement-EAMApply-Save')")
    @ApiOperation(value = "批量保存维护申请", tags = {"维护申请" },  notes = "批量保存维护申请")
	@RequestMapping(method = RequestMethod.POST, value = "/eamapplies/savebatch")
    public ResponseEntity<Boolean> saveBatch(@RequestBody List<EAMApplyDTO> eamapplydtos) {
        eamapplyService.saveBatch(eamapplyMapping.toDomain(eamapplydtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

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

