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.HREmployee;
import cn.ibizlab.humanresource.core.humanresource.service.IHREmployeeService;
import cn.ibizlab.humanresource.core.humanresource.filter.HREmployeeSearchContext;
import cn.ibizlab.humanresource.util.annotation.VersionCheck;

@Slf4j
@Api(tags = {"员工" })
@RestController("HRApi-hremployee")
@RequestMapping("")
public class HREmployeeResource {

    @Autowired
    public IHREmployeeService hremployeeService;

    @Autowired
    @Lazy
    public HREmployeeMapping hremployeeMapping;

    @PreAuthorize("hasPermission(this.hremployeeMapping.toDomain(#hremployeedto),'iBizHumanResources-HREmployee-Create')")
    @ApiOperation(value = "新建员工", tags = {"员工" },  notes = "新建员工")
	@RequestMapping(method = RequestMethod.POST, value = "/hremployees")
    @Transactional
    public ResponseEntity<HREmployeeDTO> create(@RequestBody HREmployeeDTO hremployeedto) {
        HREmployee domain = hremployeeMapping.toDomain(hremployeedto);
		hremployeeService.create(domain);
        HREmployeeDTO dto = hremployeeMapping.toDto(domain);
		return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @PreAuthorize("hasPermission(this.hremployeeMapping.toDomain(#hremployeedtos),'iBizHumanResources-HREmployee-Create')")
    @ApiOperation(value = "批量新建员工", tags = {"员工" },  notes = "批量新建员工")
	@RequestMapping(method = RequestMethod.POST, value = "/hremployees/batch")
    public ResponseEntity<Boolean> createBatch(@RequestBody List<HREmployeeDTO> hremployeedtos) {
        hremployeeService.createBatch(hremployeeMapping.toDomain(hremployeedtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @VersionCheck(entity = "hremployee" , versionfield = "updatedate")
    @PreAuthorize("hasPermission(this.hremployeeService.get(#hremployee_id),'iBizHumanResources-HREmployee-Update')")
    @ApiOperation(value = "更新员工", tags = {"员工" },  notes = "更新员工")
	@RequestMapping(method = RequestMethod.PUT, value = "/hremployees/{hremployee_id}")
    @Transactional
    public ResponseEntity<HREmployeeDTO> update(@PathVariable("hremployee_id") String hremployee_id, @RequestBody HREmployeeDTO hremployeedto) {
		HREmployee domain  = hremployeeMapping.toDomain(hremployeedto);
        domain .setEmployeeid(hremployee_id);
		hremployeeService.update(domain );
		HREmployeeDTO dto = hremployeeMapping.toDto(domain );
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @PreAuthorize("hasPermission(this.hremployeeService.getHremployeeByEntities(this.hremployeeMapping.toDomain(#hremployeedtos)),'iBizHumanResources-HREmployee-Update')")
    @ApiOperation(value = "批量更新员工", tags = {"员工" },  notes = "批量更新员工")
	@RequestMapping(method = RequestMethod.PUT, value = "/hremployees/batch")
    public ResponseEntity<Boolean> updateBatch(@RequestBody List<HREmployeeDTO> hremployeedtos) {
        hremployeeService.updateBatch(hremployeeMapping.toDomain(hremployeedtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @PreAuthorize("hasPermission(this.hremployeeService.get(#hremployee_id),'iBizHumanResources-HREmployee-Remove')")
    @ApiOperation(value = "删除员工", tags = {"员工" },  notes = "删除员工")
	@RequestMapping(method = RequestMethod.DELETE, value = "/hremployees/{hremployee_id}")
    @Transactional
    public ResponseEntity<Boolean> remove(@PathVariable("hremployee_id") String hremployee_id) {
         return ResponseEntity.status(HttpStatus.OK).body(hremployeeService.remove(hremployee_id));
    }

    @PreAuthorize("hasPermission(this.hremployeeService.getHremployeeByIds(#ids),'iBizHumanResources-HREmployee-Remove')")
    @ApiOperation(value = "批量删除员工", tags = {"员工" },  notes = "批量删除员工")
	@RequestMapping(method = RequestMethod.DELETE, value = "/hremployees/batch")
    public ResponseEntity<Boolean> removeBatch(@RequestBody List<String> ids) {
        hremployeeService.removeBatch(ids);
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @PostAuthorize("hasPermission(this.hremployeeMapping.toDomain(returnObject.body),'iBizHumanResources-HREmployee-Get')")
    @ApiOperation(value = "获取员工", tags = {"员工" },  notes = "获取员工")
	@RequestMapping(method = RequestMethod.GET, value = "/hremployees/{hremployee_id}")
    public ResponseEntity<HREmployeeDTO> get(@PathVariable("hremployee_id") String hremployee_id) {
        HREmployee domain = hremployeeService.get(hremployee_id);
        HREmployeeDTO dto = hremployeeMapping.toDto(domain);
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @ApiOperation(value = "获取员工草稿", tags = {"员工" },  notes = "获取员工草稿")
	@RequestMapping(method = RequestMethod.GET, value = "/hremployees/getdraft")
    public ResponseEntity<HREmployeeDTO> getDraft() {
        return ResponseEntity.status(HttpStatus.OK).body(hremployeeMapping.toDto(hremployeeService.getDraft(new HREmployee())));
    }

    @ApiOperation(value = "检查员工", tags = {"员工" },  notes = "检查员工")
	@RequestMapping(method = RequestMethod.POST, value = "/hremployees/checkkey")
    public ResponseEntity<Boolean> checkKey(@RequestBody HREmployeeDTO hremployeedto) {
        return  ResponseEntity.status(HttpStatus.OK).body(hremployeeService.checkKey(hremployeeMapping.toDomain(hremployeedto)));
    }

    @PreAuthorize("hasPermission(this.hremployeeMapping.toDomain(#hremployeedto),'iBizHumanResources-HREmployee-Save')")
    @ApiOperation(value = "保存员工", tags = {"员工" },  notes = "保存员工")
	@RequestMapping(method = RequestMethod.POST, value = "/hremployees/save")
    public ResponseEntity<Boolean> save(@RequestBody HREmployeeDTO hremployeedto) {
        return ResponseEntity.status(HttpStatus.OK).body(hremployeeService.save(hremployeeMapping.toDomain(hremployeedto)));
    }

    @PreAuthorize("hasPermission(this.hremployeeMapping.toDomain(#hremployeedtos),'iBizHumanResources-HREmployee-Save')")
    @ApiOperation(value = "批量保存员工", tags = {"员工" },  notes = "批量保存员工")
	@RequestMapping(method = RequestMethod.POST, value = "/hremployees/savebatch")
    public ResponseEntity<Boolean> saveBatch(@RequestBody List<HREmployeeDTO> hremployeedtos) {
        hremployeeService.saveBatch(hremployeeMapping.toDomain(hremployeedtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','iBizHumanResources-HREmployee-searchAfterProbation-all') and hasPermission(#context,'iBizHumanResources-HREmployee-Get')")
	@ApiOperation(value = "获取待定工作人员", tags = {"员工" } ,notes = "获取待定工作人员")
    @RequestMapping(method= RequestMethod.GET , value="/hremployees/fetchafterprobation")
	public ResponseEntity<List<HREmployeeDTO>> fetchAfterProbation(HREmployeeSearchContext context) {
        Page<HREmployee> domains = hremployeeService.searchAfterProbation(context) ;
        List<HREmployeeDTO> list = hremployeeMapping.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-HREmployee-searchAfterProbation-all') and hasPermission(#context,'iBizHumanResources-HREmployee-Get')")
	@ApiOperation(value = "查询待定工作人员", tags = {"员工" } ,notes = "查询待定工作人员")
    @RequestMapping(method= RequestMethod.POST , value="/hremployees/searchafterprobation")
	public ResponseEntity<Page<HREmployeeDTO>> searchAfterProbation(@RequestBody HREmployeeSearchContext context) {
        Page<HREmployee> domains = hremployeeService.searchAfterProbation(context) ;
	    return ResponseEntity.status(HttpStatus.OK)
                .body(new PageImpl(hremployeeMapping.toDto(domains.getContent()), context.getPageable(), domains.getTotalElements()));
	}
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','iBizHumanResources-HREmployee-searchDefault-all') and hasPermission(#context,'iBizHumanResources-HREmployee-Get')")
	@ApiOperation(value = "获取DEFAULT", tags = {"员工" } ,notes = "获取DEFAULT")
    @RequestMapping(method= RequestMethod.GET , value="/hremployees/fetchdefault")
	public ResponseEntity<List<HREmployeeDTO>> fetchDefault(HREmployeeSearchContext context) {
        Page<HREmployee> domains = hremployeeService.searchDefault(context) ;
        List<HREmployeeDTO> list = hremployeeMapping.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-HREmployee-searchDefault-all') and hasPermission(#context,'iBizHumanResources-HREmployee-Get')")
	@ApiOperation(value = "查询DEFAULT", tags = {"员工" } ,notes = "查询DEFAULT")
    @RequestMapping(method= RequestMethod.POST , value="/hremployees/searchdefault")
	public ResponseEntity<Page<HREmployeeDTO>> searchDefault(@RequestBody HREmployeeSearchContext context) {
        Page<HREmployee> domains = hremployeeService.searchDefault(context) ;
	    return ResponseEntity.status(HttpStatus.OK)
                .body(new PageImpl(hremployeeMapping.toDto(domains.getContent()), context.getPageable(), domains.getTotalElements()));
	}
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','iBizHumanResources-HREmployee-searchLeaveDuty-all') and hasPermission(#context,'iBizHumanResources-HREmployee-Get')")
	@ApiOperation(value = "获取前员工", tags = {"员工" } ,notes = "获取前员工")
    @RequestMapping(method= RequestMethod.GET , value="/hremployees/fetchleaveduty")
	public ResponseEntity<List<HREmployeeDTO>> fetchLeaveDuty(HREmployeeSearchContext context) {
        Page<HREmployee> domains = hremployeeService.searchLeaveDuty(context) ;
        List<HREmployeeDTO> list = hremployeeMapping.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-HREmployee-searchLeaveDuty-all') and hasPermission(#context,'iBizHumanResources-HREmployee-Get')")
	@ApiOperation(value = "查询前员工", tags = {"员工" } ,notes = "查询前员工")
    @RequestMapping(method= RequestMethod.POST , value="/hremployees/searchleaveduty")
	public ResponseEntity<Page<HREmployeeDTO>> searchLeaveDuty(@RequestBody HREmployeeSearchContext context) {
        Page<HREmployee> domains = hremployeeService.searchLeaveDuty(context) ;
	    return ResponseEntity.status(HttpStatus.OK)
                .body(new PageImpl(hremployeeMapping.toDto(domains.getContent()), context.getPageable(), domains.getTotalElements()));
	}
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','iBizHumanResources-HREmployee-searchNoContract-all') and hasPermission(#context,'iBizHumanResources-HREmployee-Get')")
	@ApiOperation(value = "获取待新签合同人员", tags = {"员工" } ,notes = "获取待新签合同人员")
    @RequestMapping(method= RequestMethod.GET , value="/hremployees/fetchnocontract")
	public ResponseEntity<List<HREmployeeDTO>> fetchNoContract(HREmployeeSearchContext context) {
        Page<HREmployee> domains = hremployeeService.searchNoContract(context) ;
        List<HREmployeeDTO> list = hremployeeMapping.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-HREmployee-searchNoContract-all') and hasPermission(#context,'iBizHumanResources-HREmployee-Get')")
	@ApiOperation(value = "查询待新签合同人员", tags = {"员工" } ,notes = "查询待新签合同人员")
    @RequestMapping(method= RequestMethod.POST , value="/hremployees/searchnocontract")
	public ResponseEntity<Page<HREmployeeDTO>> searchNoContract(@RequestBody HREmployeeSearchContext context) {
        Page<HREmployee> domains = hremployeeService.searchNoContract(context) ;
	    return ResponseEntity.status(HttpStatus.OK)
                .body(new PageImpl(hremployeeMapping.toDto(domains.getContent()), context.getPageable(), domains.getTotalElements()));
	}
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','iBizHumanResources-HREmployee-searchOnDuty-all') and hasPermission(#context,'iBizHumanResources-HREmployee-Get')")
	@ApiOperation(value = "获取在岗员工", tags = {"员工" } ,notes = "获取在岗员工")
    @RequestMapping(method= RequestMethod.GET , value="/hremployees/fetchonduty")
	public ResponseEntity<List<HREmployeeDTO>> fetchOnDuty(HREmployeeSearchContext context) {
        Page<HREmployee> domains = hremployeeService.searchOnDuty(context) ;
        List<HREmployeeDTO> list = hremployeeMapping.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-HREmployee-searchOnDuty-all') and hasPermission(#context,'iBizHumanResources-HREmployee-Get')")
	@ApiOperation(value = "查询在岗员工", tags = {"员工" } ,notes = "查询在岗员工")
    @RequestMapping(method= RequestMethod.POST , value="/hremployees/searchonduty")
	public ResponseEntity<Page<HREmployeeDTO>> searchOnDuty(@RequestBody HREmployeeSearchContext context) {
        Page<HREmployee> domains = hremployeeService.searchOnDuty(context) ;
	    return ResponseEntity.status(HttpStatus.OK)
                .body(new PageImpl(hremployeeMapping.toDto(domains.getContent()), context.getPageable(), domains.getTotalElements()));
	}
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','iBizHumanResources-HREmployee-searchWaitDuty-all') and hasPermission(#context,'iBizHumanResources-HREmployee-Get')")
	@ApiOperation(value = "获取待定工作人员", tags = {"员工" } ,notes = "获取待定工作人员")
    @RequestMapping(method= RequestMethod.GET , value="/hremployees/fetchwaitduty")
	public ResponseEntity<List<HREmployeeDTO>> fetchWaitDuty(HREmployeeSearchContext context) {
        Page<HREmployee> domains = hremployeeService.searchWaitDuty(context) ;
        List<HREmployeeDTO> list = hremployeeMapping.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-HREmployee-searchWaitDuty-all') and hasPermission(#context,'iBizHumanResources-HREmployee-Get')")
	@ApiOperation(value = "查询待定工作人员", tags = {"员工" } ,notes = "查询待定工作人员")
    @RequestMapping(method= RequestMethod.POST , value="/hremployees/searchwaitduty")
	public ResponseEntity<Page<HREmployeeDTO>> searchWaitDuty(@RequestBody HREmployeeSearchContext context) {
        Page<HREmployee> domains = hremployeeService.searchWaitDuty(context) ;
	    return ResponseEntity.status(HttpStatus.OK)
                .body(new PageImpl(hremployeeMapping.toDto(domains.getContent()), context.getPageable(), domains.getTotalElements()));
	}
}

