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.EAMApplyState;
import cn.ibizlab.assetmanagement.core.apply.service.IEAMApplyStateService;
import cn.ibizlab.assetmanagement.core.apply.filter.EAMApplyStateSearchContext;
import cn.ibizlab.assetmanagement.util.annotation.VersionCheck;

@Slf4j
@Api(tags = {"维护申请状态" })
@RestController("AssetApi-eamapplystate")
@RequestMapping("")
public class EAMApplyStateResource {

    @Autowired
    public IEAMApplyStateService eamapplystateService;

    @Autowired
    @Lazy
    public EAMApplyStateMapping eamapplystateMapping;

    @PreAuthorize("hasPermission(this.eamapplystateMapping.toDomain(#eamapplystatedto),'iBizAssetManagement-EAMApplyState-Create')")
    @ApiOperation(value = "新建维护申请状态", tags = {"维护申请状态" },  notes = "新建维护申请状态")
	@RequestMapping(method = RequestMethod.POST, value = "/eamapplystates")
    @Transactional
    public ResponseEntity<EAMApplyStateDTO> create(@RequestBody EAMApplyStateDTO eamapplystatedto) {
        EAMApplyState domain = eamapplystateMapping.toDomain(eamapplystatedto);
		eamapplystateService.create(domain);
        EAMApplyStateDTO dto = eamapplystateMapping.toDto(domain);
		return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @PreAuthorize("hasPermission(this.eamapplystateMapping.toDomain(#eamapplystatedtos),'iBizAssetManagement-EAMApplyState-Create')")
    @ApiOperation(value = "批量新建维护申请状态", tags = {"维护申请状态" },  notes = "批量新建维护申请状态")
	@RequestMapping(method = RequestMethod.POST, value = "/eamapplystates/batch")
    public ResponseEntity<Boolean> createBatch(@RequestBody List<EAMApplyStateDTO> eamapplystatedtos) {
        eamapplystateService.createBatch(eamapplystateMapping.toDomain(eamapplystatedtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @VersionCheck(entity = "eamapplystate" , versionfield = "updatedate")
    @PreAuthorize("hasPermission(this.eamapplystateService.get(#eamapplystate_id),'iBizAssetManagement-EAMApplyState-Update')")
    @ApiOperation(value = "更新维护申请状态", tags = {"维护申请状态" },  notes = "更新维护申请状态")
	@RequestMapping(method = RequestMethod.PUT, value = "/eamapplystates/{eamapplystate_id}")
    @Transactional
    public ResponseEntity<EAMApplyStateDTO> update(@PathVariable("eamapplystate_id") String eamapplystate_id, @RequestBody EAMApplyStateDTO eamapplystatedto) {
		EAMApplyState domain  = eamapplystateMapping.toDomain(eamapplystatedto);
        domain .setEamapplystateid(eamapplystate_id);
		eamapplystateService.update(domain );
		EAMApplyStateDTO dto = eamapplystateMapping.toDto(domain );
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @PreAuthorize("hasPermission(this.eamapplystateService.getEamapplystateByEntities(this.eamapplystateMapping.toDomain(#eamapplystatedtos)),'iBizAssetManagement-EAMApplyState-Update')")
    @ApiOperation(value = "批量更新维护申请状态", tags = {"维护申请状态" },  notes = "批量更新维护申请状态")
	@RequestMapping(method = RequestMethod.PUT, value = "/eamapplystates/batch")
    public ResponseEntity<Boolean> updateBatch(@RequestBody List<EAMApplyStateDTO> eamapplystatedtos) {
        eamapplystateService.updateBatch(eamapplystateMapping.toDomain(eamapplystatedtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

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

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

    @PostAuthorize("hasPermission(this.eamapplystateMapping.toDomain(returnObject.body),'iBizAssetManagement-EAMApplyState-Get')")
    @ApiOperation(value = "获取维护申请状态", tags = {"维护申请状态" },  notes = "获取维护申请状态")
	@RequestMapping(method = RequestMethod.GET, value = "/eamapplystates/{eamapplystate_id}")
    public ResponseEntity<EAMApplyStateDTO> get(@PathVariable("eamapplystate_id") String eamapplystate_id) {
        EAMApplyState domain = eamapplystateService.get(eamapplystate_id);
        EAMApplyStateDTO dto = eamapplystateMapping.toDto(domain);
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @ApiOperation(value = "获取维护申请状态草稿", tags = {"维护申请状态" },  notes = "获取维护申请状态草稿")
	@RequestMapping(method = RequestMethod.GET, value = "/eamapplystates/getdraft")
    public ResponseEntity<EAMApplyStateDTO> getDraft() {
        return ResponseEntity.status(HttpStatus.OK).body(eamapplystateMapping.toDto(eamapplystateService.getDraft(new EAMApplyState())));
    }

    @ApiOperation(value = "检查维护申请状态", tags = {"维护申请状态" },  notes = "检查维护申请状态")
	@RequestMapping(method = RequestMethod.POST, value = "/eamapplystates/checkkey")
    public ResponseEntity<Boolean> checkKey(@RequestBody EAMApplyStateDTO eamapplystatedto) {
        return  ResponseEntity.status(HttpStatus.OK).body(eamapplystateService.checkKey(eamapplystateMapping.toDomain(eamapplystatedto)));
    }

    @PreAuthorize("hasPermission(this.eamapplystateMapping.toDomain(#eamapplystatedto),'iBizAssetManagement-EAMApplyState-Save')")
    @ApiOperation(value = "保存维护申请状态", tags = {"维护申请状态" },  notes = "保存维护申请状态")
	@RequestMapping(method = RequestMethod.POST, value = "/eamapplystates/save")
    public ResponseEntity<Boolean> save(@RequestBody EAMApplyStateDTO eamapplystatedto) {
        return ResponseEntity.status(HttpStatus.OK).body(eamapplystateService.save(eamapplystateMapping.toDomain(eamapplystatedto)));
    }

    @PreAuthorize("hasPermission(this.eamapplystateMapping.toDomain(#eamapplystatedtos),'iBizAssetManagement-EAMApplyState-Save')")
    @ApiOperation(value = "批量保存维护申请状态", tags = {"维护申请状态" },  notes = "批量保存维护申请状态")
	@RequestMapping(method = RequestMethod.POST, value = "/eamapplystates/savebatch")
    public ResponseEntity<Boolean> saveBatch(@RequestBody List<EAMApplyStateDTO> eamapplystatedtos) {
        eamapplystateService.saveBatch(eamapplystateMapping.toDomain(eamapplystatedtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

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

