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.EAMSparePartsGroup;
import cn.ibizlab.assetmanagement.core.asset.service.IEAMSparePartsGroupService;
import cn.ibizlab.assetmanagement.core.asset.filter.EAMSparePartsGroupSearchContext;
import cn.ibizlab.assetmanagement.util.annotation.VersionCheck;

@Slf4j
@Api(tags = {"备件包" })
@RestController("AssetApi-eamsparepartsgroup")
@RequestMapping("")
public class EAMSparePartsGroupResource {

    @Autowired
    public IEAMSparePartsGroupService eamsparepartsgroupService;

    @Autowired
    @Lazy
    public EAMSparePartsGroupMapping eamsparepartsgroupMapping;

    @PreAuthorize("hasPermission(this.eamsparepartsgroupMapping.toDomain(#eamsparepartsgroupdto),'iBizAssetManagement-EAMSparePartsGroup-Create')")
    @ApiOperation(value = "新建备件包", tags = {"备件包" },  notes = "新建备件包")
	@RequestMapping(method = RequestMethod.POST, value = "/eamsparepartsgroups")
    @Transactional
    public ResponseEntity<EAMSparePartsGroupDTO> create(@RequestBody EAMSparePartsGroupDTO eamsparepartsgroupdto) {
        EAMSparePartsGroup domain = eamsparepartsgroupMapping.toDomain(eamsparepartsgroupdto);
		eamsparepartsgroupService.create(domain);
        EAMSparePartsGroupDTO dto = eamsparepartsgroupMapping.toDto(domain);
		return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @PreAuthorize("hasPermission(this.eamsparepartsgroupMapping.toDomain(#eamsparepartsgroupdtos),'iBizAssetManagement-EAMSparePartsGroup-Create')")
    @ApiOperation(value = "批量新建备件包", tags = {"备件包" },  notes = "批量新建备件包")
	@RequestMapping(method = RequestMethod.POST, value = "/eamsparepartsgroups/batch")
    public ResponseEntity<Boolean> createBatch(@RequestBody List<EAMSparePartsGroupDTO> eamsparepartsgroupdtos) {
        eamsparepartsgroupService.createBatch(eamsparepartsgroupMapping.toDomain(eamsparepartsgroupdtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @VersionCheck(entity = "eamsparepartsgroup" , versionfield = "updatedate")
    @PreAuthorize("hasPermission(this.eamsparepartsgroupService.get(#eamsparepartsgroup_id),'iBizAssetManagement-EAMSparePartsGroup-Update')")
    @ApiOperation(value = "更新备件包", tags = {"备件包" },  notes = "更新备件包")
	@RequestMapping(method = RequestMethod.PUT, value = "/eamsparepartsgroups/{eamsparepartsgroup_id}")
    @Transactional
    public ResponseEntity<EAMSparePartsGroupDTO> update(@PathVariable("eamsparepartsgroup_id") String eamsparepartsgroup_id, @RequestBody EAMSparePartsGroupDTO eamsparepartsgroupdto) {
		EAMSparePartsGroup domain  = eamsparepartsgroupMapping.toDomain(eamsparepartsgroupdto);
        domain .setEamsparepartsgroupid(eamsparepartsgroup_id);
		eamsparepartsgroupService.update(domain );
		EAMSparePartsGroupDTO dto = eamsparepartsgroupMapping.toDto(domain );
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @PreAuthorize("hasPermission(this.eamsparepartsgroupService.getEamsparepartsgroupByEntities(this.eamsparepartsgroupMapping.toDomain(#eamsparepartsgroupdtos)),'iBizAssetManagement-EAMSparePartsGroup-Update')")
    @ApiOperation(value = "批量更新备件包", tags = {"备件包" },  notes = "批量更新备件包")
	@RequestMapping(method = RequestMethod.PUT, value = "/eamsparepartsgroups/batch")
    public ResponseEntity<Boolean> updateBatch(@RequestBody List<EAMSparePartsGroupDTO> eamsparepartsgroupdtos) {
        eamsparepartsgroupService.updateBatch(eamsparepartsgroupMapping.toDomain(eamsparepartsgroupdtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @PreAuthorize("hasPermission(this.eamsparepartsgroupService.get(#eamsparepartsgroup_id),'iBizAssetManagement-EAMSparePartsGroup-Remove')")
    @ApiOperation(value = "删除备件包", tags = {"备件包" },  notes = "删除备件包")
	@RequestMapping(method = RequestMethod.DELETE, value = "/eamsparepartsgroups/{eamsparepartsgroup_id}")
    @Transactional
    public ResponseEntity<Boolean> remove(@PathVariable("eamsparepartsgroup_id") String eamsparepartsgroup_id) {
         return ResponseEntity.status(HttpStatus.OK).body(eamsparepartsgroupService.remove(eamsparepartsgroup_id));
    }

    @PreAuthorize("hasPermission(this.eamsparepartsgroupService.getEamsparepartsgroupByIds(#ids),'iBizAssetManagement-EAMSparePartsGroup-Remove')")
    @ApiOperation(value = "批量删除备件包", tags = {"备件包" },  notes = "批量删除备件包")
	@RequestMapping(method = RequestMethod.DELETE, value = "/eamsparepartsgroups/batch")
    public ResponseEntity<Boolean> removeBatch(@RequestBody List<String> ids) {
        eamsparepartsgroupService.removeBatch(ids);
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @PostAuthorize("hasPermission(this.eamsparepartsgroupMapping.toDomain(returnObject.body),'iBizAssetManagement-EAMSparePartsGroup-Get')")
    @ApiOperation(value = "获取备件包", tags = {"备件包" },  notes = "获取备件包")
	@RequestMapping(method = RequestMethod.GET, value = "/eamsparepartsgroups/{eamsparepartsgroup_id}")
    public ResponseEntity<EAMSparePartsGroupDTO> get(@PathVariable("eamsparepartsgroup_id") String eamsparepartsgroup_id) {
        EAMSparePartsGroup domain = eamsparepartsgroupService.get(eamsparepartsgroup_id);
        EAMSparePartsGroupDTO dto = eamsparepartsgroupMapping.toDto(domain);
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @ApiOperation(value = "获取备件包草稿", tags = {"备件包" },  notes = "获取备件包草稿")
	@RequestMapping(method = RequestMethod.GET, value = "/eamsparepartsgroups/getdraft")
    public ResponseEntity<EAMSparePartsGroupDTO> getDraft() {
        return ResponseEntity.status(HttpStatus.OK).body(eamsparepartsgroupMapping.toDto(eamsparepartsgroupService.getDraft(new EAMSparePartsGroup())));
    }

    @ApiOperation(value = "检查备件包", tags = {"备件包" },  notes = "检查备件包")
	@RequestMapping(method = RequestMethod.POST, value = "/eamsparepartsgroups/checkkey")
    public ResponseEntity<Boolean> checkKey(@RequestBody EAMSparePartsGroupDTO eamsparepartsgroupdto) {
        return  ResponseEntity.status(HttpStatus.OK).body(eamsparepartsgroupService.checkKey(eamsparepartsgroupMapping.toDomain(eamsparepartsgroupdto)));
    }

    @PreAuthorize("hasPermission(this.eamsparepartsgroupMapping.toDomain(#eamsparepartsgroupdto),'iBizAssetManagement-EAMSparePartsGroup-Save')")
    @ApiOperation(value = "保存备件包", tags = {"备件包" },  notes = "保存备件包")
	@RequestMapping(method = RequestMethod.POST, value = "/eamsparepartsgroups/save")
    public ResponseEntity<Boolean> save(@RequestBody EAMSparePartsGroupDTO eamsparepartsgroupdto) {
        return ResponseEntity.status(HttpStatus.OK).body(eamsparepartsgroupService.save(eamsparepartsgroupMapping.toDomain(eamsparepartsgroupdto)));
    }

    @PreAuthorize("hasPermission(this.eamsparepartsgroupMapping.toDomain(#eamsparepartsgroupdtos),'iBizAssetManagement-EAMSparePartsGroup-Save')")
    @ApiOperation(value = "批量保存备件包", tags = {"备件包" },  notes = "批量保存备件包")
	@RequestMapping(method = RequestMethod.POST, value = "/eamsparepartsgroups/savebatch")
    public ResponseEntity<Boolean> saveBatch(@RequestBody List<EAMSparePartsGroupDTO> eamsparepartsgroupdtos) {
        eamsparepartsgroupService.saveBatch(eamsparepartsgroupMapping.toDomain(eamsparepartsgroupdtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','iBizAssetManagement-EAMSparePartsGroup-searchDefault-all') and hasPermission(#context,'iBizAssetManagement-EAMSparePartsGroup-Get')")
	@ApiOperation(value = "获取DEFAULT", tags = {"备件包" } ,notes = "获取DEFAULT")
    @RequestMapping(method= RequestMethod.GET , value="/eamsparepartsgroups/fetchdefault")
	public ResponseEntity<List<EAMSparePartsGroupDTO>> fetchDefault(EAMSparePartsGroupSearchContext context) {
        Page<EAMSparePartsGroup> domains = eamsparepartsgroupService.searchDefault(context) ;
        List<EAMSparePartsGroupDTO> list = eamsparepartsgroupMapping.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-EAMSparePartsGroup-searchDefault-all') and hasPermission(#context,'iBizAssetManagement-EAMSparePartsGroup-Get')")
	@ApiOperation(value = "查询DEFAULT", tags = {"备件包" } ,notes = "查询DEFAULT")
    @RequestMapping(method= RequestMethod.POST , value="/eamsparepartsgroups/searchdefault")
	public ResponseEntity<Page<EAMSparePartsGroupDTO>> searchDefault(@RequestBody EAMSparePartsGroupSearchContext context) {
        Page<EAMSparePartsGroup> domains = eamsparepartsgroupService.searchDefault(context) ;
	    return ResponseEntity.status(HttpStatus.OK)
                .body(new PageImpl(eamsparepartsgroupMapping.toDto(domains.getContent()), context.getPageable(), domains.getTotalElements()));
	}
}

