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.HROMHierarchyCat;
import cn.ibizlab.humanresource.core.base.service.IHROMHierarchyCatService;
import cn.ibizlab.humanresource.core.base.filter.HROMHierarchyCatSearchContext;
import cn.ibizlab.humanresource.util.annotation.VersionCheck;

@Slf4j
@Api(tags = {"结构层次类别" })
@RestController("HRApi-hromhierarchycat")
@RequestMapping("")
public class HROMHierarchyCatResource {

    @Autowired
    public IHROMHierarchyCatService hromhierarchycatService;

    @Autowired
    @Lazy
    public HROMHierarchyCatMapping hromhierarchycatMapping;

    @PreAuthorize("hasPermission(this.hromhierarchycatMapping.toDomain(#hromhierarchycatdto),'iBizHumanResources-HROMHierarchyCat-Create')")
    @ApiOperation(value = "新建结构层次类别", tags = {"结构层次类别" },  notes = "新建结构层次类别")
	@RequestMapping(method = RequestMethod.POST, value = "/hromhierarchycats")
    @Transactional
    public ResponseEntity<HROMHierarchyCatDTO> create(@RequestBody HROMHierarchyCatDTO hromhierarchycatdto) {
        HROMHierarchyCat domain = hromhierarchycatMapping.toDomain(hromhierarchycatdto);
		hromhierarchycatService.create(domain);
        HROMHierarchyCatDTO dto = hromhierarchycatMapping.toDto(domain);
		return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @PreAuthorize("hasPermission(this.hromhierarchycatMapping.toDomain(#hromhierarchycatdtos),'iBizHumanResources-HROMHierarchyCat-Create')")
    @ApiOperation(value = "批量新建结构层次类别", tags = {"结构层次类别" },  notes = "批量新建结构层次类别")
	@RequestMapping(method = RequestMethod.POST, value = "/hromhierarchycats/batch")
    public ResponseEntity<Boolean> createBatch(@RequestBody List<HROMHierarchyCatDTO> hromhierarchycatdtos) {
        hromhierarchycatService.createBatch(hromhierarchycatMapping.toDomain(hromhierarchycatdtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @VersionCheck(entity = "hromhierarchycat" , versionfield = "updatedate")
    @PreAuthorize("hasPermission(this.hromhierarchycatService.get(#hromhierarchycat_id),'iBizHumanResources-HROMHierarchyCat-Update')")
    @ApiOperation(value = "更新结构层次类别", tags = {"结构层次类别" },  notes = "更新结构层次类别")
	@RequestMapping(method = RequestMethod.PUT, value = "/hromhierarchycats/{hromhierarchycat_id}")
    @Transactional
    public ResponseEntity<HROMHierarchyCatDTO> update(@PathVariable("hromhierarchycat_id") String hromhierarchycat_id, @RequestBody HROMHierarchyCatDTO hromhierarchycatdto) {
		HROMHierarchyCat domain  = hromhierarchycatMapping.toDomain(hromhierarchycatdto);
        domain .setOmhierarchycatid(hromhierarchycat_id);
		hromhierarchycatService.update(domain );
		HROMHierarchyCatDTO dto = hromhierarchycatMapping.toDto(domain );
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @PreAuthorize("hasPermission(this.hromhierarchycatService.getHromhierarchycatByEntities(this.hromhierarchycatMapping.toDomain(#hromhierarchycatdtos)),'iBizHumanResources-HROMHierarchyCat-Update')")
    @ApiOperation(value = "批量更新结构层次类别", tags = {"结构层次类别" },  notes = "批量更新结构层次类别")
	@RequestMapping(method = RequestMethod.PUT, value = "/hromhierarchycats/batch")
    public ResponseEntity<Boolean> updateBatch(@RequestBody List<HROMHierarchyCatDTO> hromhierarchycatdtos) {
        hromhierarchycatService.updateBatch(hromhierarchycatMapping.toDomain(hromhierarchycatdtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @PreAuthorize("hasPermission(this.hromhierarchycatService.get(#hromhierarchycat_id),'iBizHumanResources-HROMHierarchyCat-Remove')")
    @ApiOperation(value = "删除结构层次类别", tags = {"结构层次类别" },  notes = "删除结构层次类别")
	@RequestMapping(method = RequestMethod.DELETE, value = "/hromhierarchycats/{hromhierarchycat_id}")
    @Transactional
    public ResponseEntity<Boolean> remove(@PathVariable("hromhierarchycat_id") String hromhierarchycat_id) {
         return ResponseEntity.status(HttpStatus.OK).body(hromhierarchycatService.remove(hromhierarchycat_id));
    }

    @PreAuthorize("hasPermission(this.hromhierarchycatService.getHromhierarchycatByIds(#ids),'iBizHumanResources-HROMHierarchyCat-Remove')")
    @ApiOperation(value = "批量删除结构层次类别", tags = {"结构层次类别" },  notes = "批量删除结构层次类别")
	@RequestMapping(method = RequestMethod.DELETE, value = "/hromhierarchycats/batch")
    public ResponseEntity<Boolean> removeBatch(@RequestBody List<String> ids) {
        hromhierarchycatService.removeBatch(ids);
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @PostAuthorize("hasPermission(this.hromhierarchycatMapping.toDomain(returnObject.body),'iBizHumanResources-HROMHierarchyCat-Get')")
    @ApiOperation(value = "获取结构层次类别", tags = {"结构层次类别" },  notes = "获取结构层次类别")
	@RequestMapping(method = RequestMethod.GET, value = "/hromhierarchycats/{hromhierarchycat_id}")
    public ResponseEntity<HROMHierarchyCatDTO> get(@PathVariable("hromhierarchycat_id") String hromhierarchycat_id) {
        HROMHierarchyCat domain = hromhierarchycatService.get(hromhierarchycat_id);
        HROMHierarchyCatDTO dto = hromhierarchycatMapping.toDto(domain);
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @ApiOperation(value = "获取结构层次类别草稿", tags = {"结构层次类别" },  notes = "获取结构层次类别草稿")
	@RequestMapping(method = RequestMethod.GET, value = "/hromhierarchycats/getdraft")
    public ResponseEntity<HROMHierarchyCatDTO> getDraft() {
        return ResponseEntity.status(HttpStatus.OK).body(hromhierarchycatMapping.toDto(hromhierarchycatService.getDraft(new HROMHierarchyCat())));
    }

    @ApiOperation(value = "检查结构层次类别", tags = {"结构层次类别" },  notes = "检查结构层次类别")
	@RequestMapping(method = RequestMethod.POST, value = "/hromhierarchycats/checkkey")
    public ResponseEntity<Boolean> checkKey(@RequestBody HROMHierarchyCatDTO hromhierarchycatdto) {
        return  ResponseEntity.status(HttpStatus.OK).body(hromhierarchycatService.checkKey(hromhierarchycatMapping.toDomain(hromhierarchycatdto)));
    }

    @PreAuthorize("hasPermission(this.hromhierarchycatMapping.toDomain(#hromhierarchycatdto),'iBizHumanResources-HROMHierarchyCat-Save')")
    @ApiOperation(value = "保存结构层次类别", tags = {"结构层次类别" },  notes = "保存结构层次类别")
	@RequestMapping(method = RequestMethod.POST, value = "/hromhierarchycats/save")
    public ResponseEntity<Boolean> save(@RequestBody HROMHierarchyCatDTO hromhierarchycatdto) {
        return ResponseEntity.status(HttpStatus.OK).body(hromhierarchycatService.save(hromhierarchycatMapping.toDomain(hromhierarchycatdto)));
    }

    @PreAuthorize("hasPermission(this.hromhierarchycatMapping.toDomain(#hromhierarchycatdtos),'iBizHumanResources-HROMHierarchyCat-Save')")
    @ApiOperation(value = "批量保存结构层次类别", tags = {"结构层次类别" },  notes = "批量保存结构层次类别")
	@RequestMapping(method = RequestMethod.POST, value = "/hromhierarchycats/savebatch")
    public ResponseEntity<Boolean> saveBatch(@RequestBody List<HROMHierarchyCatDTO> hromhierarchycatdtos) {
        hromhierarchycatService.saveBatch(hromhierarchycatMapping.toDomain(hromhierarchycatdtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','iBizHumanResources-HROMHierarchyCat-searchDefault-all') and hasPermission(#context,'iBizHumanResources-HROMHierarchyCat-Get')")
	@ApiOperation(value = "获取DEFAULT", tags = {"结构层次类别" } ,notes = "获取DEFAULT")
    @RequestMapping(method= RequestMethod.GET , value="/hromhierarchycats/fetchdefault")
	public ResponseEntity<List<HROMHierarchyCatDTO>> fetchDefault(HROMHierarchyCatSearchContext context) {
        Page<HROMHierarchyCat> domains = hromhierarchycatService.searchDefault(context) ;
        List<HROMHierarchyCatDTO> list = hromhierarchycatMapping.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-HROMHierarchyCat-searchDefault-all') and hasPermission(#context,'iBizHumanResources-HROMHierarchyCat-Get')")
	@ApiOperation(value = "查询DEFAULT", tags = {"结构层次类别" } ,notes = "查询DEFAULT")
    @RequestMapping(method= RequestMethod.POST , value="/hromhierarchycats/searchdefault")
	public ResponseEntity<Page<HROMHierarchyCatDTO>> searchDefault(@RequestBody HROMHierarchyCatSearchContext context) {
        Page<HROMHierarchyCat> domains = hromhierarchycatService.searchDefault(context) ;
	    return ResponseEntity.status(HttpStatus.OK)
                .body(new PageImpl(hromhierarchycatMapping.toDto(domains.getContent()), context.getPageable(), domains.getTotalElements()));
	}
}

