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.HRResearch;
import cn.ibizlab.humanresource.core.humanresource.service.IHRResearchService;
import cn.ibizlab.humanresource.core.humanresource.filter.HRResearchSearchContext;
import cn.ibizlab.humanresource.util.annotation.VersionCheck;

@Slf4j
@Api(tags = {"科研成果" })
@RestController("HRApi-hrresearch")
@RequestMapping("")
public class HRResearchResource {

    @Autowired
    public IHRResearchService hrresearchService;

    @Autowired
    @Lazy
    public HRResearchMapping hrresearchMapping;

    @PreAuthorize("hasPermission(this.hrresearchMapping.toDomain(#hrresearchdto),'iBizHumanResources-HRResearch-Create')")
    @ApiOperation(value = "新建科研成果", tags = {"科研成果" },  notes = "新建科研成果")
	@RequestMapping(method = RequestMethod.POST, value = "/hrresearches")
    @Transactional
    public ResponseEntity<HRResearchDTO> create(@RequestBody HRResearchDTO hrresearchdto) {
        HRResearch domain = hrresearchMapping.toDomain(hrresearchdto);
		hrresearchService.create(domain);
        HRResearchDTO dto = hrresearchMapping.toDto(domain);
		return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @PreAuthorize("hasPermission(this.hrresearchMapping.toDomain(#hrresearchdtos),'iBizHumanResources-HRResearch-Create')")
    @ApiOperation(value = "批量新建科研成果", tags = {"科研成果" },  notes = "批量新建科研成果")
	@RequestMapping(method = RequestMethod.POST, value = "/hrresearches/batch")
    public ResponseEntity<Boolean> createBatch(@RequestBody List<HRResearchDTO> hrresearchdtos) {
        hrresearchService.createBatch(hrresearchMapping.toDomain(hrresearchdtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @VersionCheck(entity = "hrresearch" , versionfield = "updatedate")
    @PreAuthorize("hasPermission(this.hrresearchService.get(#hrresearch_id),'iBizHumanResources-HRResearch-Update')")
    @ApiOperation(value = "更新科研成果", tags = {"科研成果" },  notes = "更新科研成果")
	@RequestMapping(method = RequestMethod.PUT, value = "/hrresearches/{hrresearch_id}")
    @Transactional
    public ResponseEntity<HRResearchDTO> update(@PathVariable("hrresearch_id") String hrresearch_id, @RequestBody HRResearchDTO hrresearchdto) {
		HRResearch domain  = hrresearchMapping.toDomain(hrresearchdto);
        domain .setHrresearchid(hrresearch_id);
		hrresearchService.update(domain );
		HRResearchDTO dto = hrresearchMapping.toDto(domain );
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @PreAuthorize("hasPermission(this.hrresearchService.getHrresearchByEntities(this.hrresearchMapping.toDomain(#hrresearchdtos)),'iBizHumanResources-HRResearch-Update')")
    @ApiOperation(value = "批量更新科研成果", tags = {"科研成果" },  notes = "批量更新科研成果")
	@RequestMapping(method = RequestMethod.PUT, value = "/hrresearches/batch")
    public ResponseEntity<Boolean> updateBatch(@RequestBody List<HRResearchDTO> hrresearchdtos) {
        hrresearchService.updateBatch(hrresearchMapping.toDomain(hrresearchdtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @PreAuthorize("hasPermission(this.hrresearchService.get(#hrresearch_id),'iBizHumanResources-HRResearch-Remove')")
    @ApiOperation(value = "删除科研成果", tags = {"科研成果" },  notes = "删除科研成果")
	@RequestMapping(method = RequestMethod.DELETE, value = "/hrresearches/{hrresearch_id}")
    @Transactional
    public ResponseEntity<Boolean> remove(@PathVariable("hrresearch_id") String hrresearch_id) {
         return ResponseEntity.status(HttpStatus.OK).body(hrresearchService.remove(hrresearch_id));
    }

    @PreAuthorize("hasPermission(this.hrresearchService.getHrresearchByIds(#ids),'iBizHumanResources-HRResearch-Remove')")
    @ApiOperation(value = "批量删除科研成果", tags = {"科研成果" },  notes = "批量删除科研成果")
	@RequestMapping(method = RequestMethod.DELETE, value = "/hrresearches/batch")
    public ResponseEntity<Boolean> removeBatch(@RequestBody List<String> ids) {
        hrresearchService.removeBatch(ids);
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @PostAuthorize("hasPermission(this.hrresearchMapping.toDomain(returnObject.body),'iBizHumanResources-HRResearch-Get')")
    @ApiOperation(value = "获取科研成果", tags = {"科研成果" },  notes = "获取科研成果")
	@RequestMapping(method = RequestMethod.GET, value = "/hrresearches/{hrresearch_id}")
    public ResponseEntity<HRResearchDTO> get(@PathVariable("hrresearch_id") String hrresearch_id) {
        HRResearch domain = hrresearchService.get(hrresearch_id);
        HRResearchDTO dto = hrresearchMapping.toDto(domain);
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @ApiOperation(value = "获取科研成果草稿", tags = {"科研成果" },  notes = "获取科研成果草稿")
	@RequestMapping(method = RequestMethod.GET, value = "/hrresearches/getdraft")
    public ResponseEntity<HRResearchDTO> getDraft() {
        return ResponseEntity.status(HttpStatus.OK).body(hrresearchMapping.toDto(hrresearchService.getDraft(new HRResearch())));
    }

    @ApiOperation(value = "检查科研成果", tags = {"科研成果" },  notes = "检查科研成果")
	@RequestMapping(method = RequestMethod.POST, value = "/hrresearches/checkkey")
    public ResponseEntity<Boolean> checkKey(@RequestBody HRResearchDTO hrresearchdto) {
        return  ResponseEntity.status(HttpStatus.OK).body(hrresearchService.checkKey(hrresearchMapping.toDomain(hrresearchdto)));
    }

    @PreAuthorize("hasPermission(this.hrresearchMapping.toDomain(#hrresearchdto),'iBizHumanResources-HRResearch-Save')")
    @ApiOperation(value = "保存科研成果", tags = {"科研成果" },  notes = "保存科研成果")
	@RequestMapping(method = RequestMethod.POST, value = "/hrresearches/save")
    public ResponseEntity<Boolean> save(@RequestBody HRResearchDTO hrresearchdto) {
        return ResponseEntity.status(HttpStatus.OK).body(hrresearchService.save(hrresearchMapping.toDomain(hrresearchdto)));
    }

    @PreAuthorize("hasPermission(this.hrresearchMapping.toDomain(#hrresearchdtos),'iBizHumanResources-HRResearch-Save')")
    @ApiOperation(value = "批量保存科研成果", tags = {"科研成果" },  notes = "批量保存科研成果")
	@RequestMapping(method = RequestMethod.POST, value = "/hrresearches/savebatch")
    public ResponseEntity<Boolean> saveBatch(@RequestBody List<HRResearchDTO> hrresearchdtos) {
        hrresearchService.saveBatch(hrresearchMapping.toDomain(hrresearchdtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

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

    @PreAuthorize("hasPermission(this.hrresearchMapping.toDomain(#hrresearchdtos),'iBizHumanResources-HRResearch-Create')")
    @ApiOperation(value = "根据员工批量建立科研成果", tags = {"科研成果" },  notes = "根据员工批量建立科研成果")
	@RequestMapping(method = RequestMethod.POST, value = "/hremployees/{hremployee_id}/hrresearches/batch")
    public ResponseEntity<Boolean> createBatchByHREmployee(@PathVariable("hremployee_id") String hremployee_id, @RequestBody List<HRResearchDTO> hrresearchdtos) {
        List<HRResearch> domainlist=hrresearchMapping.toDomain(hrresearchdtos);
        for(HRResearch domain:domainlist){
            domain.setHremployeeid(hremployee_id);
        }
        hrresearchService.createBatch(domainlist);
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @VersionCheck(entity = "hrresearch" , versionfield = "updatedate")
    @PreAuthorize("hasPermission(this.hrresearchService.get(#hrresearch_id),'iBizHumanResources-HRResearch-Update')")
    @ApiOperation(value = "根据员工更新科研成果", tags = {"科研成果" },  notes = "根据员工更新科研成果")
	@RequestMapping(method = RequestMethod.PUT, value = "/hremployees/{hremployee_id}/hrresearches/{hrresearch_id}")
    @Transactional
    public ResponseEntity<HRResearchDTO> updateByHREmployee(@PathVariable("hremployee_id") String hremployee_id, @PathVariable("hrresearch_id") String hrresearch_id, @RequestBody HRResearchDTO hrresearchdto) {
        HRResearch domain = hrresearchMapping.toDomain(hrresearchdto);
        domain.setHremployeeid(hremployee_id);
        domain.setHrresearchid(hrresearch_id);
		hrresearchService.update(domain);
        HRResearchDTO dto = hrresearchMapping.toDto(domain);
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @PreAuthorize("hasPermission(this.hrresearchService.getHrresearchByEntities(this.hrresearchMapping.toDomain(#hrresearchdtos)),'iBizHumanResources-HRResearch-Update')")
    @ApiOperation(value = "根据员工批量更新科研成果", tags = {"科研成果" },  notes = "根据员工批量更新科研成果")
	@RequestMapping(method = RequestMethod.PUT, value = "/hremployees/{hremployee_id}/hrresearches/batch")
    public ResponseEntity<Boolean> updateBatchByHREmployee(@PathVariable("hremployee_id") String hremployee_id, @RequestBody List<HRResearchDTO> hrresearchdtos) {
        List<HRResearch> domainlist=hrresearchMapping.toDomain(hrresearchdtos);
        for(HRResearch domain:domainlist){
            domain.setHremployeeid(hremployee_id);
        }
        hrresearchService.updateBatch(domainlist);
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @PreAuthorize("hasPermission(this.hrresearchService.get(#hrresearch_id),'iBizHumanResources-HRResearch-Remove')")
    @ApiOperation(value = "根据员工删除科研成果", tags = {"科研成果" },  notes = "根据员工删除科研成果")
	@RequestMapping(method = RequestMethod.DELETE, value = "/hremployees/{hremployee_id}/hrresearches/{hrresearch_id}")
    @Transactional
    public ResponseEntity<Boolean> removeByHREmployee(@PathVariable("hremployee_id") String hremployee_id, @PathVariable("hrresearch_id") String hrresearch_id) {
		return ResponseEntity.status(HttpStatus.OK).body(hrresearchService.remove(hrresearch_id));
    }

    @PreAuthorize("hasPermission(this.hrresearchService.getHrresearchByIds(#ids),'iBizHumanResources-HRResearch-Remove')")
    @ApiOperation(value = "根据员工批量删除科研成果", tags = {"科研成果" },  notes = "根据员工批量删除科研成果")
	@RequestMapping(method = RequestMethod.DELETE, value = "/hremployees/{hremployee_id}/hrresearches/batch")
    public ResponseEntity<Boolean> removeBatchByHREmployee(@RequestBody List<String> ids) {
        hrresearchService.removeBatch(ids);
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @PostAuthorize("hasPermission(this.hrresearchMapping.toDomain(returnObject.body),'iBizHumanResources-HRResearch-Get')")
    @ApiOperation(value = "根据员工获取科研成果", tags = {"科研成果" },  notes = "根据员工获取科研成果")
	@RequestMapping(method = RequestMethod.GET, value = "/hremployees/{hremployee_id}/hrresearches/{hrresearch_id}")
    public ResponseEntity<HRResearchDTO> getByHREmployee(@PathVariable("hremployee_id") String hremployee_id, @PathVariable("hrresearch_id") String hrresearch_id) {
        HRResearch domain = hrresearchService.get(hrresearch_id);
        HRResearchDTO dto = hrresearchMapping.toDto(domain);
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @ApiOperation(value = "根据员工获取科研成果草稿", tags = {"科研成果" },  notes = "根据员工获取科研成果草稿")
    @RequestMapping(method = RequestMethod.GET, value = "/hremployees/{hremployee_id}/hrresearches/getdraft")
    public ResponseEntity<HRResearchDTO> getDraftByHREmployee(@PathVariable("hremployee_id") String hremployee_id) {
        HRResearch domain = new HRResearch();
        domain.setHremployeeid(hremployee_id);
        return ResponseEntity.status(HttpStatus.OK).body(hrresearchMapping.toDto(hrresearchService.getDraft(domain)));
    }

    @ApiOperation(value = "根据员工检查科研成果", tags = {"科研成果" },  notes = "根据员工检查科研成果")
	@RequestMapping(method = RequestMethod.POST, value = "/hremployees/{hremployee_id}/hrresearches/checkkey")
    public ResponseEntity<Boolean> checkKeyByHREmployee(@PathVariable("hremployee_id") String hremployee_id, @RequestBody HRResearchDTO hrresearchdto) {
        return  ResponseEntity.status(HttpStatus.OK).body(hrresearchService.checkKey(hrresearchMapping.toDomain(hrresearchdto)));
    }

    @PreAuthorize("hasPermission(this.hrresearchMapping.toDomain(#hrresearchdto),'iBizHumanResources-HRResearch-Save')")
    @ApiOperation(value = "根据员工保存科研成果", tags = {"科研成果" },  notes = "根据员工保存科研成果")
	@RequestMapping(method = RequestMethod.POST, value = "/hremployees/{hremployee_id}/hrresearches/save")
    public ResponseEntity<Boolean> saveByHREmployee(@PathVariable("hremployee_id") String hremployee_id, @RequestBody HRResearchDTO hrresearchdto) {
        HRResearch domain = hrresearchMapping.toDomain(hrresearchdto);
        domain.setHremployeeid(hremployee_id);
        return ResponseEntity.status(HttpStatus.OK).body(hrresearchService.save(domain));
    }

    @PreAuthorize("hasPermission(this.hrresearchMapping.toDomain(#hrresearchdtos),'iBizHumanResources-HRResearch-Save')")
    @ApiOperation(value = "根据员工批量保存科研成果", tags = {"科研成果" },  notes = "根据员工批量保存科研成果")
	@RequestMapping(method = RequestMethod.POST, value = "/hremployees/{hremployee_id}/hrresearches/savebatch")
    public ResponseEntity<Boolean> saveBatchByHREmployee(@PathVariable("hremployee_id") String hremployee_id, @RequestBody List<HRResearchDTO> hrresearchdtos) {
        List<HRResearch> domainlist=hrresearchMapping.toDomain(hrresearchdtos);
        for(HRResearch domain:domainlist){
             domain.setHremployeeid(hremployee_id);
        }
        hrresearchService.saveBatch(domainlist);
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','iBizHumanResources-HRResearch-searchDefault-all') and hasPermission(#context,'iBizHumanResources-HRResearch-Get')")
	@ApiOperation(value = "根据员工获取DEFAULT", tags = {"科研成果" } ,notes = "根据员工获取DEFAULT")
    @RequestMapping(method= RequestMethod.GET , value="/hremployees/{hremployee_id}/hrresearches/fetchdefault")
	public ResponseEntity<List<HRResearchDTO>> fetchHRResearchDefaultByHREmployee(@PathVariable("hremployee_id") String hremployee_id,HRResearchSearchContext context) {
        context.setN_hremployeeid_eq(hremployee_id);
        Page<HRResearch> domains = hrresearchService.searchDefault(context) ;
        List<HRResearchDTO> list = hrresearchMapping.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-HRResearch-searchDefault-all') and hasPermission(#context,'iBizHumanResources-HRResearch-Get')")
	@ApiOperation(value = "根据员工查询DEFAULT", tags = {"科研成果" } ,notes = "根据员工查询DEFAULT")
    @RequestMapping(method= RequestMethod.POST , value="/hremployees/{hremployee_id}/hrresearches/searchdefault")
	public ResponseEntity<Page<HRResearchDTO>> searchHRResearchDefaultByHREmployee(@PathVariable("hremployee_id") String hremployee_id, @RequestBody HRResearchSearchContext context) {
        context.setN_hremployeeid_eq(hremployee_id);
        Page<HRResearch> domains = hrresearchService.searchDefault(context) ;
	    return ResponseEntity.status(HttpStatus.OK)
                .body(new PageImpl(hrresearchMapping.toDto(domains.getContent()), context.getPageable(), domains.getTotalElements()));
	}
}

