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.HRTrainingRecord;
import cn.ibizlab.humanresource.core.humanresource.service.IHRTrainingRecordService;
import cn.ibizlab.humanresource.core.humanresource.filter.HRTrainingRecordSearchContext;
import cn.ibizlab.humanresource.util.annotation.VersionCheck;

@Slf4j
@Api(tags = {"培训记录" })
@RestController("HRApi-hrtrainingrecord")
@RequestMapping("")
public class HRTrainingRecordResource {

    @Autowired
    public IHRTrainingRecordService hrtrainingrecordService;

    @Autowired
    @Lazy
    public HRTrainingRecordMapping hrtrainingrecordMapping;

    @PreAuthorize("hasPermission(this.hrtrainingrecordMapping.toDomain(#hrtrainingrecorddto),'iBizHumanResources-HRTrainingRecord-Create')")
    @ApiOperation(value = "新建培训记录", tags = {"培训记录" },  notes = "新建培训记录")
	@RequestMapping(method = RequestMethod.POST, value = "/hrtrainingrecords")
    @Transactional
    public ResponseEntity<HRTrainingRecordDTO> create(@RequestBody HRTrainingRecordDTO hrtrainingrecorddto) {
        HRTrainingRecord domain = hrtrainingrecordMapping.toDomain(hrtrainingrecorddto);
		hrtrainingrecordService.create(domain);
        HRTrainingRecordDTO dto = hrtrainingrecordMapping.toDto(domain);
		return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @PreAuthorize("hasPermission(this.hrtrainingrecordMapping.toDomain(#hrtrainingrecorddtos),'iBizHumanResources-HRTrainingRecord-Create')")
    @ApiOperation(value = "批量新建培训记录", tags = {"培训记录" },  notes = "批量新建培训记录")
	@RequestMapping(method = RequestMethod.POST, value = "/hrtrainingrecords/batch")
    public ResponseEntity<Boolean> createBatch(@RequestBody List<HRTrainingRecordDTO> hrtrainingrecorddtos) {
        hrtrainingrecordService.createBatch(hrtrainingrecordMapping.toDomain(hrtrainingrecorddtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @VersionCheck(entity = "hrtrainingrecord" , versionfield = "updatedate")
    @PreAuthorize("hasPermission(this.hrtrainingrecordService.get(#hrtrainingrecord_id),'iBizHumanResources-HRTrainingRecord-Update')")
    @ApiOperation(value = "更新培训记录", tags = {"培训记录" },  notes = "更新培训记录")
	@RequestMapping(method = RequestMethod.PUT, value = "/hrtrainingrecords/{hrtrainingrecord_id}")
    @Transactional
    public ResponseEntity<HRTrainingRecordDTO> update(@PathVariable("hrtrainingrecord_id") String hrtrainingrecord_id, @RequestBody HRTrainingRecordDTO hrtrainingrecorddto) {
		HRTrainingRecord domain  = hrtrainingrecordMapping.toDomain(hrtrainingrecorddto);
        domain .setHrtrainingrecordid(hrtrainingrecord_id);
		hrtrainingrecordService.update(domain );
		HRTrainingRecordDTO dto = hrtrainingrecordMapping.toDto(domain );
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @PreAuthorize("hasPermission(this.hrtrainingrecordService.getHrtrainingrecordByEntities(this.hrtrainingrecordMapping.toDomain(#hrtrainingrecorddtos)),'iBizHumanResources-HRTrainingRecord-Update')")
    @ApiOperation(value = "批量更新培训记录", tags = {"培训记录" },  notes = "批量更新培训记录")
	@RequestMapping(method = RequestMethod.PUT, value = "/hrtrainingrecords/batch")
    public ResponseEntity<Boolean> updateBatch(@RequestBody List<HRTrainingRecordDTO> hrtrainingrecorddtos) {
        hrtrainingrecordService.updateBatch(hrtrainingrecordMapping.toDomain(hrtrainingrecorddtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @PreAuthorize("hasPermission(this.hrtrainingrecordService.get(#hrtrainingrecord_id),'iBizHumanResources-HRTrainingRecord-Remove')")
    @ApiOperation(value = "删除培训记录", tags = {"培训记录" },  notes = "删除培训记录")
	@RequestMapping(method = RequestMethod.DELETE, value = "/hrtrainingrecords/{hrtrainingrecord_id}")
    @Transactional
    public ResponseEntity<Boolean> remove(@PathVariable("hrtrainingrecord_id") String hrtrainingrecord_id) {
         return ResponseEntity.status(HttpStatus.OK).body(hrtrainingrecordService.remove(hrtrainingrecord_id));
    }

    @PreAuthorize("hasPermission(this.hrtrainingrecordService.getHrtrainingrecordByIds(#ids),'iBizHumanResources-HRTrainingRecord-Remove')")
    @ApiOperation(value = "批量删除培训记录", tags = {"培训记录" },  notes = "批量删除培训记录")
	@RequestMapping(method = RequestMethod.DELETE, value = "/hrtrainingrecords/batch")
    public ResponseEntity<Boolean> removeBatch(@RequestBody List<String> ids) {
        hrtrainingrecordService.removeBatch(ids);
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @PostAuthorize("hasPermission(this.hrtrainingrecordMapping.toDomain(returnObject.body),'iBizHumanResources-HRTrainingRecord-Get')")
    @ApiOperation(value = "获取培训记录", tags = {"培训记录" },  notes = "获取培训记录")
	@RequestMapping(method = RequestMethod.GET, value = "/hrtrainingrecords/{hrtrainingrecord_id}")
    public ResponseEntity<HRTrainingRecordDTO> get(@PathVariable("hrtrainingrecord_id") String hrtrainingrecord_id) {
        HRTrainingRecord domain = hrtrainingrecordService.get(hrtrainingrecord_id);
        HRTrainingRecordDTO dto = hrtrainingrecordMapping.toDto(domain);
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @ApiOperation(value = "获取培训记录草稿", tags = {"培训记录" },  notes = "获取培训记录草稿")
	@RequestMapping(method = RequestMethod.GET, value = "/hrtrainingrecords/getdraft")
    public ResponseEntity<HRTrainingRecordDTO> getDraft() {
        return ResponseEntity.status(HttpStatus.OK).body(hrtrainingrecordMapping.toDto(hrtrainingrecordService.getDraft(new HRTrainingRecord())));
    }

    @ApiOperation(value = "检查培训记录", tags = {"培训记录" },  notes = "检查培训记录")
	@RequestMapping(method = RequestMethod.POST, value = "/hrtrainingrecords/checkkey")
    public ResponseEntity<Boolean> checkKey(@RequestBody HRTrainingRecordDTO hrtrainingrecorddto) {
        return  ResponseEntity.status(HttpStatus.OK).body(hrtrainingrecordService.checkKey(hrtrainingrecordMapping.toDomain(hrtrainingrecorddto)));
    }

    @PreAuthorize("hasPermission(this.hrtrainingrecordMapping.toDomain(#hrtrainingrecorddto),'iBizHumanResources-HRTrainingRecord-Save')")
    @ApiOperation(value = "保存培训记录", tags = {"培训记录" },  notes = "保存培训记录")
	@RequestMapping(method = RequestMethod.POST, value = "/hrtrainingrecords/save")
    public ResponseEntity<Boolean> save(@RequestBody HRTrainingRecordDTO hrtrainingrecorddto) {
        return ResponseEntity.status(HttpStatus.OK).body(hrtrainingrecordService.save(hrtrainingrecordMapping.toDomain(hrtrainingrecorddto)));
    }

    @PreAuthorize("hasPermission(this.hrtrainingrecordMapping.toDomain(#hrtrainingrecorddtos),'iBizHumanResources-HRTrainingRecord-Save')")
    @ApiOperation(value = "批量保存培训记录", tags = {"培训记录" },  notes = "批量保存培训记录")
	@RequestMapping(method = RequestMethod.POST, value = "/hrtrainingrecords/savebatch")
    public ResponseEntity<Boolean> saveBatch(@RequestBody List<HRTrainingRecordDTO> hrtrainingrecorddtos) {
        hrtrainingrecordService.saveBatch(hrtrainingrecordMapping.toDomain(hrtrainingrecorddtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','iBizHumanResources-HRTrainingRecord-searchDefault-all') and hasPermission(#context,'iBizHumanResources-HRTrainingRecord-Get')")
	@ApiOperation(value = "获取DEFAULT", tags = {"培训记录" } ,notes = "获取DEFAULT")
    @RequestMapping(method= RequestMethod.GET , value="/hrtrainingrecords/fetchdefault")
	public ResponseEntity<List<HRTrainingRecordDTO>> fetchDefault(HRTrainingRecordSearchContext context) {
        Page<HRTrainingRecord> domains = hrtrainingrecordService.searchDefault(context) ;
        List<HRTrainingRecordDTO> list = hrtrainingrecordMapping.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-HRTrainingRecord-searchDefault-all') and hasPermission(#context,'iBizHumanResources-HRTrainingRecord-Get')")
	@ApiOperation(value = "查询DEFAULT", tags = {"培训记录" } ,notes = "查询DEFAULT")
    @RequestMapping(method= RequestMethod.POST , value="/hrtrainingrecords/searchdefault")
	public ResponseEntity<Page<HRTrainingRecordDTO>> searchDefault(@RequestBody HRTrainingRecordSearchContext context) {
        Page<HRTrainingRecord> domains = hrtrainingrecordService.searchDefault(context) ;
	    return ResponseEntity.status(HttpStatus.OK)
                .body(new PageImpl(hrtrainingrecordMapping.toDto(domains.getContent()), context.getPageable(), domains.getTotalElements()));
	}
    @PreAuthorize("hasPermission(this.hrtrainingrecordMapping.toDomain(#hrtrainingrecorddto),'iBizHumanResources-HRTrainingRecord-Create')")
    @ApiOperation(value = "根据员工建立培训记录", tags = {"培训记录" },  notes = "根据员工建立培训记录")
	@RequestMapping(method = RequestMethod.POST, value = "/hremployees/{hremployee_id}/hrtrainingrecords")
    @Transactional
    public ResponseEntity<HRTrainingRecordDTO> createByHREmployee(@PathVariable("hremployee_id") String hremployee_id, @RequestBody HRTrainingRecordDTO hrtrainingrecorddto) {
        HRTrainingRecord domain = hrtrainingrecordMapping.toDomain(hrtrainingrecorddto);
        domain.setHremployeeid(hremployee_id);
		hrtrainingrecordService.create(domain);
        HRTrainingRecordDTO dto = hrtrainingrecordMapping.toDto(domain);
		return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @PreAuthorize("hasPermission(this.hrtrainingrecordMapping.toDomain(#hrtrainingrecorddtos),'iBizHumanResources-HRTrainingRecord-Create')")
    @ApiOperation(value = "根据员工批量建立培训记录", tags = {"培训记录" },  notes = "根据员工批量建立培训记录")
	@RequestMapping(method = RequestMethod.POST, value = "/hremployees/{hremployee_id}/hrtrainingrecords/batch")
    public ResponseEntity<Boolean> createBatchByHREmployee(@PathVariable("hremployee_id") String hremployee_id, @RequestBody List<HRTrainingRecordDTO> hrtrainingrecorddtos) {
        List<HRTrainingRecord> domainlist=hrtrainingrecordMapping.toDomain(hrtrainingrecorddtos);
        for(HRTrainingRecord domain:domainlist){
            domain.setHremployeeid(hremployee_id);
        }
        hrtrainingrecordService.createBatch(domainlist);
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @VersionCheck(entity = "hrtrainingrecord" , versionfield = "updatedate")
    @PreAuthorize("hasPermission(this.hrtrainingrecordService.get(#hrtrainingrecord_id),'iBizHumanResources-HRTrainingRecord-Update')")
    @ApiOperation(value = "根据员工更新培训记录", tags = {"培训记录" },  notes = "根据员工更新培训记录")
	@RequestMapping(method = RequestMethod.PUT, value = "/hremployees/{hremployee_id}/hrtrainingrecords/{hrtrainingrecord_id}")
    @Transactional
    public ResponseEntity<HRTrainingRecordDTO> updateByHREmployee(@PathVariable("hremployee_id") String hremployee_id, @PathVariable("hrtrainingrecord_id") String hrtrainingrecord_id, @RequestBody HRTrainingRecordDTO hrtrainingrecorddto) {
        HRTrainingRecord domain = hrtrainingrecordMapping.toDomain(hrtrainingrecorddto);
        domain.setHremployeeid(hremployee_id);
        domain.setHrtrainingrecordid(hrtrainingrecord_id);
		hrtrainingrecordService.update(domain);
        HRTrainingRecordDTO dto = hrtrainingrecordMapping.toDto(domain);
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @PreAuthorize("hasPermission(this.hrtrainingrecordService.getHrtrainingrecordByEntities(this.hrtrainingrecordMapping.toDomain(#hrtrainingrecorddtos)),'iBizHumanResources-HRTrainingRecord-Update')")
    @ApiOperation(value = "根据员工批量更新培训记录", tags = {"培训记录" },  notes = "根据员工批量更新培训记录")
	@RequestMapping(method = RequestMethod.PUT, value = "/hremployees/{hremployee_id}/hrtrainingrecords/batch")
    public ResponseEntity<Boolean> updateBatchByHREmployee(@PathVariable("hremployee_id") String hremployee_id, @RequestBody List<HRTrainingRecordDTO> hrtrainingrecorddtos) {
        List<HRTrainingRecord> domainlist=hrtrainingrecordMapping.toDomain(hrtrainingrecorddtos);
        for(HRTrainingRecord domain:domainlist){
            domain.setHremployeeid(hremployee_id);
        }
        hrtrainingrecordService.updateBatch(domainlist);
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @PreAuthorize("hasPermission(this.hrtrainingrecordService.get(#hrtrainingrecord_id),'iBizHumanResources-HRTrainingRecord-Remove')")
    @ApiOperation(value = "根据员工删除培训记录", tags = {"培训记录" },  notes = "根据员工删除培训记录")
	@RequestMapping(method = RequestMethod.DELETE, value = "/hremployees/{hremployee_id}/hrtrainingrecords/{hrtrainingrecord_id}")
    @Transactional
    public ResponseEntity<Boolean> removeByHREmployee(@PathVariable("hremployee_id") String hremployee_id, @PathVariable("hrtrainingrecord_id") String hrtrainingrecord_id) {
		return ResponseEntity.status(HttpStatus.OK).body(hrtrainingrecordService.remove(hrtrainingrecord_id));
    }

    @PreAuthorize("hasPermission(this.hrtrainingrecordService.getHrtrainingrecordByIds(#ids),'iBizHumanResources-HRTrainingRecord-Remove')")
    @ApiOperation(value = "根据员工批量删除培训记录", tags = {"培训记录" },  notes = "根据员工批量删除培训记录")
	@RequestMapping(method = RequestMethod.DELETE, value = "/hremployees/{hremployee_id}/hrtrainingrecords/batch")
    public ResponseEntity<Boolean> removeBatchByHREmployee(@RequestBody List<String> ids) {
        hrtrainingrecordService.removeBatch(ids);
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @PostAuthorize("hasPermission(this.hrtrainingrecordMapping.toDomain(returnObject.body),'iBizHumanResources-HRTrainingRecord-Get')")
    @ApiOperation(value = "根据员工获取培训记录", tags = {"培训记录" },  notes = "根据员工获取培训记录")
	@RequestMapping(method = RequestMethod.GET, value = "/hremployees/{hremployee_id}/hrtrainingrecords/{hrtrainingrecord_id}")
    public ResponseEntity<HRTrainingRecordDTO> getByHREmployee(@PathVariable("hremployee_id") String hremployee_id, @PathVariable("hrtrainingrecord_id") String hrtrainingrecord_id) {
        HRTrainingRecord domain = hrtrainingrecordService.get(hrtrainingrecord_id);
        HRTrainingRecordDTO dto = hrtrainingrecordMapping.toDto(domain);
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @ApiOperation(value = "根据员工获取培训记录草稿", tags = {"培训记录" },  notes = "根据员工获取培训记录草稿")
    @RequestMapping(method = RequestMethod.GET, value = "/hremployees/{hremployee_id}/hrtrainingrecords/getdraft")
    public ResponseEntity<HRTrainingRecordDTO> getDraftByHREmployee(@PathVariable("hremployee_id") String hremployee_id) {
        HRTrainingRecord domain = new HRTrainingRecord();
        domain.setHremployeeid(hremployee_id);
        return ResponseEntity.status(HttpStatus.OK).body(hrtrainingrecordMapping.toDto(hrtrainingrecordService.getDraft(domain)));
    }

    @ApiOperation(value = "根据员工检查培训记录", tags = {"培训记录" },  notes = "根据员工检查培训记录")
	@RequestMapping(method = RequestMethod.POST, value = "/hremployees/{hremployee_id}/hrtrainingrecords/checkkey")
    public ResponseEntity<Boolean> checkKeyByHREmployee(@PathVariable("hremployee_id") String hremployee_id, @RequestBody HRTrainingRecordDTO hrtrainingrecorddto) {
        return  ResponseEntity.status(HttpStatus.OK).body(hrtrainingrecordService.checkKey(hrtrainingrecordMapping.toDomain(hrtrainingrecorddto)));
    }

    @PreAuthorize("hasPermission(this.hrtrainingrecordMapping.toDomain(#hrtrainingrecorddto),'iBizHumanResources-HRTrainingRecord-Save')")
    @ApiOperation(value = "根据员工保存培训记录", tags = {"培训记录" },  notes = "根据员工保存培训记录")
	@RequestMapping(method = RequestMethod.POST, value = "/hremployees/{hremployee_id}/hrtrainingrecords/save")
    public ResponseEntity<Boolean> saveByHREmployee(@PathVariable("hremployee_id") String hremployee_id, @RequestBody HRTrainingRecordDTO hrtrainingrecorddto) {
        HRTrainingRecord domain = hrtrainingrecordMapping.toDomain(hrtrainingrecorddto);
        domain.setHremployeeid(hremployee_id);
        return ResponseEntity.status(HttpStatus.OK).body(hrtrainingrecordService.save(domain));
    }

    @PreAuthorize("hasPermission(this.hrtrainingrecordMapping.toDomain(#hrtrainingrecorddtos),'iBizHumanResources-HRTrainingRecord-Save')")
    @ApiOperation(value = "根据员工批量保存培训记录", tags = {"培训记录" },  notes = "根据员工批量保存培训记录")
	@RequestMapping(method = RequestMethod.POST, value = "/hremployees/{hremployee_id}/hrtrainingrecords/savebatch")
    public ResponseEntity<Boolean> saveBatchByHREmployee(@PathVariable("hremployee_id") String hremployee_id, @RequestBody List<HRTrainingRecordDTO> hrtrainingrecorddtos) {
        List<HRTrainingRecord> domainlist=hrtrainingrecordMapping.toDomain(hrtrainingrecorddtos);
        for(HRTrainingRecord domain:domainlist){
             domain.setHremployeeid(hremployee_id);
        }
        hrtrainingrecordService.saveBatch(domainlist);
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','iBizHumanResources-HRTrainingRecord-searchDefault-all') and hasPermission(#context,'iBizHumanResources-HRTrainingRecord-Get')")
	@ApiOperation(value = "根据员工获取DEFAULT", tags = {"培训记录" } ,notes = "根据员工获取DEFAULT")
    @RequestMapping(method= RequestMethod.GET , value="/hremployees/{hremployee_id}/hrtrainingrecords/fetchdefault")
	public ResponseEntity<List<HRTrainingRecordDTO>> fetchHRTrainingRecordDefaultByHREmployee(@PathVariable("hremployee_id") String hremployee_id,HRTrainingRecordSearchContext context) {
        context.setN_hremployeeid_eq(hremployee_id);
        Page<HRTrainingRecord> domains = hrtrainingrecordService.searchDefault(context) ;
        List<HRTrainingRecordDTO> list = hrtrainingrecordMapping.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-HRTrainingRecord-searchDefault-all') and hasPermission(#context,'iBizHumanResources-HRTrainingRecord-Get')")
	@ApiOperation(value = "根据员工查询DEFAULT", tags = {"培训记录" } ,notes = "根据员工查询DEFAULT")
    @RequestMapping(method= RequestMethod.POST , value="/hremployees/{hremployee_id}/hrtrainingrecords/searchdefault")
	public ResponseEntity<Page<HRTrainingRecordDTO>> searchHRTrainingRecordDefaultByHREmployee(@PathVariable("hremployee_id") String hremployee_id, @RequestBody HRTrainingRecordSearchContext context) {
        context.setN_hremployeeid_eq(hremployee_id);
        Page<HRTrainingRecord> domains = hrtrainingrecordService.searchDefault(context) ;
	    return ResponseEntity.status(HttpStatus.OK)
                .body(new PageImpl(hrtrainingrecordMapping.toDto(domains.getContent()), context.getPageable(), domains.getTotalElements()));
	}
}

