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.HRDutySkill;
import cn.ibizlab.humanresource.core.humanresource.service.IHRDutySkillService;
import cn.ibizlab.humanresource.core.humanresource.filter.HRDutySkillSearchContext;
import cn.ibizlab.humanresource.util.annotation.VersionCheck;

@Slf4j
@Api(tags = {"职务技能" })
@RestController("HRApi-hrdutyskill")
@RequestMapping("")
public class HRDutySkillResource {

    @Autowired
    public IHRDutySkillService hrdutyskillService;

    @Autowired
    @Lazy
    public HRDutySkillMapping hrdutyskillMapping;

    @PreAuthorize("hasPermission(this.hrdutyskillMapping.toDomain(#hrdutyskilldto),'iBizHumanResources-HRDutySkill-Create')")
    @ApiOperation(value = "新建职务技能", tags = {"职务技能" },  notes = "新建职务技能")
	@RequestMapping(method = RequestMethod.POST, value = "/hrdutyskills")
    @Transactional
    public ResponseEntity<HRDutySkillDTO> create(@RequestBody HRDutySkillDTO hrdutyskilldto) {
        HRDutySkill domain = hrdutyskillMapping.toDomain(hrdutyskilldto);
		hrdutyskillService.create(domain);
        HRDutySkillDTO dto = hrdutyskillMapping.toDto(domain);
		return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @PreAuthorize("hasPermission(this.hrdutyskillMapping.toDomain(#hrdutyskilldtos),'iBizHumanResources-HRDutySkill-Create')")
    @ApiOperation(value = "批量新建职务技能", tags = {"职务技能" },  notes = "批量新建职务技能")
	@RequestMapping(method = RequestMethod.POST, value = "/hrdutyskills/batch")
    public ResponseEntity<Boolean> createBatch(@RequestBody List<HRDutySkillDTO> hrdutyskilldtos) {
        hrdutyskillService.createBatch(hrdutyskillMapping.toDomain(hrdutyskilldtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @VersionCheck(entity = "hrdutyskill" , versionfield = "updatedate")
    @PreAuthorize("hasPermission(this.hrdutyskillService.get(#hrdutyskill_id),'iBizHumanResources-HRDutySkill-Update')")
    @ApiOperation(value = "更新职务技能", tags = {"职务技能" },  notes = "更新职务技能")
	@RequestMapping(method = RequestMethod.PUT, value = "/hrdutyskills/{hrdutyskill_id}")
    @Transactional
    public ResponseEntity<HRDutySkillDTO> update(@PathVariable("hrdutyskill_id") String hrdutyskill_id, @RequestBody HRDutySkillDTO hrdutyskilldto) {
		HRDutySkill domain  = hrdutyskillMapping.toDomain(hrdutyskilldto);
        domain .setHrdutyskillid(hrdutyskill_id);
		hrdutyskillService.update(domain );
		HRDutySkillDTO dto = hrdutyskillMapping.toDto(domain );
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @PreAuthorize("hasPermission(this.hrdutyskillService.getHrdutyskillByEntities(this.hrdutyskillMapping.toDomain(#hrdutyskilldtos)),'iBizHumanResources-HRDutySkill-Update')")
    @ApiOperation(value = "批量更新职务技能", tags = {"职务技能" },  notes = "批量更新职务技能")
	@RequestMapping(method = RequestMethod.PUT, value = "/hrdutyskills/batch")
    public ResponseEntity<Boolean> updateBatch(@RequestBody List<HRDutySkillDTO> hrdutyskilldtos) {
        hrdutyskillService.updateBatch(hrdutyskillMapping.toDomain(hrdutyskilldtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @PreAuthorize("hasPermission(this.hrdutyskillService.get(#hrdutyskill_id),'iBizHumanResources-HRDutySkill-Remove')")
    @ApiOperation(value = "删除职务技能", tags = {"职务技能" },  notes = "删除职务技能")
	@RequestMapping(method = RequestMethod.DELETE, value = "/hrdutyskills/{hrdutyskill_id}")
    @Transactional
    public ResponseEntity<Boolean> remove(@PathVariable("hrdutyskill_id") String hrdutyskill_id) {
         return ResponseEntity.status(HttpStatus.OK).body(hrdutyskillService.remove(hrdutyskill_id));
    }

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

    @PostAuthorize("hasPermission(this.hrdutyskillMapping.toDomain(returnObject.body),'iBizHumanResources-HRDutySkill-Get')")
    @ApiOperation(value = "获取职务技能", tags = {"职务技能" },  notes = "获取职务技能")
	@RequestMapping(method = RequestMethod.GET, value = "/hrdutyskills/{hrdutyskill_id}")
    public ResponseEntity<HRDutySkillDTO> get(@PathVariable("hrdutyskill_id") String hrdutyskill_id) {
        HRDutySkill domain = hrdutyskillService.get(hrdutyskill_id);
        HRDutySkillDTO dto = hrdutyskillMapping.toDto(domain);
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @ApiOperation(value = "获取职务技能草稿", tags = {"职务技能" },  notes = "获取职务技能草稿")
	@RequestMapping(method = RequestMethod.GET, value = "/hrdutyskills/getdraft")
    public ResponseEntity<HRDutySkillDTO> getDraft() {
        return ResponseEntity.status(HttpStatus.OK).body(hrdutyskillMapping.toDto(hrdutyskillService.getDraft(new HRDutySkill())));
    }

    @ApiOperation(value = "检查职务技能", tags = {"职务技能" },  notes = "检查职务技能")
	@RequestMapping(method = RequestMethod.POST, value = "/hrdutyskills/checkkey")
    public ResponseEntity<Boolean> checkKey(@RequestBody HRDutySkillDTO hrdutyskilldto) {
        return  ResponseEntity.status(HttpStatus.OK).body(hrdutyskillService.checkKey(hrdutyskillMapping.toDomain(hrdutyskilldto)));
    }

    @PreAuthorize("hasPermission(this.hrdutyskillMapping.toDomain(#hrdutyskilldto),'iBizHumanResources-HRDutySkill-Save')")
    @ApiOperation(value = "保存职务技能", tags = {"职务技能" },  notes = "保存职务技能")
	@RequestMapping(method = RequestMethod.POST, value = "/hrdutyskills/save")
    public ResponseEntity<Boolean> save(@RequestBody HRDutySkillDTO hrdutyskilldto) {
        return ResponseEntity.status(HttpStatus.OK).body(hrdutyskillService.save(hrdutyskillMapping.toDomain(hrdutyskilldto)));
    }

    @PreAuthorize("hasPermission(this.hrdutyskillMapping.toDomain(#hrdutyskilldtos),'iBizHumanResources-HRDutySkill-Save')")
    @ApiOperation(value = "批量保存职务技能", tags = {"职务技能" },  notes = "批量保存职务技能")
	@RequestMapping(method = RequestMethod.POST, value = "/hrdutyskills/savebatch")
    public ResponseEntity<Boolean> saveBatch(@RequestBody List<HRDutySkillDTO> hrdutyskilldtos) {
        hrdutyskillService.saveBatch(hrdutyskillMapping.toDomain(hrdutyskilldtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

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

    @PreAuthorize("hasPermission(this.hrdutyskillMapping.toDomain(#hrdutyskilldtos),'iBizHumanResources-HRDutySkill-Create')")
    @ApiOperation(value = "根据职务批量建立职务技能", tags = {"职务技能" },  notes = "根据职务批量建立职务技能")
	@RequestMapping(method = RequestMethod.POST, value = "/hrduties/{hrduty_id}/hrdutyskills/batch")
    public ResponseEntity<Boolean> createBatchByHRDuty(@PathVariable("hrduty_id") String hrduty_id, @RequestBody List<HRDutySkillDTO> hrdutyskilldtos) {
        List<HRDutySkill> domainlist=hrdutyskillMapping.toDomain(hrdutyskilldtos);
        for(HRDutySkill domain:domainlist){
            domain.setHrdutyid(hrduty_id);
        }
        hrdutyskillService.createBatch(domainlist);
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @VersionCheck(entity = "hrdutyskill" , versionfield = "updatedate")
    @PreAuthorize("hasPermission(this.hrdutyskillService.get(#hrdutyskill_id),'iBizHumanResources-HRDutySkill-Update')")
    @ApiOperation(value = "根据职务更新职务技能", tags = {"职务技能" },  notes = "根据职务更新职务技能")
	@RequestMapping(method = RequestMethod.PUT, value = "/hrduties/{hrduty_id}/hrdutyskills/{hrdutyskill_id}")
    @Transactional
    public ResponseEntity<HRDutySkillDTO> updateByHRDuty(@PathVariable("hrduty_id") String hrduty_id, @PathVariable("hrdutyskill_id") String hrdutyskill_id, @RequestBody HRDutySkillDTO hrdutyskilldto) {
        HRDutySkill domain = hrdutyskillMapping.toDomain(hrdutyskilldto);
        domain.setHrdutyid(hrduty_id);
        domain.setHrdutyskillid(hrdutyskill_id);
		hrdutyskillService.update(domain);
        HRDutySkillDTO dto = hrdutyskillMapping.toDto(domain);
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @PreAuthorize("hasPermission(this.hrdutyskillService.getHrdutyskillByEntities(this.hrdutyskillMapping.toDomain(#hrdutyskilldtos)),'iBizHumanResources-HRDutySkill-Update')")
    @ApiOperation(value = "根据职务批量更新职务技能", tags = {"职务技能" },  notes = "根据职务批量更新职务技能")
	@RequestMapping(method = RequestMethod.PUT, value = "/hrduties/{hrduty_id}/hrdutyskills/batch")
    public ResponseEntity<Boolean> updateBatchByHRDuty(@PathVariable("hrduty_id") String hrduty_id, @RequestBody List<HRDutySkillDTO> hrdutyskilldtos) {
        List<HRDutySkill> domainlist=hrdutyskillMapping.toDomain(hrdutyskilldtos);
        for(HRDutySkill domain:domainlist){
            domain.setHrdutyid(hrduty_id);
        }
        hrdutyskillService.updateBatch(domainlist);
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

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

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

    @PostAuthorize("hasPermission(this.hrdutyskillMapping.toDomain(returnObject.body),'iBizHumanResources-HRDutySkill-Get')")
    @ApiOperation(value = "根据职务获取职务技能", tags = {"职务技能" },  notes = "根据职务获取职务技能")
	@RequestMapping(method = RequestMethod.GET, value = "/hrduties/{hrduty_id}/hrdutyskills/{hrdutyskill_id}")
    public ResponseEntity<HRDutySkillDTO> getByHRDuty(@PathVariable("hrduty_id") String hrduty_id, @PathVariable("hrdutyskill_id") String hrdutyskill_id) {
        HRDutySkill domain = hrdutyskillService.get(hrdutyskill_id);
        HRDutySkillDTO dto = hrdutyskillMapping.toDto(domain);
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @ApiOperation(value = "根据职务获取职务技能草稿", tags = {"职务技能" },  notes = "根据职务获取职务技能草稿")
    @RequestMapping(method = RequestMethod.GET, value = "/hrduties/{hrduty_id}/hrdutyskills/getdraft")
    public ResponseEntity<HRDutySkillDTO> getDraftByHRDuty(@PathVariable("hrduty_id") String hrduty_id) {
        HRDutySkill domain = new HRDutySkill();
        domain.setHrdutyid(hrduty_id);
        return ResponseEntity.status(HttpStatus.OK).body(hrdutyskillMapping.toDto(hrdutyskillService.getDraft(domain)));
    }

    @ApiOperation(value = "根据职务检查职务技能", tags = {"职务技能" },  notes = "根据职务检查职务技能")
	@RequestMapping(method = RequestMethod.POST, value = "/hrduties/{hrduty_id}/hrdutyskills/checkkey")
    public ResponseEntity<Boolean> checkKeyByHRDuty(@PathVariable("hrduty_id") String hrduty_id, @RequestBody HRDutySkillDTO hrdutyskilldto) {
        return  ResponseEntity.status(HttpStatus.OK).body(hrdutyskillService.checkKey(hrdutyskillMapping.toDomain(hrdutyskilldto)));
    }

    @PreAuthorize("hasPermission(this.hrdutyskillMapping.toDomain(#hrdutyskilldto),'iBizHumanResources-HRDutySkill-Save')")
    @ApiOperation(value = "根据职务保存职务技能", tags = {"职务技能" },  notes = "根据职务保存职务技能")
	@RequestMapping(method = RequestMethod.POST, value = "/hrduties/{hrduty_id}/hrdutyskills/save")
    public ResponseEntity<Boolean> saveByHRDuty(@PathVariable("hrduty_id") String hrduty_id, @RequestBody HRDutySkillDTO hrdutyskilldto) {
        HRDutySkill domain = hrdutyskillMapping.toDomain(hrdutyskilldto);
        domain.setHrdutyid(hrduty_id);
        return ResponseEntity.status(HttpStatus.OK).body(hrdutyskillService.save(domain));
    }

    @PreAuthorize("hasPermission(this.hrdutyskillMapping.toDomain(#hrdutyskilldtos),'iBizHumanResources-HRDutySkill-Save')")
    @ApiOperation(value = "根据职务批量保存职务技能", tags = {"职务技能" },  notes = "根据职务批量保存职务技能")
	@RequestMapping(method = RequestMethod.POST, value = "/hrduties/{hrduty_id}/hrdutyskills/savebatch")
    public ResponseEntity<Boolean> saveBatchByHRDuty(@PathVariable("hrduty_id") String hrduty_id, @RequestBody List<HRDutySkillDTO> hrdutyskilldtos) {
        List<HRDutySkill> domainlist=hrdutyskillMapping.toDomain(hrdutyskilldtos);
        for(HRDutySkill domain:domainlist){
             domain.setHrdutyid(hrduty_id);
        }
        hrdutyskillService.saveBatch(domainlist);
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','iBizHumanResources-HRDutySkill-searchDefault-all') and hasPermission(#context,'iBizHumanResources-HRDutySkill-Get')")
	@ApiOperation(value = "根据职务获取DEFAULT", tags = {"职务技能" } ,notes = "根据职务获取DEFAULT")
    @RequestMapping(method= RequestMethod.GET , value="/hrduties/{hrduty_id}/hrdutyskills/fetchdefault")
	public ResponseEntity<List<HRDutySkillDTO>> fetchHRDutySkillDefaultByHRDuty(@PathVariable("hrduty_id") String hrduty_id,HRDutySkillSearchContext context) {
        context.setN_hrdutyid_eq(hrduty_id);
        Page<HRDutySkill> domains = hrdutyskillService.searchDefault(context) ;
        List<HRDutySkillDTO> list = hrdutyskillMapping.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-HRDutySkill-searchDefault-all') and hasPermission(#context,'iBizHumanResources-HRDutySkill-Get')")
	@ApiOperation(value = "根据职务查询DEFAULT", tags = {"职务技能" } ,notes = "根据职务查询DEFAULT")
    @RequestMapping(method= RequestMethod.POST , value="/hrduties/{hrduty_id}/hrdutyskills/searchdefault")
	public ResponseEntity<Page<HRDutySkillDTO>> searchHRDutySkillDefaultByHRDuty(@PathVariable("hrduty_id") String hrduty_id, @RequestBody HRDutySkillSearchContext context) {
        context.setN_hrdutyid_eq(hrduty_id);
        Page<HRDutySkill> domains = hrdutyskillService.searchDefault(context) ;
	    return ResponseEntity.status(HttpStatus.OK)
                .body(new PageImpl(hrdutyskillMapping.toDto(domains.getContent()), context.getPageable(), domains.getTotalElements()));
	}
}

