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.HRWorkRecord;
import cn.ibizlab.humanresource.core.humanresource.service.IHRWorkRecordService;
import cn.ibizlab.humanresource.core.humanresource.filter.HRWorkRecordSearchContext;
import cn.ibizlab.humanresource.util.annotation.VersionCheck;

@Slf4j
@Api(tags = {"工作履历" })
@RestController("HRApi-hrworkrecord")
@RequestMapping("")
public class HRWorkRecordResource {

    @Autowired
    public IHRWorkRecordService hrworkrecordService;

    @Autowired
    @Lazy
    public HRWorkRecordMapping hrworkrecordMapping;

    @PreAuthorize("hasPermission(this.hrworkrecordMapping.toDomain(#hrworkrecorddto),'iBizHumanResources-HRWorkRecord-Create')")
    @ApiOperation(value = "新建工作履历", tags = {"工作履历" },  notes = "新建工作履历")
	@RequestMapping(method = RequestMethod.POST, value = "/hrworkrecords")
    @Transactional
    public ResponseEntity<HRWorkRecordDTO> create(@RequestBody HRWorkRecordDTO hrworkrecorddto) {
        HRWorkRecord domain = hrworkrecordMapping.toDomain(hrworkrecorddto);
		hrworkrecordService.create(domain);
        HRWorkRecordDTO dto = hrworkrecordMapping.toDto(domain);
		return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @PreAuthorize("hasPermission(this.hrworkrecordMapping.toDomain(#hrworkrecorddtos),'iBizHumanResources-HRWorkRecord-Create')")
    @ApiOperation(value = "批量新建工作履历", tags = {"工作履历" },  notes = "批量新建工作履历")
	@RequestMapping(method = RequestMethod.POST, value = "/hrworkrecords/batch")
    public ResponseEntity<Boolean> createBatch(@RequestBody List<HRWorkRecordDTO> hrworkrecorddtos) {
        hrworkrecordService.createBatch(hrworkrecordMapping.toDomain(hrworkrecorddtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @VersionCheck(entity = "hrworkrecord" , versionfield = "updatedate")
    @PreAuthorize("hasPermission(this.hrworkrecordService.get(#hrworkrecord_id),'iBizHumanResources-HRWorkRecord-Update')")
    @ApiOperation(value = "更新工作履历", tags = {"工作履历" },  notes = "更新工作履历")
	@RequestMapping(method = RequestMethod.PUT, value = "/hrworkrecords/{hrworkrecord_id}")
    @Transactional
    public ResponseEntity<HRWorkRecordDTO> update(@PathVariable("hrworkrecord_id") String hrworkrecord_id, @RequestBody HRWorkRecordDTO hrworkrecorddto) {
		HRWorkRecord domain  = hrworkrecordMapping.toDomain(hrworkrecorddto);
        domain .setHrworkrecordid(hrworkrecord_id);
		hrworkrecordService.update(domain );
		HRWorkRecordDTO dto = hrworkrecordMapping.toDto(domain );
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @PreAuthorize("hasPermission(this.hrworkrecordService.getHrworkrecordByEntities(this.hrworkrecordMapping.toDomain(#hrworkrecorddtos)),'iBizHumanResources-HRWorkRecord-Update')")
    @ApiOperation(value = "批量更新工作履历", tags = {"工作履历" },  notes = "批量更新工作履历")
	@RequestMapping(method = RequestMethod.PUT, value = "/hrworkrecords/batch")
    public ResponseEntity<Boolean> updateBatch(@RequestBody List<HRWorkRecordDTO> hrworkrecorddtos) {
        hrworkrecordService.updateBatch(hrworkrecordMapping.toDomain(hrworkrecorddtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @PreAuthorize("hasPermission(this.hrworkrecordService.get(#hrworkrecord_id),'iBizHumanResources-HRWorkRecord-Remove')")
    @ApiOperation(value = "删除工作履历", tags = {"工作履历" },  notes = "删除工作履历")
	@RequestMapping(method = RequestMethod.DELETE, value = "/hrworkrecords/{hrworkrecord_id}")
    @Transactional
    public ResponseEntity<Boolean> remove(@PathVariable("hrworkrecord_id") String hrworkrecord_id) {
         return ResponseEntity.status(HttpStatus.OK).body(hrworkrecordService.remove(hrworkrecord_id));
    }

    @PreAuthorize("hasPermission(this.hrworkrecordService.getHrworkrecordByIds(#ids),'iBizHumanResources-HRWorkRecord-Remove')")
    @ApiOperation(value = "批量删除工作履历", tags = {"工作履历" },  notes = "批量删除工作履历")
	@RequestMapping(method = RequestMethod.DELETE, value = "/hrworkrecords/batch")
    public ResponseEntity<Boolean> removeBatch(@RequestBody List<String> ids) {
        hrworkrecordService.removeBatch(ids);
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @PostAuthorize("hasPermission(this.hrworkrecordMapping.toDomain(returnObject.body),'iBizHumanResources-HRWorkRecord-Get')")
    @ApiOperation(value = "获取工作履历", tags = {"工作履历" },  notes = "获取工作履历")
	@RequestMapping(method = RequestMethod.GET, value = "/hrworkrecords/{hrworkrecord_id}")
    public ResponseEntity<HRWorkRecordDTO> get(@PathVariable("hrworkrecord_id") String hrworkrecord_id) {
        HRWorkRecord domain = hrworkrecordService.get(hrworkrecord_id);
        HRWorkRecordDTO dto = hrworkrecordMapping.toDto(domain);
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @ApiOperation(value = "获取工作履历草稿", tags = {"工作履历" },  notes = "获取工作履历草稿")
	@RequestMapping(method = RequestMethod.GET, value = "/hrworkrecords/getdraft")
    public ResponseEntity<HRWorkRecordDTO> getDraft() {
        return ResponseEntity.status(HttpStatus.OK).body(hrworkrecordMapping.toDto(hrworkrecordService.getDraft(new HRWorkRecord())));
    }

    @ApiOperation(value = "检查工作履历", tags = {"工作履历" },  notes = "检查工作履历")
	@RequestMapping(method = RequestMethod.POST, value = "/hrworkrecords/checkkey")
    public ResponseEntity<Boolean> checkKey(@RequestBody HRWorkRecordDTO hrworkrecorddto) {
        return  ResponseEntity.status(HttpStatus.OK).body(hrworkrecordService.checkKey(hrworkrecordMapping.toDomain(hrworkrecorddto)));
    }

    @PreAuthorize("hasPermission(this.hrworkrecordMapping.toDomain(#hrworkrecorddto),'iBizHumanResources-HRWorkRecord-Save')")
    @ApiOperation(value = "保存工作履历", tags = {"工作履历" },  notes = "保存工作履历")
	@RequestMapping(method = RequestMethod.POST, value = "/hrworkrecords/save")
    public ResponseEntity<Boolean> save(@RequestBody HRWorkRecordDTO hrworkrecorddto) {
        return ResponseEntity.status(HttpStatus.OK).body(hrworkrecordService.save(hrworkrecordMapping.toDomain(hrworkrecorddto)));
    }

    @PreAuthorize("hasPermission(this.hrworkrecordMapping.toDomain(#hrworkrecorddtos),'iBizHumanResources-HRWorkRecord-Save')")
    @ApiOperation(value = "批量保存工作履历", tags = {"工作履历" },  notes = "批量保存工作履历")
	@RequestMapping(method = RequestMethod.POST, value = "/hrworkrecords/savebatch")
    public ResponseEntity<Boolean> saveBatch(@RequestBody List<HRWorkRecordDTO> hrworkrecorddtos) {
        hrworkrecordService.saveBatch(hrworkrecordMapping.toDomain(hrworkrecorddtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','iBizHumanResources-HRWorkRecord-searchDefault-all') and hasPermission(#context,'iBizHumanResources-HRWorkRecord-Get')")
	@ApiOperation(value = "获取DEFAULT", tags = {"工作履历" } ,notes = "获取DEFAULT")
    @RequestMapping(method= RequestMethod.GET , value="/hrworkrecords/fetchdefault")
	public ResponseEntity<List<HRWorkRecordDTO>> fetchDefault(HRWorkRecordSearchContext context) {
        Page<HRWorkRecord> domains = hrworkrecordService.searchDefault(context) ;
        List<HRWorkRecordDTO> list = hrworkrecordMapping.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-HRWorkRecord-searchDefault-all') and hasPermission(#context,'iBizHumanResources-HRWorkRecord-Get')")
	@ApiOperation(value = "查询DEFAULT", tags = {"工作履历" } ,notes = "查询DEFAULT")
    @RequestMapping(method= RequestMethod.POST , value="/hrworkrecords/searchdefault")
	public ResponseEntity<Page<HRWorkRecordDTO>> searchDefault(@RequestBody HRWorkRecordSearchContext context) {
        Page<HRWorkRecord> domains = hrworkrecordService.searchDefault(context) ;
	    return ResponseEntity.status(HttpStatus.OK)
                .body(new PageImpl(hrworkrecordMapping.toDto(domains.getContent()), context.getPageable(), domains.getTotalElements()));
	}
    @PreAuthorize("hasPermission(this.hrworkrecordMapping.toDomain(#hrworkrecorddto),'iBizHumanResources-HRWorkRecord-Create')")
    @ApiOperation(value = "根据员工建立工作履历", tags = {"工作履历" },  notes = "根据员工建立工作履历")
	@RequestMapping(method = RequestMethod.POST, value = "/hremployees/{hremployee_id}/hrworkrecords")
    @Transactional
    public ResponseEntity<HRWorkRecordDTO> createByHREmployee(@PathVariable("hremployee_id") String hremployee_id, @RequestBody HRWorkRecordDTO hrworkrecorddto) {
        HRWorkRecord domain = hrworkrecordMapping.toDomain(hrworkrecorddto);
        domain.setHremployeeid(hremployee_id);
		hrworkrecordService.create(domain);
        HRWorkRecordDTO dto = hrworkrecordMapping.toDto(domain);
		return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @PreAuthorize("hasPermission(this.hrworkrecordMapping.toDomain(#hrworkrecorddtos),'iBizHumanResources-HRWorkRecord-Create')")
    @ApiOperation(value = "根据员工批量建立工作履历", tags = {"工作履历" },  notes = "根据员工批量建立工作履历")
	@RequestMapping(method = RequestMethod.POST, value = "/hremployees/{hremployee_id}/hrworkrecords/batch")
    public ResponseEntity<Boolean> createBatchByHREmployee(@PathVariable("hremployee_id") String hremployee_id, @RequestBody List<HRWorkRecordDTO> hrworkrecorddtos) {
        List<HRWorkRecord> domainlist=hrworkrecordMapping.toDomain(hrworkrecorddtos);
        for(HRWorkRecord domain:domainlist){
            domain.setHremployeeid(hremployee_id);
        }
        hrworkrecordService.createBatch(domainlist);
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @VersionCheck(entity = "hrworkrecord" , versionfield = "updatedate")
    @PreAuthorize("hasPermission(this.hrworkrecordService.get(#hrworkrecord_id),'iBizHumanResources-HRWorkRecord-Update')")
    @ApiOperation(value = "根据员工更新工作履历", tags = {"工作履历" },  notes = "根据员工更新工作履历")
	@RequestMapping(method = RequestMethod.PUT, value = "/hremployees/{hremployee_id}/hrworkrecords/{hrworkrecord_id}")
    @Transactional
    public ResponseEntity<HRWorkRecordDTO> updateByHREmployee(@PathVariable("hremployee_id") String hremployee_id, @PathVariable("hrworkrecord_id") String hrworkrecord_id, @RequestBody HRWorkRecordDTO hrworkrecorddto) {
        HRWorkRecord domain = hrworkrecordMapping.toDomain(hrworkrecorddto);
        domain.setHremployeeid(hremployee_id);
        domain.setHrworkrecordid(hrworkrecord_id);
		hrworkrecordService.update(domain);
        HRWorkRecordDTO dto = hrworkrecordMapping.toDto(domain);
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @PreAuthorize("hasPermission(this.hrworkrecordService.getHrworkrecordByEntities(this.hrworkrecordMapping.toDomain(#hrworkrecorddtos)),'iBizHumanResources-HRWorkRecord-Update')")
    @ApiOperation(value = "根据员工批量更新工作履历", tags = {"工作履历" },  notes = "根据员工批量更新工作履历")
	@RequestMapping(method = RequestMethod.PUT, value = "/hremployees/{hremployee_id}/hrworkrecords/batch")
    public ResponseEntity<Boolean> updateBatchByHREmployee(@PathVariable("hremployee_id") String hremployee_id, @RequestBody List<HRWorkRecordDTO> hrworkrecorddtos) {
        List<HRWorkRecord> domainlist=hrworkrecordMapping.toDomain(hrworkrecorddtos);
        for(HRWorkRecord domain:domainlist){
            domain.setHremployeeid(hremployee_id);
        }
        hrworkrecordService.updateBatch(domainlist);
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @PreAuthorize("hasPermission(this.hrworkrecordService.get(#hrworkrecord_id),'iBizHumanResources-HRWorkRecord-Remove')")
    @ApiOperation(value = "根据员工删除工作履历", tags = {"工作履历" },  notes = "根据员工删除工作履历")
	@RequestMapping(method = RequestMethod.DELETE, value = "/hremployees/{hremployee_id}/hrworkrecords/{hrworkrecord_id}")
    @Transactional
    public ResponseEntity<Boolean> removeByHREmployee(@PathVariable("hremployee_id") String hremployee_id, @PathVariable("hrworkrecord_id") String hrworkrecord_id) {
		return ResponseEntity.status(HttpStatus.OK).body(hrworkrecordService.remove(hrworkrecord_id));
    }

    @PreAuthorize("hasPermission(this.hrworkrecordService.getHrworkrecordByIds(#ids),'iBizHumanResources-HRWorkRecord-Remove')")
    @ApiOperation(value = "根据员工批量删除工作履历", tags = {"工作履历" },  notes = "根据员工批量删除工作履历")
	@RequestMapping(method = RequestMethod.DELETE, value = "/hremployees/{hremployee_id}/hrworkrecords/batch")
    public ResponseEntity<Boolean> removeBatchByHREmployee(@RequestBody List<String> ids) {
        hrworkrecordService.removeBatch(ids);
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @PostAuthorize("hasPermission(this.hrworkrecordMapping.toDomain(returnObject.body),'iBizHumanResources-HRWorkRecord-Get')")
    @ApiOperation(value = "根据员工获取工作履历", tags = {"工作履历" },  notes = "根据员工获取工作履历")
	@RequestMapping(method = RequestMethod.GET, value = "/hremployees/{hremployee_id}/hrworkrecords/{hrworkrecord_id}")
    public ResponseEntity<HRWorkRecordDTO> getByHREmployee(@PathVariable("hremployee_id") String hremployee_id, @PathVariable("hrworkrecord_id") String hrworkrecord_id) {
        HRWorkRecord domain = hrworkrecordService.get(hrworkrecord_id);
        HRWorkRecordDTO dto = hrworkrecordMapping.toDto(domain);
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @ApiOperation(value = "根据员工获取工作履历草稿", tags = {"工作履历" },  notes = "根据员工获取工作履历草稿")
    @RequestMapping(method = RequestMethod.GET, value = "/hremployees/{hremployee_id}/hrworkrecords/getdraft")
    public ResponseEntity<HRWorkRecordDTO> getDraftByHREmployee(@PathVariable("hremployee_id") String hremployee_id) {
        HRWorkRecord domain = new HRWorkRecord();
        domain.setHremployeeid(hremployee_id);
        return ResponseEntity.status(HttpStatus.OK).body(hrworkrecordMapping.toDto(hrworkrecordService.getDraft(domain)));
    }

    @ApiOperation(value = "根据员工检查工作履历", tags = {"工作履历" },  notes = "根据员工检查工作履历")
	@RequestMapping(method = RequestMethod.POST, value = "/hremployees/{hremployee_id}/hrworkrecords/checkkey")
    public ResponseEntity<Boolean> checkKeyByHREmployee(@PathVariable("hremployee_id") String hremployee_id, @RequestBody HRWorkRecordDTO hrworkrecorddto) {
        return  ResponseEntity.status(HttpStatus.OK).body(hrworkrecordService.checkKey(hrworkrecordMapping.toDomain(hrworkrecorddto)));
    }

    @PreAuthorize("hasPermission(this.hrworkrecordMapping.toDomain(#hrworkrecorddto),'iBizHumanResources-HRWorkRecord-Save')")
    @ApiOperation(value = "根据员工保存工作履历", tags = {"工作履历" },  notes = "根据员工保存工作履历")
	@RequestMapping(method = RequestMethod.POST, value = "/hremployees/{hremployee_id}/hrworkrecords/save")
    public ResponseEntity<Boolean> saveByHREmployee(@PathVariable("hremployee_id") String hremployee_id, @RequestBody HRWorkRecordDTO hrworkrecorddto) {
        HRWorkRecord domain = hrworkrecordMapping.toDomain(hrworkrecorddto);
        domain.setHremployeeid(hremployee_id);
        return ResponseEntity.status(HttpStatus.OK).body(hrworkrecordService.save(domain));
    }

    @PreAuthorize("hasPermission(this.hrworkrecordMapping.toDomain(#hrworkrecorddtos),'iBizHumanResources-HRWorkRecord-Save')")
    @ApiOperation(value = "根据员工批量保存工作履历", tags = {"工作履历" },  notes = "根据员工批量保存工作履历")
	@RequestMapping(method = RequestMethod.POST, value = "/hremployees/{hremployee_id}/hrworkrecords/savebatch")
    public ResponseEntity<Boolean> saveBatchByHREmployee(@PathVariable("hremployee_id") String hremployee_id, @RequestBody List<HRWorkRecordDTO> hrworkrecorddtos) {
        List<HRWorkRecord> domainlist=hrworkrecordMapping.toDomain(hrworkrecorddtos);
        for(HRWorkRecord domain:domainlist){
             domain.setHremployeeid(hremployee_id);
        }
        hrworkrecordService.saveBatch(domainlist);
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','iBizHumanResources-HRWorkRecord-searchDefault-all') and hasPermission(#context,'iBizHumanResources-HRWorkRecord-Get')")
	@ApiOperation(value = "根据员工获取DEFAULT", tags = {"工作履历" } ,notes = "根据员工获取DEFAULT")
    @RequestMapping(method= RequestMethod.GET , value="/hremployees/{hremployee_id}/hrworkrecords/fetchdefault")
	public ResponseEntity<List<HRWorkRecordDTO>> fetchHRWorkRecordDefaultByHREmployee(@PathVariable("hremployee_id") String hremployee_id,HRWorkRecordSearchContext context) {
        context.setN_hremployeeid_eq(hremployee_id);
        Page<HRWorkRecord> domains = hrworkrecordService.searchDefault(context) ;
        List<HRWorkRecordDTO> list = hrworkrecordMapping.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-HRWorkRecord-searchDefault-all') and hasPermission(#context,'iBizHumanResources-HRWorkRecord-Get')")
	@ApiOperation(value = "根据员工查询DEFAULT", tags = {"工作履历" } ,notes = "根据员工查询DEFAULT")
    @RequestMapping(method= RequestMethod.POST , value="/hremployees/{hremployee_id}/hrworkrecords/searchdefault")
	public ResponseEntity<Page<HRWorkRecordDTO>> searchHRWorkRecordDefaultByHREmployee(@PathVariable("hremployee_id") String hremployee_id, @RequestBody HRWorkRecordSearchContext context) {
        context.setN_hremployeeid_eq(hremployee_id);
        Page<HRWorkRecord> domains = hrworkrecordService.searchDefault(context) ;
	    return ResponseEntity.status(HttpStatus.OK)
                .body(new PageImpl(hrworkrecordMapping.toDto(domains.getContent()), context.getPageable(), domains.getTotalElements()));
	}
}

