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.HROperationUnit;
import cn.ibizlab.humanresource.core.base.service.IHROperationUnitService;
import cn.ibizlab.humanresource.core.base.filter.HROperationUnitSearchContext;
import cn.ibizlab.humanresource.util.annotation.VersionCheck;

@Slf4j
@Api(tags = {"运营单位" })
@RestController("HRApi-hroperationunit")
@RequestMapping("")
public class HROperationUnitResource {

    @Autowired
    public IHROperationUnitService hroperationunitService;

    @Autowired
    @Lazy
    public HROperationUnitMapping hroperationunitMapping;

    @PreAuthorize("hasPermission(this.hroperationunitMapping.toDomain(#hroperationunitdto),'iBizHumanResources-HROperationUnit-Create')")
    @ApiOperation(value = "新建运营单位", tags = {"运营单位" },  notes = "新建运营单位")
	@RequestMapping(method = RequestMethod.POST, value = "/hroperationunits")
    @Transactional
    public ResponseEntity<HROperationUnitDTO> create(@RequestBody HROperationUnitDTO hroperationunitdto) {
        HROperationUnit domain = hroperationunitMapping.toDomain(hroperationunitdto);
		hroperationunitService.create(domain);
        HROperationUnitDTO dto = hroperationunitMapping.toDto(domain);
		return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @PreAuthorize("hasPermission(this.hroperationunitMapping.toDomain(#hroperationunitdtos),'iBizHumanResources-HROperationUnit-Create')")
    @ApiOperation(value = "批量新建运营单位", tags = {"运营单位" },  notes = "批量新建运营单位")
	@RequestMapping(method = RequestMethod.POST, value = "/hroperationunits/batch")
    public ResponseEntity<Boolean> createBatch(@RequestBody List<HROperationUnitDTO> hroperationunitdtos) {
        hroperationunitService.createBatch(hroperationunitMapping.toDomain(hroperationunitdtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @VersionCheck(entity = "hroperationunit" , versionfield = "updatedate")
    @PreAuthorize("hasPermission(this.hroperationunitService.get(#hroperationunit_id),'iBizHumanResources-HROperationUnit-Update')")
    @ApiOperation(value = "更新运营单位", tags = {"运营单位" },  notes = "更新运营单位")
	@RequestMapping(method = RequestMethod.PUT, value = "/hroperationunits/{hroperationunit_id}")
    @Transactional
    public ResponseEntity<HROperationUnitDTO> update(@PathVariable("hroperationunit_id") String hroperationunit_id, @RequestBody HROperationUnitDTO hroperationunitdto) {
		HROperationUnit domain  = hroperationunitMapping.toDomain(hroperationunitdto);
        domain .setOperationunitid(hroperationunit_id);
		hroperationunitService.update(domain );
		HROperationUnitDTO dto = hroperationunitMapping.toDto(domain );
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @PreAuthorize("hasPermission(this.hroperationunitService.getHroperationunitByEntities(this.hroperationunitMapping.toDomain(#hroperationunitdtos)),'iBizHumanResources-HROperationUnit-Update')")
    @ApiOperation(value = "批量更新运营单位", tags = {"运营单位" },  notes = "批量更新运营单位")
	@RequestMapping(method = RequestMethod.PUT, value = "/hroperationunits/batch")
    public ResponseEntity<Boolean> updateBatch(@RequestBody List<HROperationUnitDTO> hroperationunitdtos) {
        hroperationunitService.updateBatch(hroperationunitMapping.toDomain(hroperationunitdtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @PreAuthorize("hasPermission(this.hroperationunitService.get(#hroperationunit_id),'iBizHumanResources-HROperationUnit-Remove')")
    @ApiOperation(value = "删除运营单位", tags = {"运营单位" },  notes = "删除运营单位")
	@RequestMapping(method = RequestMethod.DELETE, value = "/hroperationunits/{hroperationunit_id}")
    @Transactional
    public ResponseEntity<Boolean> remove(@PathVariable("hroperationunit_id") String hroperationunit_id) {
         return ResponseEntity.status(HttpStatus.OK).body(hroperationunitService.remove(hroperationunit_id));
    }

    @PreAuthorize("hasPermission(this.hroperationunitService.getHroperationunitByIds(#ids),'iBizHumanResources-HROperationUnit-Remove')")
    @ApiOperation(value = "批量删除运营单位", tags = {"运营单位" },  notes = "批量删除运营单位")
	@RequestMapping(method = RequestMethod.DELETE, value = "/hroperationunits/batch")
    public ResponseEntity<Boolean> removeBatch(@RequestBody List<String> ids) {
        hroperationunitService.removeBatch(ids);
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @PostAuthorize("hasPermission(this.hroperationunitMapping.toDomain(returnObject.body),'iBizHumanResources-HROperationUnit-Get')")
    @ApiOperation(value = "获取运营单位", tags = {"运营单位" },  notes = "获取运营单位")
	@RequestMapping(method = RequestMethod.GET, value = "/hroperationunits/{hroperationunit_id}")
    public ResponseEntity<HROperationUnitDTO> get(@PathVariable("hroperationunit_id") String hroperationunit_id) {
        HROperationUnit domain = hroperationunitService.get(hroperationunit_id);
        HROperationUnitDTO dto = hroperationunitMapping.toDto(domain);
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @ApiOperation(value = "获取运营单位草稿", tags = {"运营单位" },  notes = "获取运营单位草稿")
	@RequestMapping(method = RequestMethod.GET, value = "/hroperationunits/getdraft")
    public ResponseEntity<HROperationUnitDTO> getDraft() {
        return ResponseEntity.status(HttpStatus.OK).body(hroperationunitMapping.toDto(hroperationunitService.getDraft(new HROperationUnit())));
    }

    @ApiOperation(value = "检查运营单位", tags = {"运营单位" },  notes = "检查运营单位")
	@RequestMapping(method = RequestMethod.POST, value = "/hroperationunits/checkkey")
    public ResponseEntity<Boolean> checkKey(@RequestBody HROperationUnitDTO hroperationunitdto) {
        return  ResponseEntity.status(HttpStatus.OK).body(hroperationunitService.checkKey(hroperationunitMapping.toDomain(hroperationunitdto)));
    }

    @PreAuthorize("hasPermission(this.hroperationunitMapping.toDomain(#hroperationunitdto),'iBizHumanResources-HROperationUnit-Save')")
    @ApiOperation(value = "保存运营单位", tags = {"运营单位" },  notes = "保存运营单位")
	@RequestMapping(method = RequestMethod.POST, value = "/hroperationunits/save")
    public ResponseEntity<Boolean> save(@RequestBody HROperationUnitDTO hroperationunitdto) {
        return ResponseEntity.status(HttpStatus.OK).body(hroperationunitService.save(hroperationunitMapping.toDomain(hroperationunitdto)));
    }

    @PreAuthorize("hasPermission(this.hroperationunitMapping.toDomain(#hroperationunitdtos),'iBizHumanResources-HROperationUnit-Save')")
    @ApiOperation(value = "批量保存运营单位", tags = {"运营单位" },  notes = "批量保存运营单位")
	@RequestMapping(method = RequestMethod.POST, value = "/hroperationunits/savebatch")
    public ResponseEntity<Boolean> saveBatch(@RequestBody List<HROperationUnitDTO> hroperationunitdtos) {
        hroperationunitService.saveBatch(hroperationunitMapping.toDomain(hroperationunitdtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','iBizHumanResources-HROperationUnit-searchDefault-all') and hasPermission(#context,'iBizHumanResources-HROperationUnit-Get')")
	@ApiOperation(value = "获取DEFAULT", tags = {"运营单位" } ,notes = "获取DEFAULT")
    @RequestMapping(method= RequestMethod.GET , value="/hroperationunits/fetchdefault")
	public ResponseEntity<List<HROperationUnitDTO>> fetchDefault(HROperationUnitSearchContext context) {
        Page<HROperationUnit> domains = hroperationunitService.searchDefault(context) ;
        List<HROperationUnitDTO> list = hroperationunitMapping.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-HROperationUnit-searchDefault-all') and hasPermission(#context,'iBizHumanResources-HROperationUnit-Get')")
	@ApiOperation(value = "查询DEFAULT", tags = {"运营单位" } ,notes = "查询DEFAULT")
    @RequestMapping(method= RequestMethod.POST , value="/hroperationunits/searchdefault")
	public ResponseEntity<Page<HROperationUnitDTO>> searchDefault(@RequestBody HROperationUnitSearchContext context) {
        Page<HROperationUnit> domains = hroperationunitService.searchDefault(context) ;
	    return ResponseEntity.status(HttpStatus.OK)
                .body(new PageImpl(hroperationunitMapping.toDto(domains.getContent()), context.getPageable(), domains.getTotalElements()));
	}
}

