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.HRArchivesMove;
import cn.ibizlab.humanresource.core.humanresource.service.IHRArchivesMoveService;
import cn.ibizlab.humanresource.core.humanresource.filter.HRArchivesMoveSearchContext;
import cn.ibizlab.humanresource.util.annotation.VersionCheck;

@Slf4j
@Api(tags = {"档案调档" })
@RestController("HRApi-hrarchivesmove")
@RequestMapping("")
public class HRArchivesMoveResource {

    @Autowired
    public IHRArchivesMoveService hrarchivesmoveService;

    @Autowired
    @Lazy
    public HRArchivesMoveMapping hrarchivesmoveMapping;

    @PreAuthorize("hasPermission(this.hrarchivesmoveMapping.toDomain(#hrarchivesmovedto),'iBizHumanResources-HRArchivesMove-Create')")
    @ApiOperation(value = "新建档案调档", tags = {"档案调档" },  notes = "新建档案调档")
	@RequestMapping(method = RequestMethod.POST, value = "/hrarchivesmoves")
    @Transactional
    public ResponseEntity<HRArchivesMoveDTO> create(@RequestBody HRArchivesMoveDTO hrarchivesmovedto) {
        HRArchivesMove domain = hrarchivesmoveMapping.toDomain(hrarchivesmovedto);
		hrarchivesmoveService.create(domain);
        HRArchivesMoveDTO dto = hrarchivesmoveMapping.toDto(domain);
		return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @PreAuthorize("hasPermission(this.hrarchivesmoveMapping.toDomain(#hrarchivesmovedtos),'iBizHumanResources-HRArchivesMove-Create')")
    @ApiOperation(value = "批量新建档案调档", tags = {"档案调档" },  notes = "批量新建档案调档")
	@RequestMapping(method = RequestMethod.POST, value = "/hrarchivesmoves/batch")
    public ResponseEntity<Boolean> createBatch(@RequestBody List<HRArchivesMoveDTO> hrarchivesmovedtos) {
        hrarchivesmoveService.createBatch(hrarchivesmoveMapping.toDomain(hrarchivesmovedtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @VersionCheck(entity = "hrarchivesmove" , versionfield = "updatedate")
    @PreAuthorize("hasPermission(this.hrarchivesmoveService.get(#hrarchivesmove_id),'iBizHumanResources-HRArchivesMove-Update')")
    @ApiOperation(value = "更新档案调档", tags = {"档案调档" },  notes = "更新档案调档")
	@RequestMapping(method = RequestMethod.PUT, value = "/hrarchivesmoves/{hrarchivesmove_id}")
    @Transactional
    public ResponseEntity<HRArchivesMoveDTO> update(@PathVariable("hrarchivesmove_id") String hrarchivesmove_id, @RequestBody HRArchivesMoveDTO hrarchivesmovedto) {
		HRArchivesMove domain  = hrarchivesmoveMapping.toDomain(hrarchivesmovedto);
        domain .setHrarchivesmoveid(hrarchivesmove_id);
		hrarchivesmoveService.update(domain );
		HRArchivesMoveDTO dto = hrarchivesmoveMapping.toDto(domain );
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @PreAuthorize("hasPermission(this.hrarchivesmoveService.getHrarchivesmoveByEntities(this.hrarchivesmoveMapping.toDomain(#hrarchivesmovedtos)),'iBizHumanResources-HRArchivesMove-Update')")
    @ApiOperation(value = "批量更新档案调档", tags = {"档案调档" },  notes = "批量更新档案调档")
	@RequestMapping(method = RequestMethod.PUT, value = "/hrarchivesmoves/batch")
    public ResponseEntity<Boolean> updateBatch(@RequestBody List<HRArchivesMoveDTO> hrarchivesmovedtos) {
        hrarchivesmoveService.updateBatch(hrarchivesmoveMapping.toDomain(hrarchivesmovedtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

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

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

    @PostAuthorize("hasPermission(this.hrarchivesmoveMapping.toDomain(returnObject.body),'iBizHumanResources-HRArchivesMove-Get')")
    @ApiOperation(value = "获取档案调档", tags = {"档案调档" },  notes = "获取档案调档")
	@RequestMapping(method = RequestMethod.GET, value = "/hrarchivesmoves/{hrarchivesmove_id}")
    public ResponseEntity<HRArchivesMoveDTO> get(@PathVariable("hrarchivesmove_id") String hrarchivesmove_id) {
        HRArchivesMove domain = hrarchivesmoveService.get(hrarchivesmove_id);
        HRArchivesMoveDTO dto = hrarchivesmoveMapping.toDto(domain);
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @ApiOperation(value = "获取档案调档草稿", tags = {"档案调档" },  notes = "获取档案调档草稿")
	@RequestMapping(method = RequestMethod.GET, value = "/hrarchivesmoves/getdraft")
    public ResponseEntity<HRArchivesMoveDTO> getDraft() {
        return ResponseEntity.status(HttpStatus.OK).body(hrarchivesmoveMapping.toDto(hrarchivesmoveService.getDraft(new HRArchivesMove())));
    }

    @ApiOperation(value = "检查档案调档", tags = {"档案调档" },  notes = "检查档案调档")
	@RequestMapping(method = RequestMethod.POST, value = "/hrarchivesmoves/checkkey")
    public ResponseEntity<Boolean> checkKey(@RequestBody HRArchivesMoveDTO hrarchivesmovedto) {
        return  ResponseEntity.status(HttpStatus.OK).body(hrarchivesmoveService.checkKey(hrarchivesmoveMapping.toDomain(hrarchivesmovedto)));
    }

    @PreAuthorize("hasPermission(this.hrarchivesmoveMapping.toDomain(#hrarchivesmovedto),'iBizHumanResources-HRArchivesMove-Save')")
    @ApiOperation(value = "保存档案调档", tags = {"档案调档" },  notes = "保存档案调档")
	@RequestMapping(method = RequestMethod.POST, value = "/hrarchivesmoves/save")
    public ResponseEntity<Boolean> save(@RequestBody HRArchivesMoveDTO hrarchivesmovedto) {
        return ResponseEntity.status(HttpStatus.OK).body(hrarchivesmoveService.save(hrarchivesmoveMapping.toDomain(hrarchivesmovedto)));
    }

    @PreAuthorize("hasPermission(this.hrarchivesmoveMapping.toDomain(#hrarchivesmovedtos),'iBizHumanResources-HRArchivesMove-Save')")
    @ApiOperation(value = "批量保存档案调档", tags = {"档案调档" },  notes = "批量保存档案调档")
	@RequestMapping(method = RequestMethod.POST, value = "/hrarchivesmoves/savebatch")
    public ResponseEntity<Boolean> saveBatch(@RequestBody List<HRArchivesMoveDTO> hrarchivesmovedtos) {
        hrarchivesmoveService.saveBatch(hrarchivesmoveMapping.toDomain(hrarchivesmovedtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

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

