package cn.ibizlab.assetmanagement.assetapi.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.assetmanagement.assetapi.dto.*;
import cn.ibizlab.assetmanagement.assetapi.mapping.*;
import cn.ibizlab.assetmanagement.core.asset.domain.EAMPRODUCT;
import cn.ibizlab.assetmanagement.core.asset.service.IEAMPRODUCTService;
import cn.ibizlab.assetmanagement.core.asset.filter.EAMPRODUCTSearchContext;
import cn.ibizlab.assetmanagement.util.annotation.VersionCheck;

@Slf4j
@Api(tags = {"产品" })
@RestController("AssetApi-eamproduct")
@RequestMapping("")
public class EAMPRODUCTResource {

    @Autowired
    public IEAMPRODUCTService eamproductService;

    @Autowired
    @Lazy
    public EAMPRODUCTMapping eamproductMapping;

    @PreAuthorize("hasPermission(this.eamproductMapping.toDomain(#eamproductdto),'iBizAssetManagement-EAMPRODUCT-Create')")
    @ApiOperation(value = "新建产品", tags = {"产品" },  notes = "新建产品")
	@RequestMapping(method = RequestMethod.POST, value = "/eamproducts")
    @Transactional
    public ResponseEntity<EAMPRODUCTDTO> create(@RequestBody EAMPRODUCTDTO eamproductdto) {
        EAMPRODUCT domain = eamproductMapping.toDomain(eamproductdto);
		eamproductService.create(domain);
        EAMPRODUCTDTO dto = eamproductMapping.toDto(domain);
		return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @PreAuthorize("hasPermission(this.eamproductMapping.toDomain(#eamproductdtos),'iBizAssetManagement-EAMPRODUCT-Create')")
    @ApiOperation(value = "批量新建产品", tags = {"产品" },  notes = "批量新建产品")
	@RequestMapping(method = RequestMethod.POST, value = "/eamproducts/batch")
    public ResponseEntity<Boolean> createBatch(@RequestBody List<EAMPRODUCTDTO> eamproductdtos) {
        eamproductService.createBatch(eamproductMapping.toDomain(eamproductdtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @VersionCheck(entity = "eamproduct" , versionfield = "updatedate")
    @PreAuthorize("hasPermission(this.eamproductService.get(#eamproduct_id),'iBizAssetManagement-EAMPRODUCT-Update')")
    @ApiOperation(value = "更新产品", tags = {"产品" },  notes = "更新产品")
	@RequestMapping(method = RequestMethod.PUT, value = "/eamproducts/{eamproduct_id}")
    @Transactional
    public ResponseEntity<EAMPRODUCTDTO> update(@PathVariable("eamproduct_id") String eamproduct_id, @RequestBody EAMPRODUCTDTO eamproductdto) {
		EAMPRODUCT domain  = eamproductMapping.toDomain(eamproductdto);
        domain .setProductid(eamproduct_id);
		eamproductService.update(domain );
		EAMPRODUCTDTO dto = eamproductMapping.toDto(domain );
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @PreAuthorize("hasPermission(this.eamproductService.getEamproductByEntities(this.eamproductMapping.toDomain(#eamproductdtos)),'iBizAssetManagement-EAMPRODUCT-Update')")
    @ApiOperation(value = "批量更新产品", tags = {"产品" },  notes = "批量更新产品")
	@RequestMapping(method = RequestMethod.PUT, value = "/eamproducts/batch")
    public ResponseEntity<Boolean> updateBatch(@RequestBody List<EAMPRODUCTDTO> eamproductdtos) {
        eamproductService.updateBatch(eamproductMapping.toDomain(eamproductdtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @PreAuthorize("hasPermission(this.eamproductService.get(#eamproduct_id),'iBizAssetManagement-EAMPRODUCT-Remove')")
    @ApiOperation(value = "删除产品", tags = {"产品" },  notes = "删除产品")
	@RequestMapping(method = RequestMethod.DELETE, value = "/eamproducts/{eamproduct_id}")
    @Transactional
    public ResponseEntity<Boolean> remove(@PathVariable("eamproduct_id") String eamproduct_id) {
         return ResponseEntity.status(HttpStatus.OK).body(eamproductService.remove(eamproduct_id));
    }

    @PreAuthorize("hasPermission(this.eamproductService.getEamproductByIds(#ids),'iBizAssetManagement-EAMPRODUCT-Remove')")
    @ApiOperation(value = "批量删除产品", tags = {"产品" },  notes = "批量删除产品")
	@RequestMapping(method = RequestMethod.DELETE, value = "/eamproducts/batch")
    public ResponseEntity<Boolean> removeBatch(@RequestBody List<String> ids) {
        eamproductService.removeBatch(ids);
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @PostAuthorize("hasPermission(this.eamproductMapping.toDomain(returnObject.body),'iBizAssetManagement-EAMPRODUCT-Get')")
    @ApiOperation(value = "获取产品", tags = {"产品" },  notes = "获取产品")
	@RequestMapping(method = RequestMethod.GET, value = "/eamproducts/{eamproduct_id}")
    public ResponseEntity<EAMPRODUCTDTO> get(@PathVariable("eamproduct_id") String eamproduct_id) {
        EAMPRODUCT domain = eamproductService.get(eamproduct_id);
        EAMPRODUCTDTO dto = eamproductMapping.toDto(domain);
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @ApiOperation(value = "获取产品草稿", tags = {"产品" },  notes = "获取产品草稿")
	@RequestMapping(method = RequestMethod.GET, value = "/eamproducts/getdraft")
    public ResponseEntity<EAMPRODUCTDTO> getDraft() {
        return ResponseEntity.status(HttpStatus.OK).body(eamproductMapping.toDto(eamproductService.getDraft(new EAMPRODUCT())));
    }

    @ApiOperation(value = "检查产品", tags = {"产品" },  notes = "检查产品")
	@RequestMapping(method = RequestMethod.POST, value = "/eamproducts/checkkey")
    public ResponseEntity<Boolean> checkKey(@RequestBody EAMPRODUCTDTO eamproductdto) {
        return  ResponseEntity.status(HttpStatus.OK).body(eamproductService.checkKey(eamproductMapping.toDomain(eamproductdto)));
    }

    @PreAuthorize("hasPermission(this.eamproductMapping.toDomain(#eamproductdto),'iBizAssetManagement-EAMPRODUCT-Save')")
    @ApiOperation(value = "保存产品", tags = {"产品" },  notes = "保存产品")
	@RequestMapping(method = RequestMethod.POST, value = "/eamproducts/save")
    public ResponseEntity<Boolean> save(@RequestBody EAMPRODUCTDTO eamproductdto) {
        return ResponseEntity.status(HttpStatus.OK).body(eamproductService.save(eamproductMapping.toDomain(eamproductdto)));
    }

    @PreAuthorize("hasPermission(this.eamproductMapping.toDomain(#eamproductdtos),'iBizAssetManagement-EAMPRODUCT-Save')")
    @ApiOperation(value = "批量保存产品", tags = {"产品" },  notes = "批量保存产品")
	@RequestMapping(method = RequestMethod.POST, value = "/eamproducts/savebatch")
    public ResponseEntity<Boolean> saveBatch(@RequestBody List<EAMPRODUCTDTO> eamproductdtos) {
        eamproductService.saveBatch(eamproductMapping.toDomain(eamproductdtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','iBizAssetManagement-EAMPRODUCT-searchDefault-all') and hasPermission(#context,'iBizAssetManagement-EAMPRODUCT-Get')")
	@ApiOperation(value = "获取DEFAULT", tags = {"产品" } ,notes = "获取DEFAULT")
    @RequestMapping(method= RequestMethod.GET , value="/eamproducts/fetchdefault")
	public ResponseEntity<List<EAMPRODUCTDTO>> fetchDefault(EAMPRODUCTSearchContext context) {
        Page<EAMPRODUCT> domains = eamproductService.searchDefault(context) ;
        List<EAMPRODUCTDTO> list = eamproductMapping.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','iBizAssetManagement-EAMPRODUCT-searchDefault-all') and hasPermission(#context,'iBizAssetManagement-EAMPRODUCT-Get')")
	@ApiOperation(value = "查询DEFAULT", tags = {"产品" } ,notes = "查询DEFAULT")
    @RequestMapping(method= RequestMethod.POST , value="/eamproducts/searchdefault")
	public ResponseEntity<Page<EAMPRODUCTDTO>> searchDefault(@RequestBody EAMPRODUCTSearchContext context) {
        Page<EAMPRODUCT> domains = eamproductService.searchDefault(context) ;
	    return ResponseEntity.status(HttpStatus.OK)
                .body(new PageImpl(eamproductMapping.toDto(domains.getContent()), context.getPageable(), domains.getTotalElements()));
	}
}

