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.workorder.domain.EAMWOType;
import cn.ibizlab.assetmanagement.core.workorder.service.IEAMWOTypeService;
import cn.ibizlab.assetmanagement.core.workorder.filter.EAMWOTypeSearchContext;
import cn.ibizlab.assetmanagement.util.annotation.VersionCheck;

@Slf4j
@Api(tags = {"工单类型" })
@RestController("AssetApi-eamwotype")
@RequestMapping("")
public class EAMWOTypeResource {

    @Autowired
    public IEAMWOTypeService eamwotypeService;

    @Autowired
    @Lazy
    public EAMWOTypeMapping eamwotypeMapping;

    @PreAuthorize("hasPermission(this.eamwotypeMapping.toDomain(#eamwotypedto),'iBizAssetManagement-EAMWOType-Create')")
    @ApiOperation(value = "新建工单类型", tags = {"工单类型" },  notes = "新建工单类型")
	@RequestMapping(method = RequestMethod.POST, value = "/eamwotypes")
    @Transactional
    public ResponseEntity<EAMWOTypeDTO> create(@RequestBody EAMWOTypeDTO eamwotypedto) {
        EAMWOType domain = eamwotypeMapping.toDomain(eamwotypedto);
		eamwotypeService.create(domain);
        EAMWOTypeDTO dto = eamwotypeMapping.toDto(domain);
		return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @PreAuthorize("hasPermission(this.eamwotypeMapping.toDomain(#eamwotypedtos),'iBizAssetManagement-EAMWOType-Create')")
    @ApiOperation(value = "批量新建工单类型", tags = {"工单类型" },  notes = "批量新建工单类型")
	@RequestMapping(method = RequestMethod.POST, value = "/eamwotypes/batch")
    public ResponseEntity<Boolean> createBatch(@RequestBody List<EAMWOTypeDTO> eamwotypedtos) {
        eamwotypeService.createBatch(eamwotypeMapping.toDomain(eamwotypedtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @VersionCheck(entity = "eamwotype" , versionfield = "updatedate")
    @PreAuthorize("hasPermission(this.eamwotypeService.get(#eamwotype_id),'iBizAssetManagement-EAMWOType-Update')")
    @ApiOperation(value = "更新工单类型", tags = {"工单类型" },  notes = "更新工单类型")
	@RequestMapping(method = RequestMethod.PUT, value = "/eamwotypes/{eamwotype_id}")
    @Transactional
    public ResponseEntity<EAMWOTypeDTO> update(@PathVariable("eamwotype_id") String eamwotype_id, @RequestBody EAMWOTypeDTO eamwotypedto) {
		EAMWOType domain  = eamwotypeMapping.toDomain(eamwotypedto);
        domain .setEamwotypeid(eamwotype_id);
		eamwotypeService.update(domain );
		EAMWOTypeDTO dto = eamwotypeMapping.toDto(domain );
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @PreAuthorize("hasPermission(this.eamwotypeService.getEamwotypeByEntities(this.eamwotypeMapping.toDomain(#eamwotypedtos)),'iBizAssetManagement-EAMWOType-Update')")
    @ApiOperation(value = "批量更新工单类型", tags = {"工单类型" },  notes = "批量更新工单类型")
	@RequestMapping(method = RequestMethod.PUT, value = "/eamwotypes/batch")
    public ResponseEntity<Boolean> updateBatch(@RequestBody List<EAMWOTypeDTO> eamwotypedtos) {
        eamwotypeService.updateBatch(eamwotypeMapping.toDomain(eamwotypedtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @PreAuthorize("hasPermission(this.eamwotypeService.get(#eamwotype_id),'iBizAssetManagement-EAMWOType-Remove')")
    @ApiOperation(value = "删除工单类型", tags = {"工单类型" },  notes = "删除工单类型")
	@RequestMapping(method = RequestMethod.DELETE, value = "/eamwotypes/{eamwotype_id}")
    @Transactional
    public ResponseEntity<Boolean> remove(@PathVariable("eamwotype_id") String eamwotype_id) {
         return ResponseEntity.status(HttpStatus.OK).body(eamwotypeService.remove(eamwotype_id));
    }

    @PreAuthorize("hasPermission(this.eamwotypeService.getEamwotypeByIds(#ids),'iBizAssetManagement-EAMWOType-Remove')")
    @ApiOperation(value = "批量删除工单类型", tags = {"工单类型" },  notes = "批量删除工单类型")
	@RequestMapping(method = RequestMethod.DELETE, value = "/eamwotypes/batch")
    public ResponseEntity<Boolean> removeBatch(@RequestBody List<String> ids) {
        eamwotypeService.removeBatch(ids);
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @PostAuthorize("hasPermission(this.eamwotypeMapping.toDomain(returnObject.body),'iBizAssetManagement-EAMWOType-Get')")
    @ApiOperation(value = "获取工单类型", tags = {"工单类型" },  notes = "获取工单类型")
	@RequestMapping(method = RequestMethod.GET, value = "/eamwotypes/{eamwotype_id}")
    public ResponseEntity<EAMWOTypeDTO> get(@PathVariable("eamwotype_id") String eamwotype_id) {
        EAMWOType domain = eamwotypeService.get(eamwotype_id);
        EAMWOTypeDTO dto = eamwotypeMapping.toDto(domain);
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @ApiOperation(value = "获取工单类型草稿", tags = {"工单类型" },  notes = "获取工单类型草稿")
	@RequestMapping(method = RequestMethod.GET, value = "/eamwotypes/getdraft")
    public ResponseEntity<EAMWOTypeDTO> getDraft() {
        return ResponseEntity.status(HttpStatus.OK).body(eamwotypeMapping.toDto(eamwotypeService.getDraft(new EAMWOType())));
    }

    @ApiOperation(value = "检查工单类型", tags = {"工单类型" },  notes = "检查工单类型")
	@RequestMapping(method = RequestMethod.POST, value = "/eamwotypes/checkkey")
    public ResponseEntity<Boolean> checkKey(@RequestBody EAMWOTypeDTO eamwotypedto) {
        return  ResponseEntity.status(HttpStatus.OK).body(eamwotypeService.checkKey(eamwotypeMapping.toDomain(eamwotypedto)));
    }

    @PreAuthorize("hasPermission(this.eamwotypeMapping.toDomain(#eamwotypedto),'iBizAssetManagement-EAMWOType-Save')")
    @ApiOperation(value = "保存工单类型", tags = {"工单类型" },  notes = "保存工单类型")
	@RequestMapping(method = RequestMethod.POST, value = "/eamwotypes/save")
    public ResponseEntity<Boolean> save(@RequestBody EAMWOTypeDTO eamwotypedto) {
        return ResponseEntity.status(HttpStatus.OK).body(eamwotypeService.save(eamwotypeMapping.toDomain(eamwotypedto)));
    }

    @PreAuthorize("hasPermission(this.eamwotypeMapping.toDomain(#eamwotypedtos),'iBizAssetManagement-EAMWOType-Save')")
    @ApiOperation(value = "批量保存工单类型", tags = {"工单类型" },  notes = "批量保存工单类型")
	@RequestMapping(method = RequestMethod.POST, value = "/eamwotypes/savebatch")
    public ResponseEntity<Boolean> saveBatch(@RequestBody List<EAMWOTypeDTO> eamwotypedtos) {
        eamwotypeService.saveBatch(eamwotypeMapping.toDomain(eamwotypedtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','iBizAssetManagement-EAMWOType-searchDefault-all') and hasPermission(#context,'iBizAssetManagement-EAMWOType-Get')")
	@ApiOperation(value = "获取DEFAULT", tags = {"工单类型" } ,notes = "获取DEFAULT")
    @RequestMapping(method= RequestMethod.GET , value="/eamwotypes/fetchdefault")
	public ResponseEntity<List<EAMWOTypeDTO>> fetchDefault(EAMWOTypeSearchContext context) {
        Page<EAMWOType> domains = eamwotypeService.searchDefault(context) ;
        List<EAMWOTypeDTO> list = eamwotypeMapping.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-EAMWOType-searchDefault-all') and hasPermission(#context,'iBizAssetManagement-EAMWOType-Get')")
	@ApiOperation(value = "查询DEFAULT", tags = {"工单类型" } ,notes = "查询DEFAULT")
    @RequestMapping(method= RequestMethod.POST , value="/eamwotypes/searchdefault")
	public ResponseEntity<Page<EAMWOTypeDTO>> searchDefault(@RequestBody EAMWOTypeSearchContext context) {
        Page<EAMWOType> domains = eamwotypeService.searchDefault(context) ;
	    return ResponseEntity.status(HttpStatus.OK)
                .body(new PageImpl(eamwotypeMapping.toDto(domains.getContent()), context.getPageable(), domains.getTotalElements()));
	}
}

