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.EAMLocation;
import cn.ibizlab.assetmanagement.core.location.service.IEAMLocationService;
import cn.ibizlab.assetmanagement.core.location.filter.EAMLocationSearchContext;
import cn.ibizlab.assetmanagement.util.annotation.VersionCheck;

@Slf4j
@Api(tags = {"功能位置" })
@RestController("AssetApi-eamlocation")
@RequestMapping("")
public class EAMLocationResource {

    @Autowired
    public IEAMLocationService eamlocationService;

    @Autowired
    @Lazy
    public EAMLocationMapping eamlocationMapping;

    @PreAuthorize("hasPermission(this.eamlocationMapping.toDomain(#eamlocationdto),'iBizAssetManagement-EAMLocation-Create')")
    @ApiOperation(value = "新建功能位置", tags = {"功能位置" },  notes = "新建功能位置")
	@RequestMapping(method = RequestMethod.POST, value = "/eamlocations")
    @Transactional
    public ResponseEntity<EAMLocationDTO> create(@RequestBody EAMLocationDTO eamlocationdto) {
        EAMLocation domain = eamlocationMapping.toDomain(eamlocationdto);
		eamlocationService.create(domain);
        EAMLocationDTO dto = eamlocationMapping.toDto(domain);
		return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @PreAuthorize("hasPermission(this.eamlocationMapping.toDomain(#eamlocationdtos),'iBizAssetManagement-EAMLocation-Create')")
    @ApiOperation(value = "批量新建功能位置", tags = {"功能位置" },  notes = "批量新建功能位置")
	@RequestMapping(method = RequestMethod.POST, value = "/eamlocations/batch")
    public ResponseEntity<Boolean> createBatch(@RequestBody List<EAMLocationDTO> eamlocationdtos) {
        eamlocationService.createBatch(eamlocationMapping.toDomain(eamlocationdtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @VersionCheck(entity = "eamlocation" , versionfield = "updatedate")
    @PreAuthorize("hasPermission(this.eamlocationService.get(#eamlocation_id),'iBizAssetManagement-EAMLocation-Update')")
    @ApiOperation(value = "更新功能位置", tags = {"功能位置" },  notes = "更新功能位置")
	@RequestMapping(method = RequestMethod.PUT, value = "/eamlocations/{eamlocation_id}")
    @Transactional
    public ResponseEntity<EAMLocationDTO> update(@PathVariable("eamlocation_id") String eamlocation_id, @RequestBody EAMLocationDTO eamlocationdto) {
		EAMLocation domain  = eamlocationMapping.toDomain(eamlocationdto);
        domain .setEamlocationid(eamlocation_id);
		eamlocationService.update(domain );
		EAMLocationDTO dto = eamlocationMapping.toDto(domain );
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @PreAuthorize("hasPermission(this.eamlocationService.getEamlocationByEntities(this.eamlocationMapping.toDomain(#eamlocationdtos)),'iBizAssetManagement-EAMLocation-Update')")
    @ApiOperation(value = "批量更新功能位置", tags = {"功能位置" },  notes = "批量更新功能位置")
	@RequestMapping(method = RequestMethod.PUT, value = "/eamlocations/batch")
    public ResponseEntity<Boolean> updateBatch(@RequestBody List<EAMLocationDTO> eamlocationdtos) {
        eamlocationService.updateBatch(eamlocationMapping.toDomain(eamlocationdtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

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

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

    @PostAuthorize("hasPermission(this.eamlocationMapping.toDomain(returnObject.body),'iBizAssetManagement-EAMLocation-Get')")
    @ApiOperation(value = "获取功能位置", tags = {"功能位置" },  notes = "获取功能位置")
	@RequestMapping(method = RequestMethod.GET, value = "/eamlocations/{eamlocation_id}")
    public ResponseEntity<EAMLocationDTO> get(@PathVariable("eamlocation_id") String eamlocation_id) {
        EAMLocation domain = eamlocationService.get(eamlocation_id);
        EAMLocationDTO dto = eamlocationMapping.toDto(domain);
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @ApiOperation(value = "获取功能位置草稿", tags = {"功能位置" },  notes = "获取功能位置草稿")
	@RequestMapping(method = RequestMethod.GET, value = "/eamlocations/getdraft")
    public ResponseEntity<EAMLocationDTO> getDraft() {
        return ResponseEntity.status(HttpStatus.OK).body(eamlocationMapping.toDto(eamlocationService.getDraft(new EAMLocation())));
    }

    @ApiOperation(value = "检查功能位置", tags = {"功能位置" },  notes = "检查功能位置")
	@RequestMapping(method = RequestMethod.POST, value = "/eamlocations/checkkey")
    public ResponseEntity<Boolean> checkKey(@RequestBody EAMLocationDTO eamlocationdto) {
        return  ResponseEntity.status(HttpStatus.OK).body(eamlocationService.checkKey(eamlocationMapping.toDomain(eamlocationdto)));
    }

    @PreAuthorize("hasPermission(this.eamlocationMapping.toDomain(#eamlocationdto),'iBizAssetManagement-EAMLocation-Save')")
    @ApiOperation(value = "保存功能位置", tags = {"功能位置" },  notes = "保存功能位置")
	@RequestMapping(method = RequestMethod.POST, value = "/eamlocations/save")
    public ResponseEntity<Boolean> save(@RequestBody EAMLocationDTO eamlocationdto) {
        return ResponseEntity.status(HttpStatus.OK).body(eamlocationService.save(eamlocationMapping.toDomain(eamlocationdto)));
    }

    @PreAuthorize("hasPermission(this.eamlocationMapping.toDomain(#eamlocationdtos),'iBizAssetManagement-EAMLocation-Save')")
    @ApiOperation(value = "批量保存功能位置", tags = {"功能位置" },  notes = "批量保存功能位置")
	@RequestMapping(method = RequestMethod.POST, value = "/eamlocations/savebatch")
    public ResponseEntity<Boolean> saveBatch(@RequestBody List<EAMLocationDTO> eamlocationdtos) {
        eamlocationService.saveBatch(eamlocationMapping.toDomain(eamlocationdtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

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

