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.EAMWOState;
import cn.ibizlab.assetmanagement.core.workorder.service.IEAMWOStateService;
import cn.ibizlab.assetmanagement.core.workorder.filter.EAMWOStateSearchContext;
import cn.ibizlab.assetmanagement.util.annotation.VersionCheck;

@Slf4j
@Api(tags = {"工单状态" })
@RestController("AssetApi-eamwostate")
@RequestMapping("")
public class EAMWOStateResource {

    @Autowired
    public IEAMWOStateService eamwostateService;

    @Autowired
    @Lazy
    public EAMWOStateMapping eamwostateMapping;

    @PreAuthorize("hasPermission(this.eamwostateMapping.toDomain(#eamwostatedto),'iBizAssetManagement-EAMWOState-Create')")
    @ApiOperation(value = "新建工单状态", tags = {"工单状态" },  notes = "新建工单状态")
	@RequestMapping(method = RequestMethod.POST, value = "/eamwostates")
    @Transactional
    public ResponseEntity<EAMWOStateDTO> create(@RequestBody EAMWOStateDTO eamwostatedto) {
        EAMWOState domain = eamwostateMapping.toDomain(eamwostatedto);
		eamwostateService.create(domain);
        EAMWOStateDTO dto = eamwostateMapping.toDto(domain);
		return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @PreAuthorize("hasPermission(this.eamwostateMapping.toDomain(#eamwostatedtos),'iBizAssetManagement-EAMWOState-Create')")
    @ApiOperation(value = "批量新建工单状态", tags = {"工单状态" },  notes = "批量新建工单状态")
	@RequestMapping(method = RequestMethod.POST, value = "/eamwostates/batch")
    public ResponseEntity<Boolean> createBatch(@RequestBody List<EAMWOStateDTO> eamwostatedtos) {
        eamwostateService.createBatch(eamwostateMapping.toDomain(eamwostatedtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @VersionCheck(entity = "eamwostate" , versionfield = "updatedate")
    @PreAuthorize("hasPermission(this.eamwostateService.get(#eamwostate_id),'iBizAssetManagement-EAMWOState-Update')")
    @ApiOperation(value = "更新工单状态", tags = {"工单状态" },  notes = "更新工单状态")
	@RequestMapping(method = RequestMethod.PUT, value = "/eamwostates/{eamwostate_id}")
    @Transactional
    public ResponseEntity<EAMWOStateDTO> update(@PathVariable("eamwostate_id") String eamwostate_id, @RequestBody EAMWOStateDTO eamwostatedto) {
		EAMWOState domain  = eamwostateMapping.toDomain(eamwostatedto);
        domain .setEamwostateid(eamwostate_id);
		eamwostateService.update(domain );
		EAMWOStateDTO dto = eamwostateMapping.toDto(domain );
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @PreAuthorize("hasPermission(this.eamwostateService.getEamwostateByEntities(this.eamwostateMapping.toDomain(#eamwostatedtos)),'iBizAssetManagement-EAMWOState-Update')")
    @ApiOperation(value = "批量更新工单状态", tags = {"工单状态" },  notes = "批量更新工单状态")
	@RequestMapping(method = RequestMethod.PUT, value = "/eamwostates/batch")
    public ResponseEntity<Boolean> updateBatch(@RequestBody List<EAMWOStateDTO> eamwostatedtos) {
        eamwostateService.updateBatch(eamwostateMapping.toDomain(eamwostatedtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @PreAuthorize("hasPermission(this.eamwostateService.get(#eamwostate_id),'iBizAssetManagement-EAMWOState-Remove')")
    @ApiOperation(value = "删除工单状态", tags = {"工单状态" },  notes = "删除工单状态")
	@RequestMapping(method = RequestMethod.DELETE, value = "/eamwostates/{eamwostate_id}")
    @Transactional
    public ResponseEntity<Boolean> remove(@PathVariable("eamwostate_id") String eamwostate_id) {
         return ResponseEntity.status(HttpStatus.OK).body(eamwostateService.remove(eamwostate_id));
    }

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

    @PostAuthorize("hasPermission(this.eamwostateMapping.toDomain(returnObject.body),'iBizAssetManagement-EAMWOState-Get')")
    @ApiOperation(value = "获取工单状态", tags = {"工单状态" },  notes = "获取工单状态")
	@RequestMapping(method = RequestMethod.GET, value = "/eamwostates/{eamwostate_id}")
    public ResponseEntity<EAMWOStateDTO> get(@PathVariable("eamwostate_id") String eamwostate_id) {
        EAMWOState domain = eamwostateService.get(eamwostate_id);
        EAMWOStateDTO dto = eamwostateMapping.toDto(domain);
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @ApiOperation(value = "获取工单状态草稿", tags = {"工单状态" },  notes = "获取工单状态草稿")
	@RequestMapping(method = RequestMethod.GET, value = "/eamwostates/getdraft")
    public ResponseEntity<EAMWOStateDTO> getDraft() {
        return ResponseEntity.status(HttpStatus.OK).body(eamwostateMapping.toDto(eamwostateService.getDraft(new EAMWOState())));
    }

    @ApiOperation(value = "检查工单状态", tags = {"工单状态" },  notes = "检查工单状态")
	@RequestMapping(method = RequestMethod.POST, value = "/eamwostates/checkkey")
    public ResponseEntity<Boolean> checkKey(@RequestBody EAMWOStateDTO eamwostatedto) {
        return  ResponseEntity.status(HttpStatus.OK).body(eamwostateService.checkKey(eamwostateMapping.toDomain(eamwostatedto)));
    }

    @PreAuthorize("hasPermission(this.eamwostateMapping.toDomain(#eamwostatedto),'iBizAssetManagement-EAMWOState-Save')")
    @ApiOperation(value = "保存工单状态", tags = {"工单状态" },  notes = "保存工单状态")
	@RequestMapping(method = RequestMethod.POST, value = "/eamwostates/save")
    public ResponseEntity<Boolean> save(@RequestBody EAMWOStateDTO eamwostatedto) {
        return ResponseEntity.status(HttpStatus.OK).body(eamwostateService.save(eamwostateMapping.toDomain(eamwostatedto)));
    }

    @PreAuthorize("hasPermission(this.eamwostateMapping.toDomain(#eamwostatedtos),'iBizAssetManagement-EAMWOState-Save')")
    @ApiOperation(value = "批量保存工单状态", tags = {"工单状态" },  notes = "批量保存工单状态")
	@RequestMapping(method = RequestMethod.POST, value = "/eamwostates/savebatch")
    public ResponseEntity<Boolean> saveBatch(@RequestBody List<EAMWOStateDTO> eamwostatedtos) {
        eamwostateService.saveBatch(eamwostateMapping.toDomain(eamwostatedtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

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

