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_hr.domain.Hr_leave;
import cn.ibizlab.businesscentral.core.odoo_hr.service.IHr_leaveService;
import cn.ibizlab.businesscentral.core.odoo_hr.filter.Hr_leaveSearchContext;
import cn.ibizlab.businesscentral.util.annotation.VersionCheck;

@Slf4j
@Api(tags = {"休假" })
@RestController("Core-hr_leave")
@RequestMapping("")
public class Hr_leaveResource {

    @Autowired
    public IHr_leaveService hr_leaveService;

    @Autowired
    @Lazy
    public Hr_leaveMapping hr_leaveMapping;

    @PreAuthorize("hasPermission(this.hr_leaveMapping.toDomain(#hr_leavedto),'iBizBusinessCentral-Hr_leave-Create')")
    @ApiOperation(value = "新建休假", tags = {"休假" },  notes = "新建休假")
	@RequestMapping(method = RequestMethod.POST, value = "/hr_leaves")
    public ResponseEntity<Hr_leaveDTO> create(@Validated @RequestBody Hr_leaveDTO hr_leavedto) {
        Hr_leave domain = hr_leaveMapping.toDomain(hr_leavedto);
		hr_leaveService.create(domain);
        Hr_leaveDTO dto = hr_leaveMapping.toDto(domain);
		return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @PreAuthorize("hasPermission(this.hr_leaveMapping.toDomain(#hr_leavedtos),'iBizBusinessCentral-Hr_leave-Create')")
    @ApiOperation(value = "批量新建休假", tags = {"休假" },  notes = "批量新建休假")
	@RequestMapping(method = RequestMethod.POST, value = "/hr_leaves/batch")
    public ResponseEntity<Boolean> createBatch(@RequestBody List<Hr_leaveDTO> hr_leavedtos) {
        hr_leaveService.createBatch(hr_leaveMapping.toDomain(hr_leavedtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @VersionCheck(entity = "hr_leave" , versionfield = "writeDate")
    @PreAuthorize("hasPermission(this.hr_leaveService.get(#hr_leave_id),'iBizBusinessCentral-Hr_leave-Update')")
    @ApiOperation(value = "更新休假", tags = {"休假" },  notes = "更新休假")
	@RequestMapping(method = RequestMethod.PUT, value = "/hr_leaves/{hr_leave_id}")
    public ResponseEntity<Hr_leaveDTO> update(@PathVariable("hr_leave_id") Long hr_leave_id, @RequestBody Hr_leaveDTO hr_leavedto) {
		Hr_leave domain  = hr_leaveMapping.toDomain(hr_leavedto);
        domain .setId(hr_leave_id);
		hr_leaveService.update(domain );
		Hr_leaveDTO dto = hr_leaveMapping.toDto(domain );
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @PreAuthorize("hasPermission(this.hr_leaveService.getHrLeaveByEntities(this.hr_leaveMapping.toDomain(#hr_leavedtos)),'iBizBusinessCentral-Hr_leave-Update')")
    @ApiOperation(value = "批量更新休假", tags = {"休假" },  notes = "批量更新休假")
	@RequestMapping(method = RequestMethod.PUT, value = "/hr_leaves/batch")
    public ResponseEntity<Boolean> updateBatch(@RequestBody List<Hr_leaveDTO> hr_leavedtos) {
        hr_leaveService.updateBatch(hr_leaveMapping.toDomain(hr_leavedtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @PreAuthorize("hasPermission(this.hr_leaveService.get(#hr_leave_id),'iBizBusinessCentral-Hr_leave-Remove')")
    @ApiOperation(value = "删除休假", tags = {"休假" },  notes = "删除休假")
	@RequestMapping(method = RequestMethod.DELETE, value = "/hr_leaves/{hr_leave_id}")
    public ResponseEntity<Boolean> remove(@PathVariable("hr_leave_id") Long hr_leave_id) {
         return ResponseEntity.status(HttpStatus.OK).body(hr_leaveService.remove(hr_leave_id));
    }

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

    @PostAuthorize("hasPermission(this.hr_leaveMapping.toDomain(returnObject.body),'iBizBusinessCentral-Hr_leave-Get')")
    @ApiOperation(value = "获取休假", tags = {"休假" },  notes = "获取休假")
	@RequestMapping(method = RequestMethod.GET, value = "/hr_leaves/{hr_leave_id}")
    public ResponseEntity<Hr_leaveDTO> get(@PathVariable("hr_leave_id") Long hr_leave_id) {
        Hr_leave domain = hr_leaveService.get(hr_leave_id);
        Hr_leaveDTO dto = hr_leaveMapping.toDto(domain);
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }


}

