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.EAMLocationType;
import cn.ibizlab.assetmanagement.core.location.service.IEAMLocationTypeService;
import cn.ibizlab.assetmanagement.core.location.filter.EAMLocationTypeSearchContext;
import cn.ibizlab.assetmanagement.util.annotation.VersionCheck;

@Slf4j
@Api(tags = {"功能位置类型" })
@RestController("AssetApi-eamlocationtype")
@RequestMapping("")
public class EAMLocationTypeResource {

    @Autowired
    public IEAMLocationTypeService eamlocationtypeService;

    @Autowired
    @Lazy
    public EAMLocationTypeMapping eamlocationtypeMapping;

    @PreAuthorize("hasPermission(this.eamlocationtypeMapping.toDomain(#eamlocationtypedto),'iBizAssetManagement-EAMLocationType-Create')")
    @ApiOperation(value = "新建功能位置类型", tags = {"功能位置类型" },  notes = "新建功能位置类型")
	@RequestMapping(method = RequestMethod.POST, value = "/eamlocationtypes")
    @Transactional
    public ResponseEntity<EAMLocationTypeDTO> create(@RequestBody EAMLocationTypeDTO eamlocationtypedto) {
        EAMLocationType domain = eamlocationtypeMapping.toDomain(eamlocationtypedto);
		eamlocationtypeService.create(domain);
        EAMLocationTypeDTO dto = eamlocationtypeMapping.toDto(domain);
		return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @PreAuthorize("hasPermission(this.eamlocationtypeMapping.toDomain(#eamlocationtypedtos),'iBizAssetManagement-EAMLocationType-Create')")
    @ApiOperation(value = "批量新建功能位置类型", tags = {"功能位置类型" },  notes = "批量新建功能位置类型")
	@RequestMapping(method = RequestMethod.POST, value = "/eamlocationtypes/batch")
    public ResponseEntity<Boolean> createBatch(@RequestBody List<EAMLocationTypeDTO> eamlocationtypedtos) {
        eamlocationtypeService.createBatch(eamlocationtypeMapping.toDomain(eamlocationtypedtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @VersionCheck(entity = "eamlocationtype" , versionfield = "updatedate")
    @PreAuthorize("hasPermission(this.eamlocationtypeService.get(#eamlocationtype_id),'iBizAssetManagement-EAMLocationType-Update')")
    @ApiOperation(value = "更新功能位置类型", tags = {"功能位置类型" },  notes = "更新功能位置类型")
	@RequestMapping(method = RequestMethod.PUT, value = "/eamlocationtypes/{eamlocationtype_id}")
    @Transactional
    public ResponseEntity<EAMLocationTypeDTO> update(@PathVariable("eamlocationtype_id") String eamlocationtype_id, @RequestBody EAMLocationTypeDTO eamlocationtypedto) {
		EAMLocationType domain  = eamlocationtypeMapping.toDomain(eamlocationtypedto);
        domain .setEamlocationtypeid(eamlocationtype_id);
		eamlocationtypeService.update(domain );
		EAMLocationTypeDTO dto = eamlocationtypeMapping.toDto(domain );
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @PreAuthorize("hasPermission(this.eamlocationtypeService.getEamlocationtypeByEntities(this.eamlocationtypeMapping.toDomain(#eamlocationtypedtos)),'iBizAssetManagement-EAMLocationType-Update')")
    @ApiOperation(value = "批量更新功能位置类型", tags = {"功能位置类型" },  notes = "批量更新功能位置类型")
	@RequestMapping(method = RequestMethod.PUT, value = "/eamlocationtypes/batch")
    public ResponseEntity<Boolean> updateBatch(@RequestBody List<EAMLocationTypeDTO> eamlocationtypedtos) {
        eamlocationtypeService.updateBatch(eamlocationtypeMapping.toDomain(eamlocationtypedtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

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

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

    @PostAuthorize("hasPermission(this.eamlocationtypeMapping.toDomain(returnObject.body),'iBizAssetManagement-EAMLocationType-Get')")
    @ApiOperation(value = "获取功能位置类型", tags = {"功能位置类型" },  notes = "获取功能位置类型")
	@RequestMapping(method = RequestMethod.GET, value = "/eamlocationtypes/{eamlocationtype_id}")
    public ResponseEntity<EAMLocationTypeDTO> get(@PathVariable("eamlocationtype_id") String eamlocationtype_id) {
        EAMLocationType domain = eamlocationtypeService.get(eamlocationtype_id);
        EAMLocationTypeDTO dto = eamlocationtypeMapping.toDto(domain);
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @ApiOperation(value = "获取功能位置类型草稿", tags = {"功能位置类型" },  notes = "获取功能位置类型草稿")
	@RequestMapping(method = RequestMethod.GET, value = "/eamlocationtypes/getdraft")
    public ResponseEntity<EAMLocationTypeDTO> getDraft() {
        return ResponseEntity.status(HttpStatus.OK).body(eamlocationtypeMapping.toDto(eamlocationtypeService.getDraft(new EAMLocationType())));
    }

    @ApiOperation(value = "检查功能位置类型", tags = {"功能位置类型" },  notes = "检查功能位置类型")
	@RequestMapping(method = RequestMethod.POST, value = "/eamlocationtypes/checkkey")
    public ResponseEntity<Boolean> checkKey(@RequestBody EAMLocationTypeDTO eamlocationtypedto) {
        return  ResponseEntity.status(HttpStatus.OK).body(eamlocationtypeService.checkKey(eamlocationtypeMapping.toDomain(eamlocationtypedto)));
    }

    @PreAuthorize("hasPermission(this.eamlocationtypeMapping.toDomain(#eamlocationtypedto),'iBizAssetManagement-EAMLocationType-Save')")
    @ApiOperation(value = "保存功能位置类型", tags = {"功能位置类型" },  notes = "保存功能位置类型")
	@RequestMapping(method = RequestMethod.POST, value = "/eamlocationtypes/save")
    public ResponseEntity<Boolean> save(@RequestBody EAMLocationTypeDTO eamlocationtypedto) {
        return ResponseEntity.status(HttpStatus.OK).body(eamlocationtypeService.save(eamlocationtypeMapping.toDomain(eamlocationtypedto)));
    }

    @PreAuthorize("hasPermission(this.eamlocationtypeMapping.toDomain(#eamlocationtypedtos),'iBizAssetManagement-EAMLocationType-Save')")
    @ApiOperation(value = "批量保存功能位置类型", tags = {"功能位置类型" },  notes = "批量保存功能位置类型")
	@RequestMapping(method = RequestMethod.POST, value = "/eamlocationtypes/savebatch")
    public ResponseEntity<Boolean> saveBatch(@RequestBody List<EAMLocationTypeDTO> eamlocationtypedtos) {
        eamlocationtypeService.saveBatch(eamlocationtypeMapping.toDomain(eamlocationtypedtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

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

