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.HREducation;
import cn.ibizlab.humanresource.core.humanresource.service.IHREducationService;
import cn.ibizlab.humanresource.core.humanresource.filter.HREducationSearchContext;
import cn.ibizlab.humanresource.util.annotation.VersionCheck;

@Slf4j
@Api(tags = {"教育信息" })
@RestController("HRApi-hreducation")
@RequestMapping("")
public class HREducationResource {

    @Autowired
    public IHREducationService hreducationService;

    @Autowired
    @Lazy
    public HREducationMapping hreducationMapping;

    @PreAuthorize("hasPermission(this.hreducationMapping.toDomain(#hreducationdto),'iBizHumanResources-HREducation-Create')")
    @ApiOperation(value = "新建教育信息", tags = {"教育信息" },  notes = "新建教育信息")
	@RequestMapping(method = RequestMethod.POST, value = "/hreducations")
    @Transactional
    public ResponseEntity<HREducationDTO> create(@RequestBody HREducationDTO hreducationdto) {
        HREducation domain = hreducationMapping.toDomain(hreducationdto);
		hreducationService.create(domain);
        HREducationDTO dto = hreducationMapping.toDto(domain);
		return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @PreAuthorize("hasPermission(this.hreducationMapping.toDomain(#hreducationdtos),'iBizHumanResources-HREducation-Create')")
    @ApiOperation(value = "批量新建教育信息", tags = {"教育信息" },  notes = "批量新建教育信息")
	@RequestMapping(method = RequestMethod.POST, value = "/hreducations/batch")
    public ResponseEntity<Boolean> createBatch(@RequestBody List<HREducationDTO> hreducationdtos) {
        hreducationService.createBatch(hreducationMapping.toDomain(hreducationdtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @VersionCheck(entity = "hreducation" , versionfield = "updatedate")
    @PreAuthorize("hasPermission(this.hreducationService.get(#hreducation_id),'iBizHumanResources-HREducation-Update')")
    @ApiOperation(value = "更新教育信息", tags = {"教育信息" },  notes = "更新教育信息")
	@RequestMapping(method = RequestMethod.PUT, value = "/hreducations/{hreducation_id}")
    @Transactional
    public ResponseEntity<HREducationDTO> update(@PathVariable("hreducation_id") String hreducation_id, @RequestBody HREducationDTO hreducationdto) {
		HREducation domain  = hreducationMapping.toDomain(hreducationdto);
        domain .setHreducationid(hreducation_id);
		hreducationService.update(domain );
		HREducationDTO dto = hreducationMapping.toDto(domain );
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @PreAuthorize("hasPermission(this.hreducationService.getHreducationByEntities(this.hreducationMapping.toDomain(#hreducationdtos)),'iBizHumanResources-HREducation-Update')")
    @ApiOperation(value = "批量更新教育信息", tags = {"教育信息" },  notes = "批量更新教育信息")
	@RequestMapping(method = RequestMethod.PUT, value = "/hreducations/batch")
    public ResponseEntity<Boolean> updateBatch(@RequestBody List<HREducationDTO> hreducationdtos) {
        hreducationService.updateBatch(hreducationMapping.toDomain(hreducationdtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @PreAuthorize("hasPermission(this.hreducationService.get(#hreducation_id),'iBizHumanResources-HREducation-Remove')")
    @ApiOperation(value = "删除教育信息", tags = {"教育信息" },  notes = "删除教育信息")
	@RequestMapping(method = RequestMethod.DELETE, value = "/hreducations/{hreducation_id}")
    @Transactional
    public ResponseEntity<Boolean> remove(@PathVariable("hreducation_id") String hreducation_id) {
         return ResponseEntity.status(HttpStatus.OK).body(hreducationService.remove(hreducation_id));
    }

    @PreAuthorize("hasPermission(this.hreducationService.getHreducationByIds(#ids),'iBizHumanResources-HREducation-Remove')")
    @ApiOperation(value = "批量删除教育信息", tags = {"教育信息" },  notes = "批量删除教育信息")
	@RequestMapping(method = RequestMethod.DELETE, value = "/hreducations/batch")
    public ResponseEntity<Boolean> removeBatch(@RequestBody List<String> ids) {
        hreducationService.removeBatch(ids);
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @PostAuthorize("hasPermission(this.hreducationMapping.toDomain(returnObject.body),'iBizHumanResources-HREducation-Get')")
    @ApiOperation(value = "获取教育信息", tags = {"教育信息" },  notes = "获取教育信息")
	@RequestMapping(method = RequestMethod.GET, value = "/hreducations/{hreducation_id}")
    public ResponseEntity<HREducationDTO> get(@PathVariable("hreducation_id") String hreducation_id) {
        HREducation domain = hreducationService.get(hreducation_id);
        HREducationDTO dto = hreducationMapping.toDto(domain);
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @ApiOperation(value = "获取教育信息草稿", tags = {"教育信息" },  notes = "获取教育信息草稿")
	@RequestMapping(method = RequestMethod.GET, value = "/hreducations/getdraft")
    public ResponseEntity<HREducationDTO> getDraft() {
        return ResponseEntity.status(HttpStatus.OK).body(hreducationMapping.toDto(hreducationService.getDraft(new HREducation())));
    }

    @ApiOperation(value = "检查教育信息", tags = {"教育信息" },  notes = "检查教育信息")
	@RequestMapping(method = RequestMethod.POST, value = "/hreducations/checkkey")
    public ResponseEntity<Boolean> checkKey(@RequestBody HREducationDTO hreducationdto) {
        return  ResponseEntity.status(HttpStatus.OK).body(hreducationService.checkKey(hreducationMapping.toDomain(hreducationdto)));
    }

    @PreAuthorize("hasPermission(this.hreducationMapping.toDomain(#hreducationdto),'iBizHumanResources-HREducation-Save')")
    @ApiOperation(value = "保存教育信息", tags = {"教育信息" },  notes = "保存教育信息")
	@RequestMapping(method = RequestMethod.POST, value = "/hreducations/save")
    public ResponseEntity<Boolean> save(@RequestBody HREducationDTO hreducationdto) {
        return ResponseEntity.status(HttpStatus.OK).body(hreducationService.save(hreducationMapping.toDomain(hreducationdto)));
    }

    @PreAuthorize("hasPermission(this.hreducationMapping.toDomain(#hreducationdtos),'iBizHumanResources-HREducation-Save')")
    @ApiOperation(value = "批量保存教育信息", tags = {"教育信息" },  notes = "批量保存教育信息")
	@RequestMapping(method = RequestMethod.POST, value = "/hreducations/savebatch")
    public ResponseEntity<Boolean> saveBatch(@RequestBody List<HREducationDTO> hreducationdtos) {
        hreducationService.saveBatch(hreducationMapping.toDomain(hreducationdtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

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

    @PreAuthorize("hasPermission(this.hreducationMapping.toDomain(#hreducationdtos),'iBizHumanResources-HREducation-Create')")
    @ApiOperation(value = "根据员工批量建立教育信息", tags = {"教育信息" },  notes = "根据员工批量建立教育信息")
	@RequestMapping(method = RequestMethod.POST, value = "/hremployees/{hremployee_id}/hreducations/batch")
    public ResponseEntity<Boolean> createBatchByHREmployee(@PathVariable("hremployee_id") String hremployee_id, @RequestBody List<HREducationDTO> hreducationdtos) {
        List<HREducation> domainlist=hreducationMapping.toDomain(hreducationdtos);
        for(HREducation domain:domainlist){
            domain.setHremployeeid(hremployee_id);
        }
        hreducationService.createBatch(domainlist);
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @VersionCheck(entity = "hreducation" , versionfield = "updatedate")
    @PreAuthorize("hasPermission(this.hreducationService.get(#hreducation_id),'iBizHumanResources-HREducation-Update')")
    @ApiOperation(value = "根据员工更新教育信息", tags = {"教育信息" },  notes = "根据员工更新教育信息")
	@RequestMapping(method = RequestMethod.PUT, value = "/hremployees/{hremployee_id}/hreducations/{hreducation_id}")
    @Transactional
    public ResponseEntity<HREducationDTO> updateByHREmployee(@PathVariable("hremployee_id") String hremployee_id, @PathVariable("hreducation_id") String hreducation_id, @RequestBody HREducationDTO hreducationdto) {
        HREducation domain = hreducationMapping.toDomain(hreducationdto);
        domain.setHremployeeid(hremployee_id);
        domain.setHreducationid(hreducation_id);
		hreducationService.update(domain);
        HREducationDTO dto = hreducationMapping.toDto(domain);
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @PreAuthorize("hasPermission(this.hreducationService.getHreducationByEntities(this.hreducationMapping.toDomain(#hreducationdtos)),'iBizHumanResources-HREducation-Update')")
    @ApiOperation(value = "根据员工批量更新教育信息", tags = {"教育信息" },  notes = "根据员工批量更新教育信息")
	@RequestMapping(method = RequestMethod.PUT, value = "/hremployees/{hremployee_id}/hreducations/batch")
    public ResponseEntity<Boolean> updateBatchByHREmployee(@PathVariable("hremployee_id") String hremployee_id, @RequestBody List<HREducationDTO> hreducationdtos) {
        List<HREducation> domainlist=hreducationMapping.toDomain(hreducationdtos);
        for(HREducation domain:domainlist){
            domain.setHremployeeid(hremployee_id);
        }
        hreducationService.updateBatch(domainlist);
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @PreAuthorize("hasPermission(this.hreducationService.get(#hreducation_id),'iBizHumanResources-HREducation-Remove')")
    @ApiOperation(value = "根据员工删除教育信息", tags = {"教育信息" },  notes = "根据员工删除教育信息")
	@RequestMapping(method = RequestMethod.DELETE, value = "/hremployees/{hremployee_id}/hreducations/{hreducation_id}")
    @Transactional
    public ResponseEntity<Boolean> removeByHREmployee(@PathVariable("hremployee_id") String hremployee_id, @PathVariable("hreducation_id") String hreducation_id) {
		return ResponseEntity.status(HttpStatus.OK).body(hreducationService.remove(hreducation_id));
    }

    @PreAuthorize("hasPermission(this.hreducationService.getHreducationByIds(#ids),'iBizHumanResources-HREducation-Remove')")
    @ApiOperation(value = "根据员工批量删除教育信息", tags = {"教育信息" },  notes = "根据员工批量删除教育信息")
	@RequestMapping(method = RequestMethod.DELETE, value = "/hremployees/{hremployee_id}/hreducations/batch")
    public ResponseEntity<Boolean> removeBatchByHREmployee(@RequestBody List<String> ids) {
        hreducationService.removeBatch(ids);
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @PostAuthorize("hasPermission(this.hreducationMapping.toDomain(returnObject.body),'iBizHumanResources-HREducation-Get')")
    @ApiOperation(value = "根据员工获取教育信息", tags = {"教育信息" },  notes = "根据员工获取教育信息")
	@RequestMapping(method = RequestMethod.GET, value = "/hremployees/{hremployee_id}/hreducations/{hreducation_id}")
    public ResponseEntity<HREducationDTO> getByHREmployee(@PathVariable("hremployee_id") String hremployee_id, @PathVariable("hreducation_id") String hreducation_id) {
        HREducation domain = hreducationService.get(hreducation_id);
        HREducationDTO dto = hreducationMapping.toDto(domain);
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @ApiOperation(value = "根据员工获取教育信息草稿", tags = {"教育信息" },  notes = "根据员工获取教育信息草稿")
    @RequestMapping(method = RequestMethod.GET, value = "/hremployees/{hremployee_id}/hreducations/getdraft")
    public ResponseEntity<HREducationDTO> getDraftByHREmployee(@PathVariable("hremployee_id") String hremployee_id) {
        HREducation domain = new HREducation();
        domain.setHremployeeid(hremployee_id);
        return ResponseEntity.status(HttpStatus.OK).body(hreducationMapping.toDto(hreducationService.getDraft(domain)));
    }

    @ApiOperation(value = "根据员工检查教育信息", tags = {"教育信息" },  notes = "根据员工检查教育信息")
	@RequestMapping(method = RequestMethod.POST, value = "/hremployees/{hremployee_id}/hreducations/checkkey")
    public ResponseEntity<Boolean> checkKeyByHREmployee(@PathVariable("hremployee_id") String hremployee_id, @RequestBody HREducationDTO hreducationdto) {
        return  ResponseEntity.status(HttpStatus.OK).body(hreducationService.checkKey(hreducationMapping.toDomain(hreducationdto)));
    }

    @PreAuthorize("hasPermission(this.hreducationMapping.toDomain(#hreducationdto),'iBizHumanResources-HREducation-Save')")
    @ApiOperation(value = "根据员工保存教育信息", tags = {"教育信息" },  notes = "根据员工保存教育信息")
	@RequestMapping(method = RequestMethod.POST, value = "/hremployees/{hremployee_id}/hreducations/save")
    public ResponseEntity<Boolean> saveByHREmployee(@PathVariable("hremployee_id") String hremployee_id, @RequestBody HREducationDTO hreducationdto) {
        HREducation domain = hreducationMapping.toDomain(hreducationdto);
        domain.setHremployeeid(hremployee_id);
        return ResponseEntity.status(HttpStatus.OK).body(hreducationService.save(domain));
    }

    @PreAuthorize("hasPermission(this.hreducationMapping.toDomain(#hreducationdtos),'iBizHumanResources-HREducation-Save')")
    @ApiOperation(value = "根据员工批量保存教育信息", tags = {"教育信息" },  notes = "根据员工批量保存教育信息")
	@RequestMapping(method = RequestMethod.POST, value = "/hremployees/{hremployee_id}/hreducations/savebatch")
    public ResponseEntity<Boolean> saveBatchByHREmployee(@PathVariable("hremployee_id") String hremployee_id, @RequestBody List<HREducationDTO> hreducationdtos) {
        List<HREducation> domainlist=hreducationMapping.toDomain(hreducationdtos);
        for(HREducation domain:domainlist){
             domain.setHremployeeid(hremployee_id);
        }
        hreducationService.saveBatch(domainlist);
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','iBizHumanResources-HREducation-searchDefault-all') and hasPermission(#context,'iBizHumanResources-HREducation-Get')")
	@ApiOperation(value = "根据员工获取DEFAULT", tags = {"教育信息" } ,notes = "根据员工获取DEFAULT")
    @RequestMapping(method= RequestMethod.GET , value="/hremployees/{hremployee_id}/hreducations/fetchdefault")
	public ResponseEntity<List<HREducationDTO>> fetchHREducationDefaultByHREmployee(@PathVariable("hremployee_id") String hremployee_id,HREducationSearchContext context) {
        context.setN_hremployeeid_eq(hremployee_id);
        Page<HREducation> domains = hreducationService.searchDefault(context) ;
        List<HREducationDTO> list = hreducationMapping.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-HREducation-searchDefault-all') and hasPermission(#context,'iBizHumanResources-HREducation-Get')")
	@ApiOperation(value = "根据员工查询DEFAULT", tags = {"教育信息" } ,notes = "根据员工查询DEFAULT")
    @RequestMapping(method= RequestMethod.POST , value="/hremployees/{hremployee_id}/hreducations/searchdefault")
	public ResponseEntity<Page<HREducationDTO>> searchHREducationDefaultByHREmployee(@PathVariable("hremployee_id") String hremployee_id, @RequestBody HREducationSearchContext context) {
        context.setN_hremployeeid_eq(hremployee_id);
        Page<HREducation> domains = hreducationService.searchDefault(context) ;
	    return ResponseEntity.status(HttpStatus.OK)
                .body(new PageImpl(hreducationMapping.toDto(domains.getContent()), context.getPageable(), domains.getTotalElements()));
	}
}

