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.HRReward;
import cn.ibizlab.humanresource.core.humanresource.service.IHRRewardService;
import cn.ibizlab.humanresource.core.humanresource.filter.HRRewardSearchContext;
import cn.ibizlab.humanresource.util.annotation.VersionCheck;

@Slf4j
@Api(tags = {"奖励惩罚" })
@RestController("HRApi-hrreward")
@RequestMapping("")
public class HRRewardResource {

    @Autowired
    public IHRRewardService hrrewardService;

    @Autowired
    @Lazy
    public HRRewardMapping hrrewardMapping;

    @PreAuthorize("hasPermission(this.hrrewardMapping.toDomain(#hrrewarddto),'iBizHumanResources-HRReward-Create')")
    @ApiOperation(value = "新建奖励惩罚", tags = {"奖励惩罚" },  notes = "新建奖励惩罚")
	@RequestMapping(method = RequestMethod.POST, value = "/hrrewards")
    @Transactional
    public ResponseEntity<HRRewardDTO> create(@RequestBody HRRewardDTO hrrewarddto) {
        HRReward domain = hrrewardMapping.toDomain(hrrewarddto);
		hrrewardService.create(domain);
        HRRewardDTO dto = hrrewardMapping.toDto(domain);
		return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @PreAuthorize("hasPermission(this.hrrewardMapping.toDomain(#hrrewarddtos),'iBizHumanResources-HRReward-Create')")
    @ApiOperation(value = "批量新建奖励惩罚", tags = {"奖励惩罚" },  notes = "批量新建奖励惩罚")
	@RequestMapping(method = RequestMethod.POST, value = "/hrrewards/batch")
    public ResponseEntity<Boolean> createBatch(@RequestBody List<HRRewardDTO> hrrewarddtos) {
        hrrewardService.createBatch(hrrewardMapping.toDomain(hrrewarddtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @VersionCheck(entity = "hrreward" , versionfield = "updatedate")
    @PreAuthorize("hasPermission(this.hrrewardService.get(#hrreward_id),'iBizHumanResources-HRReward-Update')")
    @ApiOperation(value = "更新奖励惩罚", tags = {"奖励惩罚" },  notes = "更新奖励惩罚")
	@RequestMapping(method = RequestMethod.PUT, value = "/hrrewards/{hrreward_id}")
    @Transactional
    public ResponseEntity<HRRewardDTO> update(@PathVariable("hrreward_id") String hrreward_id, @RequestBody HRRewardDTO hrrewarddto) {
		HRReward domain  = hrrewardMapping.toDomain(hrrewarddto);
        domain .setHrrewardid(hrreward_id);
		hrrewardService.update(domain );
		HRRewardDTO dto = hrrewardMapping.toDto(domain );
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @PreAuthorize("hasPermission(this.hrrewardService.getHrrewardByEntities(this.hrrewardMapping.toDomain(#hrrewarddtos)),'iBizHumanResources-HRReward-Update')")
    @ApiOperation(value = "批量更新奖励惩罚", tags = {"奖励惩罚" },  notes = "批量更新奖励惩罚")
	@RequestMapping(method = RequestMethod.PUT, value = "/hrrewards/batch")
    public ResponseEntity<Boolean> updateBatch(@RequestBody List<HRRewardDTO> hrrewarddtos) {
        hrrewardService.updateBatch(hrrewardMapping.toDomain(hrrewarddtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @PreAuthorize("hasPermission(this.hrrewardService.get(#hrreward_id),'iBizHumanResources-HRReward-Remove')")
    @ApiOperation(value = "删除奖励惩罚", tags = {"奖励惩罚" },  notes = "删除奖励惩罚")
	@RequestMapping(method = RequestMethod.DELETE, value = "/hrrewards/{hrreward_id}")
    @Transactional
    public ResponseEntity<Boolean> remove(@PathVariable("hrreward_id") String hrreward_id) {
         return ResponseEntity.status(HttpStatus.OK).body(hrrewardService.remove(hrreward_id));
    }

    @PreAuthorize("hasPermission(this.hrrewardService.getHrrewardByIds(#ids),'iBizHumanResources-HRReward-Remove')")
    @ApiOperation(value = "批量删除奖励惩罚", tags = {"奖励惩罚" },  notes = "批量删除奖励惩罚")
	@RequestMapping(method = RequestMethod.DELETE, value = "/hrrewards/batch")
    public ResponseEntity<Boolean> removeBatch(@RequestBody List<String> ids) {
        hrrewardService.removeBatch(ids);
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @PostAuthorize("hasPermission(this.hrrewardMapping.toDomain(returnObject.body),'iBizHumanResources-HRReward-Get')")
    @ApiOperation(value = "获取奖励惩罚", tags = {"奖励惩罚" },  notes = "获取奖励惩罚")
	@RequestMapping(method = RequestMethod.GET, value = "/hrrewards/{hrreward_id}")
    public ResponseEntity<HRRewardDTO> get(@PathVariable("hrreward_id") String hrreward_id) {
        HRReward domain = hrrewardService.get(hrreward_id);
        HRRewardDTO dto = hrrewardMapping.toDto(domain);
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @ApiOperation(value = "获取奖励惩罚草稿", tags = {"奖励惩罚" },  notes = "获取奖励惩罚草稿")
	@RequestMapping(method = RequestMethod.GET, value = "/hrrewards/getdraft")
    public ResponseEntity<HRRewardDTO> getDraft() {
        return ResponseEntity.status(HttpStatus.OK).body(hrrewardMapping.toDto(hrrewardService.getDraft(new HRReward())));
    }

    @ApiOperation(value = "检查奖励惩罚", tags = {"奖励惩罚" },  notes = "检查奖励惩罚")
	@RequestMapping(method = RequestMethod.POST, value = "/hrrewards/checkkey")
    public ResponseEntity<Boolean> checkKey(@RequestBody HRRewardDTO hrrewarddto) {
        return  ResponseEntity.status(HttpStatus.OK).body(hrrewardService.checkKey(hrrewardMapping.toDomain(hrrewarddto)));
    }

    @PreAuthorize("hasPermission(this.hrrewardMapping.toDomain(#hrrewarddto),'iBizHumanResources-HRReward-Save')")
    @ApiOperation(value = "保存奖励惩罚", tags = {"奖励惩罚" },  notes = "保存奖励惩罚")
	@RequestMapping(method = RequestMethod.POST, value = "/hrrewards/save")
    public ResponseEntity<Boolean> save(@RequestBody HRRewardDTO hrrewarddto) {
        return ResponseEntity.status(HttpStatus.OK).body(hrrewardService.save(hrrewardMapping.toDomain(hrrewarddto)));
    }

    @PreAuthorize("hasPermission(this.hrrewardMapping.toDomain(#hrrewarddtos),'iBizHumanResources-HRReward-Save')")
    @ApiOperation(value = "批量保存奖励惩罚", tags = {"奖励惩罚" },  notes = "批量保存奖励惩罚")
	@RequestMapping(method = RequestMethod.POST, value = "/hrrewards/savebatch")
    public ResponseEntity<Boolean> saveBatch(@RequestBody List<HRRewardDTO> hrrewarddtos) {
        hrrewardService.saveBatch(hrrewardMapping.toDomain(hrrewarddtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','iBizHumanResources-HRReward-searchDefault-all') and hasPermission(#context,'iBizHumanResources-HRReward-Get')")
	@ApiOperation(value = "获取DEFAULT", tags = {"奖励惩罚" } ,notes = "获取DEFAULT")
    @RequestMapping(method= RequestMethod.GET , value="/hrrewards/fetchdefault")
	public ResponseEntity<List<HRRewardDTO>> fetchDefault(HRRewardSearchContext context) {
        Page<HRReward> domains = hrrewardService.searchDefault(context) ;
        List<HRRewardDTO> list = hrrewardMapping.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-HRReward-searchDefault-all') and hasPermission(#context,'iBizHumanResources-HRReward-Get')")
	@ApiOperation(value = "查询DEFAULT", tags = {"奖励惩罚" } ,notes = "查询DEFAULT")
    @RequestMapping(method= RequestMethod.POST , value="/hrrewards/searchdefault")
	public ResponseEntity<Page<HRRewardDTO>> searchDefault(@RequestBody HRRewardSearchContext context) {
        Page<HRReward> domains = hrrewardService.searchDefault(context) ;
	    return ResponseEntity.status(HttpStatus.OK)
                .body(new PageImpl(hrrewardMapping.toDto(domains.getContent()), context.getPageable(), domains.getTotalElements()));
	}
    @PreAuthorize("hasPermission(this.hrrewardMapping.toDomain(#hrrewarddto),'iBizHumanResources-HRReward-Create')")
    @ApiOperation(value = "根据员工建立奖励惩罚", tags = {"奖励惩罚" },  notes = "根据员工建立奖励惩罚")
	@RequestMapping(method = RequestMethod.POST, value = "/hremployees/{hremployee_id}/hrrewards")
    @Transactional
    public ResponseEntity<HRRewardDTO> createByHREmployee(@PathVariable("hremployee_id") String hremployee_id, @RequestBody HRRewardDTO hrrewarddto) {
        HRReward domain = hrrewardMapping.toDomain(hrrewarddto);
        domain.setHremployeeid(hremployee_id);
		hrrewardService.create(domain);
        HRRewardDTO dto = hrrewardMapping.toDto(domain);
		return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @PreAuthorize("hasPermission(this.hrrewardMapping.toDomain(#hrrewarddtos),'iBizHumanResources-HRReward-Create')")
    @ApiOperation(value = "根据员工批量建立奖励惩罚", tags = {"奖励惩罚" },  notes = "根据员工批量建立奖励惩罚")
	@RequestMapping(method = RequestMethod.POST, value = "/hremployees/{hremployee_id}/hrrewards/batch")
    public ResponseEntity<Boolean> createBatchByHREmployee(@PathVariable("hremployee_id") String hremployee_id, @RequestBody List<HRRewardDTO> hrrewarddtos) {
        List<HRReward> domainlist=hrrewardMapping.toDomain(hrrewarddtos);
        for(HRReward domain:domainlist){
            domain.setHremployeeid(hremployee_id);
        }
        hrrewardService.createBatch(domainlist);
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @VersionCheck(entity = "hrreward" , versionfield = "updatedate")
    @PreAuthorize("hasPermission(this.hrrewardService.get(#hrreward_id),'iBizHumanResources-HRReward-Update')")
    @ApiOperation(value = "根据员工更新奖励惩罚", tags = {"奖励惩罚" },  notes = "根据员工更新奖励惩罚")
	@RequestMapping(method = RequestMethod.PUT, value = "/hremployees/{hremployee_id}/hrrewards/{hrreward_id}")
    @Transactional
    public ResponseEntity<HRRewardDTO> updateByHREmployee(@PathVariable("hremployee_id") String hremployee_id, @PathVariable("hrreward_id") String hrreward_id, @RequestBody HRRewardDTO hrrewarddto) {
        HRReward domain = hrrewardMapping.toDomain(hrrewarddto);
        domain.setHremployeeid(hremployee_id);
        domain.setHrrewardid(hrreward_id);
		hrrewardService.update(domain);
        HRRewardDTO dto = hrrewardMapping.toDto(domain);
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @PreAuthorize("hasPermission(this.hrrewardService.getHrrewardByEntities(this.hrrewardMapping.toDomain(#hrrewarddtos)),'iBizHumanResources-HRReward-Update')")
    @ApiOperation(value = "根据员工批量更新奖励惩罚", tags = {"奖励惩罚" },  notes = "根据员工批量更新奖励惩罚")
	@RequestMapping(method = RequestMethod.PUT, value = "/hremployees/{hremployee_id}/hrrewards/batch")
    public ResponseEntity<Boolean> updateBatchByHREmployee(@PathVariable("hremployee_id") String hremployee_id, @RequestBody List<HRRewardDTO> hrrewarddtos) {
        List<HRReward> domainlist=hrrewardMapping.toDomain(hrrewarddtos);
        for(HRReward domain:domainlist){
            domain.setHremployeeid(hremployee_id);
        }
        hrrewardService.updateBatch(domainlist);
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @PreAuthorize("hasPermission(this.hrrewardService.get(#hrreward_id),'iBizHumanResources-HRReward-Remove')")
    @ApiOperation(value = "根据员工删除奖励惩罚", tags = {"奖励惩罚" },  notes = "根据员工删除奖励惩罚")
	@RequestMapping(method = RequestMethod.DELETE, value = "/hremployees/{hremployee_id}/hrrewards/{hrreward_id}")
    @Transactional
    public ResponseEntity<Boolean> removeByHREmployee(@PathVariable("hremployee_id") String hremployee_id, @PathVariable("hrreward_id") String hrreward_id) {
		return ResponseEntity.status(HttpStatus.OK).body(hrrewardService.remove(hrreward_id));
    }

    @PreAuthorize("hasPermission(this.hrrewardService.getHrrewardByIds(#ids),'iBizHumanResources-HRReward-Remove')")
    @ApiOperation(value = "根据员工批量删除奖励惩罚", tags = {"奖励惩罚" },  notes = "根据员工批量删除奖励惩罚")
	@RequestMapping(method = RequestMethod.DELETE, value = "/hremployees/{hremployee_id}/hrrewards/batch")
    public ResponseEntity<Boolean> removeBatchByHREmployee(@RequestBody List<String> ids) {
        hrrewardService.removeBatch(ids);
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @PostAuthorize("hasPermission(this.hrrewardMapping.toDomain(returnObject.body),'iBizHumanResources-HRReward-Get')")
    @ApiOperation(value = "根据员工获取奖励惩罚", tags = {"奖励惩罚" },  notes = "根据员工获取奖励惩罚")
	@RequestMapping(method = RequestMethod.GET, value = "/hremployees/{hremployee_id}/hrrewards/{hrreward_id}")
    public ResponseEntity<HRRewardDTO> getByHREmployee(@PathVariable("hremployee_id") String hremployee_id, @PathVariable("hrreward_id") String hrreward_id) {
        HRReward domain = hrrewardService.get(hrreward_id);
        HRRewardDTO dto = hrrewardMapping.toDto(domain);
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @ApiOperation(value = "根据员工获取奖励惩罚草稿", tags = {"奖励惩罚" },  notes = "根据员工获取奖励惩罚草稿")
    @RequestMapping(method = RequestMethod.GET, value = "/hremployees/{hremployee_id}/hrrewards/getdraft")
    public ResponseEntity<HRRewardDTO> getDraftByHREmployee(@PathVariable("hremployee_id") String hremployee_id) {
        HRReward domain = new HRReward();
        domain.setHremployeeid(hremployee_id);
        return ResponseEntity.status(HttpStatus.OK).body(hrrewardMapping.toDto(hrrewardService.getDraft(domain)));
    }

    @ApiOperation(value = "根据员工检查奖励惩罚", tags = {"奖励惩罚" },  notes = "根据员工检查奖励惩罚")
	@RequestMapping(method = RequestMethod.POST, value = "/hremployees/{hremployee_id}/hrrewards/checkkey")
    public ResponseEntity<Boolean> checkKeyByHREmployee(@PathVariable("hremployee_id") String hremployee_id, @RequestBody HRRewardDTO hrrewarddto) {
        return  ResponseEntity.status(HttpStatus.OK).body(hrrewardService.checkKey(hrrewardMapping.toDomain(hrrewarddto)));
    }

    @PreAuthorize("hasPermission(this.hrrewardMapping.toDomain(#hrrewarddto),'iBizHumanResources-HRReward-Save')")
    @ApiOperation(value = "根据员工保存奖励惩罚", tags = {"奖励惩罚" },  notes = "根据员工保存奖励惩罚")
	@RequestMapping(method = RequestMethod.POST, value = "/hremployees/{hremployee_id}/hrrewards/save")
    public ResponseEntity<Boolean> saveByHREmployee(@PathVariable("hremployee_id") String hremployee_id, @RequestBody HRRewardDTO hrrewarddto) {
        HRReward domain = hrrewardMapping.toDomain(hrrewarddto);
        domain.setHremployeeid(hremployee_id);
        return ResponseEntity.status(HttpStatus.OK).body(hrrewardService.save(domain));
    }

    @PreAuthorize("hasPermission(this.hrrewardMapping.toDomain(#hrrewarddtos),'iBizHumanResources-HRReward-Save')")
    @ApiOperation(value = "根据员工批量保存奖励惩罚", tags = {"奖励惩罚" },  notes = "根据员工批量保存奖励惩罚")
	@RequestMapping(method = RequestMethod.POST, value = "/hremployees/{hremployee_id}/hrrewards/savebatch")
    public ResponseEntity<Boolean> saveBatchByHREmployee(@PathVariable("hremployee_id") String hremployee_id, @RequestBody List<HRRewardDTO> hrrewarddtos) {
        List<HRReward> domainlist=hrrewardMapping.toDomain(hrrewarddtos);
        for(HRReward domain:domainlist){
             domain.setHremployeeid(hremployee_id);
        }
        hrrewardService.saveBatch(domainlist);
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','iBizHumanResources-HRReward-searchDefault-all') and hasPermission(#context,'iBizHumanResources-HRReward-Get')")
	@ApiOperation(value = "根据员工获取DEFAULT", tags = {"奖励惩罚" } ,notes = "根据员工获取DEFAULT")
    @RequestMapping(method= RequestMethod.GET , value="/hremployees/{hremployee_id}/hrrewards/fetchdefault")
	public ResponseEntity<List<HRRewardDTO>> fetchHRRewardDefaultByHREmployee(@PathVariable("hremployee_id") String hremployee_id,HRRewardSearchContext context) {
        context.setN_hremployeeid_eq(hremployee_id);
        Page<HRReward> domains = hrrewardService.searchDefault(context) ;
        List<HRRewardDTO> list = hrrewardMapping.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-HRReward-searchDefault-all') and hasPermission(#context,'iBizHumanResources-HRReward-Get')")
	@ApiOperation(value = "根据员工查询DEFAULT", tags = {"奖励惩罚" } ,notes = "根据员工查询DEFAULT")
    @RequestMapping(method= RequestMethod.POST , value="/hremployees/{hremployee_id}/hrrewards/searchdefault")
	public ResponseEntity<Page<HRRewardDTO>> searchHRRewardDefaultByHREmployee(@PathVariable("hremployee_id") String hremployee_id, @RequestBody HRRewardSearchContext context) {
        context.setN_hremployeeid_eq(hremployee_id);
        Page<HRReward> domains = hrrewardService.searchDefault(context) ;
	    return ResponseEntity.status(HttpStatus.OK)
                .body(new PageImpl(hrrewardMapping.toDto(domains.getContent()), context.getPageable(), domains.getTotalElements()));
	}
}

