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_mro.domain.Mro_task;
import cn.ibizlab.businesscentral.core.odoo_mro.service.IMro_taskService;
import cn.ibizlab.businesscentral.core.odoo_mro.filter.Mro_taskSearchContext;
import cn.ibizlab.businesscentral.util.annotation.VersionCheck;

@Slf4j
@Api(tags = {"Maintenance Task" })
@RestController("Core-mro_task")
@RequestMapping("")
public class Mro_taskResource {

    @Autowired
    public IMro_taskService mro_taskService;

    @Autowired
    @Lazy
    public Mro_taskMapping mro_taskMapping;

    @PreAuthorize("hasPermission(this.mro_taskMapping.toDomain(#mro_taskdto),'iBizBusinessCentral-Mro_task-Create')")
    @ApiOperation(value = "新建Maintenance Task", tags = {"Maintenance Task" },  notes = "新建Maintenance Task")
	@RequestMapping(method = RequestMethod.POST, value = "/mro_tasks")
    public ResponseEntity<Mro_taskDTO> create(@Validated @RequestBody Mro_taskDTO mro_taskdto) {
        Mro_task domain = mro_taskMapping.toDomain(mro_taskdto);
		mro_taskService.create(domain);
        Mro_taskDTO dto = mro_taskMapping.toDto(domain);
		return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @PreAuthorize("hasPermission(this.mro_taskMapping.toDomain(#mro_taskdtos),'iBizBusinessCentral-Mro_task-Create')")
    @ApiOperation(value = "批量新建Maintenance Task", tags = {"Maintenance Task" },  notes = "批量新建Maintenance Task")
	@RequestMapping(method = RequestMethod.POST, value = "/mro_tasks/batch")
    public ResponseEntity<Boolean> createBatch(@RequestBody List<Mro_taskDTO> mro_taskdtos) {
        mro_taskService.createBatch(mro_taskMapping.toDomain(mro_taskdtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @VersionCheck(entity = "mro_task" , versionfield = "writeDate")
    @PreAuthorize("hasPermission(this.mro_taskService.get(#mro_task_id),'iBizBusinessCentral-Mro_task-Update')")
    @ApiOperation(value = "更新Maintenance Task", tags = {"Maintenance Task" },  notes = "更新Maintenance Task")
	@RequestMapping(method = RequestMethod.PUT, value = "/mro_tasks/{mro_task_id}")
    public ResponseEntity<Mro_taskDTO> update(@PathVariable("mro_task_id") Long mro_task_id, @RequestBody Mro_taskDTO mro_taskdto) {
		Mro_task domain  = mro_taskMapping.toDomain(mro_taskdto);
        domain .setId(mro_task_id);
		mro_taskService.update(domain );
		Mro_taskDTO dto = mro_taskMapping.toDto(domain );
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @PreAuthorize("hasPermission(this.mro_taskService.getMroTaskByEntities(this.mro_taskMapping.toDomain(#mro_taskdtos)),'iBizBusinessCentral-Mro_task-Update')")
    @ApiOperation(value = "批量更新Maintenance Task", tags = {"Maintenance Task" },  notes = "批量更新Maintenance Task")
	@RequestMapping(method = RequestMethod.PUT, value = "/mro_tasks/batch")
    public ResponseEntity<Boolean> updateBatch(@RequestBody List<Mro_taskDTO> mro_taskdtos) {
        mro_taskService.updateBatch(mro_taskMapping.toDomain(mro_taskdtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @PreAuthorize("hasPermission(this.mro_taskService.get(#mro_task_id),'iBizBusinessCentral-Mro_task-Remove')")
    @ApiOperation(value = "删除Maintenance Task", tags = {"Maintenance Task" },  notes = "删除Maintenance Task")
	@RequestMapping(method = RequestMethod.DELETE, value = "/mro_tasks/{mro_task_id}")
    public ResponseEntity<Boolean> remove(@PathVariable("mro_task_id") Long mro_task_id) {
         return ResponseEntity.status(HttpStatus.OK).body(mro_taskService.remove(mro_task_id));
    }

    @PreAuthorize("hasPermission(this.mro_taskService.getMroTaskByIds(#ids),'iBizBusinessCentral-Mro_task-Remove')")
    @ApiOperation(value = "批量删除Maintenance Task", tags = {"Maintenance Task" },  notes = "批量删除Maintenance Task")
	@RequestMapping(method = RequestMethod.DELETE, value = "/mro_tasks/batch")
    public ResponseEntity<Boolean> removeBatch(@RequestBody List<Long> ids) {
        mro_taskService.removeBatch(ids);
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @PostAuthorize("hasPermission(this.mro_taskMapping.toDomain(returnObject.body),'iBizBusinessCentral-Mro_task-Get')")
    @ApiOperation(value = "获取Maintenance Task", tags = {"Maintenance Task" },  notes = "获取Maintenance Task")
	@RequestMapping(method = RequestMethod.GET, value = "/mro_tasks/{mro_task_id}")
    public ResponseEntity<Mro_taskDTO> get(@PathVariable("mro_task_id") Long mro_task_id) {
        Mro_task domain = mro_taskService.get(mro_task_id);
        Mro_taskDTO dto = mro_taskMapping.toDto(domain);
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }


}

