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.HRDuty;
import cn.ibizlab.humanresource.core.humanresource.service.IHRDutyService;
import cn.ibizlab.humanresource.core.humanresource.filter.HRDutySearchContext;
import cn.ibizlab.humanresource.util.annotation.VersionCheck;

@Slf4j
@Api(tags = {"职务" })
@RestController("HRApi-hrduty")
@RequestMapping("")
public class HRDutyResource {

    @Autowired
    public IHRDutyService hrdutyService;

    @Autowired
    @Lazy
    public HRDutyMapping hrdutyMapping;

    @PreAuthorize("hasPermission(this.hrdutyMapping.toDomain(#hrdutydto),'iBizHumanResources-HRDuty-Create')")
    @ApiOperation(value = "新建职务", tags = {"职务" },  notes = "新建职务")
	@RequestMapping(method = RequestMethod.POST, value = "/hrduties")
    @Transactional
    public ResponseEntity<HRDutyDTO> create(@RequestBody HRDutyDTO hrdutydto) {
        HRDuty domain = hrdutyMapping.toDomain(hrdutydto);
		hrdutyService.create(domain);
        HRDutyDTO dto = hrdutyMapping.toDto(domain);
		return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @PreAuthorize("hasPermission(this.hrdutyMapping.toDomain(#hrdutydtos),'iBizHumanResources-HRDuty-Create')")
    @ApiOperation(value = "批量新建职务", tags = {"职务" },  notes = "批量新建职务")
	@RequestMapping(method = RequestMethod.POST, value = "/hrduties/batch")
    public ResponseEntity<Boolean> createBatch(@RequestBody List<HRDutyDTO> hrdutydtos) {
        hrdutyService.createBatch(hrdutyMapping.toDomain(hrdutydtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @VersionCheck(entity = "hrduty" , versionfield = "updatedate")
    @PreAuthorize("hasPermission(this.hrdutyService.get(#hrduty_id),'iBizHumanResources-HRDuty-Update')")
    @ApiOperation(value = "更新职务", tags = {"职务" },  notes = "更新职务")
	@RequestMapping(method = RequestMethod.PUT, value = "/hrduties/{hrduty_id}")
    @Transactional
    public ResponseEntity<HRDutyDTO> update(@PathVariable("hrduty_id") String hrduty_id, @RequestBody HRDutyDTO hrdutydto) {
		HRDuty domain  = hrdutyMapping.toDomain(hrdutydto);
        domain .setHrdutyid(hrduty_id);
		hrdutyService.update(domain );
		HRDutyDTO dto = hrdutyMapping.toDto(domain );
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @PreAuthorize("hasPermission(this.hrdutyService.getHrdutyByEntities(this.hrdutyMapping.toDomain(#hrdutydtos)),'iBizHumanResources-HRDuty-Update')")
    @ApiOperation(value = "批量更新职务", tags = {"职务" },  notes = "批量更新职务")
	@RequestMapping(method = RequestMethod.PUT, value = "/hrduties/batch")
    public ResponseEntity<Boolean> updateBatch(@RequestBody List<HRDutyDTO> hrdutydtos) {
        hrdutyService.updateBatch(hrdutyMapping.toDomain(hrdutydtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

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

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

    @PostAuthorize("hasPermission(this.hrdutyMapping.toDomain(returnObject.body),'iBizHumanResources-HRDuty-Get')")
    @ApiOperation(value = "获取职务", tags = {"职务" },  notes = "获取职务")
	@RequestMapping(method = RequestMethod.GET, value = "/hrduties/{hrduty_id}")
    public ResponseEntity<HRDutyDTO> get(@PathVariable("hrduty_id") String hrduty_id) {
        HRDuty domain = hrdutyService.get(hrduty_id);
        HRDutyDTO dto = hrdutyMapping.toDto(domain);
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @ApiOperation(value = "获取职务草稿", tags = {"职务" },  notes = "获取职务草稿")
	@RequestMapping(method = RequestMethod.GET, value = "/hrduties/getdraft")
    public ResponseEntity<HRDutyDTO> getDraft() {
        return ResponseEntity.status(HttpStatus.OK).body(hrdutyMapping.toDto(hrdutyService.getDraft(new HRDuty())));
    }

    @ApiOperation(value = "检查职务", tags = {"职务" },  notes = "检查职务")
	@RequestMapping(method = RequestMethod.POST, value = "/hrduties/checkkey")
    public ResponseEntity<Boolean> checkKey(@RequestBody HRDutyDTO hrdutydto) {
        return  ResponseEntity.status(HttpStatus.OK).body(hrdutyService.checkKey(hrdutyMapping.toDomain(hrdutydto)));
    }

    @PreAuthorize("hasPermission(this.hrdutyMapping.toDomain(#hrdutydto),'iBizHumanResources-HRDuty-Save')")
    @ApiOperation(value = "保存职务", tags = {"职务" },  notes = "保存职务")
	@RequestMapping(method = RequestMethod.POST, value = "/hrduties/save")
    public ResponseEntity<Boolean> save(@RequestBody HRDutyDTO hrdutydto) {
        return ResponseEntity.status(HttpStatus.OK).body(hrdutyService.save(hrdutyMapping.toDomain(hrdutydto)));
    }

    @PreAuthorize("hasPermission(this.hrdutyMapping.toDomain(#hrdutydtos),'iBizHumanResources-HRDuty-Save')")
    @ApiOperation(value = "批量保存职务", tags = {"职务" },  notes = "批量保存职务")
	@RequestMapping(method = RequestMethod.POST, value = "/hrduties/savebatch")
    public ResponseEntity<Boolean> saveBatch(@RequestBody List<HRDutyDTO> hrdutydtos) {
        hrdutyService.saveBatch(hrdutyMapping.toDomain(hrdutydtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

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

