package cn.ibizlab.businesscentral.core.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.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 org.springframework.validation.annotation.Validated;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import cn.ibizlab.businesscentral.core.dto.*;
import cn.ibizlab.businesscentral.core.mapping.*;
import cn.ibizlab.businesscentral.core.odoo_repair.domain.Repair_order;
import cn.ibizlab.businesscentral.core.odoo_repair.service.IRepair_orderService;
import cn.ibizlab.businesscentral.core.odoo_repair.filter.Repair_orderSearchContext;
import cn.ibizlab.businesscentral.util.annotation.VersionCheck;

@Slf4j
@Api(tags = {"维修单" })
@RestController("Core-repair_order")
@RequestMapping("")
public class Repair_orderResource {

    @Autowired
    public IRepair_orderService repair_orderService;

    @Autowired
    @Lazy
    public Repair_orderMapping repair_orderMapping;

    @PreAuthorize("hasPermission(this.repair_orderMapping.toDomain(#repair_orderdto),'iBizBusinessCentral-Repair_order-Create')")
    @ApiOperation(value = "新建维修单", tags = {"维修单" },  notes = "新建维修单")
	@RequestMapping(method = RequestMethod.POST, value = "/repair_orders")
    public ResponseEntity<Repair_orderDTO> create(@Validated @RequestBody Repair_orderDTO repair_orderdto) {
        Repair_order domain = repair_orderMapping.toDomain(repair_orderdto);
		repair_orderService.create(domain);
        Repair_orderDTO dto = repair_orderMapping.toDto(domain);
		return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @PreAuthorize("hasPermission(this.repair_orderMapping.toDomain(#repair_orderdtos),'iBizBusinessCentral-Repair_order-Create')")
    @ApiOperation(value = "批量新建维修单", tags = {"维修单" },  notes = "批量新建维修单")
	@RequestMapping(method = RequestMethod.POST, value = "/repair_orders/batch")
    public ResponseEntity<Boolean> createBatch(@RequestBody List<Repair_orderDTO> repair_orderdtos) {
        repair_orderService.createBatch(repair_orderMapping.toDomain(repair_orderdtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @VersionCheck(entity = "repair_order" , versionfield = "writeDate")
    @PreAuthorize("hasPermission(this.repair_orderService.get(#repair_order_id),'iBizBusinessCentral-Repair_order-Update')")
    @ApiOperation(value = "更新维修单", tags = {"维修单" },  notes = "更新维修单")
	@RequestMapping(method = RequestMethod.PUT, value = "/repair_orders/{repair_order_id}")
    public ResponseEntity<Repair_orderDTO> update(@PathVariable("repair_order_id") Long repair_order_id, @RequestBody Repair_orderDTO repair_orderdto) {
		Repair_order domain  = repair_orderMapping.toDomain(repair_orderdto);
        domain .setId(repair_order_id);
		repair_orderService.update(domain );
		Repair_orderDTO dto = repair_orderMapping.toDto(domain );
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @PreAuthorize("hasPermission(this.repair_orderService.getRepairOrderByEntities(this.repair_orderMapping.toDomain(#repair_orderdtos)),'iBizBusinessCentral-Repair_order-Update')")
    @ApiOperation(value = "批量更新维修单", tags = {"维修单" },  notes = "批量更新维修单")
	@RequestMapping(method = RequestMethod.PUT, value = "/repair_orders/batch")
    public ResponseEntity<Boolean> updateBatch(@RequestBody List<Repair_orderDTO> repair_orderdtos) {
        repair_orderService.updateBatch(repair_orderMapping.toDomain(repair_orderdtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @PreAuthorize("hasPermission(this.repair_orderService.get(#repair_order_id),'iBizBusinessCentral-Repair_order-Remove')")
    @ApiOperation(value = "删除维修单", tags = {"维修单" },  notes = "删除维修单")
	@RequestMapping(method = RequestMethod.DELETE, value = "/repair_orders/{repair_order_id}")
    public ResponseEntity<Boolean> remove(@PathVariable("repair_order_id") Long repair_order_id) {
         return ResponseEntity.status(HttpStatus.OK).body(repair_orderService.remove(repair_order_id));
    }

    @PreAuthorize("hasPermission(this.repair_orderService.getRepairOrderByIds(#ids),'iBizBusinessCentral-Repair_order-Remove')")
    @ApiOperation(value = "批量删除维修单", tags = {"维修单" },  notes = "批量删除维修单")
	@RequestMapping(method = RequestMethod.DELETE, value = "/repair_orders/batch")
    public ResponseEntity<Boolean> removeBatch(@RequestBody List<Long> ids) {
        repair_orderService.removeBatch(ids);
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @PostAuthorize("hasPermission(this.repair_orderMapping.toDomain(returnObject.body),'iBizBusinessCentral-Repair_order-Get')")
    @ApiOperation(value = "获取维修单", tags = {"维修单" },  notes = "获取维修单")
	@RequestMapping(method = RequestMethod.GET, value = "/repair_orders/{repair_order_id}")
    public ResponseEntity<Repair_orderDTO> get(@PathVariable("repair_order_id") Long repair_order_id) {
        Repair_order domain = repair_orderService.get(repair_order_id);
        Repair_orderDTO dto = repair_orderMapping.toDto(domain);
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }


}

