package cn.ibizlab.humanresource.hrapi.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.humanresource.hrapi.dto.*;
import cn.ibizlab.humanresource.hrapi.mapping.*;
import cn.ibizlab.humanresource.core.base.domain.HROMHierarchyPurposeRef;
import cn.ibizlab.humanresource.core.base.service.IHROMHierarchyPurposeRefService;
import cn.ibizlab.humanresource.core.base.filter.HROMHierarchyPurposeRefSearchContext;
import cn.ibizlab.humanresource.util.annotation.VersionCheck;

@Slf4j
@Api(tags = {"组织层次结构分配" })
@RestController("HRApi-hromhierarchypurposeref")
@RequestMapping("")
public class HROMHierarchyPurposeRefResource {

    @Autowired
    public IHROMHierarchyPurposeRefService hromhierarchypurposerefService;

    @Autowired
    @Lazy
    public HROMHierarchyPurposeRefMapping hromhierarchypurposerefMapping;

    @PreAuthorize("hasPermission(this.hromhierarchypurposerefMapping.toDomain(#hromhierarchypurposerefdto),'iBizHumanResources-HROMHierarchyPurposeRef-Create')")
    @ApiOperation(value = "新建组织层次结构分配", tags = {"组织层次结构分配" },  notes = "新建组织层次结构分配")
	@RequestMapping(method = RequestMethod.POST, value = "/hromhierarchypurposerefs")
    @Transactional
    public ResponseEntity<HROMHierarchyPurposeRefDTO> create(@RequestBody HROMHierarchyPurposeRefDTO hromhierarchypurposerefdto) {
        HROMHierarchyPurposeRef domain = hromhierarchypurposerefMapping.toDomain(hromhierarchypurposerefdto);
		hromhierarchypurposerefService.create(domain);
        HROMHierarchyPurposeRefDTO dto = hromhierarchypurposerefMapping.toDto(domain);
		return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @PreAuthorize("hasPermission(this.hromhierarchypurposerefMapping.toDomain(#hromhierarchypurposerefdtos),'iBizHumanResources-HROMHierarchyPurposeRef-Create')")
    @ApiOperation(value = "批量新建组织层次结构分配", tags = {"组织层次结构分配" },  notes = "批量新建组织层次结构分配")
	@RequestMapping(method = RequestMethod.POST, value = "/hromhierarchypurposerefs/batch")
    public ResponseEntity<Boolean> createBatch(@RequestBody List<HROMHierarchyPurposeRefDTO> hromhierarchypurposerefdtos) {
        hromhierarchypurposerefService.createBatch(hromhierarchypurposerefMapping.toDomain(hromhierarchypurposerefdtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @VersionCheck(entity = "hromhierarchypurposeref" , versionfield = "updatedate")
    @PreAuthorize("hasPermission(this.hromhierarchypurposerefService.get(#hromhierarchypurposeref_id),'iBizHumanResources-HROMHierarchyPurposeRef-Update')")
    @ApiOperation(value = "更新组织层次结构分配", tags = {"组织层次结构分配" },  notes = "更新组织层次结构分配")
	@RequestMapping(method = RequestMethod.PUT, value = "/hromhierarchypurposerefs/{hromhierarchypurposeref_id}")
    @Transactional
    public ResponseEntity<HROMHierarchyPurposeRefDTO> update(@PathVariable("hromhierarchypurposeref_id") String hromhierarchypurposeref_id, @RequestBody HROMHierarchyPurposeRefDTO hromhierarchypurposerefdto) {
		HROMHierarchyPurposeRef domain  = hromhierarchypurposerefMapping.toDomain(hromhierarchypurposerefdto);
        domain .setOmhierarchypurposerefid(hromhierarchypurposeref_id);
		hromhierarchypurposerefService.update(domain );
		HROMHierarchyPurposeRefDTO dto = hromhierarchypurposerefMapping.toDto(domain );
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @PreAuthorize("hasPermission(this.hromhierarchypurposerefService.getHromhierarchypurposerefByEntities(this.hromhierarchypurposerefMapping.toDomain(#hromhierarchypurposerefdtos)),'iBizHumanResources-HROMHierarchyPurposeRef-Update')")
    @ApiOperation(value = "批量更新组织层次结构分配", tags = {"组织层次结构分配" },  notes = "批量更新组织层次结构分配")
	@RequestMapping(method = RequestMethod.PUT, value = "/hromhierarchypurposerefs/batch")
    public ResponseEntity<Boolean> updateBatch(@RequestBody List<HROMHierarchyPurposeRefDTO> hromhierarchypurposerefdtos) {
        hromhierarchypurposerefService.updateBatch(hromhierarchypurposerefMapping.toDomain(hromhierarchypurposerefdtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @PreAuthorize("hasPermission(this.hromhierarchypurposerefService.get(#hromhierarchypurposeref_id),'iBizHumanResources-HROMHierarchyPurposeRef-Remove')")
    @ApiOperation(value = "删除组织层次结构分配", tags = {"组织层次结构分配" },  notes = "删除组织层次结构分配")
	@RequestMapping(method = RequestMethod.DELETE, value = "/hromhierarchypurposerefs/{hromhierarchypurposeref_id}")
    @Transactional
    public ResponseEntity<Boolean> remove(@PathVariable("hromhierarchypurposeref_id") String hromhierarchypurposeref_id) {
         return ResponseEntity.status(HttpStatus.OK).body(hromhierarchypurposerefService.remove(hromhierarchypurposeref_id));
    }

    @PreAuthorize("hasPermission(this.hromhierarchypurposerefService.getHromhierarchypurposerefByIds(#ids),'iBizHumanResources-HROMHierarchyPurposeRef-Remove')")
    @ApiOperation(value = "批量删除组织层次结构分配", tags = {"组织层次结构分配" },  notes = "批量删除组织层次结构分配")
	@RequestMapping(method = RequestMethod.DELETE, value = "/hromhierarchypurposerefs/batch")
    public ResponseEntity<Boolean> removeBatch(@RequestBody List<String> ids) {
        hromhierarchypurposerefService.removeBatch(ids);
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @PostAuthorize("hasPermission(this.hromhierarchypurposerefMapping.toDomain(returnObject.body),'iBizHumanResources-HROMHierarchyPurposeRef-Get')")
    @ApiOperation(value = "获取组织层次结构分配", tags = {"组织层次结构分配" },  notes = "获取组织层次结构分配")
	@RequestMapping(method = RequestMethod.GET, value = "/hromhierarchypurposerefs/{hromhierarchypurposeref_id}")
    public ResponseEntity<HROMHierarchyPurposeRefDTO> get(@PathVariable("hromhierarchypurposeref_id") String hromhierarchypurposeref_id) {
        HROMHierarchyPurposeRef domain = hromhierarchypurposerefService.get(hromhierarchypurposeref_id);
        HROMHierarchyPurposeRefDTO dto = hromhierarchypurposerefMapping.toDto(domain);
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @ApiOperation(value = "获取组织层次结构分配草稿", tags = {"组织层次结构分配" },  notes = "获取组织层次结构分配草稿")
	@RequestMapping(method = RequestMethod.GET, value = "/hromhierarchypurposerefs/getdraft")
    public ResponseEntity<HROMHierarchyPurposeRefDTO> getDraft() {
        return ResponseEntity.status(HttpStatus.OK).body(hromhierarchypurposerefMapping.toDto(hromhierarchypurposerefService.getDraft(new HROMHierarchyPurposeRef())));
    }

    @ApiOperation(value = "检查组织层次结构分配", tags = {"组织层次结构分配" },  notes = "检查组织层次结构分配")
	@RequestMapping(method = RequestMethod.POST, value = "/hromhierarchypurposerefs/checkkey")
    public ResponseEntity<Boolean> checkKey(@RequestBody HROMHierarchyPurposeRefDTO hromhierarchypurposerefdto) {
        return  ResponseEntity.status(HttpStatus.OK).body(hromhierarchypurposerefService.checkKey(hromhierarchypurposerefMapping.toDomain(hromhierarchypurposerefdto)));
    }

    @PreAuthorize("hasPermission(this.hromhierarchypurposerefMapping.toDomain(#hromhierarchypurposerefdto),'iBizHumanResources-HROMHierarchyPurposeRef-Save')")
    @ApiOperation(value = "保存组织层次结构分配", tags = {"组织层次结构分配" },  notes = "保存组织层次结构分配")
	@RequestMapping(method = RequestMethod.POST, value = "/hromhierarchypurposerefs/save")
    public ResponseEntity<Boolean> save(@RequestBody HROMHierarchyPurposeRefDTO hromhierarchypurposerefdto) {
        return ResponseEntity.status(HttpStatus.OK).body(hromhierarchypurposerefService.save(hromhierarchypurposerefMapping.toDomain(hromhierarchypurposerefdto)));
    }

    @PreAuthorize("hasPermission(this.hromhierarchypurposerefMapping.toDomain(#hromhierarchypurposerefdtos),'iBizHumanResources-HROMHierarchyPurposeRef-Save')")
    @ApiOperation(value = "批量保存组织层次结构分配", tags = {"组织层次结构分配" },  notes = "批量保存组织层次结构分配")
	@RequestMapping(method = RequestMethod.POST, value = "/hromhierarchypurposerefs/savebatch")
    public ResponseEntity<Boolean> saveBatch(@RequestBody List<HROMHierarchyPurposeRefDTO> hromhierarchypurposerefdtos) {
        hromhierarchypurposerefService.saveBatch(hromhierarchypurposerefMapping.toDomain(hromhierarchypurposerefdtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','iBizHumanResources-HROMHierarchyPurposeRef-searchDefault-all') and hasPermission(#context,'iBizHumanResources-HROMHierarchyPurposeRef-Get')")
	@ApiOperation(value = "获取DEFAULT", tags = {"组织层次结构分配" } ,notes = "获取DEFAULT")
    @RequestMapping(method= RequestMethod.GET , value="/hromhierarchypurposerefs/fetchdefault")
	public ResponseEntity<List<HROMHierarchyPurposeRefDTO>> fetchDefault(HROMHierarchyPurposeRefSearchContext context) {
        Page<HROMHierarchyPurposeRef> domains = hromhierarchypurposerefService.searchDefault(context) ;
        List<HROMHierarchyPurposeRefDTO> list = hromhierarchypurposerefMapping.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','iBizHumanResources-HROMHierarchyPurposeRef-searchDefault-all') and hasPermission(#context,'iBizHumanResources-HROMHierarchyPurposeRef-Get')")
	@ApiOperation(value = "查询DEFAULT", tags = {"组织层次结构分配" } ,notes = "查询DEFAULT")
    @RequestMapping(method= RequestMethod.POST , value="/hromhierarchypurposerefs/searchdefault")
	public ResponseEntity<Page<HROMHierarchyPurposeRefDTO>> searchDefault(@RequestBody HROMHierarchyPurposeRefSearchContext context) {
        Page<HROMHierarchyPurposeRef> domains = hromhierarchypurposerefService.searchDefault(context) ;
	    return ResponseEntity.status(HttpStatus.OK)
                .body(new PageImpl(hromhierarchypurposerefMapping.toDto(domains.getContent()), context.getPageable(), domains.getTotalElements()));
	}
}

