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.EAMLocationStateModelLine;
import cn.ibizlab.assetmanagement.core.location.service.IEAMLocationStateModelLineService;
import cn.ibizlab.assetmanagement.core.location.filter.EAMLocationStateModelLineSearchContext;
import cn.ibizlab.assetmanagement.util.annotation.VersionCheck;

@Slf4j
@Api(tags = {"功能位置状态模型明细" })
@RestController("AssetApi-eamlocationstatemodelline")
@RequestMapping("")
public class EAMLocationStateModelLineResource {

    @Autowired
    public IEAMLocationStateModelLineService eamlocationstatemodellineService;

    @Autowired
    @Lazy
    public EAMLocationStateModelLineMapping eamlocationstatemodellineMapping;

    @PreAuthorize("hasPermission(this.eamlocationstatemodellineMapping.toDomain(#eamlocationstatemodellinedto),'iBizAssetManagement-EAMLocationStateModelLine-Create')")
    @ApiOperation(value = "新建功能位置状态模型明细", tags = {"功能位置状态模型明细" },  notes = "新建功能位置状态模型明细")
	@RequestMapping(method = RequestMethod.POST, value = "/eamlocationstatemodellines")
    @Transactional
    public ResponseEntity<EAMLocationStateModelLineDTO> create(@RequestBody EAMLocationStateModelLineDTO eamlocationstatemodellinedto) {
        EAMLocationStateModelLine domain = eamlocationstatemodellineMapping.toDomain(eamlocationstatemodellinedto);
		eamlocationstatemodellineService.create(domain);
        EAMLocationStateModelLineDTO dto = eamlocationstatemodellineMapping.toDto(domain);
		return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @PreAuthorize("hasPermission(this.eamlocationstatemodellineMapping.toDomain(#eamlocationstatemodellinedtos),'iBizAssetManagement-EAMLocationStateModelLine-Create')")
    @ApiOperation(value = "批量新建功能位置状态模型明细", tags = {"功能位置状态模型明细" },  notes = "批量新建功能位置状态模型明细")
	@RequestMapping(method = RequestMethod.POST, value = "/eamlocationstatemodellines/batch")
    public ResponseEntity<Boolean> createBatch(@RequestBody List<EAMLocationStateModelLineDTO> eamlocationstatemodellinedtos) {
        eamlocationstatemodellineService.createBatch(eamlocationstatemodellineMapping.toDomain(eamlocationstatemodellinedtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @VersionCheck(entity = "eamlocationstatemodelline" , versionfield = "updatedate")
    @PreAuthorize("hasPermission(this.eamlocationstatemodellineService.get(#eamlocationstatemodelline_id),'iBizAssetManagement-EAMLocationStateModelLine-Update')")
    @ApiOperation(value = "更新功能位置状态模型明细", tags = {"功能位置状态模型明细" },  notes = "更新功能位置状态模型明细")
	@RequestMapping(method = RequestMethod.PUT, value = "/eamlocationstatemodellines/{eamlocationstatemodelline_id}")
    @Transactional
    public ResponseEntity<EAMLocationStateModelLineDTO> update(@PathVariable("eamlocationstatemodelline_id") String eamlocationstatemodelline_id, @RequestBody EAMLocationStateModelLineDTO eamlocationstatemodellinedto) {
		EAMLocationStateModelLine domain  = eamlocationstatemodellineMapping.toDomain(eamlocationstatemodellinedto);
        domain .setEamlocationstatemodellineid(eamlocationstatemodelline_id);
		eamlocationstatemodellineService.update(domain );
		EAMLocationStateModelLineDTO dto = eamlocationstatemodellineMapping.toDto(domain );
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @PreAuthorize("hasPermission(this.eamlocationstatemodellineService.getEamlocationstatemodellineByEntities(this.eamlocationstatemodellineMapping.toDomain(#eamlocationstatemodellinedtos)),'iBizAssetManagement-EAMLocationStateModelLine-Update')")
    @ApiOperation(value = "批量更新功能位置状态模型明细", tags = {"功能位置状态模型明细" },  notes = "批量更新功能位置状态模型明细")
	@RequestMapping(method = RequestMethod.PUT, value = "/eamlocationstatemodellines/batch")
    public ResponseEntity<Boolean> updateBatch(@RequestBody List<EAMLocationStateModelLineDTO> eamlocationstatemodellinedtos) {
        eamlocationstatemodellineService.updateBatch(eamlocationstatemodellineMapping.toDomain(eamlocationstatemodellinedtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

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

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

    @PostAuthorize("hasPermission(this.eamlocationstatemodellineMapping.toDomain(returnObject.body),'iBizAssetManagement-EAMLocationStateModelLine-Get')")
    @ApiOperation(value = "获取功能位置状态模型明细", tags = {"功能位置状态模型明细" },  notes = "获取功能位置状态模型明细")
	@RequestMapping(method = RequestMethod.GET, value = "/eamlocationstatemodellines/{eamlocationstatemodelline_id}")
    public ResponseEntity<EAMLocationStateModelLineDTO> get(@PathVariable("eamlocationstatemodelline_id") String eamlocationstatemodelline_id) {
        EAMLocationStateModelLine domain = eamlocationstatemodellineService.get(eamlocationstatemodelline_id);
        EAMLocationStateModelLineDTO dto = eamlocationstatemodellineMapping.toDto(domain);
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @ApiOperation(value = "获取功能位置状态模型明细草稿", tags = {"功能位置状态模型明细" },  notes = "获取功能位置状态模型明细草稿")
	@RequestMapping(method = RequestMethod.GET, value = "/eamlocationstatemodellines/getdraft")
    public ResponseEntity<EAMLocationStateModelLineDTO> getDraft() {
        return ResponseEntity.status(HttpStatus.OK).body(eamlocationstatemodellineMapping.toDto(eamlocationstatemodellineService.getDraft(new EAMLocationStateModelLine())));
    }

    @ApiOperation(value = "检查功能位置状态模型明细", tags = {"功能位置状态模型明细" },  notes = "检查功能位置状态模型明细")
	@RequestMapping(method = RequestMethod.POST, value = "/eamlocationstatemodellines/checkkey")
    public ResponseEntity<Boolean> checkKey(@RequestBody EAMLocationStateModelLineDTO eamlocationstatemodellinedto) {
        return  ResponseEntity.status(HttpStatus.OK).body(eamlocationstatemodellineService.checkKey(eamlocationstatemodellineMapping.toDomain(eamlocationstatemodellinedto)));
    }

    @PreAuthorize("hasPermission(this.eamlocationstatemodellineMapping.toDomain(#eamlocationstatemodellinedto),'iBizAssetManagement-EAMLocationStateModelLine-Save')")
    @ApiOperation(value = "保存功能位置状态模型明细", tags = {"功能位置状态模型明细" },  notes = "保存功能位置状态模型明细")
	@RequestMapping(method = RequestMethod.POST, value = "/eamlocationstatemodellines/save")
    public ResponseEntity<Boolean> save(@RequestBody EAMLocationStateModelLineDTO eamlocationstatemodellinedto) {
        return ResponseEntity.status(HttpStatus.OK).body(eamlocationstatemodellineService.save(eamlocationstatemodellineMapping.toDomain(eamlocationstatemodellinedto)));
    }

    @PreAuthorize("hasPermission(this.eamlocationstatemodellineMapping.toDomain(#eamlocationstatemodellinedtos),'iBizAssetManagement-EAMLocationStateModelLine-Save')")
    @ApiOperation(value = "批量保存功能位置状态模型明细", tags = {"功能位置状态模型明细" },  notes = "批量保存功能位置状态模型明细")
	@RequestMapping(method = RequestMethod.POST, value = "/eamlocationstatemodellines/savebatch")
    public ResponseEntity<Boolean> saveBatch(@RequestBody List<EAMLocationStateModelLineDTO> eamlocationstatemodellinedtos) {
        eamlocationstatemodellineService.saveBatch(eamlocationstatemodellineMapping.toDomain(eamlocationstatemodellinedtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

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

