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.HRDutyCert;
import cn.ibizlab.humanresource.core.humanresource.service.IHRDutyCertService;
import cn.ibizlab.humanresource.core.humanresource.filter.HRDutyCertSearchContext;
import cn.ibizlab.humanresource.util.annotation.VersionCheck;

@Slf4j
@Api(tags = {"职务证书" })
@RestController("HRApi-hrdutycert")
@RequestMapping("")
public class HRDutyCertResource {

    @Autowired
    public IHRDutyCertService hrdutycertService;

    @Autowired
    @Lazy
    public HRDutyCertMapping hrdutycertMapping;

    @PreAuthorize("hasPermission(this.hrdutycertMapping.toDomain(#hrdutycertdto),'iBizHumanResources-HRDutyCert-Create')")
    @ApiOperation(value = "新建职务证书", tags = {"职务证书" },  notes = "新建职务证书")
	@RequestMapping(method = RequestMethod.POST, value = "/hrdutycerts")
    @Transactional
    public ResponseEntity<HRDutyCertDTO> create(@RequestBody HRDutyCertDTO hrdutycertdto) {
        HRDutyCert domain = hrdutycertMapping.toDomain(hrdutycertdto);
		hrdutycertService.create(domain);
        HRDutyCertDTO dto = hrdutycertMapping.toDto(domain);
		return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @PreAuthorize("hasPermission(this.hrdutycertMapping.toDomain(#hrdutycertdtos),'iBizHumanResources-HRDutyCert-Create')")
    @ApiOperation(value = "批量新建职务证书", tags = {"职务证书" },  notes = "批量新建职务证书")
	@RequestMapping(method = RequestMethod.POST, value = "/hrdutycerts/batch")
    public ResponseEntity<Boolean> createBatch(@RequestBody List<HRDutyCertDTO> hrdutycertdtos) {
        hrdutycertService.createBatch(hrdutycertMapping.toDomain(hrdutycertdtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @VersionCheck(entity = "hrdutycert" , versionfield = "updatedate")
    @PreAuthorize("hasPermission(this.hrdutycertService.get(#hrdutycert_id),'iBizHumanResources-HRDutyCert-Update')")
    @ApiOperation(value = "更新职务证书", tags = {"职务证书" },  notes = "更新职务证书")
	@RequestMapping(method = RequestMethod.PUT, value = "/hrdutycerts/{hrdutycert_id}")
    @Transactional
    public ResponseEntity<HRDutyCertDTO> update(@PathVariable("hrdutycert_id") String hrdutycert_id, @RequestBody HRDutyCertDTO hrdutycertdto) {
		HRDutyCert domain  = hrdutycertMapping.toDomain(hrdutycertdto);
        domain .setHrdutycertid(hrdutycert_id);
		hrdutycertService.update(domain );
		HRDutyCertDTO dto = hrdutycertMapping.toDto(domain );
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @PreAuthorize("hasPermission(this.hrdutycertService.getHrdutycertByEntities(this.hrdutycertMapping.toDomain(#hrdutycertdtos)),'iBizHumanResources-HRDutyCert-Update')")
    @ApiOperation(value = "批量更新职务证书", tags = {"职务证书" },  notes = "批量更新职务证书")
	@RequestMapping(method = RequestMethod.PUT, value = "/hrdutycerts/batch")
    public ResponseEntity<Boolean> updateBatch(@RequestBody List<HRDutyCertDTO> hrdutycertdtos) {
        hrdutycertService.updateBatch(hrdutycertMapping.toDomain(hrdutycertdtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @PreAuthorize("hasPermission(this.hrdutycertService.get(#hrdutycert_id),'iBizHumanResources-HRDutyCert-Remove')")
    @ApiOperation(value = "删除职务证书", tags = {"职务证书" },  notes = "删除职务证书")
	@RequestMapping(method = RequestMethod.DELETE, value = "/hrdutycerts/{hrdutycert_id}")
    @Transactional
    public ResponseEntity<Boolean> remove(@PathVariable("hrdutycert_id") String hrdutycert_id) {
         return ResponseEntity.status(HttpStatus.OK).body(hrdutycertService.remove(hrdutycert_id));
    }

    @PreAuthorize("hasPermission(this.hrdutycertService.getHrdutycertByIds(#ids),'iBizHumanResources-HRDutyCert-Remove')")
    @ApiOperation(value = "批量删除职务证书", tags = {"职务证书" },  notes = "批量删除职务证书")
	@RequestMapping(method = RequestMethod.DELETE, value = "/hrdutycerts/batch")
    public ResponseEntity<Boolean> removeBatch(@RequestBody List<String> ids) {
        hrdutycertService.removeBatch(ids);
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @PostAuthorize("hasPermission(this.hrdutycertMapping.toDomain(returnObject.body),'iBizHumanResources-HRDutyCert-Get')")
    @ApiOperation(value = "获取职务证书", tags = {"职务证书" },  notes = "获取职务证书")
	@RequestMapping(method = RequestMethod.GET, value = "/hrdutycerts/{hrdutycert_id}")
    public ResponseEntity<HRDutyCertDTO> get(@PathVariable("hrdutycert_id") String hrdutycert_id) {
        HRDutyCert domain = hrdutycertService.get(hrdutycert_id);
        HRDutyCertDTO dto = hrdutycertMapping.toDto(domain);
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @ApiOperation(value = "获取职务证书草稿", tags = {"职务证书" },  notes = "获取职务证书草稿")
	@RequestMapping(method = RequestMethod.GET, value = "/hrdutycerts/getdraft")
    public ResponseEntity<HRDutyCertDTO> getDraft() {
        return ResponseEntity.status(HttpStatus.OK).body(hrdutycertMapping.toDto(hrdutycertService.getDraft(new HRDutyCert())));
    }

    @ApiOperation(value = "检查职务证书", tags = {"职务证书" },  notes = "检查职务证书")
	@RequestMapping(method = RequestMethod.POST, value = "/hrdutycerts/checkkey")
    public ResponseEntity<Boolean> checkKey(@RequestBody HRDutyCertDTO hrdutycertdto) {
        return  ResponseEntity.status(HttpStatus.OK).body(hrdutycertService.checkKey(hrdutycertMapping.toDomain(hrdutycertdto)));
    }

    @PreAuthorize("hasPermission(this.hrdutycertMapping.toDomain(#hrdutycertdto),'iBizHumanResources-HRDutyCert-Save')")
    @ApiOperation(value = "保存职务证书", tags = {"职务证书" },  notes = "保存职务证书")
	@RequestMapping(method = RequestMethod.POST, value = "/hrdutycerts/save")
    public ResponseEntity<Boolean> save(@RequestBody HRDutyCertDTO hrdutycertdto) {
        return ResponseEntity.status(HttpStatus.OK).body(hrdutycertService.save(hrdutycertMapping.toDomain(hrdutycertdto)));
    }

    @PreAuthorize("hasPermission(this.hrdutycertMapping.toDomain(#hrdutycertdtos),'iBizHumanResources-HRDutyCert-Save')")
    @ApiOperation(value = "批量保存职务证书", tags = {"职务证书" },  notes = "批量保存职务证书")
	@RequestMapping(method = RequestMethod.POST, value = "/hrdutycerts/savebatch")
    public ResponseEntity<Boolean> saveBatch(@RequestBody List<HRDutyCertDTO> hrdutycertdtos) {
        hrdutycertService.saveBatch(hrdutycertMapping.toDomain(hrdutycertdtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','iBizHumanResources-HRDutyCert-searchDefault-all') and hasPermission(#context,'iBizHumanResources-HRDutyCert-Get')")
	@ApiOperation(value = "获取DEFAULT", tags = {"职务证书" } ,notes = "获取DEFAULT")
    @RequestMapping(method= RequestMethod.GET , value="/hrdutycerts/fetchdefault")
	public ResponseEntity<List<HRDutyCertDTO>> fetchDefault(HRDutyCertSearchContext context) {
        Page<HRDutyCert> domains = hrdutycertService.searchDefault(context) ;
        List<HRDutyCertDTO> list = hrdutycertMapping.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-HRDutyCert-searchDefault-all') and hasPermission(#context,'iBizHumanResources-HRDutyCert-Get')")
	@ApiOperation(value = "查询DEFAULT", tags = {"职务证书" } ,notes = "查询DEFAULT")
    @RequestMapping(method= RequestMethod.POST , value="/hrdutycerts/searchdefault")
	public ResponseEntity<Page<HRDutyCertDTO>> searchDefault(@RequestBody HRDutyCertSearchContext context) {
        Page<HRDutyCert> domains = hrdutycertService.searchDefault(context) ;
	    return ResponseEntity.status(HttpStatus.OK)
                .body(new PageImpl(hrdutycertMapping.toDto(domains.getContent()), context.getPageable(), domains.getTotalElements()));
	}
    @PreAuthorize("hasPermission(this.hrdutycertMapping.toDomain(#hrdutycertdto),'iBizHumanResources-HRDutyCert-Create')")
    @ApiOperation(value = "根据职务建立职务证书", tags = {"职务证书" },  notes = "根据职务建立职务证书")
	@RequestMapping(method = RequestMethod.POST, value = "/hrduties/{hrduty_id}/hrdutycerts")
    @Transactional
    public ResponseEntity<HRDutyCertDTO> createByHRDuty(@PathVariable("hrduty_id") String hrduty_id, @RequestBody HRDutyCertDTO hrdutycertdto) {
        HRDutyCert domain = hrdutycertMapping.toDomain(hrdutycertdto);
        domain.setHrdutyid(hrduty_id);
		hrdutycertService.create(domain);
        HRDutyCertDTO dto = hrdutycertMapping.toDto(domain);
		return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @PreAuthorize("hasPermission(this.hrdutycertMapping.toDomain(#hrdutycertdtos),'iBizHumanResources-HRDutyCert-Create')")
    @ApiOperation(value = "根据职务批量建立职务证书", tags = {"职务证书" },  notes = "根据职务批量建立职务证书")
	@RequestMapping(method = RequestMethod.POST, value = "/hrduties/{hrduty_id}/hrdutycerts/batch")
    public ResponseEntity<Boolean> createBatchByHRDuty(@PathVariable("hrduty_id") String hrduty_id, @RequestBody List<HRDutyCertDTO> hrdutycertdtos) {
        List<HRDutyCert> domainlist=hrdutycertMapping.toDomain(hrdutycertdtos);
        for(HRDutyCert domain:domainlist){
            domain.setHrdutyid(hrduty_id);
        }
        hrdutycertService.createBatch(domainlist);
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @VersionCheck(entity = "hrdutycert" , versionfield = "updatedate")
    @PreAuthorize("hasPermission(this.hrdutycertService.get(#hrdutycert_id),'iBizHumanResources-HRDutyCert-Update')")
    @ApiOperation(value = "根据职务更新职务证书", tags = {"职务证书" },  notes = "根据职务更新职务证书")
	@RequestMapping(method = RequestMethod.PUT, value = "/hrduties/{hrduty_id}/hrdutycerts/{hrdutycert_id}")
    @Transactional
    public ResponseEntity<HRDutyCertDTO> updateByHRDuty(@PathVariable("hrduty_id") String hrduty_id, @PathVariable("hrdutycert_id") String hrdutycert_id, @RequestBody HRDutyCertDTO hrdutycertdto) {
        HRDutyCert domain = hrdutycertMapping.toDomain(hrdutycertdto);
        domain.setHrdutyid(hrduty_id);
        domain.setHrdutycertid(hrdutycert_id);
		hrdutycertService.update(domain);
        HRDutyCertDTO dto = hrdutycertMapping.toDto(domain);
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @PreAuthorize("hasPermission(this.hrdutycertService.getHrdutycertByEntities(this.hrdutycertMapping.toDomain(#hrdutycertdtos)),'iBizHumanResources-HRDutyCert-Update')")
    @ApiOperation(value = "根据职务批量更新职务证书", tags = {"职务证书" },  notes = "根据职务批量更新职务证书")
	@RequestMapping(method = RequestMethod.PUT, value = "/hrduties/{hrduty_id}/hrdutycerts/batch")
    public ResponseEntity<Boolean> updateBatchByHRDuty(@PathVariable("hrduty_id") String hrduty_id, @RequestBody List<HRDutyCertDTO> hrdutycertdtos) {
        List<HRDutyCert> domainlist=hrdutycertMapping.toDomain(hrdutycertdtos);
        for(HRDutyCert domain:domainlist){
            domain.setHrdutyid(hrduty_id);
        }
        hrdutycertService.updateBatch(domainlist);
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @PreAuthorize("hasPermission(this.hrdutycertService.get(#hrdutycert_id),'iBizHumanResources-HRDutyCert-Remove')")
    @ApiOperation(value = "根据职务删除职务证书", tags = {"职务证书" },  notes = "根据职务删除职务证书")
	@RequestMapping(method = RequestMethod.DELETE, value = "/hrduties/{hrduty_id}/hrdutycerts/{hrdutycert_id}")
    @Transactional
    public ResponseEntity<Boolean> removeByHRDuty(@PathVariable("hrduty_id") String hrduty_id, @PathVariable("hrdutycert_id") String hrdutycert_id) {
		return ResponseEntity.status(HttpStatus.OK).body(hrdutycertService.remove(hrdutycert_id));
    }

    @PreAuthorize("hasPermission(this.hrdutycertService.getHrdutycertByIds(#ids),'iBizHumanResources-HRDutyCert-Remove')")
    @ApiOperation(value = "根据职务批量删除职务证书", tags = {"职务证书" },  notes = "根据职务批量删除职务证书")
	@RequestMapping(method = RequestMethod.DELETE, value = "/hrduties/{hrduty_id}/hrdutycerts/batch")
    public ResponseEntity<Boolean> removeBatchByHRDuty(@RequestBody List<String> ids) {
        hrdutycertService.removeBatch(ids);
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @PostAuthorize("hasPermission(this.hrdutycertMapping.toDomain(returnObject.body),'iBizHumanResources-HRDutyCert-Get')")
    @ApiOperation(value = "根据职务获取职务证书", tags = {"职务证书" },  notes = "根据职务获取职务证书")
	@RequestMapping(method = RequestMethod.GET, value = "/hrduties/{hrduty_id}/hrdutycerts/{hrdutycert_id}")
    public ResponseEntity<HRDutyCertDTO> getByHRDuty(@PathVariable("hrduty_id") String hrduty_id, @PathVariable("hrdutycert_id") String hrdutycert_id) {
        HRDutyCert domain = hrdutycertService.get(hrdutycert_id);
        HRDutyCertDTO dto = hrdutycertMapping.toDto(domain);
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @ApiOperation(value = "根据职务获取职务证书草稿", tags = {"职务证书" },  notes = "根据职务获取职务证书草稿")
    @RequestMapping(method = RequestMethod.GET, value = "/hrduties/{hrduty_id}/hrdutycerts/getdraft")
    public ResponseEntity<HRDutyCertDTO> getDraftByHRDuty(@PathVariable("hrduty_id") String hrduty_id) {
        HRDutyCert domain = new HRDutyCert();
        domain.setHrdutyid(hrduty_id);
        return ResponseEntity.status(HttpStatus.OK).body(hrdutycertMapping.toDto(hrdutycertService.getDraft(domain)));
    }

    @ApiOperation(value = "根据职务检查职务证书", tags = {"职务证书" },  notes = "根据职务检查职务证书")
	@RequestMapping(method = RequestMethod.POST, value = "/hrduties/{hrduty_id}/hrdutycerts/checkkey")
    public ResponseEntity<Boolean> checkKeyByHRDuty(@PathVariable("hrduty_id") String hrduty_id, @RequestBody HRDutyCertDTO hrdutycertdto) {
        return  ResponseEntity.status(HttpStatus.OK).body(hrdutycertService.checkKey(hrdutycertMapping.toDomain(hrdutycertdto)));
    }

    @PreAuthorize("hasPermission(this.hrdutycertMapping.toDomain(#hrdutycertdto),'iBizHumanResources-HRDutyCert-Save')")
    @ApiOperation(value = "根据职务保存职务证书", tags = {"职务证书" },  notes = "根据职务保存职务证书")
	@RequestMapping(method = RequestMethod.POST, value = "/hrduties/{hrduty_id}/hrdutycerts/save")
    public ResponseEntity<Boolean> saveByHRDuty(@PathVariable("hrduty_id") String hrduty_id, @RequestBody HRDutyCertDTO hrdutycertdto) {
        HRDutyCert domain = hrdutycertMapping.toDomain(hrdutycertdto);
        domain.setHrdutyid(hrduty_id);
        return ResponseEntity.status(HttpStatus.OK).body(hrdutycertService.save(domain));
    }

    @PreAuthorize("hasPermission(this.hrdutycertMapping.toDomain(#hrdutycertdtos),'iBizHumanResources-HRDutyCert-Save')")
    @ApiOperation(value = "根据职务批量保存职务证书", tags = {"职务证书" },  notes = "根据职务批量保存职务证书")
	@RequestMapping(method = RequestMethod.POST, value = "/hrduties/{hrduty_id}/hrdutycerts/savebatch")
    public ResponseEntity<Boolean> saveBatchByHRDuty(@PathVariable("hrduty_id") String hrduty_id, @RequestBody List<HRDutyCertDTO> hrdutycertdtos) {
        List<HRDutyCert> domainlist=hrdutycertMapping.toDomain(hrdutycertdtos);
        for(HRDutyCert domain:domainlist){
             domain.setHrdutyid(hrduty_id);
        }
        hrdutycertService.saveBatch(domainlist);
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','iBizHumanResources-HRDutyCert-searchDefault-all') and hasPermission(#context,'iBizHumanResources-HRDutyCert-Get')")
	@ApiOperation(value = "根据职务获取DEFAULT", tags = {"职务证书" } ,notes = "根据职务获取DEFAULT")
    @RequestMapping(method= RequestMethod.GET , value="/hrduties/{hrduty_id}/hrdutycerts/fetchdefault")
	public ResponseEntity<List<HRDutyCertDTO>> fetchHRDutyCertDefaultByHRDuty(@PathVariable("hrduty_id") String hrduty_id,HRDutyCertSearchContext context) {
        context.setN_hrdutyid_eq(hrduty_id);
        Page<HRDutyCert> domains = hrdutycertService.searchDefault(context) ;
        List<HRDutyCertDTO> list = hrdutycertMapping.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-HRDutyCert-searchDefault-all') and hasPermission(#context,'iBizHumanResources-HRDutyCert-Get')")
	@ApiOperation(value = "根据职务查询DEFAULT", tags = {"职务证书" } ,notes = "根据职务查询DEFAULT")
    @RequestMapping(method= RequestMethod.POST , value="/hrduties/{hrduty_id}/hrdutycerts/searchdefault")
	public ResponseEntity<Page<HRDutyCertDTO>> searchHRDutyCertDefaultByHRDuty(@PathVariable("hrduty_id") String hrduty_id, @RequestBody HRDutyCertSearchContext context) {
        context.setN_hrdutyid_eq(hrduty_id);
        Page<HRDutyCert> domains = hrdutycertService.searchDefault(context) ;
	    return ResponseEntity.status(HttpStatus.OK)
                .body(new PageImpl(hrdutycertMapping.toDto(domains.getContent()), context.getPageable(), domains.getTotalElements()));
	}
}

