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.HRDistribution;
import cn.ibizlab.humanresource.core.humanresource.service.IHRDistributionService;
import cn.ibizlab.humanresource.core.humanresource.filter.HRDistributionSearchContext;
import cn.ibizlab.humanresource.util.annotation.VersionCheck;

@Slf4j
@Api(tags = {"工作人员分配" })
@RestController("HRApi-hrdistribution")
@RequestMapping("")
public class HRDistributionResource {

    @Autowired
    public IHRDistributionService hrdistributionService;

    @Autowired
    @Lazy
    public HRDistributionMapping hrdistributionMapping;

    @PreAuthorize("hasPermission(this.hrdistributionMapping.toDomain(#hrdistributiondto),'iBizHumanResources-HRDistribution-Create')")
    @ApiOperation(value = "新建工作人员分配", tags = {"工作人员分配" },  notes = "新建工作人员分配")
	@RequestMapping(method = RequestMethod.POST, value = "/hrdistributions")
    @Transactional
    public ResponseEntity<HRDistributionDTO> create(@RequestBody HRDistributionDTO hrdistributiondto) {
        HRDistribution domain = hrdistributionMapping.toDomain(hrdistributiondto);
		hrdistributionService.create(domain);
        HRDistributionDTO dto = hrdistributionMapping.toDto(domain);
		return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @PreAuthorize("hasPermission(this.hrdistributionMapping.toDomain(#hrdistributiondtos),'iBizHumanResources-HRDistribution-Create')")
    @ApiOperation(value = "批量新建工作人员分配", tags = {"工作人员分配" },  notes = "批量新建工作人员分配")
	@RequestMapping(method = RequestMethod.POST, value = "/hrdistributions/batch")
    public ResponseEntity<Boolean> createBatch(@RequestBody List<HRDistributionDTO> hrdistributiondtos) {
        hrdistributionService.createBatch(hrdistributionMapping.toDomain(hrdistributiondtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @VersionCheck(entity = "hrdistribution" , versionfield = "updatedate")
    @PreAuthorize("hasPermission(this.hrdistributionService.get(#hrdistribution_id),'iBizHumanResources-HRDistribution-Update')")
    @ApiOperation(value = "更新工作人员分配", tags = {"工作人员分配" },  notes = "更新工作人员分配")
	@RequestMapping(method = RequestMethod.PUT, value = "/hrdistributions/{hrdistribution_id}")
    @Transactional
    public ResponseEntity<HRDistributionDTO> update(@PathVariable("hrdistribution_id") String hrdistribution_id, @RequestBody HRDistributionDTO hrdistributiondto) {
		HRDistribution domain  = hrdistributionMapping.toDomain(hrdistributiondto);
        domain .setHrdistributionid(hrdistribution_id);
		hrdistributionService.update(domain );
		HRDistributionDTO dto = hrdistributionMapping.toDto(domain );
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @PreAuthorize("hasPermission(this.hrdistributionService.getHrdistributionByEntities(this.hrdistributionMapping.toDomain(#hrdistributiondtos)),'iBizHumanResources-HRDistribution-Update')")
    @ApiOperation(value = "批量更新工作人员分配", tags = {"工作人员分配" },  notes = "批量更新工作人员分配")
	@RequestMapping(method = RequestMethod.PUT, value = "/hrdistributions/batch")
    public ResponseEntity<Boolean> updateBatch(@RequestBody List<HRDistributionDTO> hrdistributiondtos) {
        hrdistributionService.updateBatch(hrdistributionMapping.toDomain(hrdistributiondtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @PreAuthorize("hasPermission(this.hrdistributionService.get(#hrdistribution_id),'iBizHumanResources-HRDistribution-Remove')")
    @ApiOperation(value = "删除工作人员分配", tags = {"工作人员分配" },  notes = "删除工作人员分配")
	@RequestMapping(method = RequestMethod.DELETE, value = "/hrdistributions/{hrdistribution_id}")
    @Transactional
    public ResponseEntity<Boolean> remove(@PathVariable("hrdistribution_id") String hrdistribution_id) {
         return ResponseEntity.status(HttpStatus.OK).body(hrdistributionService.remove(hrdistribution_id));
    }

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

    @PostAuthorize("hasPermission(this.hrdistributionMapping.toDomain(returnObject.body),'iBizHumanResources-HRDistribution-Get')")
    @ApiOperation(value = "获取工作人员分配", tags = {"工作人员分配" },  notes = "获取工作人员分配")
	@RequestMapping(method = RequestMethod.GET, value = "/hrdistributions/{hrdistribution_id}")
    public ResponseEntity<HRDistributionDTO> get(@PathVariable("hrdistribution_id") String hrdistribution_id) {
        HRDistribution domain = hrdistributionService.get(hrdistribution_id);
        HRDistributionDTO dto = hrdistributionMapping.toDto(domain);
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @ApiOperation(value = "获取工作人员分配草稿", tags = {"工作人员分配" },  notes = "获取工作人员分配草稿")
	@RequestMapping(method = RequestMethod.GET, value = "/hrdistributions/getdraft")
    public ResponseEntity<HRDistributionDTO> getDraft() {
        return ResponseEntity.status(HttpStatus.OK).body(hrdistributionMapping.toDto(hrdistributionService.getDraft(new HRDistribution())));
    }

    @ApiOperation(value = "检查工作人员分配", tags = {"工作人员分配" },  notes = "检查工作人员分配")
	@RequestMapping(method = RequestMethod.POST, value = "/hrdistributions/checkkey")
    public ResponseEntity<Boolean> checkKey(@RequestBody HRDistributionDTO hrdistributiondto) {
        return  ResponseEntity.status(HttpStatus.OK).body(hrdistributionService.checkKey(hrdistributionMapping.toDomain(hrdistributiondto)));
    }

    @PreAuthorize("hasPermission(this.hrdistributionMapping.toDomain(#hrdistributiondto),'iBizHumanResources-HRDistribution-Save')")
    @ApiOperation(value = "保存工作人员分配", tags = {"工作人员分配" },  notes = "保存工作人员分配")
	@RequestMapping(method = RequestMethod.POST, value = "/hrdistributions/save")
    public ResponseEntity<Boolean> save(@RequestBody HRDistributionDTO hrdistributiondto) {
        return ResponseEntity.status(HttpStatus.OK).body(hrdistributionService.save(hrdistributionMapping.toDomain(hrdistributiondto)));
    }

    @PreAuthorize("hasPermission(this.hrdistributionMapping.toDomain(#hrdistributiondtos),'iBizHumanResources-HRDistribution-Save')")
    @ApiOperation(value = "批量保存工作人员分配", tags = {"工作人员分配" },  notes = "批量保存工作人员分配")
	@RequestMapping(method = RequestMethod.POST, value = "/hrdistributions/savebatch")
    public ResponseEntity<Boolean> saveBatch(@RequestBody List<HRDistributionDTO> hrdistributiondtos) {
        hrdistributionService.saveBatch(hrdistributionMapping.toDomain(hrdistributiondtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','iBizHumanResources-HRDistribution-searchDefault-all') and hasPermission(#context,'iBizHumanResources-HRDistribution-Get')")
	@ApiOperation(value = "获取DEFAULT", tags = {"工作人员分配" } ,notes = "获取DEFAULT")
    @RequestMapping(method= RequestMethod.GET , value="/hrdistributions/fetchdefault")
	public ResponseEntity<List<HRDistributionDTO>> fetchDefault(HRDistributionSearchContext context) {
        Page<HRDistribution> domains = hrdistributionService.searchDefault(context) ;
        List<HRDistributionDTO> list = hrdistributionMapping.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-HRDistribution-searchDefault-all') and hasPermission(#context,'iBizHumanResources-HRDistribution-Get')")
	@ApiOperation(value = "查询DEFAULT", tags = {"工作人员分配" } ,notes = "查询DEFAULT")
    @RequestMapping(method= RequestMethod.POST , value="/hrdistributions/searchdefault")
	public ResponseEntity<Page<HRDistributionDTO>> searchDefault(@RequestBody HRDistributionSearchContext context) {
        Page<HRDistribution> domains = hrdistributionService.searchDefault(context) ;
	    return ResponseEntity.status(HttpStatus.OK)
                .body(new PageImpl(hrdistributionMapping.toDto(domains.getContent()), context.getPageable(), domains.getTotalElements()));
	}
    @PreAuthorize("hasPermission(this.hrdistributionMapping.toDomain(#hrdistributiondto),'iBizHumanResources-HRDistribution-Create')")
    @ApiOperation(value = "根据职位建立工作人员分配", tags = {"工作人员分配" },  notes = "根据职位建立工作人员分配")
	@RequestMapping(method = RequestMethod.POST, value = "/hrposts/{hrpost_id}/hrdistributions")
    @Transactional
    public ResponseEntity<HRDistributionDTO> createByHRPost(@PathVariable("hrpost_id") String hrpost_id, @RequestBody HRDistributionDTO hrdistributiondto) {
        HRDistribution domain = hrdistributionMapping.toDomain(hrdistributiondto);
        domain.setHrpostid(hrpost_id);
		hrdistributionService.create(domain);
        HRDistributionDTO dto = hrdistributionMapping.toDto(domain);
		return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @PreAuthorize("hasPermission(this.hrdistributionMapping.toDomain(#hrdistributiondtos),'iBizHumanResources-HRDistribution-Create')")
    @ApiOperation(value = "根据职位批量建立工作人员分配", tags = {"工作人员分配" },  notes = "根据职位批量建立工作人员分配")
	@RequestMapping(method = RequestMethod.POST, value = "/hrposts/{hrpost_id}/hrdistributions/batch")
    public ResponseEntity<Boolean> createBatchByHRPost(@PathVariable("hrpost_id") String hrpost_id, @RequestBody List<HRDistributionDTO> hrdistributiondtos) {
        List<HRDistribution> domainlist=hrdistributionMapping.toDomain(hrdistributiondtos);
        for(HRDistribution domain:domainlist){
            domain.setHrpostid(hrpost_id);
        }
        hrdistributionService.createBatch(domainlist);
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @VersionCheck(entity = "hrdistribution" , versionfield = "updatedate")
    @PreAuthorize("hasPermission(this.hrdistributionService.get(#hrdistribution_id),'iBizHumanResources-HRDistribution-Update')")
    @ApiOperation(value = "根据职位更新工作人员分配", tags = {"工作人员分配" },  notes = "根据职位更新工作人员分配")
	@RequestMapping(method = RequestMethod.PUT, value = "/hrposts/{hrpost_id}/hrdistributions/{hrdistribution_id}")
    @Transactional
    public ResponseEntity<HRDistributionDTO> updateByHRPost(@PathVariable("hrpost_id") String hrpost_id, @PathVariable("hrdistribution_id") String hrdistribution_id, @RequestBody HRDistributionDTO hrdistributiondto) {
        HRDistribution domain = hrdistributionMapping.toDomain(hrdistributiondto);
        domain.setHrpostid(hrpost_id);
        domain.setHrdistributionid(hrdistribution_id);
		hrdistributionService.update(domain);
        HRDistributionDTO dto = hrdistributionMapping.toDto(domain);
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @PreAuthorize("hasPermission(this.hrdistributionService.getHrdistributionByEntities(this.hrdistributionMapping.toDomain(#hrdistributiondtos)),'iBizHumanResources-HRDistribution-Update')")
    @ApiOperation(value = "根据职位批量更新工作人员分配", tags = {"工作人员分配" },  notes = "根据职位批量更新工作人员分配")
	@RequestMapping(method = RequestMethod.PUT, value = "/hrposts/{hrpost_id}/hrdistributions/batch")
    public ResponseEntity<Boolean> updateBatchByHRPost(@PathVariable("hrpost_id") String hrpost_id, @RequestBody List<HRDistributionDTO> hrdistributiondtos) {
        List<HRDistribution> domainlist=hrdistributionMapping.toDomain(hrdistributiondtos);
        for(HRDistribution domain:domainlist){
            domain.setHrpostid(hrpost_id);
        }
        hrdistributionService.updateBatch(domainlist);
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @PreAuthorize("hasPermission(this.hrdistributionService.get(#hrdistribution_id),'iBizHumanResources-HRDistribution-Remove')")
    @ApiOperation(value = "根据职位删除工作人员分配", tags = {"工作人员分配" },  notes = "根据职位删除工作人员分配")
	@RequestMapping(method = RequestMethod.DELETE, value = "/hrposts/{hrpost_id}/hrdistributions/{hrdistribution_id}")
    @Transactional
    public ResponseEntity<Boolean> removeByHRPost(@PathVariable("hrpost_id") String hrpost_id, @PathVariable("hrdistribution_id") String hrdistribution_id) {
		return ResponseEntity.status(HttpStatus.OK).body(hrdistributionService.remove(hrdistribution_id));
    }

    @PreAuthorize("hasPermission(this.hrdistributionService.getHrdistributionByIds(#ids),'iBizHumanResources-HRDistribution-Remove')")
    @ApiOperation(value = "根据职位批量删除工作人员分配", tags = {"工作人员分配" },  notes = "根据职位批量删除工作人员分配")
	@RequestMapping(method = RequestMethod.DELETE, value = "/hrposts/{hrpost_id}/hrdistributions/batch")
    public ResponseEntity<Boolean> removeBatchByHRPost(@RequestBody List<String> ids) {
        hrdistributionService.removeBatch(ids);
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @PostAuthorize("hasPermission(this.hrdistributionMapping.toDomain(returnObject.body),'iBizHumanResources-HRDistribution-Get')")
    @ApiOperation(value = "根据职位获取工作人员分配", tags = {"工作人员分配" },  notes = "根据职位获取工作人员分配")
	@RequestMapping(method = RequestMethod.GET, value = "/hrposts/{hrpost_id}/hrdistributions/{hrdistribution_id}")
    public ResponseEntity<HRDistributionDTO> getByHRPost(@PathVariable("hrpost_id") String hrpost_id, @PathVariable("hrdistribution_id") String hrdistribution_id) {
        HRDistribution domain = hrdistributionService.get(hrdistribution_id);
        HRDistributionDTO dto = hrdistributionMapping.toDto(domain);
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @ApiOperation(value = "根据职位获取工作人员分配草稿", tags = {"工作人员分配" },  notes = "根据职位获取工作人员分配草稿")
    @RequestMapping(method = RequestMethod.GET, value = "/hrposts/{hrpost_id}/hrdistributions/getdraft")
    public ResponseEntity<HRDistributionDTO> getDraftByHRPost(@PathVariable("hrpost_id") String hrpost_id) {
        HRDistribution domain = new HRDistribution();
        domain.setHrpostid(hrpost_id);
        return ResponseEntity.status(HttpStatus.OK).body(hrdistributionMapping.toDto(hrdistributionService.getDraft(domain)));
    }

    @ApiOperation(value = "根据职位检查工作人员分配", tags = {"工作人员分配" },  notes = "根据职位检查工作人员分配")
	@RequestMapping(method = RequestMethod.POST, value = "/hrposts/{hrpost_id}/hrdistributions/checkkey")
    public ResponseEntity<Boolean> checkKeyByHRPost(@PathVariable("hrpost_id") String hrpost_id, @RequestBody HRDistributionDTO hrdistributiondto) {
        return  ResponseEntity.status(HttpStatus.OK).body(hrdistributionService.checkKey(hrdistributionMapping.toDomain(hrdistributiondto)));
    }

    @PreAuthorize("hasPermission(this.hrdistributionMapping.toDomain(#hrdistributiondto),'iBizHumanResources-HRDistribution-Save')")
    @ApiOperation(value = "根据职位保存工作人员分配", tags = {"工作人员分配" },  notes = "根据职位保存工作人员分配")
	@RequestMapping(method = RequestMethod.POST, value = "/hrposts/{hrpost_id}/hrdistributions/save")
    public ResponseEntity<Boolean> saveByHRPost(@PathVariable("hrpost_id") String hrpost_id, @RequestBody HRDistributionDTO hrdistributiondto) {
        HRDistribution domain = hrdistributionMapping.toDomain(hrdistributiondto);
        domain.setHrpostid(hrpost_id);
        return ResponseEntity.status(HttpStatus.OK).body(hrdistributionService.save(domain));
    }

    @PreAuthorize("hasPermission(this.hrdistributionMapping.toDomain(#hrdistributiondtos),'iBizHumanResources-HRDistribution-Save')")
    @ApiOperation(value = "根据职位批量保存工作人员分配", tags = {"工作人员分配" },  notes = "根据职位批量保存工作人员分配")
	@RequestMapping(method = RequestMethod.POST, value = "/hrposts/{hrpost_id}/hrdistributions/savebatch")
    public ResponseEntity<Boolean> saveBatchByHRPost(@PathVariable("hrpost_id") String hrpost_id, @RequestBody List<HRDistributionDTO> hrdistributiondtos) {
        List<HRDistribution> domainlist=hrdistributionMapping.toDomain(hrdistributiondtos);
        for(HRDistribution domain:domainlist){
             domain.setHrpostid(hrpost_id);
        }
        hrdistributionService.saveBatch(domainlist);
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','iBizHumanResources-HRDistribution-searchDefault-all') and hasPermission(#context,'iBizHumanResources-HRDistribution-Get')")
	@ApiOperation(value = "根据职位获取DEFAULT", tags = {"工作人员分配" } ,notes = "根据职位获取DEFAULT")
    @RequestMapping(method= RequestMethod.GET , value="/hrposts/{hrpost_id}/hrdistributions/fetchdefault")
	public ResponseEntity<List<HRDistributionDTO>> fetchHRDistributionDefaultByHRPost(@PathVariable("hrpost_id") String hrpost_id,HRDistributionSearchContext context) {
        context.setN_hrpostid_eq(hrpost_id);
        Page<HRDistribution> domains = hrdistributionService.searchDefault(context) ;
        List<HRDistributionDTO> list = hrdistributionMapping.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-HRDistribution-searchDefault-all') and hasPermission(#context,'iBizHumanResources-HRDistribution-Get')")
	@ApiOperation(value = "根据职位查询DEFAULT", tags = {"工作人员分配" } ,notes = "根据职位查询DEFAULT")
    @RequestMapping(method= RequestMethod.POST , value="/hrposts/{hrpost_id}/hrdistributions/searchdefault")
	public ResponseEntity<Page<HRDistributionDTO>> searchHRDistributionDefaultByHRPost(@PathVariable("hrpost_id") String hrpost_id, @RequestBody HRDistributionSearchContext context) {
        context.setN_hrpostid_eq(hrpost_id);
        Page<HRDistribution> domains = hrdistributionService.searchDefault(context) ;
	    return ResponseEntity.status(HttpStatus.OK)
                .body(new PageImpl(hrdistributionMapping.toDto(domains.getContent()), context.getPageable(), domains.getTotalElements()));
	}
}

