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.HRPatent;
import cn.ibizlab.humanresource.core.humanresource.service.IHRPatentService;
import cn.ibizlab.humanresource.core.humanresource.filter.HRPatentSearchContext;
import cn.ibizlab.humanresource.util.annotation.VersionCheck;

@Slf4j
@Api(tags = {"专利信息" })
@RestController("HRApi-hrpatent")
@RequestMapping("")
public class HRPatentResource {

    @Autowired
    public IHRPatentService hrpatentService;

    @Autowired
    @Lazy
    public HRPatentMapping hrpatentMapping;

    @PreAuthorize("hasPermission(this.hrpatentMapping.toDomain(#hrpatentdto),'iBizHumanResources-HRPatent-Create')")
    @ApiOperation(value = "新建专利信息", tags = {"专利信息" },  notes = "新建专利信息")
	@RequestMapping(method = RequestMethod.POST, value = "/hrpatents")
    @Transactional
    public ResponseEntity<HRPatentDTO> create(@RequestBody HRPatentDTO hrpatentdto) {
        HRPatent domain = hrpatentMapping.toDomain(hrpatentdto);
		hrpatentService.create(domain);
        HRPatentDTO dto = hrpatentMapping.toDto(domain);
		return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @PreAuthorize("hasPermission(this.hrpatentMapping.toDomain(#hrpatentdtos),'iBizHumanResources-HRPatent-Create')")
    @ApiOperation(value = "批量新建专利信息", tags = {"专利信息" },  notes = "批量新建专利信息")
	@RequestMapping(method = RequestMethod.POST, value = "/hrpatents/batch")
    public ResponseEntity<Boolean> createBatch(@RequestBody List<HRPatentDTO> hrpatentdtos) {
        hrpatentService.createBatch(hrpatentMapping.toDomain(hrpatentdtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @VersionCheck(entity = "hrpatent" , versionfield = "updatedate")
    @PreAuthorize("hasPermission(this.hrpatentService.get(#hrpatent_id),'iBizHumanResources-HRPatent-Update')")
    @ApiOperation(value = "更新专利信息", tags = {"专利信息" },  notes = "更新专利信息")
	@RequestMapping(method = RequestMethod.PUT, value = "/hrpatents/{hrpatent_id}")
    @Transactional
    public ResponseEntity<HRPatentDTO> update(@PathVariable("hrpatent_id") String hrpatent_id, @RequestBody HRPatentDTO hrpatentdto) {
		HRPatent domain  = hrpatentMapping.toDomain(hrpatentdto);
        domain .setHrpatentid(hrpatent_id);
		hrpatentService.update(domain );
		HRPatentDTO dto = hrpatentMapping.toDto(domain );
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @PreAuthorize("hasPermission(this.hrpatentService.getHrpatentByEntities(this.hrpatentMapping.toDomain(#hrpatentdtos)),'iBizHumanResources-HRPatent-Update')")
    @ApiOperation(value = "批量更新专利信息", tags = {"专利信息" },  notes = "批量更新专利信息")
	@RequestMapping(method = RequestMethod.PUT, value = "/hrpatents/batch")
    public ResponseEntity<Boolean> updateBatch(@RequestBody List<HRPatentDTO> hrpatentdtos) {
        hrpatentService.updateBatch(hrpatentMapping.toDomain(hrpatentdtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @PreAuthorize("hasPermission(this.hrpatentService.get(#hrpatent_id),'iBizHumanResources-HRPatent-Remove')")
    @ApiOperation(value = "删除专利信息", tags = {"专利信息" },  notes = "删除专利信息")
	@RequestMapping(method = RequestMethod.DELETE, value = "/hrpatents/{hrpatent_id}")
    @Transactional
    public ResponseEntity<Boolean> remove(@PathVariable("hrpatent_id") String hrpatent_id) {
         return ResponseEntity.status(HttpStatus.OK).body(hrpatentService.remove(hrpatent_id));
    }

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

    @PostAuthorize("hasPermission(this.hrpatentMapping.toDomain(returnObject.body),'iBizHumanResources-HRPatent-Get')")
    @ApiOperation(value = "获取专利信息", tags = {"专利信息" },  notes = "获取专利信息")
	@RequestMapping(method = RequestMethod.GET, value = "/hrpatents/{hrpatent_id}")
    public ResponseEntity<HRPatentDTO> get(@PathVariable("hrpatent_id") String hrpatent_id) {
        HRPatent domain = hrpatentService.get(hrpatent_id);
        HRPatentDTO dto = hrpatentMapping.toDto(domain);
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @ApiOperation(value = "获取专利信息草稿", tags = {"专利信息" },  notes = "获取专利信息草稿")
	@RequestMapping(method = RequestMethod.GET, value = "/hrpatents/getdraft")
    public ResponseEntity<HRPatentDTO> getDraft() {
        return ResponseEntity.status(HttpStatus.OK).body(hrpatentMapping.toDto(hrpatentService.getDraft(new HRPatent())));
    }

    @ApiOperation(value = "检查专利信息", tags = {"专利信息" },  notes = "检查专利信息")
	@RequestMapping(method = RequestMethod.POST, value = "/hrpatents/checkkey")
    public ResponseEntity<Boolean> checkKey(@RequestBody HRPatentDTO hrpatentdto) {
        return  ResponseEntity.status(HttpStatus.OK).body(hrpatentService.checkKey(hrpatentMapping.toDomain(hrpatentdto)));
    }

    @PreAuthorize("hasPermission(this.hrpatentMapping.toDomain(#hrpatentdto),'iBizHumanResources-HRPatent-Save')")
    @ApiOperation(value = "保存专利信息", tags = {"专利信息" },  notes = "保存专利信息")
	@RequestMapping(method = RequestMethod.POST, value = "/hrpatents/save")
    public ResponseEntity<Boolean> save(@RequestBody HRPatentDTO hrpatentdto) {
        return ResponseEntity.status(HttpStatus.OK).body(hrpatentService.save(hrpatentMapping.toDomain(hrpatentdto)));
    }

    @PreAuthorize("hasPermission(this.hrpatentMapping.toDomain(#hrpatentdtos),'iBizHumanResources-HRPatent-Save')")
    @ApiOperation(value = "批量保存专利信息", tags = {"专利信息" },  notes = "批量保存专利信息")
	@RequestMapping(method = RequestMethod.POST, value = "/hrpatents/savebatch")
    public ResponseEntity<Boolean> saveBatch(@RequestBody List<HRPatentDTO> hrpatentdtos) {
        hrpatentService.saveBatch(hrpatentMapping.toDomain(hrpatentdtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

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

    @PreAuthorize("hasPermission(this.hrpatentMapping.toDomain(#hrpatentdtos),'iBizHumanResources-HRPatent-Create')")
    @ApiOperation(value = "根据员工批量建立专利信息", tags = {"专利信息" },  notes = "根据员工批量建立专利信息")
	@RequestMapping(method = RequestMethod.POST, value = "/hremployees/{hremployee_id}/hrpatents/batch")
    public ResponseEntity<Boolean> createBatchByHREmployee(@PathVariable("hremployee_id") String hremployee_id, @RequestBody List<HRPatentDTO> hrpatentdtos) {
        List<HRPatent> domainlist=hrpatentMapping.toDomain(hrpatentdtos);
        for(HRPatent domain:domainlist){
            domain.setHremployeeid(hremployee_id);
        }
        hrpatentService.createBatch(domainlist);
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @VersionCheck(entity = "hrpatent" , versionfield = "updatedate")
    @PreAuthorize("hasPermission(this.hrpatentService.get(#hrpatent_id),'iBizHumanResources-HRPatent-Update')")
    @ApiOperation(value = "根据员工更新专利信息", tags = {"专利信息" },  notes = "根据员工更新专利信息")
	@RequestMapping(method = RequestMethod.PUT, value = "/hremployees/{hremployee_id}/hrpatents/{hrpatent_id}")
    @Transactional
    public ResponseEntity<HRPatentDTO> updateByHREmployee(@PathVariable("hremployee_id") String hremployee_id, @PathVariable("hrpatent_id") String hrpatent_id, @RequestBody HRPatentDTO hrpatentdto) {
        HRPatent domain = hrpatentMapping.toDomain(hrpatentdto);
        domain.setHremployeeid(hremployee_id);
        domain.setHrpatentid(hrpatent_id);
		hrpatentService.update(domain);
        HRPatentDTO dto = hrpatentMapping.toDto(domain);
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @PreAuthorize("hasPermission(this.hrpatentService.getHrpatentByEntities(this.hrpatentMapping.toDomain(#hrpatentdtos)),'iBizHumanResources-HRPatent-Update')")
    @ApiOperation(value = "根据员工批量更新专利信息", tags = {"专利信息" },  notes = "根据员工批量更新专利信息")
	@RequestMapping(method = RequestMethod.PUT, value = "/hremployees/{hremployee_id}/hrpatents/batch")
    public ResponseEntity<Boolean> updateBatchByHREmployee(@PathVariable("hremployee_id") String hremployee_id, @RequestBody List<HRPatentDTO> hrpatentdtos) {
        List<HRPatent> domainlist=hrpatentMapping.toDomain(hrpatentdtos);
        for(HRPatent domain:domainlist){
            domain.setHremployeeid(hremployee_id);
        }
        hrpatentService.updateBatch(domainlist);
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

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

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

    @PostAuthorize("hasPermission(this.hrpatentMapping.toDomain(returnObject.body),'iBizHumanResources-HRPatent-Get')")
    @ApiOperation(value = "根据员工获取专利信息", tags = {"专利信息" },  notes = "根据员工获取专利信息")
	@RequestMapping(method = RequestMethod.GET, value = "/hremployees/{hremployee_id}/hrpatents/{hrpatent_id}")
    public ResponseEntity<HRPatentDTO> getByHREmployee(@PathVariable("hremployee_id") String hremployee_id, @PathVariable("hrpatent_id") String hrpatent_id) {
        HRPatent domain = hrpatentService.get(hrpatent_id);
        HRPatentDTO dto = hrpatentMapping.toDto(domain);
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @ApiOperation(value = "根据员工获取专利信息草稿", tags = {"专利信息" },  notes = "根据员工获取专利信息草稿")
    @RequestMapping(method = RequestMethod.GET, value = "/hremployees/{hremployee_id}/hrpatents/getdraft")
    public ResponseEntity<HRPatentDTO> getDraftByHREmployee(@PathVariable("hremployee_id") String hremployee_id) {
        HRPatent domain = new HRPatent();
        domain.setHremployeeid(hremployee_id);
        return ResponseEntity.status(HttpStatus.OK).body(hrpatentMapping.toDto(hrpatentService.getDraft(domain)));
    }

    @ApiOperation(value = "根据员工检查专利信息", tags = {"专利信息" },  notes = "根据员工检查专利信息")
	@RequestMapping(method = RequestMethod.POST, value = "/hremployees/{hremployee_id}/hrpatents/checkkey")
    public ResponseEntity<Boolean> checkKeyByHREmployee(@PathVariable("hremployee_id") String hremployee_id, @RequestBody HRPatentDTO hrpatentdto) {
        return  ResponseEntity.status(HttpStatus.OK).body(hrpatentService.checkKey(hrpatentMapping.toDomain(hrpatentdto)));
    }

    @PreAuthorize("hasPermission(this.hrpatentMapping.toDomain(#hrpatentdto),'iBizHumanResources-HRPatent-Save')")
    @ApiOperation(value = "根据员工保存专利信息", tags = {"专利信息" },  notes = "根据员工保存专利信息")
	@RequestMapping(method = RequestMethod.POST, value = "/hremployees/{hremployee_id}/hrpatents/save")
    public ResponseEntity<Boolean> saveByHREmployee(@PathVariable("hremployee_id") String hremployee_id, @RequestBody HRPatentDTO hrpatentdto) {
        HRPatent domain = hrpatentMapping.toDomain(hrpatentdto);
        domain.setHremployeeid(hremployee_id);
        return ResponseEntity.status(HttpStatus.OK).body(hrpatentService.save(domain));
    }

    @PreAuthorize("hasPermission(this.hrpatentMapping.toDomain(#hrpatentdtos),'iBizHumanResources-HRPatent-Save')")
    @ApiOperation(value = "根据员工批量保存专利信息", tags = {"专利信息" },  notes = "根据员工批量保存专利信息")
	@RequestMapping(method = RequestMethod.POST, value = "/hremployees/{hremployee_id}/hrpatents/savebatch")
    public ResponseEntity<Boolean> saveBatchByHREmployee(@PathVariable("hremployee_id") String hremployee_id, @RequestBody List<HRPatentDTO> hrpatentdtos) {
        List<HRPatent> domainlist=hrpatentMapping.toDomain(hrpatentdtos);
        for(HRPatent domain:domainlist){
             domain.setHremployeeid(hremployee_id);
        }
        hrpatentService.saveBatch(domainlist);
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','iBizHumanResources-HRPatent-searchDefault-all') and hasPermission(#context,'iBizHumanResources-HRPatent-Get')")
	@ApiOperation(value = "根据员工获取DEFAULT", tags = {"专利信息" } ,notes = "根据员工获取DEFAULT")
    @RequestMapping(method= RequestMethod.GET , value="/hremployees/{hremployee_id}/hrpatents/fetchdefault")
	public ResponseEntity<List<HRPatentDTO>> fetchHRPatentDefaultByHREmployee(@PathVariable("hremployee_id") String hremployee_id,HRPatentSearchContext context) {
        context.setN_hremployeeid_eq(hremployee_id);
        Page<HRPatent> domains = hrpatentService.searchDefault(context) ;
        List<HRPatentDTO> list = hrpatentMapping.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-HRPatent-searchDefault-all') and hasPermission(#context,'iBizHumanResources-HRPatent-Get')")
	@ApiOperation(value = "根据员工查询DEFAULT", tags = {"专利信息" } ,notes = "根据员工查询DEFAULT")
    @RequestMapping(method= RequestMethod.POST , value="/hremployees/{hremployee_id}/hrpatents/searchdefault")
	public ResponseEntity<Page<HRPatentDTO>> searchHRPatentDefaultByHREmployee(@PathVariable("hremployee_id") String hremployee_id, @RequestBody HRPatentSearchContext context) {
        context.setN_hremployeeid_eq(hremployee_id);
        Page<HRPatent> domains = hrpatentService.searchDefault(context) ;
	    return ResponseEntity.status(HttpStatus.OK)
                .body(new PageImpl(hrpatentMapping.toDto(domains.getContent()), context.getPageable(), domains.getTotalElements()));
	}
}

