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.HRFamily;
import cn.ibizlab.humanresource.core.humanresource.service.IHRFamilyService;
import cn.ibizlab.humanresource.core.humanresource.filter.HRFamilySearchContext;
import cn.ibizlab.humanresource.util.annotation.VersionCheck;

@Slf4j
@Api(tags = {"家庭情况" })
@RestController("HRApi-hrfamily")
@RequestMapping("")
public class HRFamilyResource {

    @Autowired
    public IHRFamilyService hrfamilyService;

    @Autowired
    @Lazy
    public HRFamilyMapping hrfamilyMapping;

    @PreAuthorize("hasPermission(this.hrfamilyMapping.toDomain(#hrfamilydto),'iBizHumanResources-HRFamily-Create')")
    @ApiOperation(value = "新建家庭情况", tags = {"家庭情况" },  notes = "新建家庭情况")
	@RequestMapping(method = RequestMethod.POST, value = "/hrfamilies")
    @Transactional
    public ResponseEntity<HRFamilyDTO> create(@RequestBody HRFamilyDTO hrfamilydto) {
        HRFamily domain = hrfamilyMapping.toDomain(hrfamilydto);
		hrfamilyService.create(domain);
        HRFamilyDTO dto = hrfamilyMapping.toDto(domain);
		return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @PreAuthorize("hasPermission(this.hrfamilyMapping.toDomain(#hrfamilydtos),'iBizHumanResources-HRFamily-Create')")
    @ApiOperation(value = "批量新建家庭情况", tags = {"家庭情况" },  notes = "批量新建家庭情况")
	@RequestMapping(method = RequestMethod.POST, value = "/hrfamilies/batch")
    public ResponseEntity<Boolean> createBatch(@RequestBody List<HRFamilyDTO> hrfamilydtos) {
        hrfamilyService.createBatch(hrfamilyMapping.toDomain(hrfamilydtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @VersionCheck(entity = "hrfamily" , versionfield = "updatedate")
    @PreAuthorize("hasPermission(this.hrfamilyService.get(#hrfamily_id),'iBizHumanResources-HRFamily-Update')")
    @ApiOperation(value = "更新家庭情况", tags = {"家庭情况" },  notes = "更新家庭情况")
	@RequestMapping(method = RequestMethod.PUT, value = "/hrfamilies/{hrfamily_id}")
    @Transactional
    public ResponseEntity<HRFamilyDTO> update(@PathVariable("hrfamily_id") String hrfamily_id, @RequestBody HRFamilyDTO hrfamilydto) {
		HRFamily domain  = hrfamilyMapping.toDomain(hrfamilydto);
        domain .setHrfamilyid(hrfamily_id);
		hrfamilyService.update(domain );
		HRFamilyDTO dto = hrfamilyMapping.toDto(domain );
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @PreAuthorize("hasPermission(this.hrfamilyService.getHrfamilyByEntities(this.hrfamilyMapping.toDomain(#hrfamilydtos)),'iBizHumanResources-HRFamily-Update')")
    @ApiOperation(value = "批量更新家庭情况", tags = {"家庭情况" },  notes = "批量更新家庭情况")
	@RequestMapping(method = RequestMethod.PUT, value = "/hrfamilies/batch")
    public ResponseEntity<Boolean> updateBatch(@RequestBody List<HRFamilyDTO> hrfamilydtos) {
        hrfamilyService.updateBatch(hrfamilyMapping.toDomain(hrfamilydtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @PreAuthorize("hasPermission(this.hrfamilyService.get(#hrfamily_id),'iBizHumanResources-HRFamily-Remove')")
    @ApiOperation(value = "删除家庭情况", tags = {"家庭情况" },  notes = "删除家庭情况")
	@RequestMapping(method = RequestMethod.DELETE, value = "/hrfamilies/{hrfamily_id}")
    @Transactional
    public ResponseEntity<Boolean> remove(@PathVariable("hrfamily_id") String hrfamily_id) {
         return ResponseEntity.status(HttpStatus.OK).body(hrfamilyService.remove(hrfamily_id));
    }

    @PreAuthorize("hasPermission(this.hrfamilyService.getHrfamilyByIds(#ids),'iBizHumanResources-HRFamily-Remove')")
    @ApiOperation(value = "批量删除家庭情况", tags = {"家庭情况" },  notes = "批量删除家庭情况")
	@RequestMapping(method = RequestMethod.DELETE, value = "/hrfamilies/batch")
    public ResponseEntity<Boolean> removeBatch(@RequestBody List<String> ids) {
        hrfamilyService.removeBatch(ids);
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @PostAuthorize("hasPermission(this.hrfamilyMapping.toDomain(returnObject.body),'iBizHumanResources-HRFamily-Get')")
    @ApiOperation(value = "获取家庭情况", tags = {"家庭情况" },  notes = "获取家庭情况")
	@RequestMapping(method = RequestMethod.GET, value = "/hrfamilies/{hrfamily_id}")
    public ResponseEntity<HRFamilyDTO> get(@PathVariable("hrfamily_id") String hrfamily_id) {
        HRFamily domain = hrfamilyService.get(hrfamily_id);
        HRFamilyDTO dto = hrfamilyMapping.toDto(domain);
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @ApiOperation(value = "获取家庭情况草稿", tags = {"家庭情况" },  notes = "获取家庭情况草稿")
	@RequestMapping(method = RequestMethod.GET, value = "/hrfamilies/getdraft")
    public ResponseEntity<HRFamilyDTO> getDraft() {
        return ResponseEntity.status(HttpStatus.OK).body(hrfamilyMapping.toDto(hrfamilyService.getDraft(new HRFamily())));
    }

    @ApiOperation(value = "检查家庭情况", tags = {"家庭情况" },  notes = "检查家庭情况")
	@RequestMapping(method = RequestMethod.POST, value = "/hrfamilies/checkkey")
    public ResponseEntity<Boolean> checkKey(@RequestBody HRFamilyDTO hrfamilydto) {
        return  ResponseEntity.status(HttpStatus.OK).body(hrfamilyService.checkKey(hrfamilyMapping.toDomain(hrfamilydto)));
    }

    @PreAuthorize("hasPermission(this.hrfamilyMapping.toDomain(#hrfamilydto),'iBizHumanResources-HRFamily-Save')")
    @ApiOperation(value = "保存家庭情况", tags = {"家庭情况" },  notes = "保存家庭情况")
	@RequestMapping(method = RequestMethod.POST, value = "/hrfamilies/save")
    public ResponseEntity<Boolean> save(@RequestBody HRFamilyDTO hrfamilydto) {
        return ResponseEntity.status(HttpStatus.OK).body(hrfamilyService.save(hrfamilyMapping.toDomain(hrfamilydto)));
    }

    @PreAuthorize("hasPermission(this.hrfamilyMapping.toDomain(#hrfamilydtos),'iBizHumanResources-HRFamily-Save')")
    @ApiOperation(value = "批量保存家庭情况", tags = {"家庭情况" },  notes = "批量保存家庭情况")
	@RequestMapping(method = RequestMethod.POST, value = "/hrfamilies/savebatch")
    public ResponseEntity<Boolean> saveBatch(@RequestBody List<HRFamilyDTO> hrfamilydtos) {
        hrfamilyService.saveBatch(hrfamilyMapping.toDomain(hrfamilydtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','iBizHumanResources-HRFamily-searchDefault-all') and hasPermission(#context,'iBizHumanResources-HRFamily-Get')")
	@ApiOperation(value = "获取DEFAULT", tags = {"家庭情况" } ,notes = "获取DEFAULT")
    @RequestMapping(method= RequestMethod.GET , value="/hrfamilies/fetchdefault")
	public ResponseEntity<List<HRFamilyDTO>> fetchDefault(HRFamilySearchContext context) {
        Page<HRFamily> domains = hrfamilyService.searchDefault(context) ;
        List<HRFamilyDTO> list = hrfamilyMapping.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-HRFamily-searchDefault-all') and hasPermission(#context,'iBizHumanResources-HRFamily-Get')")
	@ApiOperation(value = "查询DEFAULT", tags = {"家庭情况" } ,notes = "查询DEFAULT")
    @RequestMapping(method= RequestMethod.POST , value="/hrfamilies/searchdefault")
	public ResponseEntity<Page<HRFamilyDTO>> searchDefault(@RequestBody HRFamilySearchContext context) {
        Page<HRFamily> domains = hrfamilyService.searchDefault(context) ;
	    return ResponseEntity.status(HttpStatus.OK)
                .body(new PageImpl(hrfamilyMapping.toDto(domains.getContent()), context.getPageable(), domains.getTotalElements()));
	}
    @PreAuthorize("hasPermission(this.hrfamilyMapping.toDomain(#hrfamilydto),'iBizHumanResources-HRFamily-Create')")
    @ApiOperation(value = "根据员工建立家庭情况", tags = {"家庭情况" },  notes = "根据员工建立家庭情况")
	@RequestMapping(method = RequestMethod.POST, value = "/hremployees/{hremployee_id}/hrfamilies")
    @Transactional
    public ResponseEntity<HRFamilyDTO> createByHREmployee(@PathVariable("hremployee_id") String hremployee_id, @RequestBody HRFamilyDTO hrfamilydto) {
        HRFamily domain = hrfamilyMapping.toDomain(hrfamilydto);
        domain.setHremployeeid(hremployee_id);
		hrfamilyService.create(domain);
        HRFamilyDTO dto = hrfamilyMapping.toDto(domain);
		return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @PreAuthorize("hasPermission(this.hrfamilyMapping.toDomain(#hrfamilydtos),'iBizHumanResources-HRFamily-Create')")
    @ApiOperation(value = "根据员工批量建立家庭情况", tags = {"家庭情况" },  notes = "根据员工批量建立家庭情况")
	@RequestMapping(method = RequestMethod.POST, value = "/hremployees/{hremployee_id}/hrfamilies/batch")
    public ResponseEntity<Boolean> createBatchByHREmployee(@PathVariable("hremployee_id") String hremployee_id, @RequestBody List<HRFamilyDTO> hrfamilydtos) {
        List<HRFamily> domainlist=hrfamilyMapping.toDomain(hrfamilydtos);
        for(HRFamily domain:domainlist){
            domain.setHremployeeid(hremployee_id);
        }
        hrfamilyService.createBatch(domainlist);
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @VersionCheck(entity = "hrfamily" , versionfield = "updatedate")
    @PreAuthorize("hasPermission(this.hrfamilyService.get(#hrfamily_id),'iBizHumanResources-HRFamily-Update')")
    @ApiOperation(value = "根据员工更新家庭情况", tags = {"家庭情况" },  notes = "根据员工更新家庭情况")
	@RequestMapping(method = RequestMethod.PUT, value = "/hremployees/{hremployee_id}/hrfamilies/{hrfamily_id}")
    @Transactional
    public ResponseEntity<HRFamilyDTO> updateByHREmployee(@PathVariable("hremployee_id") String hremployee_id, @PathVariable("hrfamily_id") String hrfamily_id, @RequestBody HRFamilyDTO hrfamilydto) {
        HRFamily domain = hrfamilyMapping.toDomain(hrfamilydto);
        domain.setHremployeeid(hremployee_id);
        domain.setHrfamilyid(hrfamily_id);
		hrfamilyService.update(domain);
        HRFamilyDTO dto = hrfamilyMapping.toDto(domain);
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @PreAuthorize("hasPermission(this.hrfamilyService.getHrfamilyByEntities(this.hrfamilyMapping.toDomain(#hrfamilydtos)),'iBizHumanResources-HRFamily-Update')")
    @ApiOperation(value = "根据员工批量更新家庭情况", tags = {"家庭情况" },  notes = "根据员工批量更新家庭情况")
	@RequestMapping(method = RequestMethod.PUT, value = "/hremployees/{hremployee_id}/hrfamilies/batch")
    public ResponseEntity<Boolean> updateBatchByHREmployee(@PathVariable("hremployee_id") String hremployee_id, @RequestBody List<HRFamilyDTO> hrfamilydtos) {
        List<HRFamily> domainlist=hrfamilyMapping.toDomain(hrfamilydtos);
        for(HRFamily domain:domainlist){
            domain.setHremployeeid(hremployee_id);
        }
        hrfamilyService.updateBatch(domainlist);
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @PreAuthorize("hasPermission(this.hrfamilyService.get(#hrfamily_id),'iBizHumanResources-HRFamily-Remove')")
    @ApiOperation(value = "根据员工删除家庭情况", tags = {"家庭情况" },  notes = "根据员工删除家庭情况")
	@RequestMapping(method = RequestMethod.DELETE, value = "/hremployees/{hremployee_id}/hrfamilies/{hrfamily_id}")
    @Transactional
    public ResponseEntity<Boolean> removeByHREmployee(@PathVariable("hremployee_id") String hremployee_id, @PathVariable("hrfamily_id") String hrfamily_id) {
		return ResponseEntity.status(HttpStatus.OK).body(hrfamilyService.remove(hrfamily_id));
    }

    @PreAuthorize("hasPermission(this.hrfamilyService.getHrfamilyByIds(#ids),'iBizHumanResources-HRFamily-Remove')")
    @ApiOperation(value = "根据员工批量删除家庭情况", tags = {"家庭情况" },  notes = "根据员工批量删除家庭情况")
	@RequestMapping(method = RequestMethod.DELETE, value = "/hremployees/{hremployee_id}/hrfamilies/batch")
    public ResponseEntity<Boolean> removeBatchByHREmployee(@RequestBody List<String> ids) {
        hrfamilyService.removeBatch(ids);
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @PostAuthorize("hasPermission(this.hrfamilyMapping.toDomain(returnObject.body),'iBizHumanResources-HRFamily-Get')")
    @ApiOperation(value = "根据员工获取家庭情况", tags = {"家庭情况" },  notes = "根据员工获取家庭情况")
	@RequestMapping(method = RequestMethod.GET, value = "/hremployees/{hremployee_id}/hrfamilies/{hrfamily_id}")
    public ResponseEntity<HRFamilyDTO> getByHREmployee(@PathVariable("hremployee_id") String hremployee_id, @PathVariable("hrfamily_id") String hrfamily_id) {
        HRFamily domain = hrfamilyService.get(hrfamily_id);
        HRFamilyDTO dto = hrfamilyMapping.toDto(domain);
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @ApiOperation(value = "根据员工获取家庭情况草稿", tags = {"家庭情况" },  notes = "根据员工获取家庭情况草稿")
    @RequestMapping(method = RequestMethod.GET, value = "/hremployees/{hremployee_id}/hrfamilies/getdraft")
    public ResponseEntity<HRFamilyDTO> getDraftByHREmployee(@PathVariable("hremployee_id") String hremployee_id) {
        HRFamily domain = new HRFamily();
        domain.setHremployeeid(hremployee_id);
        return ResponseEntity.status(HttpStatus.OK).body(hrfamilyMapping.toDto(hrfamilyService.getDraft(domain)));
    }

    @ApiOperation(value = "根据员工检查家庭情况", tags = {"家庭情况" },  notes = "根据员工检查家庭情况")
	@RequestMapping(method = RequestMethod.POST, value = "/hremployees/{hremployee_id}/hrfamilies/checkkey")
    public ResponseEntity<Boolean> checkKeyByHREmployee(@PathVariable("hremployee_id") String hremployee_id, @RequestBody HRFamilyDTO hrfamilydto) {
        return  ResponseEntity.status(HttpStatus.OK).body(hrfamilyService.checkKey(hrfamilyMapping.toDomain(hrfamilydto)));
    }

    @PreAuthorize("hasPermission(this.hrfamilyMapping.toDomain(#hrfamilydto),'iBizHumanResources-HRFamily-Save')")
    @ApiOperation(value = "根据员工保存家庭情况", tags = {"家庭情况" },  notes = "根据员工保存家庭情况")
	@RequestMapping(method = RequestMethod.POST, value = "/hremployees/{hremployee_id}/hrfamilies/save")
    public ResponseEntity<Boolean> saveByHREmployee(@PathVariable("hremployee_id") String hremployee_id, @RequestBody HRFamilyDTO hrfamilydto) {
        HRFamily domain = hrfamilyMapping.toDomain(hrfamilydto);
        domain.setHremployeeid(hremployee_id);
        return ResponseEntity.status(HttpStatus.OK).body(hrfamilyService.save(domain));
    }

    @PreAuthorize("hasPermission(this.hrfamilyMapping.toDomain(#hrfamilydtos),'iBizHumanResources-HRFamily-Save')")
    @ApiOperation(value = "根据员工批量保存家庭情况", tags = {"家庭情况" },  notes = "根据员工批量保存家庭情况")
	@RequestMapping(method = RequestMethod.POST, value = "/hremployees/{hremployee_id}/hrfamilies/savebatch")
    public ResponseEntity<Boolean> saveBatchByHREmployee(@PathVariable("hremployee_id") String hremployee_id, @RequestBody List<HRFamilyDTO> hrfamilydtos) {
        List<HRFamily> domainlist=hrfamilyMapping.toDomain(hrfamilydtos);
        for(HRFamily domain:domainlist){
             domain.setHremployeeid(hremployee_id);
        }
        hrfamilyService.saveBatch(domainlist);
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','iBizHumanResources-HRFamily-searchDefault-all') and hasPermission(#context,'iBizHumanResources-HRFamily-Get')")
	@ApiOperation(value = "根据员工获取DEFAULT", tags = {"家庭情况" } ,notes = "根据员工获取DEFAULT")
    @RequestMapping(method= RequestMethod.GET , value="/hremployees/{hremployee_id}/hrfamilies/fetchdefault")
	public ResponseEntity<List<HRFamilyDTO>> fetchHRFamilyDefaultByHREmployee(@PathVariable("hremployee_id") String hremployee_id,HRFamilySearchContext context) {
        context.setN_hremployeeid_eq(hremployee_id);
        Page<HRFamily> domains = hrfamilyService.searchDefault(context) ;
        List<HRFamilyDTO> list = hrfamilyMapping.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-HRFamily-searchDefault-all') and hasPermission(#context,'iBizHumanResources-HRFamily-Get')")
	@ApiOperation(value = "根据员工查询DEFAULT", tags = {"家庭情况" } ,notes = "根据员工查询DEFAULT")
    @RequestMapping(method= RequestMethod.POST , value="/hremployees/{hremployee_id}/hrfamilies/searchdefault")
	public ResponseEntity<Page<HRFamilyDTO>> searchHRFamilyDefaultByHREmployee(@PathVariable("hremployee_id") String hremployee_id, @RequestBody HRFamilySearchContext context) {
        context.setN_hremployeeid_eq(hremployee_id);
        Page<HRFamily> domains = hrfamilyService.searchDefault(context) ;
	    return ResponseEntity.status(HttpStatus.OK)
                .body(new PageImpl(hrfamilyMapping.toDto(domains.getContent()), context.getPageable(), domains.getTotalElements()));
	}
}

