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.HRPost;
import cn.ibizlab.humanresource.core.humanresource.service.IHRPostService;
import cn.ibizlab.humanresource.core.humanresource.filter.HRPostSearchContext;
import cn.ibizlab.humanresource.util.annotation.VersionCheck;

@Slf4j
@Api(tags = {"职位" })
@RestController("HRApi-hrpost")
@RequestMapping("")
public class HRPostResource {

    @Autowired
    public IHRPostService hrpostService;

    @Autowired
    @Lazy
    public HRPostMapping hrpostMapping;

    @PreAuthorize("hasPermission(this.hrpostMapping.toDomain(#hrpostdto),'iBizHumanResources-HRPost-Create')")
    @ApiOperation(value = "新建职位", tags = {"职位" },  notes = "新建职位")
	@RequestMapping(method = RequestMethod.POST, value = "/hrposts")
    @Transactional
    public ResponseEntity<HRPostDTO> create(@RequestBody HRPostDTO hrpostdto) {
        HRPost domain = hrpostMapping.toDomain(hrpostdto);
		hrpostService.create(domain);
        HRPostDTO dto = hrpostMapping.toDto(domain);
		return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @PreAuthorize("hasPermission(this.hrpostMapping.toDomain(#hrpostdtos),'iBizHumanResources-HRPost-Create')")
    @ApiOperation(value = "批量新建职位", tags = {"职位" },  notes = "批量新建职位")
	@RequestMapping(method = RequestMethod.POST, value = "/hrposts/batch")
    public ResponseEntity<Boolean> createBatch(@RequestBody List<HRPostDTO> hrpostdtos) {
        hrpostService.createBatch(hrpostMapping.toDomain(hrpostdtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @VersionCheck(entity = "hrpost" , versionfield = "updatedate")
    @PreAuthorize("hasPermission(this.hrpostService.get(#hrpost_id),'iBizHumanResources-HRPost-Update')")
    @ApiOperation(value = "更新职位", tags = {"职位" },  notes = "更新职位")
	@RequestMapping(method = RequestMethod.PUT, value = "/hrposts/{hrpost_id}")
    @Transactional
    public ResponseEntity<HRPostDTO> update(@PathVariable("hrpost_id") String hrpost_id, @RequestBody HRPostDTO hrpostdto) {
		HRPost domain  = hrpostMapping.toDomain(hrpostdto);
        domain .setHrpostid(hrpost_id);
		hrpostService.update(domain );
		HRPostDTO dto = hrpostMapping.toDto(domain );
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @PreAuthorize("hasPermission(this.hrpostService.getHrpostByEntities(this.hrpostMapping.toDomain(#hrpostdtos)),'iBizHumanResources-HRPost-Update')")
    @ApiOperation(value = "批量更新职位", tags = {"职位" },  notes = "批量更新职位")
	@RequestMapping(method = RequestMethod.PUT, value = "/hrposts/batch")
    public ResponseEntity<Boolean> updateBatch(@RequestBody List<HRPostDTO> hrpostdtos) {
        hrpostService.updateBatch(hrpostMapping.toDomain(hrpostdtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

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

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

    @PostAuthorize("hasPermission(this.hrpostMapping.toDomain(returnObject.body),'iBizHumanResources-HRPost-Get')")
    @ApiOperation(value = "获取职位", tags = {"职位" },  notes = "获取职位")
	@RequestMapping(method = RequestMethod.GET, value = "/hrposts/{hrpost_id}")
    public ResponseEntity<HRPostDTO> get(@PathVariable("hrpost_id") String hrpost_id) {
        HRPost domain = hrpostService.get(hrpost_id);
        HRPostDTO dto = hrpostMapping.toDto(domain);
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @ApiOperation(value = "获取职位草稿", tags = {"职位" },  notes = "获取职位草稿")
	@RequestMapping(method = RequestMethod.GET, value = "/hrposts/getdraft")
    public ResponseEntity<HRPostDTO> getDraft() {
        return ResponseEntity.status(HttpStatus.OK).body(hrpostMapping.toDto(hrpostService.getDraft(new HRPost())));
    }

    @ApiOperation(value = "检查职位", tags = {"职位" },  notes = "检查职位")
	@RequestMapping(method = RequestMethod.POST, value = "/hrposts/checkkey")
    public ResponseEntity<Boolean> checkKey(@RequestBody HRPostDTO hrpostdto) {
        return  ResponseEntity.status(HttpStatus.OK).body(hrpostService.checkKey(hrpostMapping.toDomain(hrpostdto)));
    }

    @PreAuthorize("hasPermission(this.hrpostMapping.toDomain(#hrpostdto),'iBizHumanResources-HRPost-Save')")
    @ApiOperation(value = "保存职位", tags = {"职位" },  notes = "保存职位")
	@RequestMapping(method = RequestMethod.POST, value = "/hrposts/save")
    public ResponseEntity<Boolean> save(@RequestBody HRPostDTO hrpostdto) {
        return ResponseEntity.status(HttpStatus.OK).body(hrpostService.save(hrpostMapping.toDomain(hrpostdto)));
    }

    @PreAuthorize("hasPermission(this.hrpostMapping.toDomain(#hrpostdtos),'iBizHumanResources-HRPost-Save')")
    @ApiOperation(value = "批量保存职位", tags = {"职位" },  notes = "批量保存职位")
	@RequestMapping(method = RequestMethod.POST, value = "/hrposts/savebatch")
    public ResponseEntity<Boolean> saveBatch(@RequestBody List<HRPostDTO> hrpostdtos) {
        hrpostService.saveBatch(hrpostMapping.toDomain(hrpostdtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','iBizHumanResources-HRPost-searchDefault-all') and hasPermission(#context,'iBizHumanResources-HRPost-Get')")
	@ApiOperation(value = "获取DEFAULT", tags = {"职位" } ,notes = "获取DEFAULT")
    @RequestMapping(method= RequestMethod.GET , value="/hrposts/fetchdefault")
	public ResponseEntity<List<HRPostDTO>> fetchDefault(HRPostSearchContext context) {
        Page<HRPost> domains = hrpostService.searchDefault(context) ;
        List<HRPostDTO> list = hrpostMapping.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-HRPost-searchDefault-all') and hasPermission(#context,'iBizHumanResources-HRPost-Get')")
	@ApiOperation(value = "查询DEFAULT", tags = {"职位" } ,notes = "查询DEFAULT")
    @RequestMapping(method= RequestMethod.POST , value="/hrposts/searchdefault")
	public ResponseEntity<Page<HRPostDTO>> searchDefault(@RequestBody HRPostSearchContext context) {
        Page<HRPost> domains = hrpostService.searchDefault(context) ;
	    return ResponseEntity.status(HttpStatus.OK)
                .body(new PageImpl(hrpostMapping.toDto(domains.getContent()), context.getPageable(), domains.getTotalElements()));
	}
}

