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.HRLanguageAbility;
import cn.ibizlab.humanresource.core.humanresource.service.IHRLanguageAbilityService;
import cn.ibizlab.humanresource.core.humanresource.filter.HRLanguageAbilitySearchContext;
import cn.ibizlab.humanresource.util.annotation.VersionCheck;

@Slf4j
@Api(tags = {"语言能力" })
@RestController("HRApi-hrlanguageability")
@RequestMapping("")
public class HRLanguageAbilityResource {

    @Autowired
    public IHRLanguageAbilityService hrlanguageabilityService;

    @Autowired
    @Lazy
    public HRLanguageAbilityMapping hrlanguageabilityMapping;

    @PreAuthorize("hasPermission(this.hrlanguageabilityMapping.toDomain(#hrlanguageabilitydto),'iBizHumanResources-HRLanguageAbility-Create')")
    @ApiOperation(value = "新建语言能力", tags = {"语言能力" },  notes = "新建语言能力")
	@RequestMapping(method = RequestMethod.POST, value = "/hrlanguageabilities")
    @Transactional
    public ResponseEntity<HRLanguageAbilityDTO> create(@RequestBody HRLanguageAbilityDTO hrlanguageabilitydto) {
        HRLanguageAbility domain = hrlanguageabilityMapping.toDomain(hrlanguageabilitydto);
		hrlanguageabilityService.create(domain);
        HRLanguageAbilityDTO dto = hrlanguageabilityMapping.toDto(domain);
		return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @PreAuthorize("hasPermission(this.hrlanguageabilityMapping.toDomain(#hrlanguageabilitydtos),'iBizHumanResources-HRLanguageAbility-Create')")
    @ApiOperation(value = "批量新建语言能力", tags = {"语言能力" },  notes = "批量新建语言能力")
	@RequestMapping(method = RequestMethod.POST, value = "/hrlanguageabilities/batch")
    public ResponseEntity<Boolean> createBatch(@RequestBody List<HRLanguageAbilityDTO> hrlanguageabilitydtos) {
        hrlanguageabilityService.createBatch(hrlanguageabilityMapping.toDomain(hrlanguageabilitydtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @VersionCheck(entity = "hrlanguageability" , versionfield = "updatedate")
    @PreAuthorize("hasPermission(this.hrlanguageabilityService.get(#hrlanguageability_id),'iBizHumanResources-HRLanguageAbility-Update')")
    @ApiOperation(value = "更新语言能力", tags = {"语言能力" },  notes = "更新语言能力")
	@RequestMapping(method = RequestMethod.PUT, value = "/hrlanguageabilities/{hrlanguageability_id}")
    @Transactional
    public ResponseEntity<HRLanguageAbilityDTO> update(@PathVariable("hrlanguageability_id") String hrlanguageability_id, @RequestBody HRLanguageAbilityDTO hrlanguageabilitydto) {
		HRLanguageAbility domain  = hrlanguageabilityMapping.toDomain(hrlanguageabilitydto);
        domain .setHrlanguageabilityid(hrlanguageability_id);
		hrlanguageabilityService.update(domain );
		HRLanguageAbilityDTO dto = hrlanguageabilityMapping.toDto(domain );
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @PreAuthorize("hasPermission(this.hrlanguageabilityService.getHrlanguageabilityByEntities(this.hrlanguageabilityMapping.toDomain(#hrlanguageabilitydtos)),'iBizHumanResources-HRLanguageAbility-Update')")
    @ApiOperation(value = "批量更新语言能力", tags = {"语言能力" },  notes = "批量更新语言能力")
	@RequestMapping(method = RequestMethod.PUT, value = "/hrlanguageabilities/batch")
    public ResponseEntity<Boolean> updateBatch(@RequestBody List<HRLanguageAbilityDTO> hrlanguageabilitydtos) {
        hrlanguageabilityService.updateBatch(hrlanguageabilityMapping.toDomain(hrlanguageabilitydtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @PreAuthorize("hasPermission(this.hrlanguageabilityService.get(#hrlanguageability_id),'iBizHumanResources-HRLanguageAbility-Remove')")
    @ApiOperation(value = "删除语言能力", tags = {"语言能力" },  notes = "删除语言能力")
	@RequestMapping(method = RequestMethod.DELETE, value = "/hrlanguageabilities/{hrlanguageability_id}")
    @Transactional
    public ResponseEntity<Boolean> remove(@PathVariable("hrlanguageability_id") String hrlanguageability_id) {
         return ResponseEntity.status(HttpStatus.OK).body(hrlanguageabilityService.remove(hrlanguageability_id));
    }

    @PreAuthorize("hasPermission(this.hrlanguageabilityService.getHrlanguageabilityByIds(#ids),'iBizHumanResources-HRLanguageAbility-Remove')")
    @ApiOperation(value = "批量删除语言能力", tags = {"语言能力" },  notes = "批量删除语言能力")
	@RequestMapping(method = RequestMethod.DELETE, value = "/hrlanguageabilities/batch")
    public ResponseEntity<Boolean> removeBatch(@RequestBody List<String> ids) {
        hrlanguageabilityService.removeBatch(ids);
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @PostAuthorize("hasPermission(this.hrlanguageabilityMapping.toDomain(returnObject.body),'iBizHumanResources-HRLanguageAbility-Get')")
    @ApiOperation(value = "获取语言能力", tags = {"语言能力" },  notes = "获取语言能力")
	@RequestMapping(method = RequestMethod.GET, value = "/hrlanguageabilities/{hrlanguageability_id}")
    public ResponseEntity<HRLanguageAbilityDTO> get(@PathVariable("hrlanguageability_id") String hrlanguageability_id) {
        HRLanguageAbility domain = hrlanguageabilityService.get(hrlanguageability_id);
        HRLanguageAbilityDTO dto = hrlanguageabilityMapping.toDto(domain);
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @ApiOperation(value = "获取语言能力草稿", tags = {"语言能力" },  notes = "获取语言能力草稿")
	@RequestMapping(method = RequestMethod.GET, value = "/hrlanguageabilities/getdraft")
    public ResponseEntity<HRLanguageAbilityDTO> getDraft() {
        return ResponseEntity.status(HttpStatus.OK).body(hrlanguageabilityMapping.toDto(hrlanguageabilityService.getDraft(new HRLanguageAbility())));
    }

    @ApiOperation(value = "检查语言能力", tags = {"语言能力" },  notes = "检查语言能力")
	@RequestMapping(method = RequestMethod.POST, value = "/hrlanguageabilities/checkkey")
    public ResponseEntity<Boolean> checkKey(@RequestBody HRLanguageAbilityDTO hrlanguageabilitydto) {
        return  ResponseEntity.status(HttpStatus.OK).body(hrlanguageabilityService.checkKey(hrlanguageabilityMapping.toDomain(hrlanguageabilitydto)));
    }

    @PreAuthorize("hasPermission(this.hrlanguageabilityMapping.toDomain(#hrlanguageabilitydto),'iBizHumanResources-HRLanguageAbility-Save')")
    @ApiOperation(value = "保存语言能力", tags = {"语言能力" },  notes = "保存语言能力")
	@RequestMapping(method = RequestMethod.POST, value = "/hrlanguageabilities/save")
    public ResponseEntity<Boolean> save(@RequestBody HRLanguageAbilityDTO hrlanguageabilitydto) {
        return ResponseEntity.status(HttpStatus.OK).body(hrlanguageabilityService.save(hrlanguageabilityMapping.toDomain(hrlanguageabilitydto)));
    }

    @PreAuthorize("hasPermission(this.hrlanguageabilityMapping.toDomain(#hrlanguageabilitydtos),'iBizHumanResources-HRLanguageAbility-Save')")
    @ApiOperation(value = "批量保存语言能力", tags = {"语言能力" },  notes = "批量保存语言能力")
	@RequestMapping(method = RequestMethod.POST, value = "/hrlanguageabilities/savebatch")
    public ResponseEntity<Boolean> saveBatch(@RequestBody List<HRLanguageAbilityDTO> hrlanguageabilitydtos) {
        hrlanguageabilityService.saveBatch(hrlanguageabilityMapping.toDomain(hrlanguageabilitydtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

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

    @PreAuthorize("hasPermission(this.hrlanguageabilityMapping.toDomain(#hrlanguageabilitydtos),'iBizHumanResources-HRLanguageAbility-Create')")
    @ApiOperation(value = "根据员工批量建立语言能力", tags = {"语言能力" },  notes = "根据员工批量建立语言能力")
	@RequestMapping(method = RequestMethod.POST, value = "/hremployees/{hremployee_id}/hrlanguageabilities/batch")
    public ResponseEntity<Boolean> createBatchByHREmployee(@PathVariable("hremployee_id") String hremployee_id, @RequestBody List<HRLanguageAbilityDTO> hrlanguageabilitydtos) {
        List<HRLanguageAbility> domainlist=hrlanguageabilityMapping.toDomain(hrlanguageabilitydtos);
        for(HRLanguageAbility domain:domainlist){
            domain.setHremployeeid(hremployee_id);
        }
        hrlanguageabilityService.createBatch(domainlist);
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @VersionCheck(entity = "hrlanguageability" , versionfield = "updatedate")
    @PreAuthorize("hasPermission(this.hrlanguageabilityService.get(#hrlanguageability_id),'iBizHumanResources-HRLanguageAbility-Update')")
    @ApiOperation(value = "根据员工更新语言能力", tags = {"语言能力" },  notes = "根据员工更新语言能力")
	@RequestMapping(method = RequestMethod.PUT, value = "/hremployees/{hremployee_id}/hrlanguageabilities/{hrlanguageability_id}")
    @Transactional
    public ResponseEntity<HRLanguageAbilityDTO> updateByHREmployee(@PathVariable("hremployee_id") String hremployee_id, @PathVariable("hrlanguageability_id") String hrlanguageability_id, @RequestBody HRLanguageAbilityDTO hrlanguageabilitydto) {
        HRLanguageAbility domain = hrlanguageabilityMapping.toDomain(hrlanguageabilitydto);
        domain.setHremployeeid(hremployee_id);
        domain.setHrlanguageabilityid(hrlanguageability_id);
		hrlanguageabilityService.update(domain);
        HRLanguageAbilityDTO dto = hrlanguageabilityMapping.toDto(domain);
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @PreAuthorize("hasPermission(this.hrlanguageabilityService.getHrlanguageabilityByEntities(this.hrlanguageabilityMapping.toDomain(#hrlanguageabilitydtos)),'iBizHumanResources-HRLanguageAbility-Update')")
    @ApiOperation(value = "根据员工批量更新语言能力", tags = {"语言能力" },  notes = "根据员工批量更新语言能力")
	@RequestMapping(method = RequestMethod.PUT, value = "/hremployees/{hremployee_id}/hrlanguageabilities/batch")
    public ResponseEntity<Boolean> updateBatchByHREmployee(@PathVariable("hremployee_id") String hremployee_id, @RequestBody List<HRLanguageAbilityDTO> hrlanguageabilitydtos) {
        List<HRLanguageAbility> domainlist=hrlanguageabilityMapping.toDomain(hrlanguageabilitydtos);
        for(HRLanguageAbility domain:domainlist){
            domain.setHremployeeid(hremployee_id);
        }
        hrlanguageabilityService.updateBatch(domainlist);
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @PreAuthorize("hasPermission(this.hrlanguageabilityService.get(#hrlanguageability_id),'iBizHumanResources-HRLanguageAbility-Remove')")
    @ApiOperation(value = "根据员工删除语言能力", tags = {"语言能力" },  notes = "根据员工删除语言能力")
	@RequestMapping(method = RequestMethod.DELETE, value = "/hremployees/{hremployee_id}/hrlanguageabilities/{hrlanguageability_id}")
    @Transactional
    public ResponseEntity<Boolean> removeByHREmployee(@PathVariable("hremployee_id") String hremployee_id, @PathVariable("hrlanguageability_id") String hrlanguageability_id) {
		return ResponseEntity.status(HttpStatus.OK).body(hrlanguageabilityService.remove(hrlanguageability_id));
    }

    @PreAuthorize("hasPermission(this.hrlanguageabilityService.getHrlanguageabilityByIds(#ids),'iBizHumanResources-HRLanguageAbility-Remove')")
    @ApiOperation(value = "根据员工批量删除语言能力", tags = {"语言能力" },  notes = "根据员工批量删除语言能力")
	@RequestMapping(method = RequestMethod.DELETE, value = "/hremployees/{hremployee_id}/hrlanguageabilities/batch")
    public ResponseEntity<Boolean> removeBatchByHREmployee(@RequestBody List<String> ids) {
        hrlanguageabilityService.removeBatch(ids);
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @PostAuthorize("hasPermission(this.hrlanguageabilityMapping.toDomain(returnObject.body),'iBizHumanResources-HRLanguageAbility-Get')")
    @ApiOperation(value = "根据员工获取语言能力", tags = {"语言能力" },  notes = "根据员工获取语言能力")
	@RequestMapping(method = RequestMethod.GET, value = "/hremployees/{hremployee_id}/hrlanguageabilities/{hrlanguageability_id}")
    public ResponseEntity<HRLanguageAbilityDTO> getByHREmployee(@PathVariable("hremployee_id") String hremployee_id, @PathVariable("hrlanguageability_id") String hrlanguageability_id) {
        HRLanguageAbility domain = hrlanguageabilityService.get(hrlanguageability_id);
        HRLanguageAbilityDTO dto = hrlanguageabilityMapping.toDto(domain);
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @ApiOperation(value = "根据员工获取语言能力草稿", tags = {"语言能力" },  notes = "根据员工获取语言能力草稿")
    @RequestMapping(method = RequestMethod.GET, value = "/hremployees/{hremployee_id}/hrlanguageabilities/getdraft")
    public ResponseEntity<HRLanguageAbilityDTO> getDraftByHREmployee(@PathVariable("hremployee_id") String hremployee_id) {
        HRLanguageAbility domain = new HRLanguageAbility();
        domain.setHremployeeid(hremployee_id);
        return ResponseEntity.status(HttpStatus.OK).body(hrlanguageabilityMapping.toDto(hrlanguageabilityService.getDraft(domain)));
    }

    @ApiOperation(value = "根据员工检查语言能力", tags = {"语言能力" },  notes = "根据员工检查语言能力")
	@RequestMapping(method = RequestMethod.POST, value = "/hremployees/{hremployee_id}/hrlanguageabilities/checkkey")
    public ResponseEntity<Boolean> checkKeyByHREmployee(@PathVariable("hremployee_id") String hremployee_id, @RequestBody HRLanguageAbilityDTO hrlanguageabilitydto) {
        return  ResponseEntity.status(HttpStatus.OK).body(hrlanguageabilityService.checkKey(hrlanguageabilityMapping.toDomain(hrlanguageabilitydto)));
    }

    @PreAuthorize("hasPermission(this.hrlanguageabilityMapping.toDomain(#hrlanguageabilitydto),'iBizHumanResources-HRLanguageAbility-Save')")
    @ApiOperation(value = "根据员工保存语言能力", tags = {"语言能力" },  notes = "根据员工保存语言能力")
	@RequestMapping(method = RequestMethod.POST, value = "/hremployees/{hremployee_id}/hrlanguageabilities/save")
    public ResponseEntity<Boolean> saveByHREmployee(@PathVariable("hremployee_id") String hremployee_id, @RequestBody HRLanguageAbilityDTO hrlanguageabilitydto) {
        HRLanguageAbility domain = hrlanguageabilityMapping.toDomain(hrlanguageabilitydto);
        domain.setHremployeeid(hremployee_id);
        return ResponseEntity.status(HttpStatus.OK).body(hrlanguageabilityService.save(domain));
    }

    @PreAuthorize("hasPermission(this.hrlanguageabilityMapping.toDomain(#hrlanguageabilitydtos),'iBizHumanResources-HRLanguageAbility-Save')")
    @ApiOperation(value = "根据员工批量保存语言能力", tags = {"语言能力" },  notes = "根据员工批量保存语言能力")
	@RequestMapping(method = RequestMethod.POST, value = "/hremployees/{hremployee_id}/hrlanguageabilities/savebatch")
    public ResponseEntity<Boolean> saveBatchByHREmployee(@PathVariable("hremployee_id") String hremployee_id, @RequestBody List<HRLanguageAbilityDTO> hrlanguageabilitydtos) {
        List<HRLanguageAbility> domainlist=hrlanguageabilityMapping.toDomain(hrlanguageabilitydtos);
        for(HRLanguageAbility domain:domainlist){
             domain.setHremployeeid(hremployee_id);
        }
        hrlanguageabilityService.saveBatch(domainlist);
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','iBizHumanResources-HRLanguageAbility-searchDefault-all') and hasPermission(#context,'iBizHumanResources-HRLanguageAbility-Get')")
	@ApiOperation(value = "根据员工获取DEFAULT", tags = {"语言能力" } ,notes = "根据员工获取DEFAULT")
    @RequestMapping(method= RequestMethod.GET , value="/hremployees/{hremployee_id}/hrlanguageabilities/fetchdefault")
	public ResponseEntity<List<HRLanguageAbilityDTO>> fetchHRLanguageAbilityDefaultByHREmployee(@PathVariable("hremployee_id") String hremployee_id,HRLanguageAbilitySearchContext context) {
        context.setN_hremployeeid_eq(hremployee_id);
        Page<HRLanguageAbility> domains = hrlanguageabilityService.searchDefault(context) ;
        List<HRLanguageAbilityDTO> list = hrlanguageabilityMapping.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-HRLanguageAbility-searchDefault-all') and hasPermission(#context,'iBizHumanResources-HRLanguageAbility-Get')")
	@ApiOperation(value = "根据员工查询DEFAULT", tags = {"语言能力" } ,notes = "根据员工查询DEFAULT")
    @RequestMapping(method= RequestMethod.POST , value="/hremployees/{hremployee_id}/hrlanguageabilities/searchdefault")
	public ResponseEntity<Page<HRLanguageAbilityDTO>> searchHRLanguageAbilityDefaultByHREmployee(@PathVariable("hremployee_id") String hremployee_id, @RequestBody HRLanguageAbilitySearchContext context) {
        context.setN_hremployeeid_eq(hremployee_id);
        Page<HRLanguageAbility> domains = hrlanguageabilityService.searchDefault(context) ;
	    return ResponseEntity.status(HttpStatus.OK)
                .body(new PageImpl(hrlanguageabilityMapping.toDto(domains.getContent()), context.getPageable(), domains.getTotalElements()));
	}
}

