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_contract;
import cn.ibizlab.businesscentral.core.odoo_hr.service.IHr_contractService;
import cn.ibizlab.businesscentral.core.odoo_hr.filter.Hr_contractSearchContext;
import cn.ibizlab.businesscentral.util.annotation.VersionCheck;

@Slf4j
@Api(tags = {"Contract" })
@RestController("Core-hr_contract")
@RequestMapping("")
public class Hr_contractResource {

    @Autowired
    public IHr_contractService hr_contractService;

    @Autowired
    @Lazy
    public Hr_contractMapping hr_contractMapping;

    @PreAuthorize("hasPermission(this.hr_contractMapping.toDomain(#hr_contractdto),'iBizBusinessCentral-Hr_contract-Create')")
    @ApiOperation(value = "新建Contract", tags = {"Contract" },  notes = "新建Contract")
	@RequestMapping(method = RequestMethod.POST, value = "/hr_contracts")
    public ResponseEntity<Hr_contractDTO> create(@Validated @RequestBody Hr_contractDTO hr_contractdto) {
        Hr_contract domain = hr_contractMapping.toDomain(hr_contractdto);
		hr_contractService.create(domain);
        Hr_contractDTO dto = hr_contractMapping.toDto(domain);
		return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @PreAuthorize("hasPermission(this.hr_contractMapping.toDomain(#hr_contractdtos),'iBizBusinessCentral-Hr_contract-Create')")
    @ApiOperation(value = "批量新建Contract", tags = {"Contract" },  notes = "批量新建Contract")
	@RequestMapping(method = RequestMethod.POST, value = "/hr_contracts/batch")
    public ResponseEntity<Boolean> createBatch(@RequestBody List<Hr_contractDTO> hr_contractdtos) {
        hr_contractService.createBatch(hr_contractMapping.toDomain(hr_contractdtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @VersionCheck(entity = "hr_contract" , versionfield = "writeDate")
    @PreAuthorize("hasPermission(this.hr_contractService.get(#hr_contract_id),'iBizBusinessCentral-Hr_contract-Update')")
    @ApiOperation(value = "更新Contract", tags = {"Contract" },  notes = "更新Contract")
	@RequestMapping(method = RequestMethod.PUT, value = "/hr_contracts/{hr_contract_id}")
    public ResponseEntity<Hr_contractDTO> update(@PathVariable("hr_contract_id") Long hr_contract_id, @RequestBody Hr_contractDTO hr_contractdto) {
		Hr_contract domain  = hr_contractMapping.toDomain(hr_contractdto);
        domain .setId(hr_contract_id);
		hr_contractService.update(domain );
		Hr_contractDTO dto = hr_contractMapping.toDto(domain );
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @PreAuthorize("hasPermission(this.hr_contractService.getHrContractByEntities(this.hr_contractMapping.toDomain(#hr_contractdtos)),'iBizBusinessCentral-Hr_contract-Update')")
    @ApiOperation(value = "批量更新Contract", tags = {"Contract" },  notes = "批量更新Contract")
	@RequestMapping(method = RequestMethod.PUT, value = "/hr_contracts/batch")
    public ResponseEntity<Boolean> updateBatch(@RequestBody List<Hr_contractDTO> hr_contractdtos) {
        hr_contractService.updateBatch(hr_contractMapping.toDomain(hr_contractdtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @PreAuthorize("hasPermission(this.hr_contractService.get(#hr_contract_id),'iBizBusinessCentral-Hr_contract-Remove')")
    @ApiOperation(value = "删除Contract", tags = {"Contract" },  notes = "删除Contract")
	@RequestMapping(method = RequestMethod.DELETE, value = "/hr_contracts/{hr_contract_id}")
    public ResponseEntity<Boolean> remove(@PathVariable("hr_contract_id") Long hr_contract_id) {
         return ResponseEntity.status(HttpStatus.OK).body(hr_contractService.remove(hr_contract_id));
    }

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

    @PostAuthorize("hasPermission(this.hr_contractMapping.toDomain(returnObject.body),'iBizBusinessCentral-Hr_contract-Get')")
    @ApiOperation(value = "获取Contract", tags = {"Contract" },  notes = "获取Contract")
	@RequestMapping(method = RequestMethod.GET, value = "/hr_contracts/{hr_contract_id}")
    public ResponseEntity<Hr_contractDTO> get(@PathVariable("hr_contract_id") Long hr_contract_id) {
        Hr_contract domain = hr_contractService.get(hr_contract_id);
        Hr_contractDTO dto = hr_contractMapping.toDto(domain);
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }


}

