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.HRDutyEdu;
import cn.ibizlab.humanresource.core.humanresource.service.IHRDutyEduService;
import cn.ibizlab.humanresource.core.humanresource.filter.HRDutyEduSearchContext;
import cn.ibizlab.humanresource.util.annotation.VersionCheck;

@Slf4j
@Api(tags = {"职务教育" })
@RestController("HRApi-hrdutyedu")
@RequestMapping("")
public class HRDutyEduResource {

    @Autowired
    public IHRDutyEduService hrdutyeduService;

    @Autowired
    @Lazy
    public HRDutyEduMapping hrdutyeduMapping;

    @PreAuthorize("hasPermission(this.hrdutyeduMapping.toDomain(#hrdutyedudto),'iBizHumanResources-HRDutyEdu-Create')")
    @ApiOperation(value = "新建职务教育", tags = {"职务教育" },  notes = "新建职务教育")
	@RequestMapping(method = RequestMethod.POST, value = "/hrdutyedus")
    @Transactional
    public ResponseEntity<HRDutyEduDTO> create(@RequestBody HRDutyEduDTO hrdutyedudto) {
        HRDutyEdu domain = hrdutyeduMapping.toDomain(hrdutyedudto);
		hrdutyeduService.create(domain);
        HRDutyEduDTO dto = hrdutyeduMapping.toDto(domain);
		return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @PreAuthorize("hasPermission(this.hrdutyeduMapping.toDomain(#hrdutyedudtos),'iBizHumanResources-HRDutyEdu-Create')")
    @ApiOperation(value = "批量新建职务教育", tags = {"职务教育" },  notes = "批量新建职务教育")
	@RequestMapping(method = RequestMethod.POST, value = "/hrdutyedus/batch")
    public ResponseEntity<Boolean> createBatch(@RequestBody List<HRDutyEduDTO> hrdutyedudtos) {
        hrdutyeduService.createBatch(hrdutyeduMapping.toDomain(hrdutyedudtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @VersionCheck(entity = "hrdutyedu" , versionfield = "updatedate")
    @PreAuthorize("hasPermission(this.hrdutyeduService.get(#hrdutyedu_id),'iBizHumanResources-HRDutyEdu-Update')")
    @ApiOperation(value = "更新职务教育", tags = {"职务教育" },  notes = "更新职务教育")
	@RequestMapping(method = RequestMethod.PUT, value = "/hrdutyedus/{hrdutyedu_id}")
    @Transactional
    public ResponseEntity<HRDutyEduDTO> update(@PathVariable("hrdutyedu_id") String hrdutyedu_id, @RequestBody HRDutyEduDTO hrdutyedudto) {
		HRDutyEdu domain  = hrdutyeduMapping.toDomain(hrdutyedudto);
        domain .setHrdutyeduid(hrdutyedu_id);
		hrdutyeduService.update(domain );
		HRDutyEduDTO dto = hrdutyeduMapping.toDto(domain );
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @PreAuthorize("hasPermission(this.hrdutyeduService.getHrdutyeduByEntities(this.hrdutyeduMapping.toDomain(#hrdutyedudtos)),'iBizHumanResources-HRDutyEdu-Update')")
    @ApiOperation(value = "批量更新职务教育", tags = {"职务教育" },  notes = "批量更新职务教育")
	@RequestMapping(method = RequestMethod.PUT, value = "/hrdutyedus/batch")
    public ResponseEntity<Boolean> updateBatch(@RequestBody List<HRDutyEduDTO> hrdutyedudtos) {
        hrdutyeduService.updateBatch(hrdutyeduMapping.toDomain(hrdutyedudtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @PreAuthorize("hasPermission(this.hrdutyeduService.get(#hrdutyedu_id),'iBizHumanResources-HRDutyEdu-Remove')")
    @ApiOperation(value = "删除职务教育", tags = {"职务教育" },  notes = "删除职务教育")
	@RequestMapping(method = RequestMethod.DELETE, value = "/hrdutyedus/{hrdutyedu_id}")
    @Transactional
    public ResponseEntity<Boolean> remove(@PathVariable("hrdutyedu_id") String hrdutyedu_id) {
         return ResponseEntity.status(HttpStatus.OK).body(hrdutyeduService.remove(hrdutyedu_id));
    }

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

    @PostAuthorize("hasPermission(this.hrdutyeduMapping.toDomain(returnObject.body),'iBizHumanResources-HRDutyEdu-Get')")
    @ApiOperation(value = "获取职务教育", tags = {"职务教育" },  notes = "获取职务教育")
	@RequestMapping(method = RequestMethod.GET, value = "/hrdutyedus/{hrdutyedu_id}")
    public ResponseEntity<HRDutyEduDTO> get(@PathVariable("hrdutyedu_id") String hrdutyedu_id) {
        HRDutyEdu domain = hrdutyeduService.get(hrdutyedu_id);
        HRDutyEduDTO dto = hrdutyeduMapping.toDto(domain);
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @ApiOperation(value = "获取职务教育草稿", tags = {"职务教育" },  notes = "获取职务教育草稿")
	@RequestMapping(method = RequestMethod.GET, value = "/hrdutyedus/getdraft")
    public ResponseEntity<HRDutyEduDTO> getDraft() {
        return ResponseEntity.status(HttpStatus.OK).body(hrdutyeduMapping.toDto(hrdutyeduService.getDraft(new HRDutyEdu())));
    }

    @ApiOperation(value = "检查职务教育", tags = {"职务教育" },  notes = "检查职务教育")
	@RequestMapping(method = RequestMethod.POST, value = "/hrdutyedus/checkkey")
    public ResponseEntity<Boolean> checkKey(@RequestBody HRDutyEduDTO hrdutyedudto) {
        return  ResponseEntity.status(HttpStatus.OK).body(hrdutyeduService.checkKey(hrdutyeduMapping.toDomain(hrdutyedudto)));
    }

    @PreAuthorize("hasPermission(this.hrdutyeduMapping.toDomain(#hrdutyedudto),'iBizHumanResources-HRDutyEdu-Save')")
    @ApiOperation(value = "保存职务教育", tags = {"职务教育" },  notes = "保存职务教育")
	@RequestMapping(method = RequestMethod.POST, value = "/hrdutyedus/save")
    public ResponseEntity<Boolean> save(@RequestBody HRDutyEduDTO hrdutyedudto) {
        return ResponseEntity.status(HttpStatus.OK).body(hrdutyeduService.save(hrdutyeduMapping.toDomain(hrdutyedudto)));
    }

    @PreAuthorize("hasPermission(this.hrdutyeduMapping.toDomain(#hrdutyedudtos),'iBizHumanResources-HRDutyEdu-Save')")
    @ApiOperation(value = "批量保存职务教育", tags = {"职务教育" },  notes = "批量保存职务教育")
	@RequestMapping(method = RequestMethod.POST, value = "/hrdutyedus/savebatch")
    public ResponseEntity<Boolean> saveBatch(@RequestBody List<HRDutyEduDTO> hrdutyedudtos) {
        hrdutyeduService.saveBatch(hrdutyeduMapping.toDomain(hrdutyedudtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','iBizHumanResources-HRDutyEdu-searchDefault-all') and hasPermission(#context,'iBizHumanResources-HRDutyEdu-Get')")
	@ApiOperation(value = "获取DEFAULT", tags = {"职务教育" } ,notes = "获取DEFAULT")
    @RequestMapping(method= RequestMethod.GET , value="/hrdutyedus/fetchdefault")
	public ResponseEntity<List<HRDutyEduDTO>> fetchDefault(HRDutyEduSearchContext context) {
        Page<HRDutyEdu> domains = hrdutyeduService.searchDefault(context) ;
        List<HRDutyEduDTO> list = hrdutyeduMapping.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-HRDutyEdu-searchDefault-all') and hasPermission(#context,'iBizHumanResources-HRDutyEdu-Get')")
	@ApiOperation(value = "查询DEFAULT", tags = {"职务教育" } ,notes = "查询DEFAULT")
    @RequestMapping(method= RequestMethod.POST , value="/hrdutyedus/searchdefault")
	public ResponseEntity<Page<HRDutyEduDTO>> searchDefault(@RequestBody HRDutyEduSearchContext context) {
        Page<HRDutyEdu> domains = hrdutyeduService.searchDefault(context) ;
	    return ResponseEntity.status(HttpStatus.OK)
                .body(new PageImpl(hrdutyeduMapping.toDto(domains.getContent()), context.getPageable(), domains.getTotalElements()));
	}
    @PreAuthorize("hasPermission(this.hrdutyeduMapping.toDomain(#hrdutyedudto),'iBizHumanResources-HRDutyEdu-Create')")
    @ApiOperation(value = "根据职务建立职务教育", tags = {"职务教育" },  notes = "根据职务建立职务教育")
	@RequestMapping(method = RequestMethod.POST, value = "/hrduties/{hrduty_id}/hrdutyedus")
    @Transactional
    public ResponseEntity<HRDutyEduDTO> createByHRDuty(@PathVariable("hrduty_id") String hrduty_id, @RequestBody HRDutyEduDTO hrdutyedudto) {
        HRDutyEdu domain = hrdutyeduMapping.toDomain(hrdutyedudto);
        domain.setHrdutyid(hrduty_id);
		hrdutyeduService.create(domain);
        HRDutyEduDTO dto = hrdutyeduMapping.toDto(domain);
		return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @PreAuthorize("hasPermission(this.hrdutyeduMapping.toDomain(#hrdutyedudtos),'iBizHumanResources-HRDutyEdu-Create')")
    @ApiOperation(value = "根据职务批量建立职务教育", tags = {"职务教育" },  notes = "根据职务批量建立职务教育")
	@RequestMapping(method = RequestMethod.POST, value = "/hrduties/{hrduty_id}/hrdutyedus/batch")
    public ResponseEntity<Boolean> createBatchByHRDuty(@PathVariable("hrduty_id") String hrduty_id, @RequestBody List<HRDutyEduDTO> hrdutyedudtos) {
        List<HRDutyEdu> domainlist=hrdutyeduMapping.toDomain(hrdutyedudtos);
        for(HRDutyEdu domain:domainlist){
            domain.setHrdutyid(hrduty_id);
        }
        hrdutyeduService.createBatch(domainlist);
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @VersionCheck(entity = "hrdutyedu" , versionfield = "updatedate")
    @PreAuthorize("hasPermission(this.hrdutyeduService.get(#hrdutyedu_id),'iBizHumanResources-HRDutyEdu-Update')")
    @ApiOperation(value = "根据职务更新职务教育", tags = {"职务教育" },  notes = "根据职务更新职务教育")
	@RequestMapping(method = RequestMethod.PUT, value = "/hrduties/{hrduty_id}/hrdutyedus/{hrdutyedu_id}")
    @Transactional
    public ResponseEntity<HRDutyEduDTO> updateByHRDuty(@PathVariable("hrduty_id") String hrduty_id, @PathVariable("hrdutyedu_id") String hrdutyedu_id, @RequestBody HRDutyEduDTO hrdutyedudto) {
        HRDutyEdu domain = hrdutyeduMapping.toDomain(hrdutyedudto);
        domain.setHrdutyid(hrduty_id);
        domain.setHrdutyeduid(hrdutyedu_id);
		hrdutyeduService.update(domain);
        HRDutyEduDTO dto = hrdutyeduMapping.toDto(domain);
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @PreAuthorize("hasPermission(this.hrdutyeduService.getHrdutyeduByEntities(this.hrdutyeduMapping.toDomain(#hrdutyedudtos)),'iBizHumanResources-HRDutyEdu-Update')")
    @ApiOperation(value = "根据职务批量更新职务教育", tags = {"职务教育" },  notes = "根据职务批量更新职务教育")
	@RequestMapping(method = RequestMethod.PUT, value = "/hrduties/{hrduty_id}/hrdutyedus/batch")
    public ResponseEntity<Boolean> updateBatchByHRDuty(@PathVariable("hrduty_id") String hrduty_id, @RequestBody List<HRDutyEduDTO> hrdutyedudtos) {
        List<HRDutyEdu> domainlist=hrdutyeduMapping.toDomain(hrdutyedudtos);
        for(HRDutyEdu domain:domainlist){
            domain.setHrdutyid(hrduty_id);
        }
        hrdutyeduService.updateBatch(domainlist);
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @PreAuthorize("hasPermission(this.hrdutyeduService.get(#hrdutyedu_id),'iBizHumanResources-HRDutyEdu-Remove')")
    @ApiOperation(value = "根据职务删除职务教育", tags = {"职务教育" },  notes = "根据职务删除职务教育")
	@RequestMapping(method = RequestMethod.DELETE, value = "/hrduties/{hrduty_id}/hrdutyedus/{hrdutyedu_id}")
    @Transactional
    public ResponseEntity<Boolean> removeByHRDuty(@PathVariable("hrduty_id") String hrduty_id, @PathVariable("hrdutyedu_id") String hrdutyedu_id) {
		return ResponseEntity.status(HttpStatus.OK).body(hrdutyeduService.remove(hrdutyedu_id));
    }

    @PreAuthorize("hasPermission(this.hrdutyeduService.getHrdutyeduByIds(#ids),'iBizHumanResources-HRDutyEdu-Remove')")
    @ApiOperation(value = "根据职务批量删除职务教育", tags = {"职务教育" },  notes = "根据职务批量删除职务教育")
	@RequestMapping(method = RequestMethod.DELETE, value = "/hrduties/{hrduty_id}/hrdutyedus/batch")
    public ResponseEntity<Boolean> removeBatchByHRDuty(@RequestBody List<String> ids) {
        hrdutyeduService.removeBatch(ids);
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @PostAuthorize("hasPermission(this.hrdutyeduMapping.toDomain(returnObject.body),'iBizHumanResources-HRDutyEdu-Get')")
    @ApiOperation(value = "根据职务获取职务教育", tags = {"职务教育" },  notes = "根据职务获取职务教育")
	@RequestMapping(method = RequestMethod.GET, value = "/hrduties/{hrduty_id}/hrdutyedus/{hrdutyedu_id}")
    public ResponseEntity<HRDutyEduDTO> getByHRDuty(@PathVariable("hrduty_id") String hrduty_id, @PathVariable("hrdutyedu_id") String hrdutyedu_id) {
        HRDutyEdu domain = hrdutyeduService.get(hrdutyedu_id);
        HRDutyEduDTO dto = hrdutyeduMapping.toDto(domain);
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @ApiOperation(value = "根据职务获取职务教育草稿", tags = {"职务教育" },  notes = "根据职务获取职务教育草稿")
    @RequestMapping(method = RequestMethod.GET, value = "/hrduties/{hrduty_id}/hrdutyedus/getdraft")
    public ResponseEntity<HRDutyEduDTO> getDraftByHRDuty(@PathVariable("hrduty_id") String hrduty_id) {
        HRDutyEdu domain = new HRDutyEdu();
        domain.setHrdutyid(hrduty_id);
        return ResponseEntity.status(HttpStatus.OK).body(hrdutyeduMapping.toDto(hrdutyeduService.getDraft(domain)));
    }

    @ApiOperation(value = "根据职务检查职务教育", tags = {"职务教育" },  notes = "根据职务检查职务教育")
	@RequestMapping(method = RequestMethod.POST, value = "/hrduties/{hrduty_id}/hrdutyedus/checkkey")
    public ResponseEntity<Boolean> checkKeyByHRDuty(@PathVariable("hrduty_id") String hrduty_id, @RequestBody HRDutyEduDTO hrdutyedudto) {
        return  ResponseEntity.status(HttpStatus.OK).body(hrdutyeduService.checkKey(hrdutyeduMapping.toDomain(hrdutyedudto)));
    }

    @PreAuthorize("hasPermission(this.hrdutyeduMapping.toDomain(#hrdutyedudto),'iBizHumanResources-HRDutyEdu-Save')")
    @ApiOperation(value = "根据职务保存职务教育", tags = {"职务教育" },  notes = "根据职务保存职务教育")
	@RequestMapping(method = RequestMethod.POST, value = "/hrduties/{hrduty_id}/hrdutyedus/save")
    public ResponseEntity<Boolean> saveByHRDuty(@PathVariable("hrduty_id") String hrduty_id, @RequestBody HRDutyEduDTO hrdutyedudto) {
        HRDutyEdu domain = hrdutyeduMapping.toDomain(hrdutyedudto);
        domain.setHrdutyid(hrduty_id);
        return ResponseEntity.status(HttpStatus.OK).body(hrdutyeduService.save(domain));
    }

    @PreAuthorize("hasPermission(this.hrdutyeduMapping.toDomain(#hrdutyedudtos),'iBizHumanResources-HRDutyEdu-Save')")
    @ApiOperation(value = "根据职务批量保存职务教育", tags = {"职务教育" },  notes = "根据职务批量保存职务教育")
	@RequestMapping(method = RequestMethod.POST, value = "/hrduties/{hrduty_id}/hrdutyedus/savebatch")
    public ResponseEntity<Boolean> saveBatchByHRDuty(@PathVariable("hrduty_id") String hrduty_id, @RequestBody List<HRDutyEduDTO> hrdutyedudtos) {
        List<HRDutyEdu> domainlist=hrdutyeduMapping.toDomain(hrdutyedudtos);
        for(HRDutyEdu domain:domainlist){
             domain.setHrdutyid(hrduty_id);
        }
        hrdutyeduService.saveBatch(domainlist);
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','iBizHumanResources-HRDutyEdu-searchDefault-all') and hasPermission(#context,'iBizHumanResources-HRDutyEdu-Get')")
	@ApiOperation(value = "根据职务获取DEFAULT", tags = {"职务教育" } ,notes = "根据职务获取DEFAULT")
    @RequestMapping(method= RequestMethod.GET , value="/hrduties/{hrduty_id}/hrdutyedus/fetchdefault")
	public ResponseEntity<List<HRDutyEduDTO>> fetchHRDutyEduDefaultByHRDuty(@PathVariable("hrduty_id") String hrduty_id,HRDutyEduSearchContext context) {
        context.setN_hrdutyid_eq(hrduty_id);
        Page<HRDutyEdu> domains = hrdutyeduService.searchDefault(context) ;
        List<HRDutyEduDTO> list = hrdutyeduMapping.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-HRDutyEdu-searchDefault-all') and hasPermission(#context,'iBizHumanResources-HRDutyEdu-Get')")
	@ApiOperation(value = "根据职务查询DEFAULT", tags = {"职务教育" } ,notes = "根据职务查询DEFAULT")
    @RequestMapping(method= RequestMethod.POST , value="/hrduties/{hrduty_id}/hrdutyedus/searchdefault")
	public ResponseEntity<Page<HRDutyEduDTO>> searchHRDutyEduDefaultByHRDuty(@PathVariable("hrduty_id") String hrduty_id, @RequestBody HRDutyEduSearchContext context) {
        context.setN_hrdutyid_eq(hrduty_id);
        Page<HRDutyEdu> domains = hrdutyeduService.searchDefault(context) ;
	    return ResponseEntity.status(HttpStatus.OK)
                .body(new PageImpl(hrdutyeduMapping.toDto(domains.getContent()), context.getPageable(), domains.getTotalElements()));
	}
}

