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.WOOrigin;
import cn.ibizlab.assetmanagement.core.workorder.service.IWOOriginService;
import cn.ibizlab.assetmanagement.core.workorder.filter.WOOriginSearchContext;
import cn.ibizlab.assetmanagement.util.annotation.VersionCheck;

@Slf4j
@Api(tags = {"工单来源" })
@RestController("AssetApi-woorigin")
@RequestMapping("")
public class WOOriginResource {

    @Autowired
    public IWOOriginService wooriginService;

    @Autowired
    @Lazy
    public WOOriginMapping wooriginMapping;

    @PreAuthorize("hasPermission(this.wooriginMapping.toDomain(#woorigindto),'iBizAssetManagement-WOOrigin-Create')")
    @ApiOperation(value = "新建工单来源", tags = {"工单来源" },  notes = "新建工单来源")
	@RequestMapping(method = RequestMethod.POST, value = "/woorigins")
    @Transactional
    public ResponseEntity<WOOriginDTO> create(@RequestBody WOOriginDTO woorigindto) {
        WOOrigin domain = wooriginMapping.toDomain(woorigindto);
		wooriginService.create(domain);
        WOOriginDTO dto = wooriginMapping.toDto(domain);
		return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @PreAuthorize("hasPermission(this.wooriginMapping.toDomain(#woorigindtos),'iBizAssetManagement-WOOrigin-Create')")
    @ApiOperation(value = "批量新建工单来源", tags = {"工单来源" },  notes = "批量新建工单来源")
	@RequestMapping(method = RequestMethod.POST, value = "/woorigins/batch")
    public ResponseEntity<Boolean> createBatch(@RequestBody List<WOOriginDTO> woorigindtos) {
        wooriginService.createBatch(wooriginMapping.toDomain(woorigindtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @VersionCheck(entity = "woorigin" , versionfield = "updatedate")
    @PreAuthorize("hasPermission(this.wooriginService.get(#woorigin_id),'iBizAssetManagement-WOOrigin-Update')")
    @ApiOperation(value = "更新工单来源", tags = {"工单来源" },  notes = "更新工单来源")
	@RequestMapping(method = RequestMethod.PUT, value = "/woorigins/{woorigin_id}")
    @Transactional
    public ResponseEntity<WOOriginDTO> update(@PathVariable("woorigin_id") String woorigin_id, @RequestBody WOOriginDTO woorigindto) {
		WOOrigin domain  = wooriginMapping.toDomain(woorigindto);
        domain .setWooriginid(woorigin_id);
		wooriginService.update(domain );
		WOOriginDTO dto = wooriginMapping.toDto(domain );
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @PreAuthorize("hasPermission(this.wooriginService.getWooriginByEntities(this.wooriginMapping.toDomain(#woorigindtos)),'iBizAssetManagement-WOOrigin-Update')")
    @ApiOperation(value = "批量更新工单来源", tags = {"工单来源" },  notes = "批量更新工单来源")
	@RequestMapping(method = RequestMethod.PUT, value = "/woorigins/batch")
    public ResponseEntity<Boolean> updateBatch(@RequestBody List<WOOriginDTO> woorigindtos) {
        wooriginService.updateBatch(wooriginMapping.toDomain(woorigindtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @PreAuthorize("hasPermission(this.wooriginService.get(#woorigin_id),'iBizAssetManagement-WOOrigin-Remove')")
    @ApiOperation(value = "删除工单来源", tags = {"工单来源" },  notes = "删除工单来源")
	@RequestMapping(method = RequestMethod.DELETE, value = "/woorigins/{woorigin_id}")
    @Transactional
    public ResponseEntity<Boolean> remove(@PathVariable("woorigin_id") String woorigin_id) {
         return ResponseEntity.status(HttpStatus.OK).body(wooriginService.remove(woorigin_id));
    }

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

    @PostAuthorize("hasPermission(this.wooriginMapping.toDomain(returnObject.body),'iBizAssetManagement-WOOrigin-Get')")
    @ApiOperation(value = "获取工单来源", tags = {"工单来源" },  notes = "获取工单来源")
	@RequestMapping(method = RequestMethod.GET, value = "/woorigins/{woorigin_id}")
    public ResponseEntity<WOOriginDTO> get(@PathVariable("woorigin_id") String woorigin_id) {
        WOOrigin domain = wooriginService.get(woorigin_id);
        WOOriginDTO dto = wooriginMapping.toDto(domain);
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @ApiOperation(value = "获取工单来源草稿", tags = {"工单来源" },  notes = "获取工单来源草稿")
	@RequestMapping(method = RequestMethod.GET, value = "/woorigins/getdraft")
    public ResponseEntity<WOOriginDTO> getDraft() {
        return ResponseEntity.status(HttpStatus.OK).body(wooriginMapping.toDto(wooriginService.getDraft(new WOOrigin())));
    }

    @ApiOperation(value = "检查工单来源", tags = {"工单来源" },  notes = "检查工单来源")
	@RequestMapping(method = RequestMethod.POST, value = "/woorigins/checkkey")
    public ResponseEntity<Boolean> checkKey(@RequestBody WOOriginDTO woorigindto) {
        return  ResponseEntity.status(HttpStatus.OK).body(wooriginService.checkKey(wooriginMapping.toDomain(woorigindto)));
    }

    @PreAuthorize("hasPermission(this.wooriginMapping.toDomain(#woorigindto),'iBizAssetManagement-WOOrigin-Save')")
    @ApiOperation(value = "保存工单来源", tags = {"工单来源" },  notes = "保存工单来源")
	@RequestMapping(method = RequestMethod.POST, value = "/woorigins/save")
    public ResponseEntity<Boolean> save(@RequestBody WOOriginDTO woorigindto) {
        return ResponseEntity.status(HttpStatus.OK).body(wooriginService.save(wooriginMapping.toDomain(woorigindto)));
    }

    @PreAuthorize("hasPermission(this.wooriginMapping.toDomain(#woorigindtos),'iBizAssetManagement-WOOrigin-Save')")
    @ApiOperation(value = "批量保存工单来源", tags = {"工单来源" },  notes = "批量保存工单来源")
	@RequestMapping(method = RequestMethod.POST, value = "/woorigins/savebatch")
    public ResponseEntity<Boolean> saveBatch(@RequestBody List<WOOriginDTO> woorigindtos) {
        wooriginService.saveBatch(wooriginMapping.toDomain(woorigindtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

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

