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.base.domain.HROrganization;
import cn.ibizlab.humanresource.core.base.service.IHROrganizationService;
import cn.ibizlab.humanresource.core.base.filter.HROrganizationSearchContext;
import cn.ibizlab.humanresource.util.annotation.VersionCheck;

@Slf4j
@Api(tags = {"组织" })
@RestController("HRApi-hrorganization")
@RequestMapping("")
public class HROrganizationResource {

    @Autowired
    public IHROrganizationService hrorganizationService;

    @Autowired
    @Lazy
    public HROrganizationMapping hrorganizationMapping;

    @PreAuthorize("hasPermission(this.hrorganizationMapping.toDomain(#hrorganizationdto),'iBizHumanResources-HROrganization-Create')")
    @ApiOperation(value = "新建组织", tags = {"组织" },  notes = "新建组织")
	@RequestMapping(method = RequestMethod.POST, value = "/hrorganizations")
    @Transactional
    public ResponseEntity<HROrganizationDTO> create(@RequestBody HROrganizationDTO hrorganizationdto) {
        HROrganization domain = hrorganizationMapping.toDomain(hrorganizationdto);
		hrorganizationService.create(domain);
        HROrganizationDTO dto = hrorganizationMapping.toDto(domain);
		return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @PreAuthorize("hasPermission(this.hrorganizationMapping.toDomain(#hrorganizationdtos),'iBizHumanResources-HROrganization-Create')")
    @ApiOperation(value = "批量新建组织", tags = {"组织" },  notes = "批量新建组织")
	@RequestMapping(method = RequestMethod.POST, value = "/hrorganizations/batch")
    public ResponseEntity<Boolean> createBatch(@RequestBody List<HROrganizationDTO> hrorganizationdtos) {
        hrorganizationService.createBatch(hrorganizationMapping.toDomain(hrorganizationdtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @VersionCheck(entity = "hrorganization" , versionfield = "updatedate")
    @PreAuthorize("hasPermission(this.hrorganizationService.get(#hrorganization_id),'iBizHumanResources-HROrganization-Update')")
    @ApiOperation(value = "更新组织", tags = {"组织" },  notes = "更新组织")
	@RequestMapping(method = RequestMethod.PUT, value = "/hrorganizations/{hrorganization_id}")
    @Transactional
    public ResponseEntity<HROrganizationDTO> update(@PathVariable("hrorganization_id") String hrorganization_id, @RequestBody HROrganizationDTO hrorganizationdto) {
		HROrganization domain  = hrorganizationMapping.toDomain(hrorganizationdto);
        domain .setOrganizationid(hrorganization_id);
		hrorganizationService.update(domain );
		HROrganizationDTO dto = hrorganizationMapping.toDto(domain );
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @PreAuthorize("hasPermission(this.hrorganizationService.getHrorganizationByEntities(this.hrorganizationMapping.toDomain(#hrorganizationdtos)),'iBizHumanResources-HROrganization-Update')")
    @ApiOperation(value = "批量更新组织", tags = {"组织" },  notes = "批量更新组织")
	@RequestMapping(method = RequestMethod.PUT, value = "/hrorganizations/batch")
    public ResponseEntity<Boolean> updateBatch(@RequestBody List<HROrganizationDTO> hrorganizationdtos) {
        hrorganizationService.updateBatch(hrorganizationMapping.toDomain(hrorganizationdtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @PreAuthorize("hasPermission(this.hrorganizationService.get(#hrorganization_id),'iBizHumanResources-HROrganization-Remove')")
    @ApiOperation(value = "删除组织", tags = {"组织" },  notes = "删除组织")
	@RequestMapping(method = RequestMethod.DELETE, value = "/hrorganizations/{hrorganization_id}")
    @Transactional
    public ResponseEntity<Boolean> remove(@PathVariable("hrorganization_id") String hrorganization_id) {
         return ResponseEntity.status(HttpStatus.OK).body(hrorganizationService.remove(hrorganization_id));
    }

    @PreAuthorize("hasPermission(this.hrorganizationService.getHrorganizationByIds(#ids),'iBizHumanResources-HROrganization-Remove')")
    @ApiOperation(value = "批量删除组织", tags = {"组织" },  notes = "批量删除组织")
	@RequestMapping(method = RequestMethod.DELETE, value = "/hrorganizations/batch")
    public ResponseEntity<Boolean> removeBatch(@RequestBody List<String> ids) {
        hrorganizationService.removeBatch(ids);
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @PostAuthorize("hasPermission(this.hrorganizationMapping.toDomain(returnObject.body),'iBizHumanResources-HROrganization-Get')")
    @ApiOperation(value = "获取组织", tags = {"组织" },  notes = "获取组织")
	@RequestMapping(method = RequestMethod.GET, value = "/hrorganizations/{hrorganization_id}")
    public ResponseEntity<HROrganizationDTO> get(@PathVariable("hrorganization_id") String hrorganization_id) {
        HROrganization domain = hrorganizationService.get(hrorganization_id);
        HROrganizationDTO dto = hrorganizationMapping.toDto(domain);
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @ApiOperation(value = "获取组织草稿", tags = {"组织" },  notes = "获取组织草稿")
	@RequestMapping(method = RequestMethod.GET, value = "/hrorganizations/getdraft")
    public ResponseEntity<HROrganizationDTO> getDraft() {
        return ResponseEntity.status(HttpStatus.OK).body(hrorganizationMapping.toDto(hrorganizationService.getDraft(new HROrganization())));
    }

    @ApiOperation(value = "检查组织", tags = {"组织" },  notes = "检查组织")
	@RequestMapping(method = RequestMethod.POST, value = "/hrorganizations/checkkey")
    public ResponseEntity<Boolean> checkKey(@RequestBody HROrganizationDTO hrorganizationdto) {
        return  ResponseEntity.status(HttpStatus.OK).body(hrorganizationService.checkKey(hrorganizationMapping.toDomain(hrorganizationdto)));
    }

    @PreAuthorize("hasPermission(this.hrorganizationMapping.toDomain(#hrorganizationdto),'iBizHumanResources-HROrganization-Save')")
    @ApiOperation(value = "保存组织", tags = {"组织" },  notes = "保存组织")
	@RequestMapping(method = RequestMethod.POST, value = "/hrorganizations/save")
    public ResponseEntity<Boolean> save(@RequestBody HROrganizationDTO hrorganizationdto) {
        return ResponseEntity.status(HttpStatus.OK).body(hrorganizationService.save(hrorganizationMapping.toDomain(hrorganizationdto)));
    }

    @PreAuthorize("hasPermission(this.hrorganizationMapping.toDomain(#hrorganizationdtos),'iBizHumanResources-HROrganization-Save')")
    @ApiOperation(value = "批量保存组织", tags = {"组织" },  notes = "批量保存组织")
	@RequestMapping(method = RequestMethod.POST, value = "/hrorganizations/savebatch")
    public ResponseEntity<Boolean> saveBatch(@RequestBody List<HROrganizationDTO> hrorganizationdtos) {
        hrorganizationService.saveBatch(hrorganizationMapping.toDomain(hrorganizationdtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','iBizHumanResources-HROrganization-searchDefault-all') and hasPermission(#context,'iBizHumanResources-HROrganization-Get')")
	@ApiOperation(value = "获取DEFAULT", tags = {"组织" } ,notes = "获取DEFAULT")
    @RequestMapping(method= RequestMethod.GET , value="/hrorganizations/fetchdefault")
	public ResponseEntity<List<HROrganizationDTO>> fetchDefault(HROrganizationSearchContext context) {
        Page<HROrganization> domains = hrorganizationService.searchDefault(context) ;
        List<HROrganizationDTO> list = hrorganizationMapping.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-HROrganization-searchDefault-all') and hasPermission(#context,'iBizHumanResources-HROrganization-Get')")
	@ApiOperation(value = "查询DEFAULT", tags = {"组织" } ,notes = "查询DEFAULT")
    @RequestMapping(method= RequestMethod.POST , value="/hrorganizations/searchdefault")
	public ResponseEntity<Page<HROrganizationDTO>> searchDefault(@RequestBody HROrganizationSearchContext context) {
        Page<HROrganization> domains = hrorganizationService.searchDefault(context) ;
	    return ResponseEntity.status(HttpStatus.OK)
                .body(new PageImpl(hrorganizationMapping.toDto(domains.getContent()), context.getPageable(), domains.getTotalElements()));
	}
}

