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.HRPaper;
import cn.ibizlab.humanresource.core.humanresource.service.IHRPaperService;
import cn.ibizlab.humanresource.core.humanresource.filter.HRPaperSearchContext;
import cn.ibizlab.humanresource.util.annotation.VersionCheck;

@Slf4j
@Api(tags = {"论文信息" })
@RestController("HRApi-hrpaper")
@RequestMapping("")
public class HRPaperResource {

    @Autowired
    public IHRPaperService hrpaperService;

    @Autowired
    @Lazy
    public HRPaperMapping hrpaperMapping;

    @PreAuthorize("hasPermission(this.hrpaperMapping.toDomain(#hrpaperdto),'iBizHumanResources-HRPaper-Create')")
    @ApiOperation(value = "新建论文信息", tags = {"论文信息" },  notes = "新建论文信息")
	@RequestMapping(method = RequestMethod.POST, value = "/hrpapers")
    @Transactional
    public ResponseEntity<HRPaperDTO> create(@RequestBody HRPaperDTO hrpaperdto) {
        HRPaper domain = hrpaperMapping.toDomain(hrpaperdto);
		hrpaperService.create(domain);
        HRPaperDTO dto = hrpaperMapping.toDto(domain);
		return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @PreAuthorize("hasPermission(this.hrpaperMapping.toDomain(#hrpaperdtos),'iBizHumanResources-HRPaper-Create')")
    @ApiOperation(value = "批量新建论文信息", tags = {"论文信息" },  notes = "批量新建论文信息")
	@RequestMapping(method = RequestMethod.POST, value = "/hrpapers/batch")
    public ResponseEntity<Boolean> createBatch(@RequestBody List<HRPaperDTO> hrpaperdtos) {
        hrpaperService.createBatch(hrpaperMapping.toDomain(hrpaperdtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @VersionCheck(entity = "hrpaper" , versionfield = "updatedate")
    @PreAuthorize("hasPermission(this.hrpaperService.get(#hrpaper_id),'iBizHumanResources-HRPaper-Update')")
    @ApiOperation(value = "更新论文信息", tags = {"论文信息" },  notes = "更新论文信息")
	@RequestMapping(method = RequestMethod.PUT, value = "/hrpapers/{hrpaper_id}")
    @Transactional
    public ResponseEntity<HRPaperDTO> update(@PathVariable("hrpaper_id") String hrpaper_id, @RequestBody HRPaperDTO hrpaperdto) {
		HRPaper domain  = hrpaperMapping.toDomain(hrpaperdto);
        domain .setHrpaperid(hrpaper_id);
		hrpaperService.update(domain );
		HRPaperDTO dto = hrpaperMapping.toDto(domain );
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @PreAuthorize("hasPermission(this.hrpaperService.getHrpaperByEntities(this.hrpaperMapping.toDomain(#hrpaperdtos)),'iBizHumanResources-HRPaper-Update')")
    @ApiOperation(value = "批量更新论文信息", tags = {"论文信息" },  notes = "批量更新论文信息")
	@RequestMapping(method = RequestMethod.PUT, value = "/hrpapers/batch")
    public ResponseEntity<Boolean> updateBatch(@RequestBody List<HRPaperDTO> hrpaperdtos) {
        hrpaperService.updateBatch(hrpaperMapping.toDomain(hrpaperdtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @PreAuthorize("hasPermission(this.hrpaperService.get(#hrpaper_id),'iBizHumanResources-HRPaper-Remove')")
    @ApiOperation(value = "删除论文信息", tags = {"论文信息" },  notes = "删除论文信息")
	@RequestMapping(method = RequestMethod.DELETE, value = "/hrpapers/{hrpaper_id}")
    @Transactional
    public ResponseEntity<Boolean> remove(@PathVariable("hrpaper_id") String hrpaper_id) {
         return ResponseEntity.status(HttpStatus.OK).body(hrpaperService.remove(hrpaper_id));
    }

    @PreAuthorize("hasPermission(this.hrpaperService.getHrpaperByIds(#ids),'iBizHumanResources-HRPaper-Remove')")
    @ApiOperation(value = "批量删除论文信息", tags = {"论文信息" },  notes = "批量删除论文信息")
	@RequestMapping(method = RequestMethod.DELETE, value = "/hrpapers/batch")
    public ResponseEntity<Boolean> removeBatch(@RequestBody List<String> ids) {
        hrpaperService.removeBatch(ids);
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @PostAuthorize("hasPermission(this.hrpaperMapping.toDomain(returnObject.body),'iBizHumanResources-HRPaper-Get')")
    @ApiOperation(value = "获取论文信息", tags = {"论文信息" },  notes = "获取论文信息")
	@RequestMapping(method = RequestMethod.GET, value = "/hrpapers/{hrpaper_id}")
    public ResponseEntity<HRPaperDTO> get(@PathVariable("hrpaper_id") String hrpaper_id) {
        HRPaper domain = hrpaperService.get(hrpaper_id);
        HRPaperDTO dto = hrpaperMapping.toDto(domain);
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @ApiOperation(value = "获取论文信息草稿", tags = {"论文信息" },  notes = "获取论文信息草稿")
	@RequestMapping(method = RequestMethod.GET, value = "/hrpapers/getdraft")
    public ResponseEntity<HRPaperDTO> getDraft() {
        return ResponseEntity.status(HttpStatus.OK).body(hrpaperMapping.toDto(hrpaperService.getDraft(new HRPaper())));
    }

    @ApiOperation(value = "检查论文信息", tags = {"论文信息" },  notes = "检查论文信息")
	@RequestMapping(method = RequestMethod.POST, value = "/hrpapers/checkkey")
    public ResponseEntity<Boolean> checkKey(@RequestBody HRPaperDTO hrpaperdto) {
        return  ResponseEntity.status(HttpStatus.OK).body(hrpaperService.checkKey(hrpaperMapping.toDomain(hrpaperdto)));
    }

    @PreAuthorize("hasPermission(this.hrpaperMapping.toDomain(#hrpaperdto),'iBizHumanResources-HRPaper-Save')")
    @ApiOperation(value = "保存论文信息", tags = {"论文信息" },  notes = "保存论文信息")
	@RequestMapping(method = RequestMethod.POST, value = "/hrpapers/save")
    public ResponseEntity<Boolean> save(@RequestBody HRPaperDTO hrpaperdto) {
        return ResponseEntity.status(HttpStatus.OK).body(hrpaperService.save(hrpaperMapping.toDomain(hrpaperdto)));
    }

    @PreAuthorize("hasPermission(this.hrpaperMapping.toDomain(#hrpaperdtos),'iBizHumanResources-HRPaper-Save')")
    @ApiOperation(value = "批量保存论文信息", tags = {"论文信息" },  notes = "批量保存论文信息")
	@RequestMapping(method = RequestMethod.POST, value = "/hrpapers/savebatch")
    public ResponseEntity<Boolean> saveBatch(@RequestBody List<HRPaperDTO> hrpaperdtos) {
        hrpaperService.saveBatch(hrpaperMapping.toDomain(hrpaperdtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','iBizHumanResources-HRPaper-searchDefault-all') and hasPermission(#context,'iBizHumanResources-HRPaper-Get')")
	@ApiOperation(value = "获取DEFAULT", tags = {"论文信息" } ,notes = "获取DEFAULT")
    @RequestMapping(method= RequestMethod.GET , value="/hrpapers/fetchdefault")
	public ResponseEntity<List<HRPaperDTO>> fetchDefault(HRPaperSearchContext context) {
        Page<HRPaper> domains = hrpaperService.searchDefault(context) ;
        List<HRPaperDTO> list = hrpaperMapping.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-HRPaper-searchDefault-all') and hasPermission(#context,'iBizHumanResources-HRPaper-Get')")
	@ApiOperation(value = "查询DEFAULT", tags = {"论文信息" } ,notes = "查询DEFAULT")
    @RequestMapping(method= RequestMethod.POST , value="/hrpapers/searchdefault")
	public ResponseEntity<Page<HRPaperDTO>> searchDefault(@RequestBody HRPaperSearchContext context) {
        Page<HRPaper> domains = hrpaperService.searchDefault(context) ;
	    return ResponseEntity.status(HttpStatus.OK)
                .body(new PageImpl(hrpaperMapping.toDto(domains.getContent()), context.getPageable(), domains.getTotalElements()));
	}
    @PreAuthorize("hasPermission(this.hrpaperMapping.toDomain(#hrpaperdto),'iBizHumanResources-HRPaper-Create')")
    @ApiOperation(value = "根据员工建立论文信息", tags = {"论文信息" },  notes = "根据员工建立论文信息")
	@RequestMapping(method = RequestMethod.POST, value = "/hremployees/{hremployee_id}/hrpapers")
    @Transactional
    public ResponseEntity<HRPaperDTO> createByHREmployee(@PathVariable("hremployee_id") String hremployee_id, @RequestBody HRPaperDTO hrpaperdto) {
        HRPaper domain = hrpaperMapping.toDomain(hrpaperdto);
        domain.setHremployeeid(hremployee_id);
		hrpaperService.create(domain);
        HRPaperDTO dto = hrpaperMapping.toDto(domain);
		return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @PreAuthorize("hasPermission(this.hrpaperMapping.toDomain(#hrpaperdtos),'iBizHumanResources-HRPaper-Create')")
    @ApiOperation(value = "根据员工批量建立论文信息", tags = {"论文信息" },  notes = "根据员工批量建立论文信息")
	@RequestMapping(method = RequestMethod.POST, value = "/hremployees/{hremployee_id}/hrpapers/batch")
    public ResponseEntity<Boolean> createBatchByHREmployee(@PathVariable("hremployee_id") String hremployee_id, @RequestBody List<HRPaperDTO> hrpaperdtos) {
        List<HRPaper> domainlist=hrpaperMapping.toDomain(hrpaperdtos);
        for(HRPaper domain:domainlist){
            domain.setHremployeeid(hremployee_id);
        }
        hrpaperService.createBatch(domainlist);
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @VersionCheck(entity = "hrpaper" , versionfield = "updatedate")
    @PreAuthorize("hasPermission(this.hrpaperService.get(#hrpaper_id),'iBizHumanResources-HRPaper-Update')")
    @ApiOperation(value = "根据员工更新论文信息", tags = {"论文信息" },  notes = "根据员工更新论文信息")
	@RequestMapping(method = RequestMethod.PUT, value = "/hremployees/{hremployee_id}/hrpapers/{hrpaper_id}")
    @Transactional
    public ResponseEntity<HRPaperDTO> updateByHREmployee(@PathVariable("hremployee_id") String hremployee_id, @PathVariable("hrpaper_id") String hrpaper_id, @RequestBody HRPaperDTO hrpaperdto) {
        HRPaper domain = hrpaperMapping.toDomain(hrpaperdto);
        domain.setHremployeeid(hremployee_id);
        domain.setHrpaperid(hrpaper_id);
		hrpaperService.update(domain);
        HRPaperDTO dto = hrpaperMapping.toDto(domain);
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @PreAuthorize("hasPermission(this.hrpaperService.getHrpaperByEntities(this.hrpaperMapping.toDomain(#hrpaperdtos)),'iBizHumanResources-HRPaper-Update')")
    @ApiOperation(value = "根据员工批量更新论文信息", tags = {"论文信息" },  notes = "根据员工批量更新论文信息")
	@RequestMapping(method = RequestMethod.PUT, value = "/hremployees/{hremployee_id}/hrpapers/batch")
    public ResponseEntity<Boolean> updateBatchByHREmployee(@PathVariable("hremployee_id") String hremployee_id, @RequestBody List<HRPaperDTO> hrpaperdtos) {
        List<HRPaper> domainlist=hrpaperMapping.toDomain(hrpaperdtos);
        for(HRPaper domain:domainlist){
            domain.setHremployeeid(hremployee_id);
        }
        hrpaperService.updateBatch(domainlist);
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @PreAuthorize("hasPermission(this.hrpaperService.get(#hrpaper_id),'iBizHumanResources-HRPaper-Remove')")
    @ApiOperation(value = "根据员工删除论文信息", tags = {"论文信息" },  notes = "根据员工删除论文信息")
	@RequestMapping(method = RequestMethod.DELETE, value = "/hremployees/{hremployee_id}/hrpapers/{hrpaper_id}")
    @Transactional
    public ResponseEntity<Boolean> removeByHREmployee(@PathVariable("hremployee_id") String hremployee_id, @PathVariable("hrpaper_id") String hrpaper_id) {
		return ResponseEntity.status(HttpStatus.OK).body(hrpaperService.remove(hrpaper_id));
    }

    @PreAuthorize("hasPermission(this.hrpaperService.getHrpaperByIds(#ids),'iBizHumanResources-HRPaper-Remove')")
    @ApiOperation(value = "根据员工批量删除论文信息", tags = {"论文信息" },  notes = "根据员工批量删除论文信息")
	@RequestMapping(method = RequestMethod.DELETE, value = "/hremployees/{hremployee_id}/hrpapers/batch")
    public ResponseEntity<Boolean> removeBatchByHREmployee(@RequestBody List<String> ids) {
        hrpaperService.removeBatch(ids);
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @PostAuthorize("hasPermission(this.hrpaperMapping.toDomain(returnObject.body),'iBizHumanResources-HRPaper-Get')")
    @ApiOperation(value = "根据员工获取论文信息", tags = {"论文信息" },  notes = "根据员工获取论文信息")
	@RequestMapping(method = RequestMethod.GET, value = "/hremployees/{hremployee_id}/hrpapers/{hrpaper_id}")
    public ResponseEntity<HRPaperDTO> getByHREmployee(@PathVariable("hremployee_id") String hremployee_id, @PathVariable("hrpaper_id") String hrpaper_id) {
        HRPaper domain = hrpaperService.get(hrpaper_id);
        HRPaperDTO dto = hrpaperMapping.toDto(domain);
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @ApiOperation(value = "根据员工获取论文信息草稿", tags = {"论文信息" },  notes = "根据员工获取论文信息草稿")
    @RequestMapping(method = RequestMethod.GET, value = "/hremployees/{hremployee_id}/hrpapers/getdraft")
    public ResponseEntity<HRPaperDTO> getDraftByHREmployee(@PathVariable("hremployee_id") String hremployee_id) {
        HRPaper domain = new HRPaper();
        domain.setHremployeeid(hremployee_id);
        return ResponseEntity.status(HttpStatus.OK).body(hrpaperMapping.toDto(hrpaperService.getDraft(domain)));
    }

    @ApiOperation(value = "根据员工检查论文信息", tags = {"论文信息" },  notes = "根据员工检查论文信息")
	@RequestMapping(method = RequestMethod.POST, value = "/hremployees/{hremployee_id}/hrpapers/checkkey")
    public ResponseEntity<Boolean> checkKeyByHREmployee(@PathVariable("hremployee_id") String hremployee_id, @RequestBody HRPaperDTO hrpaperdto) {
        return  ResponseEntity.status(HttpStatus.OK).body(hrpaperService.checkKey(hrpaperMapping.toDomain(hrpaperdto)));
    }

    @PreAuthorize("hasPermission(this.hrpaperMapping.toDomain(#hrpaperdto),'iBizHumanResources-HRPaper-Save')")
    @ApiOperation(value = "根据员工保存论文信息", tags = {"论文信息" },  notes = "根据员工保存论文信息")
	@RequestMapping(method = RequestMethod.POST, value = "/hremployees/{hremployee_id}/hrpapers/save")
    public ResponseEntity<Boolean> saveByHREmployee(@PathVariable("hremployee_id") String hremployee_id, @RequestBody HRPaperDTO hrpaperdto) {
        HRPaper domain = hrpaperMapping.toDomain(hrpaperdto);
        domain.setHremployeeid(hremployee_id);
        return ResponseEntity.status(HttpStatus.OK).body(hrpaperService.save(domain));
    }

    @PreAuthorize("hasPermission(this.hrpaperMapping.toDomain(#hrpaperdtos),'iBizHumanResources-HRPaper-Save')")
    @ApiOperation(value = "根据员工批量保存论文信息", tags = {"论文信息" },  notes = "根据员工批量保存论文信息")
	@RequestMapping(method = RequestMethod.POST, value = "/hremployees/{hremployee_id}/hrpapers/savebatch")
    public ResponseEntity<Boolean> saveBatchByHREmployee(@PathVariable("hremployee_id") String hremployee_id, @RequestBody List<HRPaperDTO> hrpaperdtos) {
        List<HRPaper> domainlist=hrpaperMapping.toDomain(hrpaperdtos);
        for(HRPaper domain:domainlist){
             domain.setHremployeeid(hremployee_id);
        }
        hrpaperService.saveBatch(domainlist);
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','iBizHumanResources-HRPaper-searchDefault-all') and hasPermission(#context,'iBizHumanResources-HRPaper-Get')")
	@ApiOperation(value = "根据员工获取DEFAULT", tags = {"论文信息" } ,notes = "根据员工获取DEFAULT")
    @RequestMapping(method= RequestMethod.GET , value="/hremployees/{hremployee_id}/hrpapers/fetchdefault")
	public ResponseEntity<List<HRPaperDTO>> fetchHRPaperDefaultByHREmployee(@PathVariable("hremployee_id") String hremployee_id,HRPaperSearchContext context) {
        context.setN_hremployeeid_eq(hremployee_id);
        Page<HRPaper> domains = hrpaperService.searchDefault(context) ;
        List<HRPaperDTO> list = hrpaperMapping.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-HRPaper-searchDefault-all') and hasPermission(#context,'iBizHumanResources-HRPaper-Get')")
	@ApiOperation(value = "根据员工查询DEFAULT", tags = {"论文信息" } ,notes = "根据员工查询DEFAULT")
    @RequestMapping(method= RequestMethod.POST , value="/hremployees/{hremployee_id}/hrpapers/searchdefault")
	public ResponseEntity<Page<HRPaperDTO>> searchHRPaperDefaultByHREmployee(@PathVariable("hremployee_id") String hremployee_id, @RequestBody HRPaperSearchContext context) {
        context.setN_hremployeeid_eq(hremployee_id);
        Page<HRPaper> domains = hrpaperService.searchDefault(context) ;
	    return ResponseEntity.status(HttpStatus.OK)
                .body(new PageImpl(hrpaperMapping.toDto(domains.getContent()), context.getPageable(), domains.getTotalElements()));
	}
}

