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.apply.domain.EAMApplyStateModelLine;
import cn.ibizlab.assetmanagement.core.apply.service.IEAMApplyStateModelLineService;
import cn.ibizlab.assetmanagement.core.apply.filter.EAMApplyStateModelLineSearchContext;
import cn.ibizlab.assetmanagement.util.annotation.VersionCheck;

@Slf4j
@Api(tags = {"维护申请状态模型明细" })
@RestController("AssetApi-eamapplystatemodelline")
@RequestMapping("")
public class EAMApplyStateModelLineResource {

    @Autowired
    public IEAMApplyStateModelLineService eamapplystatemodellineService;

    @Autowired
    @Lazy
    public EAMApplyStateModelLineMapping eamapplystatemodellineMapping;

    @PreAuthorize("hasPermission(this.eamapplystatemodellineMapping.toDomain(#eamapplystatemodellinedto),'iBizAssetManagement-EAMApplyStateModelLine-Create')")
    @ApiOperation(value = "新建维护申请状态模型明细", tags = {"维护申请状态模型明细" },  notes = "新建维护申请状态模型明细")
	@RequestMapping(method = RequestMethod.POST, value = "/eamapplystatemodellines")
    @Transactional
    public ResponseEntity<EAMApplyStateModelLineDTO> create(@RequestBody EAMApplyStateModelLineDTO eamapplystatemodellinedto) {
        EAMApplyStateModelLine domain = eamapplystatemodellineMapping.toDomain(eamapplystatemodellinedto);
		eamapplystatemodellineService.create(domain);
        EAMApplyStateModelLineDTO dto = eamapplystatemodellineMapping.toDto(domain);
		return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @PreAuthorize("hasPermission(this.eamapplystatemodellineMapping.toDomain(#eamapplystatemodellinedtos),'iBizAssetManagement-EAMApplyStateModelLine-Create')")
    @ApiOperation(value = "批量新建维护申请状态模型明细", tags = {"维护申请状态模型明细" },  notes = "批量新建维护申请状态模型明细")
	@RequestMapping(method = RequestMethod.POST, value = "/eamapplystatemodellines/batch")
    public ResponseEntity<Boolean> createBatch(@RequestBody List<EAMApplyStateModelLineDTO> eamapplystatemodellinedtos) {
        eamapplystatemodellineService.createBatch(eamapplystatemodellineMapping.toDomain(eamapplystatemodellinedtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @VersionCheck(entity = "eamapplystatemodelline" , versionfield = "updatedate")
    @PreAuthorize("hasPermission(this.eamapplystatemodellineService.get(#eamapplystatemodelline_id),'iBizAssetManagement-EAMApplyStateModelLine-Update')")
    @ApiOperation(value = "更新维护申请状态模型明细", tags = {"维护申请状态模型明细" },  notes = "更新维护申请状态模型明细")
	@RequestMapping(method = RequestMethod.PUT, value = "/eamapplystatemodellines/{eamapplystatemodelline_id}")
    @Transactional
    public ResponseEntity<EAMApplyStateModelLineDTO> update(@PathVariable("eamapplystatemodelline_id") String eamapplystatemodelline_id, @RequestBody EAMApplyStateModelLineDTO eamapplystatemodellinedto) {
		EAMApplyStateModelLine domain  = eamapplystatemodellineMapping.toDomain(eamapplystatemodellinedto);
        domain .setEamapplystatemodellineid(eamapplystatemodelline_id);
		eamapplystatemodellineService.update(domain );
		EAMApplyStateModelLineDTO dto = eamapplystatemodellineMapping.toDto(domain );
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @PreAuthorize("hasPermission(this.eamapplystatemodellineService.getEamapplystatemodellineByEntities(this.eamapplystatemodellineMapping.toDomain(#eamapplystatemodellinedtos)),'iBizAssetManagement-EAMApplyStateModelLine-Update')")
    @ApiOperation(value = "批量更新维护申请状态模型明细", tags = {"维护申请状态模型明细" },  notes = "批量更新维护申请状态模型明细")
	@RequestMapping(method = RequestMethod.PUT, value = "/eamapplystatemodellines/batch")
    public ResponseEntity<Boolean> updateBatch(@RequestBody List<EAMApplyStateModelLineDTO> eamapplystatemodellinedtos) {
        eamapplystatemodellineService.updateBatch(eamapplystatemodellineMapping.toDomain(eamapplystatemodellinedtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @PreAuthorize("hasPermission(this.eamapplystatemodellineService.get(#eamapplystatemodelline_id),'iBizAssetManagement-EAMApplyStateModelLine-Remove')")
    @ApiOperation(value = "删除维护申请状态模型明细", tags = {"维护申请状态模型明细" },  notes = "删除维护申请状态模型明细")
	@RequestMapping(method = RequestMethod.DELETE, value = "/eamapplystatemodellines/{eamapplystatemodelline_id}")
    @Transactional
    public ResponseEntity<Boolean> remove(@PathVariable("eamapplystatemodelline_id") String eamapplystatemodelline_id) {
         return ResponseEntity.status(HttpStatus.OK).body(eamapplystatemodellineService.remove(eamapplystatemodelline_id));
    }

    @PreAuthorize("hasPermission(this.eamapplystatemodellineService.getEamapplystatemodellineByIds(#ids),'iBizAssetManagement-EAMApplyStateModelLine-Remove')")
    @ApiOperation(value = "批量删除维护申请状态模型明细", tags = {"维护申请状态模型明细" },  notes = "批量删除维护申请状态模型明细")
	@RequestMapping(method = RequestMethod.DELETE, value = "/eamapplystatemodellines/batch")
    public ResponseEntity<Boolean> removeBatch(@RequestBody List<String> ids) {
        eamapplystatemodellineService.removeBatch(ids);
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @PostAuthorize("hasPermission(this.eamapplystatemodellineMapping.toDomain(returnObject.body),'iBizAssetManagement-EAMApplyStateModelLine-Get')")
    @ApiOperation(value = "获取维护申请状态模型明细", tags = {"维护申请状态模型明细" },  notes = "获取维护申请状态模型明细")
	@RequestMapping(method = RequestMethod.GET, value = "/eamapplystatemodellines/{eamapplystatemodelline_id}")
    public ResponseEntity<EAMApplyStateModelLineDTO> get(@PathVariable("eamapplystatemodelline_id") String eamapplystatemodelline_id) {
        EAMApplyStateModelLine domain = eamapplystatemodellineService.get(eamapplystatemodelline_id);
        EAMApplyStateModelLineDTO dto = eamapplystatemodellineMapping.toDto(domain);
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @ApiOperation(value = "获取维护申请状态模型明细草稿", tags = {"维护申请状态模型明细" },  notes = "获取维护申请状态模型明细草稿")
	@RequestMapping(method = RequestMethod.GET, value = "/eamapplystatemodellines/getdraft")
    public ResponseEntity<EAMApplyStateModelLineDTO> getDraft() {
        return ResponseEntity.status(HttpStatus.OK).body(eamapplystatemodellineMapping.toDto(eamapplystatemodellineService.getDraft(new EAMApplyStateModelLine())));
    }

    @ApiOperation(value = "检查维护申请状态模型明细", tags = {"维护申请状态模型明细" },  notes = "检查维护申请状态模型明细")
	@RequestMapping(method = RequestMethod.POST, value = "/eamapplystatemodellines/checkkey")
    public ResponseEntity<Boolean> checkKey(@RequestBody EAMApplyStateModelLineDTO eamapplystatemodellinedto) {
        return  ResponseEntity.status(HttpStatus.OK).body(eamapplystatemodellineService.checkKey(eamapplystatemodellineMapping.toDomain(eamapplystatemodellinedto)));
    }

    @PreAuthorize("hasPermission(this.eamapplystatemodellineMapping.toDomain(#eamapplystatemodellinedto),'iBizAssetManagement-EAMApplyStateModelLine-Save')")
    @ApiOperation(value = "保存维护申请状态模型明细", tags = {"维护申请状态模型明细" },  notes = "保存维护申请状态模型明细")
	@RequestMapping(method = RequestMethod.POST, value = "/eamapplystatemodellines/save")
    public ResponseEntity<Boolean> save(@RequestBody EAMApplyStateModelLineDTO eamapplystatemodellinedto) {
        return ResponseEntity.status(HttpStatus.OK).body(eamapplystatemodellineService.save(eamapplystatemodellineMapping.toDomain(eamapplystatemodellinedto)));
    }

    @PreAuthorize("hasPermission(this.eamapplystatemodellineMapping.toDomain(#eamapplystatemodellinedtos),'iBizAssetManagement-EAMApplyStateModelLine-Save')")
    @ApiOperation(value = "批量保存维护申请状态模型明细", tags = {"维护申请状态模型明细" },  notes = "批量保存维护申请状态模型明细")
	@RequestMapping(method = RequestMethod.POST, value = "/eamapplystatemodellines/savebatch")
    public ResponseEntity<Boolean> saveBatch(@RequestBody List<EAMApplyStateModelLineDTO> eamapplystatemodellinedtos) {
        eamapplystatemodellineService.saveBatch(eamapplystatemodellineMapping.toDomain(eamapplystatemodellinedtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

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

