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.humanresource.domain.HRArchivesBorrow;
import cn.ibizlab.humanresource.core.humanresource.service.IHRArchivesBorrowService;
import cn.ibizlab.humanresource.core.humanresource.filter.HRArchivesBorrowSearchContext;
import cn.ibizlab.humanresource.util.annotation.VersionCheck;

@Slf4j
@Api(tags = {"档案借阅" })
@RestController("HRApi-hrarchivesborrow")
@RequestMapping("")
public class HRArchivesBorrowResource {

    @Autowired
    public IHRArchivesBorrowService hrarchivesborrowService;

    @Autowired
    @Lazy
    public HRArchivesBorrowMapping hrarchivesborrowMapping;

    @PreAuthorize("hasPermission(this.hrarchivesborrowMapping.toDomain(#hrarchivesborrowdto),'iBizHumanResources-HRArchivesBorrow-Create')")
    @ApiOperation(value = "新建档案借阅", tags = {"档案借阅" },  notes = "新建档案借阅")
	@RequestMapping(method = RequestMethod.POST, value = "/hrarchivesborrows")
    @Transactional
    public ResponseEntity<HRArchivesBorrowDTO> create(@RequestBody HRArchivesBorrowDTO hrarchivesborrowdto) {
        HRArchivesBorrow domain = hrarchivesborrowMapping.toDomain(hrarchivesborrowdto);
		hrarchivesborrowService.create(domain);
        HRArchivesBorrowDTO dto = hrarchivesborrowMapping.toDto(domain);
		return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @PreAuthorize("hasPermission(this.hrarchivesborrowMapping.toDomain(#hrarchivesborrowdtos),'iBizHumanResources-HRArchivesBorrow-Create')")
    @ApiOperation(value = "批量新建档案借阅", tags = {"档案借阅" },  notes = "批量新建档案借阅")
	@RequestMapping(method = RequestMethod.POST, value = "/hrarchivesborrows/batch")
    public ResponseEntity<Boolean> createBatch(@RequestBody List<HRArchivesBorrowDTO> hrarchivesborrowdtos) {
        hrarchivesborrowService.createBatch(hrarchivesborrowMapping.toDomain(hrarchivesborrowdtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @VersionCheck(entity = "hrarchivesborrow" , versionfield = "updatedate")
    @PreAuthorize("hasPermission(this.hrarchivesborrowService.get(#hrarchivesborrow_id),'iBizHumanResources-HRArchivesBorrow-Update')")
    @ApiOperation(value = "更新档案借阅", tags = {"档案借阅" },  notes = "更新档案借阅")
	@RequestMapping(method = RequestMethod.PUT, value = "/hrarchivesborrows/{hrarchivesborrow_id}")
    @Transactional
    public ResponseEntity<HRArchivesBorrowDTO> update(@PathVariable("hrarchivesborrow_id") String hrarchivesborrow_id, @RequestBody HRArchivesBorrowDTO hrarchivesborrowdto) {
		HRArchivesBorrow domain  = hrarchivesborrowMapping.toDomain(hrarchivesborrowdto);
        domain .setHrarchivesborrowid(hrarchivesborrow_id);
		hrarchivesborrowService.update(domain );
		HRArchivesBorrowDTO dto = hrarchivesborrowMapping.toDto(domain );
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @PreAuthorize("hasPermission(this.hrarchivesborrowService.getHrarchivesborrowByEntities(this.hrarchivesborrowMapping.toDomain(#hrarchivesborrowdtos)),'iBizHumanResources-HRArchivesBorrow-Update')")
    @ApiOperation(value = "批量更新档案借阅", tags = {"档案借阅" },  notes = "批量更新档案借阅")
	@RequestMapping(method = RequestMethod.PUT, value = "/hrarchivesborrows/batch")
    public ResponseEntity<Boolean> updateBatch(@RequestBody List<HRArchivesBorrowDTO> hrarchivesborrowdtos) {
        hrarchivesborrowService.updateBatch(hrarchivesborrowMapping.toDomain(hrarchivesborrowdtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @PreAuthorize("hasPermission(this.hrarchivesborrowService.get(#hrarchivesborrow_id),'iBizHumanResources-HRArchivesBorrow-Remove')")
    @ApiOperation(value = "删除档案借阅", tags = {"档案借阅" },  notes = "删除档案借阅")
	@RequestMapping(method = RequestMethod.DELETE, value = "/hrarchivesborrows/{hrarchivesborrow_id}")
    @Transactional
    public ResponseEntity<Boolean> remove(@PathVariable("hrarchivesborrow_id") String hrarchivesborrow_id) {
         return ResponseEntity.status(HttpStatus.OK).body(hrarchivesborrowService.remove(hrarchivesborrow_id));
    }

    @PreAuthorize("hasPermission(this.hrarchivesborrowService.getHrarchivesborrowByIds(#ids),'iBizHumanResources-HRArchivesBorrow-Remove')")
    @ApiOperation(value = "批量删除档案借阅", tags = {"档案借阅" },  notes = "批量删除档案借阅")
	@RequestMapping(method = RequestMethod.DELETE, value = "/hrarchivesborrows/batch")
    public ResponseEntity<Boolean> removeBatch(@RequestBody List<String> ids) {
        hrarchivesborrowService.removeBatch(ids);
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @PostAuthorize("hasPermission(this.hrarchivesborrowMapping.toDomain(returnObject.body),'iBizHumanResources-HRArchivesBorrow-Get')")
    @ApiOperation(value = "获取档案借阅", tags = {"档案借阅" },  notes = "获取档案借阅")
	@RequestMapping(method = RequestMethod.GET, value = "/hrarchivesborrows/{hrarchivesborrow_id}")
    public ResponseEntity<HRArchivesBorrowDTO> get(@PathVariable("hrarchivesborrow_id") String hrarchivesborrow_id) {
        HRArchivesBorrow domain = hrarchivesborrowService.get(hrarchivesborrow_id);
        HRArchivesBorrowDTO dto = hrarchivesborrowMapping.toDto(domain);
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @ApiOperation(value = "获取档案借阅草稿", tags = {"档案借阅" },  notes = "获取档案借阅草稿")
	@RequestMapping(method = RequestMethod.GET, value = "/hrarchivesborrows/getdraft")
    public ResponseEntity<HRArchivesBorrowDTO> getDraft() {
        return ResponseEntity.status(HttpStatus.OK).body(hrarchivesborrowMapping.toDto(hrarchivesborrowService.getDraft(new HRArchivesBorrow())));
    }

    @ApiOperation(value = "检查档案借阅", tags = {"档案借阅" },  notes = "检查档案借阅")
	@RequestMapping(method = RequestMethod.POST, value = "/hrarchivesborrows/checkkey")
    public ResponseEntity<Boolean> checkKey(@RequestBody HRArchivesBorrowDTO hrarchivesborrowdto) {
        return  ResponseEntity.status(HttpStatus.OK).body(hrarchivesborrowService.checkKey(hrarchivesborrowMapping.toDomain(hrarchivesborrowdto)));
    }

    @PreAuthorize("hasPermission(this.hrarchivesborrowMapping.toDomain(#hrarchivesborrowdto),'iBizHumanResources-HRArchivesBorrow-Save')")
    @ApiOperation(value = "保存档案借阅", tags = {"档案借阅" },  notes = "保存档案借阅")
	@RequestMapping(method = RequestMethod.POST, value = "/hrarchivesborrows/save")
    public ResponseEntity<Boolean> save(@RequestBody HRArchivesBorrowDTO hrarchivesborrowdto) {
        return ResponseEntity.status(HttpStatus.OK).body(hrarchivesborrowService.save(hrarchivesborrowMapping.toDomain(hrarchivesborrowdto)));
    }

    @PreAuthorize("hasPermission(this.hrarchivesborrowMapping.toDomain(#hrarchivesborrowdtos),'iBizHumanResources-HRArchivesBorrow-Save')")
    @ApiOperation(value = "批量保存档案借阅", tags = {"档案借阅" },  notes = "批量保存档案借阅")
	@RequestMapping(method = RequestMethod.POST, value = "/hrarchivesborrows/savebatch")
    public ResponseEntity<Boolean> saveBatch(@RequestBody List<HRArchivesBorrowDTO> hrarchivesborrowdtos) {
        hrarchivesborrowService.saveBatch(hrarchivesborrowMapping.toDomain(hrarchivesborrowdtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','iBizHumanResources-HRArchivesBorrow-searchDefault-all') and hasPermission(#context,'iBizHumanResources-HRArchivesBorrow-Get')")
	@ApiOperation(value = "获取DEFAULT", tags = {"档案借阅" } ,notes = "获取DEFAULT")
    @RequestMapping(method= RequestMethod.GET , value="/hrarchivesborrows/fetchdefault")
	public ResponseEntity<List<HRArchivesBorrowDTO>> fetchDefault(HRArchivesBorrowSearchContext context) {
        Page<HRArchivesBorrow> domains = hrarchivesborrowService.searchDefault(context) ;
        List<HRArchivesBorrowDTO> list = hrarchivesborrowMapping.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-HRArchivesBorrow-searchDefault-all') and hasPermission(#context,'iBizHumanResources-HRArchivesBorrow-Get')")
	@ApiOperation(value = "查询DEFAULT", tags = {"档案借阅" } ,notes = "查询DEFAULT")
    @RequestMapping(method= RequestMethod.POST , value="/hrarchivesborrows/searchdefault")
	public ResponseEntity<Page<HRArchivesBorrowDTO>> searchDefault(@RequestBody HRArchivesBorrowSearchContext context) {
        Page<HRArchivesBorrow> domains = hrarchivesborrowService.searchDefault(context) ;
	    return ResponseEntity.status(HttpStatus.OK)
                .body(new PageImpl(hrarchivesborrowMapping.toDto(domains.getContent()), context.getPageable(), domains.getTotalElements()));
	}
}

