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.HRPostRel;
import cn.ibizlab.humanresource.core.humanresource.service.IHRPostRelService;
import cn.ibizlab.humanresource.core.humanresource.filter.HRPostRelSearchContext;
import cn.ibizlab.humanresource.util.annotation.VersionCheck;

@Slf4j
@Api(tags = {"职位关系" })
@RestController("HRApi-hrpostrel")
@RequestMapping("")
public class HRPostRelResource {

    @Autowired
    public IHRPostRelService hrpostrelService;

    @Autowired
    @Lazy
    public HRPostRelMapping hrpostrelMapping;

    @PreAuthorize("hasPermission(this.hrpostrelMapping.toDomain(#hrpostreldto),'iBizHumanResources-HRPostRel-Create')")
    @ApiOperation(value = "新建职位关系", tags = {"职位关系" },  notes = "新建职位关系")
	@RequestMapping(method = RequestMethod.POST, value = "/hrpostrels")
    @Transactional
    public ResponseEntity<HRPostRelDTO> create(@RequestBody HRPostRelDTO hrpostreldto) {
        HRPostRel domain = hrpostrelMapping.toDomain(hrpostreldto);
		hrpostrelService.create(domain);
        HRPostRelDTO dto = hrpostrelMapping.toDto(domain);
		return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @PreAuthorize("hasPermission(this.hrpostrelMapping.toDomain(#hrpostreldtos),'iBizHumanResources-HRPostRel-Create')")
    @ApiOperation(value = "批量新建职位关系", tags = {"职位关系" },  notes = "批量新建职位关系")
	@RequestMapping(method = RequestMethod.POST, value = "/hrpostrels/batch")
    public ResponseEntity<Boolean> createBatch(@RequestBody List<HRPostRelDTO> hrpostreldtos) {
        hrpostrelService.createBatch(hrpostrelMapping.toDomain(hrpostreldtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @VersionCheck(entity = "hrpostrel" , versionfield = "updatedate")
    @PreAuthorize("hasPermission(this.hrpostrelService.get(#hrpostrel_id),'iBizHumanResources-HRPostRel-Update')")
    @ApiOperation(value = "更新职位关系", tags = {"职位关系" },  notes = "更新职位关系")
	@RequestMapping(method = RequestMethod.PUT, value = "/hrpostrels/{hrpostrel_id}")
    @Transactional
    public ResponseEntity<HRPostRelDTO> update(@PathVariable("hrpostrel_id") String hrpostrel_id, @RequestBody HRPostRelDTO hrpostreldto) {
		HRPostRel domain  = hrpostrelMapping.toDomain(hrpostreldto);
        domain .setHrpostrelid(hrpostrel_id);
		hrpostrelService.update(domain );
		HRPostRelDTO dto = hrpostrelMapping.toDto(domain );
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @PreAuthorize("hasPermission(this.hrpostrelService.getHrpostrelByEntities(this.hrpostrelMapping.toDomain(#hrpostreldtos)),'iBizHumanResources-HRPostRel-Update')")
    @ApiOperation(value = "批量更新职位关系", tags = {"职位关系" },  notes = "批量更新职位关系")
	@RequestMapping(method = RequestMethod.PUT, value = "/hrpostrels/batch")
    public ResponseEntity<Boolean> updateBatch(@RequestBody List<HRPostRelDTO> hrpostreldtos) {
        hrpostrelService.updateBatch(hrpostrelMapping.toDomain(hrpostreldtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @PreAuthorize("hasPermission(this.hrpostrelService.get(#hrpostrel_id),'iBizHumanResources-HRPostRel-Remove')")
    @ApiOperation(value = "删除职位关系", tags = {"职位关系" },  notes = "删除职位关系")
	@RequestMapping(method = RequestMethod.DELETE, value = "/hrpostrels/{hrpostrel_id}")
    @Transactional
    public ResponseEntity<Boolean> remove(@PathVariable("hrpostrel_id") String hrpostrel_id) {
         return ResponseEntity.status(HttpStatus.OK).body(hrpostrelService.remove(hrpostrel_id));
    }

    @PreAuthorize("hasPermission(this.hrpostrelService.getHrpostrelByIds(#ids),'iBizHumanResources-HRPostRel-Remove')")
    @ApiOperation(value = "批量删除职位关系", tags = {"职位关系" },  notes = "批量删除职位关系")
	@RequestMapping(method = RequestMethod.DELETE, value = "/hrpostrels/batch")
    public ResponseEntity<Boolean> removeBatch(@RequestBody List<String> ids) {
        hrpostrelService.removeBatch(ids);
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @PostAuthorize("hasPermission(this.hrpostrelMapping.toDomain(returnObject.body),'iBizHumanResources-HRPostRel-Get')")
    @ApiOperation(value = "获取职位关系", tags = {"职位关系" },  notes = "获取职位关系")
	@RequestMapping(method = RequestMethod.GET, value = "/hrpostrels/{hrpostrel_id}")
    public ResponseEntity<HRPostRelDTO> get(@PathVariable("hrpostrel_id") String hrpostrel_id) {
        HRPostRel domain = hrpostrelService.get(hrpostrel_id);
        HRPostRelDTO dto = hrpostrelMapping.toDto(domain);
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @ApiOperation(value = "获取职位关系草稿", tags = {"职位关系" },  notes = "获取职位关系草稿")
	@RequestMapping(method = RequestMethod.GET, value = "/hrpostrels/getdraft")
    public ResponseEntity<HRPostRelDTO> getDraft() {
        return ResponseEntity.status(HttpStatus.OK).body(hrpostrelMapping.toDto(hrpostrelService.getDraft(new HRPostRel())));
    }

    @ApiOperation(value = "检查职位关系", tags = {"职位关系" },  notes = "检查职位关系")
	@RequestMapping(method = RequestMethod.POST, value = "/hrpostrels/checkkey")
    public ResponseEntity<Boolean> checkKey(@RequestBody HRPostRelDTO hrpostreldto) {
        return  ResponseEntity.status(HttpStatus.OK).body(hrpostrelService.checkKey(hrpostrelMapping.toDomain(hrpostreldto)));
    }

    @PreAuthorize("hasPermission(this.hrpostrelMapping.toDomain(#hrpostreldto),'iBizHumanResources-HRPostRel-Save')")
    @ApiOperation(value = "保存职位关系", tags = {"职位关系" },  notes = "保存职位关系")
	@RequestMapping(method = RequestMethod.POST, value = "/hrpostrels/save")
    public ResponseEntity<Boolean> save(@RequestBody HRPostRelDTO hrpostreldto) {
        return ResponseEntity.status(HttpStatus.OK).body(hrpostrelService.save(hrpostrelMapping.toDomain(hrpostreldto)));
    }

    @PreAuthorize("hasPermission(this.hrpostrelMapping.toDomain(#hrpostreldtos),'iBizHumanResources-HRPostRel-Save')")
    @ApiOperation(value = "批量保存职位关系", tags = {"职位关系" },  notes = "批量保存职位关系")
	@RequestMapping(method = RequestMethod.POST, value = "/hrpostrels/savebatch")
    public ResponseEntity<Boolean> saveBatch(@RequestBody List<HRPostRelDTO> hrpostreldtos) {
        hrpostrelService.saveBatch(hrpostrelMapping.toDomain(hrpostreldtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','iBizHumanResources-HRPostRel-searchDefault-all') and hasPermission(#context,'iBizHumanResources-HRPostRel-Get')")
	@ApiOperation(value = "获取DEFAULT", tags = {"职位关系" } ,notes = "获取DEFAULT")
    @RequestMapping(method= RequestMethod.GET , value="/hrpostrels/fetchdefault")
	public ResponseEntity<List<HRPostRelDTO>> fetchDefault(HRPostRelSearchContext context) {
        Page<HRPostRel> domains = hrpostrelService.searchDefault(context) ;
        List<HRPostRelDTO> list = hrpostrelMapping.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-HRPostRel-searchDefault-all') and hasPermission(#context,'iBizHumanResources-HRPostRel-Get')")
	@ApiOperation(value = "查询DEFAULT", tags = {"职位关系" } ,notes = "查询DEFAULT")
    @RequestMapping(method= RequestMethod.POST , value="/hrpostrels/searchdefault")
	public ResponseEntity<Page<HRPostRelDTO>> searchDefault(@RequestBody HRPostRelSearchContext context) {
        Page<HRPostRel> domains = hrpostrelService.searchDefault(context) ;
	    return ResponseEntity.status(HttpStatus.OK)
                .body(new PageImpl(hrpostrelMapping.toDto(domains.getContent()), context.getPageable(), domains.getTotalElements()));
	}
    @PreAuthorize("hasPermission(this.hrpostrelMapping.toDomain(#hrpostreldto),'iBizHumanResources-HRPostRel-Create')")
    @ApiOperation(value = "根据职位建立职位关系", tags = {"职位关系" },  notes = "根据职位建立职位关系")
	@RequestMapping(method = RequestMethod.POST, value = "/hrposts/{hrpost_id}/hrpostrels")
    @Transactional
    public ResponseEntity<HRPostRelDTO> createByHRPost(@PathVariable("hrpost_id") String hrpost_id, @RequestBody HRPostRelDTO hrpostreldto) {
        HRPostRel domain = hrpostrelMapping.toDomain(hrpostreldto);
        domain.setHrpostid(hrpost_id);
		hrpostrelService.create(domain);
        HRPostRelDTO dto = hrpostrelMapping.toDto(domain);
		return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @PreAuthorize("hasPermission(this.hrpostrelMapping.toDomain(#hrpostreldtos),'iBizHumanResources-HRPostRel-Create')")
    @ApiOperation(value = "根据职位批量建立职位关系", tags = {"职位关系" },  notes = "根据职位批量建立职位关系")
	@RequestMapping(method = RequestMethod.POST, value = "/hrposts/{hrpost_id}/hrpostrels/batch")
    public ResponseEntity<Boolean> createBatchByHRPost(@PathVariable("hrpost_id") String hrpost_id, @RequestBody List<HRPostRelDTO> hrpostreldtos) {
        List<HRPostRel> domainlist=hrpostrelMapping.toDomain(hrpostreldtos);
        for(HRPostRel domain:domainlist){
            domain.setHrpostid(hrpost_id);
        }
        hrpostrelService.createBatch(domainlist);
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @VersionCheck(entity = "hrpostrel" , versionfield = "updatedate")
    @PreAuthorize("hasPermission(this.hrpostrelService.get(#hrpostrel_id),'iBizHumanResources-HRPostRel-Update')")
    @ApiOperation(value = "根据职位更新职位关系", tags = {"职位关系" },  notes = "根据职位更新职位关系")
	@RequestMapping(method = RequestMethod.PUT, value = "/hrposts/{hrpost_id}/hrpostrels/{hrpostrel_id}")
    @Transactional
    public ResponseEntity<HRPostRelDTO> updateByHRPost(@PathVariable("hrpost_id") String hrpost_id, @PathVariable("hrpostrel_id") String hrpostrel_id, @RequestBody HRPostRelDTO hrpostreldto) {
        HRPostRel domain = hrpostrelMapping.toDomain(hrpostreldto);
        domain.setHrpostid(hrpost_id);
        domain.setHrpostrelid(hrpostrel_id);
		hrpostrelService.update(domain);
        HRPostRelDTO dto = hrpostrelMapping.toDto(domain);
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @PreAuthorize("hasPermission(this.hrpostrelService.getHrpostrelByEntities(this.hrpostrelMapping.toDomain(#hrpostreldtos)),'iBizHumanResources-HRPostRel-Update')")
    @ApiOperation(value = "根据职位批量更新职位关系", tags = {"职位关系" },  notes = "根据职位批量更新职位关系")
	@RequestMapping(method = RequestMethod.PUT, value = "/hrposts/{hrpost_id}/hrpostrels/batch")
    public ResponseEntity<Boolean> updateBatchByHRPost(@PathVariable("hrpost_id") String hrpost_id, @RequestBody List<HRPostRelDTO> hrpostreldtos) {
        List<HRPostRel> domainlist=hrpostrelMapping.toDomain(hrpostreldtos);
        for(HRPostRel domain:domainlist){
            domain.setHrpostid(hrpost_id);
        }
        hrpostrelService.updateBatch(domainlist);
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @PreAuthorize("hasPermission(this.hrpostrelService.get(#hrpostrel_id),'iBizHumanResources-HRPostRel-Remove')")
    @ApiOperation(value = "根据职位删除职位关系", tags = {"职位关系" },  notes = "根据职位删除职位关系")
	@RequestMapping(method = RequestMethod.DELETE, value = "/hrposts/{hrpost_id}/hrpostrels/{hrpostrel_id}")
    @Transactional
    public ResponseEntity<Boolean> removeByHRPost(@PathVariable("hrpost_id") String hrpost_id, @PathVariable("hrpostrel_id") String hrpostrel_id) {
		return ResponseEntity.status(HttpStatus.OK).body(hrpostrelService.remove(hrpostrel_id));
    }

    @PreAuthorize("hasPermission(this.hrpostrelService.getHrpostrelByIds(#ids),'iBizHumanResources-HRPostRel-Remove')")
    @ApiOperation(value = "根据职位批量删除职位关系", tags = {"职位关系" },  notes = "根据职位批量删除职位关系")
	@RequestMapping(method = RequestMethod.DELETE, value = "/hrposts/{hrpost_id}/hrpostrels/batch")
    public ResponseEntity<Boolean> removeBatchByHRPost(@RequestBody List<String> ids) {
        hrpostrelService.removeBatch(ids);
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @PostAuthorize("hasPermission(this.hrpostrelMapping.toDomain(returnObject.body),'iBizHumanResources-HRPostRel-Get')")
    @ApiOperation(value = "根据职位获取职位关系", tags = {"职位关系" },  notes = "根据职位获取职位关系")
	@RequestMapping(method = RequestMethod.GET, value = "/hrposts/{hrpost_id}/hrpostrels/{hrpostrel_id}")
    public ResponseEntity<HRPostRelDTO> getByHRPost(@PathVariable("hrpost_id") String hrpost_id, @PathVariable("hrpostrel_id") String hrpostrel_id) {
        HRPostRel domain = hrpostrelService.get(hrpostrel_id);
        HRPostRelDTO dto = hrpostrelMapping.toDto(domain);
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @ApiOperation(value = "根据职位获取职位关系草稿", tags = {"职位关系" },  notes = "根据职位获取职位关系草稿")
    @RequestMapping(method = RequestMethod.GET, value = "/hrposts/{hrpost_id}/hrpostrels/getdraft")
    public ResponseEntity<HRPostRelDTO> getDraftByHRPost(@PathVariable("hrpost_id") String hrpost_id) {
        HRPostRel domain = new HRPostRel();
        domain.setHrpostid(hrpost_id);
        return ResponseEntity.status(HttpStatus.OK).body(hrpostrelMapping.toDto(hrpostrelService.getDraft(domain)));
    }

    @ApiOperation(value = "根据职位检查职位关系", tags = {"职位关系" },  notes = "根据职位检查职位关系")
	@RequestMapping(method = RequestMethod.POST, value = "/hrposts/{hrpost_id}/hrpostrels/checkkey")
    public ResponseEntity<Boolean> checkKeyByHRPost(@PathVariable("hrpost_id") String hrpost_id, @RequestBody HRPostRelDTO hrpostreldto) {
        return  ResponseEntity.status(HttpStatus.OK).body(hrpostrelService.checkKey(hrpostrelMapping.toDomain(hrpostreldto)));
    }

    @PreAuthorize("hasPermission(this.hrpostrelMapping.toDomain(#hrpostreldto),'iBizHumanResources-HRPostRel-Save')")
    @ApiOperation(value = "根据职位保存职位关系", tags = {"职位关系" },  notes = "根据职位保存职位关系")
	@RequestMapping(method = RequestMethod.POST, value = "/hrposts/{hrpost_id}/hrpostrels/save")
    public ResponseEntity<Boolean> saveByHRPost(@PathVariable("hrpost_id") String hrpost_id, @RequestBody HRPostRelDTO hrpostreldto) {
        HRPostRel domain = hrpostrelMapping.toDomain(hrpostreldto);
        domain.setHrpostid(hrpost_id);
        return ResponseEntity.status(HttpStatus.OK).body(hrpostrelService.save(domain));
    }

    @PreAuthorize("hasPermission(this.hrpostrelMapping.toDomain(#hrpostreldtos),'iBizHumanResources-HRPostRel-Save')")
    @ApiOperation(value = "根据职位批量保存职位关系", tags = {"职位关系" },  notes = "根据职位批量保存职位关系")
	@RequestMapping(method = RequestMethod.POST, value = "/hrposts/{hrpost_id}/hrpostrels/savebatch")
    public ResponseEntity<Boolean> saveBatchByHRPost(@PathVariable("hrpost_id") String hrpost_id, @RequestBody List<HRPostRelDTO> hrpostreldtos) {
        List<HRPostRel> domainlist=hrpostrelMapping.toDomain(hrpostreldtos);
        for(HRPostRel domain:domainlist){
             domain.setHrpostid(hrpost_id);
        }
        hrpostrelService.saveBatch(domainlist);
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','iBizHumanResources-HRPostRel-searchDefault-all') and hasPermission(#context,'iBizHumanResources-HRPostRel-Get')")
	@ApiOperation(value = "根据职位获取DEFAULT", tags = {"职位关系" } ,notes = "根据职位获取DEFAULT")
    @RequestMapping(method= RequestMethod.GET , value="/hrposts/{hrpost_id}/hrpostrels/fetchdefault")
	public ResponseEntity<List<HRPostRelDTO>> fetchHRPostRelDefaultByHRPost(@PathVariable("hrpost_id") String hrpost_id,HRPostRelSearchContext context) {
        context.setN_hrpostid_eq(hrpost_id);
        Page<HRPostRel> domains = hrpostrelService.searchDefault(context) ;
        List<HRPostRelDTO> list = hrpostrelMapping.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-HRPostRel-searchDefault-all') and hasPermission(#context,'iBizHumanResources-HRPostRel-Get')")
	@ApiOperation(value = "根据职位查询DEFAULT", tags = {"职位关系" } ,notes = "根据职位查询DEFAULT")
    @RequestMapping(method= RequestMethod.POST , value="/hrposts/{hrpost_id}/hrpostrels/searchdefault")
	public ResponseEntity<Page<HRPostRelDTO>> searchHRPostRelDefaultByHRPost(@PathVariable("hrpost_id") String hrpost_id, @RequestBody HRPostRelSearchContext context) {
        context.setN_hrpostid_eq(hrpost_id);
        Page<HRPostRel> domains = hrpostrelService.searchDefault(context) ;
	    return ResponseEntity.status(HttpStatus.OK)
                .body(new PageImpl(hrpostrelMapping.toDto(domains.getContent()), context.getPageable(), domains.getTotalElements()));
	}
}

