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.HROMHIERARCHYPURPOSE;
import cn.ibizlab.humanresource.core.base.service.IHROMHIERARCHYPURPOSEService;
import cn.ibizlab.humanresource.core.base.filter.HROMHIERARCHYPURPOSESearchContext;
import cn.ibizlab.humanresource.util.annotation.VersionCheck;

@Slf4j
@Api(tags = {"组织层次结构应用" })
@RestController("HRApi-hromhierarchypurpose")
@RequestMapping("")
public class HROMHIERARCHYPURPOSEResource {

    @Autowired
    public IHROMHIERARCHYPURPOSEService hromhierarchypurposeService;

    @Autowired
    @Lazy
    public HROMHIERARCHYPURPOSEMapping hromhierarchypurposeMapping;

    @PreAuthorize("hasPermission(this.hromhierarchypurposeMapping.toDomain(#hromhierarchypurposedto),'iBizHumanResources-HROMHIERARCHYPURPOSE-Create')")
    @ApiOperation(value = "新建组织层次结构应用", tags = {"组织层次结构应用" },  notes = "新建组织层次结构应用")
	@RequestMapping(method = RequestMethod.POST, value = "/hromhierarchypurposes")
    @Transactional
    public ResponseEntity<HROMHIERARCHYPURPOSEDTO> create(@RequestBody HROMHIERARCHYPURPOSEDTO hromhierarchypurposedto) {
        HROMHIERARCHYPURPOSE domain = hromhierarchypurposeMapping.toDomain(hromhierarchypurposedto);
		hromhierarchypurposeService.create(domain);
        HROMHIERARCHYPURPOSEDTO dto = hromhierarchypurposeMapping.toDto(domain);
		return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @PreAuthorize("hasPermission(this.hromhierarchypurposeMapping.toDomain(#hromhierarchypurposedtos),'iBizHumanResources-HROMHIERARCHYPURPOSE-Create')")
    @ApiOperation(value = "批量新建组织层次结构应用", tags = {"组织层次结构应用" },  notes = "批量新建组织层次结构应用")
	@RequestMapping(method = RequestMethod.POST, value = "/hromhierarchypurposes/batch")
    public ResponseEntity<Boolean> createBatch(@RequestBody List<HROMHIERARCHYPURPOSEDTO> hromhierarchypurposedtos) {
        hromhierarchypurposeService.createBatch(hromhierarchypurposeMapping.toDomain(hromhierarchypurposedtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @VersionCheck(entity = "hromhierarchypurpose" , versionfield = "updatedate")
    @PreAuthorize("hasPermission(this.hromhierarchypurposeService.get(#hromhierarchypurpose_id),'iBizHumanResources-HROMHIERARCHYPURPOSE-Update')")
    @ApiOperation(value = "更新组织层次结构应用", tags = {"组织层次结构应用" },  notes = "更新组织层次结构应用")
	@RequestMapping(method = RequestMethod.PUT, value = "/hromhierarchypurposes/{hromhierarchypurpose_id}")
    @Transactional
    public ResponseEntity<HROMHIERARCHYPURPOSEDTO> update(@PathVariable("hromhierarchypurpose_id") String hromhierarchypurpose_id, @RequestBody HROMHIERARCHYPURPOSEDTO hromhierarchypurposedto) {
		HROMHIERARCHYPURPOSE domain  = hromhierarchypurposeMapping.toDomain(hromhierarchypurposedto);
        domain .setOmhierarchypurposeid(hromhierarchypurpose_id);
		hromhierarchypurposeService.update(domain );
		HROMHIERARCHYPURPOSEDTO dto = hromhierarchypurposeMapping.toDto(domain );
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @PreAuthorize("hasPermission(this.hromhierarchypurposeService.getHromhierarchypurposeByEntities(this.hromhierarchypurposeMapping.toDomain(#hromhierarchypurposedtos)),'iBizHumanResources-HROMHIERARCHYPURPOSE-Update')")
    @ApiOperation(value = "批量更新组织层次结构应用", tags = {"组织层次结构应用" },  notes = "批量更新组织层次结构应用")
	@RequestMapping(method = RequestMethod.PUT, value = "/hromhierarchypurposes/batch")
    public ResponseEntity<Boolean> updateBatch(@RequestBody List<HROMHIERARCHYPURPOSEDTO> hromhierarchypurposedtos) {
        hromhierarchypurposeService.updateBatch(hromhierarchypurposeMapping.toDomain(hromhierarchypurposedtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

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

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

    @PostAuthorize("hasPermission(this.hromhierarchypurposeMapping.toDomain(returnObject.body),'iBizHumanResources-HROMHIERARCHYPURPOSE-Get')")
    @ApiOperation(value = "获取组织层次结构应用", tags = {"组织层次结构应用" },  notes = "获取组织层次结构应用")
	@RequestMapping(method = RequestMethod.GET, value = "/hromhierarchypurposes/{hromhierarchypurpose_id}")
    public ResponseEntity<HROMHIERARCHYPURPOSEDTO> get(@PathVariable("hromhierarchypurpose_id") String hromhierarchypurpose_id) {
        HROMHIERARCHYPURPOSE domain = hromhierarchypurposeService.get(hromhierarchypurpose_id);
        HROMHIERARCHYPURPOSEDTO dto = hromhierarchypurposeMapping.toDto(domain);
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @ApiOperation(value = "获取组织层次结构应用草稿", tags = {"组织层次结构应用" },  notes = "获取组织层次结构应用草稿")
	@RequestMapping(method = RequestMethod.GET, value = "/hromhierarchypurposes/getdraft")
    public ResponseEntity<HROMHIERARCHYPURPOSEDTO> getDraft() {
        return ResponseEntity.status(HttpStatus.OK).body(hromhierarchypurposeMapping.toDto(hromhierarchypurposeService.getDraft(new HROMHIERARCHYPURPOSE())));
    }

    @ApiOperation(value = "检查组织层次结构应用", tags = {"组织层次结构应用" },  notes = "检查组织层次结构应用")
	@RequestMapping(method = RequestMethod.POST, value = "/hromhierarchypurposes/checkkey")
    public ResponseEntity<Boolean> checkKey(@RequestBody HROMHIERARCHYPURPOSEDTO hromhierarchypurposedto) {
        return  ResponseEntity.status(HttpStatus.OK).body(hromhierarchypurposeService.checkKey(hromhierarchypurposeMapping.toDomain(hromhierarchypurposedto)));
    }

    @PreAuthorize("hasPermission(this.hromhierarchypurposeMapping.toDomain(#hromhierarchypurposedto),'iBizHumanResources-HROMHIERARCHYPURPOSE-Save')")
    @ApiOperation(value = "保存组织层次结构应用", tags = {"组织层次结构应用" },  notes = "保存组织层次结构应用")
	@RequestMapping(method = RequestMethod.POST, value = "/hromhierarchypurposes/save")
    public ResponseEntity<Boolean> save(@RequestBody HROMHIERARCHYPURPOSEDTO hromhierarchypurposedto) {
        return ResponseEntity.status(HttpStatus.OK).body(hromhierarchypurposeService.save(hromhierarchypurposeMapping.toDomain(hromhierarchypurposedto)));
    }

    @PreAuthorize("hasPermission(this.hromhierarchypurposeMapping.toDomain(#hromhierarchypurposedtos),'iBizHumanResources-HROMHIERARCHYPURPOSE-Save')")
    @ApiOperation(value = "批量保存组织层次结构应用", tags = {"组织层次结构应用" },  notes = "批量保存组织层次结构应用")
	@RequestMapping(method = RequestMethod.POST, value = "/hromhierarchypurposes/savebatch")
    public ResponseEntity<Boolean> saveBatch(@RequestBody List<HROMHIERARCHYPURPOSEDTO> hromhierarchypurposedtos) {
        hromhierarchypurposeService.saveBatch(hromhierarchypurposeMapping.toDomain(hromhierarchypurposedtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

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

