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.location.domain.EAMLocationState;
import cn.ibizlab.assetmanagement.core.location.service.IEAMLocationStateService;
import cn.ibizlab.assetmanagement.core.location.filter.EAMLocationStateSearchContext;
import cn.ibizlab.assetmanagement.util.annotation.VersionCheck;

@Slf4j
@Api(tags = {"功能位置状态" })
@RestController("AssetApi-eamlocationstate")
@RequestMapping("")
public class EAMLocationStateResource {

    @Autowired
    public IEAMLocationStateService eamlocationstateService;

    @Autowired
    @Lazy
    public EAMLocationStateMapping eamlocationstateMapping;

    @PreAuthorize("hasPermission(this.eamlocationstateMapping.toDomain(#eamlocationstatedto),'iBizAssetManagement-EAMLocationState-Create')")
    @ApiOperation(value = "新建功能位置状态", tags = {"功能位置状态" },  notes = "新建功能位置状态")
	@RequestMapping(method = RequestMethod.POST, value = "/eamlocationstates")
    @Transactional
    public ResponseEntity<EAMLocationStateDTO> create(@RequestBody EAMLocationStateDTO eamlocationstatedto) {
        EAMLocationState domain = eamlocationstateMapping.toDomain(eamlocationstatedto);
		eamlocationstateService.create(domain);
        EAMLocationStateDTO dto = eamlocationstateMapping.toDto(domain);
		return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @PreAuthorize("hasPermission(this.eamlocationstateMapping.toDomain(#eamlocationstatedtos),'iBizAssetManagement-EAMLocationState-Create')")
    @ApiOperation(value = "批量新建功能位置状态", tags = {"功能位置状态" },  notes = "批量新建功能位置状态")
	@RequestMapping(method = RequestMethod.POST, value = "/eamlocationstates/batch")
    public ResponseEntity<Boolean> createBatch(@RequestBody List<EAMLocationStateDTO> eamlocationstatedtos) {
        eamlocationstateService.createBatch(eamlocationstateMapping.toDomain(eamlocationstatedtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @VersionCheck(entity = "eamlocationstate" , versionfield = "updatedate")
    @PreAuthorize("hasPermission(this.eamlocationstateService.get(#eamlocationstate_id),'iBizAssetManagement-EAMLocationState-Update')")
    @ApiOperation(value = "更新功能位置状态", tags = {"功能位置状态" },  notes = "更新功能位置状态")
	@RequestMapping(method = RequestMethod.PUT, value = "/eamlocationstates/{eamlocationstate_id}")
    @Transactional
    public ResponseEntity<EAMLocationStateDTO> update(@PathVariable("eamlocationstate_id") String eamlocationstate_id, @RequestBody EAMLocationStateDTO eamlocationstatedto) {
		EAMLocationState domain  = eamlocationstateMapping.toDomain(eamlocationstatedto);
        domain .setEamlocationstateid(eamlocationstate_id);
		eamlocationstateService.update(domain );
		EAMLocationStateDTO dto = eamlocationstateMapping.toDto(domain );
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @PreAuthorize("hasPermission(this.eamlocationstateService.getEamlocationstateByEntities(this.eamlocationstateMapping.toDomain(#eamlocationstatedtos)),'iBizAssetManagement-EAMLocationState-Update')")
    @ApiOperation(value = "批量更新功能位置状态", tags = {"功能位置状态" },  notes = "批量更新功能位置状态")
	@RequestMapping(method = RequestMethod.PUT, value = "/eamlocationstates/batch")
    public ResponseEntity<Boolean> updateBatch(@RequestBody List<EAMLocationStateDTO> eamlocationstatedtos) {
        eamlocationstateService.updateBatch(eamlocationstateMapping.toDomain(eamlocationstatedtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @PreAuthorize("hasPermission(this.eamlocationstateService.get(#eamlocationstate_id),'iBizAssetManagement-EAMLocationState-Remove')")
    @ApiOperation(value = "删除功能位置状态", tags = {"功能位置状态" },  notes = "删除功能位置状态")
	@RequestMapping(method = RequestMethod.DELETE, value = "/eamlocationstates/{eamlocationstate_id}")
    @Transactional
    public ResponseEntity<Boolean> remove(@PathVariable("eamlocationstate_id") String eamlocationstate_id) {
         return ResponseEntity.status(HttpStatus.OK).body(eamlocationstateService.remove(eamlocationstate_id));
    }

    @PreAuthorize("hasPermission(this.eamlocationstateService.getEamlocationstateByIds(#ids),'iBizAssetManagement-EAMLocationState-Remove')")
    @ApiOperation(value = "批量删除功能位置状态", tags = {"功能位置状态" },  notes = "批量删除功能位置状态")
	@RequestMapping(method = RequestMethod.DELETE, value = "/eamlocationstates/batch")
    public ResponseEntity<Boolean> removeBatch(@RequestBody List<String> ids) {
        eamlocationstateService.removeBatch(ids);
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @PostAuthorize("hasPermission(this.eamlocationstateMapping.toDomain(returnObject.body),'iBizAssetManagement-EAMLocationState-Get')")
    @ApiOperation(value = "获取功能位置状态", tags = {"功能位置状态" },  notes = "获取功能位置状态")
	@RequestMapping(method = RequestMethod.GET, value = "/eamlocationstates/{eamlocationstate_id}")
    public ResponseEntity<EAMLocationStateDTO> get(@PathVariable("eamlocationstate_id") String eamlocationstate_id) {
        EAMLocationState domain = eamlocationstateService.get(eamlocationstate_id);
        EAMLocationStateDTO dto = eamlocationstateMapping.toDto(domain);
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @ApiOperation(value = "获取功能位置状态草稿", tags = {"功能位置状态" },  notes = "获取功能位置状态草稿")
	@RequestMapping(method = RequestMethod.GET, value = "/eamlocationstates/getdraft")
    public ResponseEntity<EAMLocationStateDTO> getDraft() {
        return ResponseEntity.status(HttpStatus.OK).body(eamlocationstateMapping.toDto(eamlocationstateService.getDraft(new EAMLocationState())));
    }

    @ApiOperation(value = "检查功能位置状态", tags = {"功能位置状态" },  notes = "检查功能位置状态")
	@RequestMapping(method = RequestMethod.POST, value = "/eamlocationstates/checkkey")
    public ResponseEntity<Boolean> checkKey(@RequestBody EAMLocationStateDTO eamlocationstatedto) {
        return  ResponseEntity.status(HttpStatus.OK).body(eamlocationstateService.checkKey(eamlocationstateMapping.toDomain(eamlocationstatedto)));
    }

    @PreAuthorize("hasPermission(this.eamlocationstateMapping.toDomain(#eamlocationstatedto),'iBizAssetManagement-EAMLocationState-Save')")
    @ApiOperation(value = "保存功能位置状态", tags = {"功能位置状态" },  notes = "保存功能位置状态")
	@RequestMapping(method = RequestMethod.POST, value = "/eamlocationstates/save")
    public ResponseEntity<Boolean> save(@RequestBody EAMLocationStateDTO eamlocationstatedto) {
        return ResponseEntity.status(HttpStatus.OK).body(eamlocationstateService.save(eamlocationstateMapping.toDomain(eamlocationstatedto)));
    }

    @PreAuthorize("hasPermission(this.eamlocationstateMapping.toDomain(#eamlocationstatedtos),'iBizAssetManagement-EAMLocationState-Save')")
    @ApiOperation(value = "批量保存功能位置状态", tags = {"功能位置状态" },  notes = "批量保存功能位置状态")
	@RequestMapping(method = RequestMethod.POST, value = "/eamlocationstates/savebatch")
    public ResponseEntity<Boolean> saveBatch(@RequestBody List<EAMLocationStateDTO> eamlocationstatedtos) {
        eamlocationstateService.saveBatch(eamlocationstateMapping.toDomain(eamlocationstatedtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','iBizAssetManagement-EAMLocationState-searchDefault-all') and hasPermission(#context,'iBizAssetManagement-EAMLocationState-Get')")
	@ApiOperation(value = "获取DEFAULT", tags = {"功能位置状态" } ,notes = "获取DEFAULT")
    @RequestMapping(method= RequestMethod.GET , value="/eamlocationstates/fetchdefault")
	public ResponseEntity<List<EAMLocationStateDTO>> fetchDefault(EAMLocationStateSearchContext context) {
        Page<EAMLocationState> domains = eamlocationstateService.searchDefault(context) ;
        List<EAMLocationStateDTO> list = eamlocationstateMapping.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-EAMLocationState-searchDefault-all') and hasPermission(#context,'iBizAssetManagement-EAMLocationState-Get')")
	@ApiOperation(value = "查询DEFAULT", tags = {"功能位置状态" } ,notes = "查询DEFAULT")
    @RequestMapping(method= RequestMethod.POST , value="/eamlocationstates/searchdefault")
	public ResponseEntity<Page<EAMLocationStateDTO>> searchDefault(@RequestBody EAMLocationStateSearchContext context) {
        Page<EAMLocationState> domains = eamlocationstateService.searchDefault(context) ;
	    return ResponseEntity.status(HttpStatus.OK)
                .body(new PageImpl(eamlocationstateMapping.toDto(domains.getContent()), context.getPageable(), domains.getTotalElements()));
	}
}

