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.asset.domain.EAMAssetStateModelLine;
import cn.ibizlab.assetmanagement.core.asset.service.IEAMAssetStateModelLineService;
import cn.ibizlab.assetmanagement.core.asset.filter.EAMAssetStateModelLineSearchContext;
import cn.ibizlab.assetmanagement.util.annotation.VersionCheck;

@Slf4j
@Api(tags = {"资产状态模型明细" })
@RestController("AssetApi-eamassetstatemodelline")
@RequestMapping("")
public class EAMAssetStateModelLineResource {

    @Autowired
    public IEAMAssetStateModelLineService eamassetstatemodellineService;

    @Autowired
    @Lazy
    public EAMAssetStateModelLineMapping eamassetstatemodellineMapping;

    @PreAuthorize("hasPermission(this.eamassetstatemodellineMapping.toDomain(#eamassetstatemodellinedto),'iBizAssetManagement-EAMAssetStateModelLine-Create')")
    @ApiOperation(value = "新建资产状态模型明细", tags = {"资产状态模型明细" },  notes = "新建资产状态模型明细")
	@RequestMapping(method = RequestMethod.POST, value = "/eamassetstatemodellines")
    @Transactional
    public ResponseEntity<EAMAssetStateModelLineDTO> create(@RequestBody EAMAssetStateModelLineDTO eamassetstatemodellinedto) {
        EAMAssetStateModelLine domain = eamassetstatemodellineMapping.toDomain(eamassetstatemodellinedto);
		eamassetstatemodellineService.create(domain);
        EAMAssetStateModelLineDTO dto = eamassetstatemodellineMapping.toDto(domain);
		return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @PreAuthorize("hasPermission(this.eamassetstatemodellineMapping.toDomain(#eamassetstatemodellinedtos),'iBizAssetManagement-EAMAssetStateModelLine-Create')")
    @ApiOperation(value = "批量新建资产状态模型明细", tags = {"资产状态模型明细" },  notes = "批量新建资产状态模型明细")
	@RequestMapping(method = RequestMethod.POST, value = "/eamassetstatemodellines/batch")
    public ResponseEntity<Boolean> createBatch(@RequestBody List<EAMAssetStateModelLineDTO> eamassetstatemodellinedtos) {
        eamassetstatemodellineService.createBatch(eamassetstatemodellineMapping.toDomain(eamassetstatemodellinedtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @VersionCheck(entity = "eamassetstatemodelline" , versionfield = "updatedate")
    @PreAuthorize("hasPermission(this.eamassetstatemodellineService.get(#eamassetstatemodelline_id),'iBizAssetManagement-EAMAssetStateModelLine-Update')")
    @ApiOperation(value = "更新资产状态模型明细", tags = {"资产状态模型明细" },  notes = "更新资产状态模型明细")
	@RequestMapping(method = RequestMethod.PUT, value = "/eamassetstatemodellines/{eamassetstatemodelline_id}")
    @Transactional
    public ResponseEntity<EAMAssetStateModelLineDTO> update(@PathVariable("eamassetstatemodelline_id") String eamassetstatemodelline_id, @RequestBody EAMAssetStateModelLineDTO eamassetstatemodellinedto) {
		EAMAssetStateModelLine domain  = eamassetstatemodellineMapping.toDomain(eamassetstatemodellinedto);
        domain .setEamassetstatemodellineid(eamassetstatemodelline_id);
		eamassetstatemodellineService.update(domain );
		EAMAssetStateModelLineDTO dto = eamassetstatemodellineMapping.toDto(domain );
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @PreAuthorize("hasPermission(this.eamassetstatemodellineService.getEamassetstatemodellineByEntities(this.eamassetstatemodellineMapping.toDomain(#eamassetstatemodellinedtos)),'iBizAssetManagement-EAMAssetStateModelLine-Update')")
    @ApiOperation(value = "批量更新资产状态模型明细", tags = {"资产状态模型明细" },  notes = "批量更新资产状态模型明细")
	@RequestMapping(method = RequestMethod.PUT, value = "/eamassetstatemodellines/batch")
    public ResponseEntity<Boolean> updateBatch(@RequestBody List<EAMAssetStateModelLineDTO> eamassetstatemodellinedtos) {
        eamassetstatemodellineService.updateBatch(eamassetstatemodellineMapping.toDomain(eamassetstatemodellinedtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @PreAuthorize("hasPermission(this.eamassetstatemodellineService.get(#eamassetstatemodelline_id),'iBizAssetManagement-EAMAssetStateModelLine-Remove')")
    @ApiOperation(value = "删除资产状态模型明细", tags = {"资产状态模型明细" },  notes = "删除资产状态模型明细")
	@RequestMapping(method = RequestMethod.DELETE, value = "/eamassetstatemodellines/{eamassetstatemodelline_id}")
    @Transactional
    public ResponseEntity<Boolean> remove(@PathVariable("eamassetstatemodelline_id") String eamassetstatemodelline_id) {
         return ResponseEntity.status(HttpStatus.OK).body(eamassetstatemodellineService.remove(eamassetstatemodelline_id));
    }

    @PreAuthorize("hasPermission(this.eamassetstatemodellineService.getEamassetstatemodellineByIds(#ids),'iBizAssetManagement-EAMAssetStateModelLine-Remove')")
    @ApiOperation(value = "批量删除资产状态模型明细", tags = {"资产状态模型明细" },  notes = "批量删除资产状态模型明细")
	@RequestMapping(method = RequestMethod.DELETE, value = "/eamassetstatemodellines/batch")
    public ResponseEntity<Boolean> removeBatch(@RequestBody List<String> ids) {
        eamassetstatemodellineService.removeBatch(ids);
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @PostAuthorize("hasPermission(this.eamassetstatemodellineMapping.toDomain(returnObject.body),'iBizAssetManagement-EAMAssetStateModelLine-Get')")
    @ApiOperation(value = "获取资产状态模型明细", tags = {"资产状态模型明细" },  notes = "获取资产状态模型明细")
	@RequestMapping(method = RequestMethod.GET, value = "/eamassetstatemodellines/{eamassetstatemodelline_id}")
    public ResponseEntity<EAMAssetStateModelLineDTO> get(@PathVariable("eamassetstatemodelline_id") String eamassetstatemodelline_id) {
        EAMAssetStateModelLine domain = eamassetstatemodellineService.get(eamassetstatemodelline_id);
        EAMAssetStateModelLineDTO dto = eamassetstatemodellineMapping.toDto(domain);
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @ApiOperation(value = "获取资产状态模型明细草稿", tags = {"资产状态模型明细" },  notes = "获取资产状态模型明细草稿")
	@RequestMapping(method = RequestMethod.GET, value = "/eamassetstatemodellines/getdraft")
    public ResponseEntity<EAMAssetStateModelLineDTO> getDraft() {
        return ResponseEntity.status(HttpStatus.OK).body(eamassetstatemodellineMapping.toDto(eamassetstatemodellineService.getDraft(new EAMAssetStateModelLine())));
    }

    @ApiOperation(value = "检查资产状态模型明细", tags = {"资产状态模型明细" },  notes = "检查资产状态模型明细")
	@RequestMapping(method = RequestMethod.POST, value = "/eamassetstatemodellines/checkkey")
    public ResponseEntity<Boolean> checkKey(@RequestBody EAMAssetStateModelLineDTO eamassetstatemodellinedto) {
        return  ResponseEntity.status(HttpStatus.OK).body(eamassetstatemodellineService.checkKey(eamassetstatemodellineMapping.toDomain(eamassetstatemodellinedto)));
    }

    @PreAuthorize("hasPermission(this.eamassetstatemodellineMapping.toDomain(#eamassetstatemodellinedto),'iBizAssetManagement-EAMAssetStateModelLine-Save')")
    @ApiOperation(value = "保存资产状态模型明细", tags = {"资产状态模型明细" },  notes = "保存资产状态模型明细")
	@RequestMapping(method = RequestMethod.POST, value = "/eamassetstatemodellines/save")
    public ResponseEntity<Boolean> save(@RequestBody EAMAssetStateModelLineDTO eamassetstatemodellinedto) {
        return ResponseEntity.status(HttpStatus.OK).body(eamassetstatemodellineService.save(eamassetstatemodellineMapping.toDomain(eamassetstatemodellinedto)));
    }

    @PreAuthorize("hasPermission(this.eamassetstatemodellineMapping.toDomain(#eamassetstatemodellinedtos),'iBizAssetManagement-EAMAssetStateModelLine-Save')")
    @ApiOperation(value = "批量保存资产状态模型明细", tags = {"资产状态模型明细" },  notes = "批量保存资产状态模型明细")
	@RequestMapping(method = RequestMethod.POST, value = "/eamassetstatemodellines/savebatch")
    public ResponseEntity<Boolean> saveBatch(@RequestBody List<EAMAssetStateModelLineDTO> eamassetstatemodellinedtos) {
        eamassetstatemodellineService.saveBatch(eamassetstatemodellineMapping.toDomain(eamassetstatemodellinedtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

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

