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.workorder.domain.EAMServiceLevel;
import cn.ibizlab.assetmanagement.core.workorder.service.IEAMServiceLevelService;
import cn.ibizlab.assetmanagement.core.workorder.filter.EAMServiceLevelSearchContext;
import cn.ibizlab.assetmanagement.util.annotation.VersionCheck;

@Slf4j
@Api(tags = {"服务等级" })
@RestController("AssetApi-eamservicelevel")
@RequestMapping("")
public class EAMServiceLevelResource {

    @Autowired
    public IEAMServiceLevelService eamservicelevelService;

    @Autowired
    @Lazy
    public EAMServiceLevelMapping eamservicelevelMapping;

    @PreAuthorize("hasPermission(this.eamservicelevelMapping.toDomain(#eamserviceleveldto),'iBizAssetManagement-EAMServiceLevel-Create')")
    @ApiOperation(value = "新建服务等级", tags = {"服务等级" },  notes = "新建服务等级")
	@RequestMapping(method = RequestMethod.POST, value = "/eamservicelevels")
    @Transactional
    public ResponseEntity<EAMServiceLevelDTO> create(@RequestBody EAMServiceLevelDTO eamserviceleveldto) {
        EAMServiceLevel domain = eamservicelevelMapping.toDomain(eamserviceleveldto);
		eamservicelevelService.create(domain);
        EAMServiceLevelDTO dto = eamservicelevelMapping.toDto(domain);
		return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @PreAuthorize("hasPermission(this.eamservicelevelMapping.toDomain(#eamserviceleveldtos),'iBizAssetManagement-EAMServiceLevel-Create')")
    @ApiOperation(value = "批量新建服务等级", tags = {"服务等级" },  notes = "批量新建服务等级")
	@RequestMapping(method = RequestMethod.POST, value = "/eamservicelevels/batch")
    public ResponseEntity<Boolean> createBatch(@RequestBody List<EAMServiceLevelDTO> eamserviceleveldtos) {
        eamservicelevelService.createBatch(eamservicelevelMapping.toDomain(eamserviceleveldtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @VersionCheck(entity = "eamservicelevel" , versionfield = "updatedate")
    @PreAuthorize("hasPermission(this.eamservicelevelService.get(#eamservicelevel_id),'iBizAssetManagement-EAMServiceLevel-Update')")
    @ApiOperation(value = "更新服务等级", tags = {"服务等级" },  notes = "更新服务等级")
	@RequestMapping(method = RequestMethod.PUT, value = "/eamservicelevels/{eamservicelevel_id}")
    @Transactional
    public ResponseEntity<EAMServiceLevelDTO> update(@PathVariable("eamservicelevel_id") String eamservicelevel_id, @RequestBody EAMServiceLevelDTO eamserviceleveldto) {
		EAMServiceLevel domain  = eamservicelevelMapping.toDomain(eamserviceleveldto);
        domain .setEamservicelevelid(eamservicelevel_id);
		eamservicelevelService.update(domain );
		EAMServiceLevelDTO dto = eamservicelevelMapping.toDto(domain );
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @PreAuthorize("hasPermission(this.eamservicelevelService.getEamservicelevelByEntities(this.eamservicelevelMapping.toDomain(#eamserviceleveldtos)),'iBizAssetManagement-EAMServiceLevel-Update')")
    @ApiOperation(value = "批量更新服务等级", tags = {"服务等级" },  notes = "批量更新服务等级")
	@RequestMapping(method = RequestMethod.PUT, value = "/eamservicelevels/batch")
    public ResponseEntity<Boolean> updateBatch(@RequestBody List<EAMServiceLevelDTO> eamserviceleveldtos) {
        eamservicelevelService.updateBatch(eamservicelevelMapping.toDomain(eamserviceleveldtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @PreAuthorize("hasPermission(this.eamservicelevelService.get(#eamservicelevel_id),'iBizAssetManagement-EAMServiceLevel-Remove')")
    @ApiOperation(value = "删除服务等级", tags = {"服务等级" },  notes = "删除服务等级")
	@RequestMapping(method = RequestMethod.DELETE, value = "/eamservicelevels/{eamservicelevel_id}")
    @Transactional
    public ResponseEntity<Boolean> remove(@PathVariable("eamservicelevel_id") String eamservicelevel_id) {
         return ResponseEntity.status(HttpStatus.OK).body(eamservicelevelService.remove(eamservicelevel_id));
    }

    @PreAuthorize("hasPermission(this.eamservicelevelService.getEamservicelevelByIds(#ids),'iBizAssetManagement-EAMServiceLevel-Remove')")
    @ApiOperation(value = "批量删除服务等级", tags = {"服务等级" },  notes = "批量删除服务等级")
	@RequestMapping(method = RequestMethod.DELETE, value = "/eamservicelevels/batch")
    public ResponseEntity<Boolean> removeBatch(@RequestBody List<String> ids) {
        eamservicelevelService.removeBatch(ids);
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @PostAuthorize("hasPermission(this.eamservicelevelMapping.toDomain(returnObject.body),'iBizAssetManagement-EAMServiceLevel-Get')")
    @ApiOperation(value = "获取服务等级", tags = {"服务等级" },  notes = "获取服务等级")
	@RequestMapping(method = RequestMethod.GET, value = "/eamservicelevels/{eamservicelevel_id}")
    public ResponseEntity<EAMServiceLevelDTO> get(@PathVariable("eamservicelevel_id") String eamservicelevel_id) {
        EAMServiceLevel domain = eamservicelevelService.get(eamservicelevel_id);
        EAMServiceLevelDTO dto = eamservicelevelMapping.toDto(domain);
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @ApiOperation(value = "获取服务等级草稿", tags = {"服务等级" },  notes = "获取服务等级草稿")
	@RequestMapping(method = RequestMethod.GET, value = "/eamservicelevels/getdraft")
    public ResponseEntity<EAMServiceLevelDTO> getDraft() {
        return ResponseEntity.status(HttpStatus.OK).body(eamservicelevelMapping.toDto(eamservicelevelService.getDraft(new EAMServiceLevel())));
    }

    @ApiOperation(value = "检查服务等级", tags = {"服务等级" },  notes = "检查服务等级")
	@RequestMapping(method = RequestMethod.POST, value = "/eamservicelevels/checkkey")
    public ResponseEntity<Boolean> checkKey(@RequestBody EAMServiceLevelDTO eamserviceleveldto) {
        return  ResponseEntity.status(HttpStatus.OK).body(eamservicelevelService.checkKey(eamservicelevelMapping.toDomain(eamserviceleveldto)));
    }

    @PreAuthorize("hasPermission(this.eamservicelevelMapping.toDomain(#eamserviceleveldto),'iBizAssetManagement-EAMServiceLevel-Save')")
    @ApiOperation(value = "保存服务等级", tags = {"服务等级" },  notes = "保存服务等级")
	@RequestMapping(method = RequestMethod.POST, value = "/eamservicelevels/save")
    public ResponseEntity<Boolean> save(@RequestBody EAMServiceLevelDTO eamserviceleveldto) {
        return ResponseEntity.status(HttpStatus.OK).body(eamservicelevelService.save(eamservicelevelMapping.toDomain(eamserviceleveldto)));
    }

    @PreAuthorize("hasPermission(this.eamservicelevelMapping.toDomain(#eamserviceleveldtos),'iBizAssetManagement-EAMServiceLevel-Save')")
    @ApiOperation(value = "批量保存服务等级", tags = {"服务等级" },  notes = "批量保存服务等级")
	@RequestMapping(method = RequestMethod.POST, value = "/eamservicelevels/savebatch")
    public ResponseEntity<Boolean> saveBatch(@RequestBody List<EAMServiceLevelDTO> eamserviceleveldtos) {
        eamservicelevelService.saveBatch(eamservicelevelMapping.toDomain(eamserviceleveldtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','iBizAssetManagement-EAMServiceLevel-searchDefault-all') and hasPermission(#context,'iBizAssetManagement-EAMServiceLevel-Get')")
	@ApiOperation(value = "获取DEFAULT", tags = {"服务等级" } ,notes = "获取DEFAULT")
    @RequestMapping(method= RequestMethod.GET , value="/eamservicelevels/fetchdefault")
	public ResponseEntity<List<EAMServiceLevelDTO>> fetchDefault(EAMServiceLevelSearchContext context) {
        Page<EAMServiceLevel> domains = eamservicelevelService.searchDefault(context) ;
        List<EAMServiceLevelDTO> list = eamservicelevelMapping.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-EAMServiceLevel-searchDefault-all') and hasPermission(#context,'iBizAssetManagement-EAMServiceLevel-Get')")
	@ApiOperation(value = "查询DEFAULT", tags = {"服务等级" } ,notes = "查询DEFAULT")
    @RequestMapping(method= RequestMethod.POST , value="/eamservicelevels/searchdefault")
	public ResponseEntity<Page<EAMServiceLevelDTO>> searchDefault(@RequestBody EAMServiceLevelSearchContext context) {
        Page<EAMServiceLevel> domains = eamservicelevelService.searchDefault(context) ;
	    return ResponseEntity.status(HttpStatus.OK)
                .body(new PageImpl(eamservicelevelMapping.toDto(domains.getContent()), context.getPageable(), domains.getTotalElements()));
	}
}

