package com.persagy.dmp.rwd.digital.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.StrUtil;
import com.persagy.dmp.basic.dto.RequestData;
import com.persagy.dmp.common.constant.ResponseCode;
import com.persagy.dmp.common.exception.BusinessException;
import com.persagy.dmp.common.model.response.CommonResult;
import com.persagy.dmp.digital.entity.ObjectRelation;
import com.persagy.dmp.rwd.digital.service.RelationCalService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/***
 * Description: 关系计算入口
 * @author : lijie
 * @date :2021/9/2 21:19
 * Update By lijie 2021/9/2 21:19
 */
@RestController
@RequestMapping("/rwd/instance/object/compatible/")
@RequiredArgsConstructor
public class RelationCaclController {

    private final RelationCalService relationCaclService;

    /***
     * Description: 根据id,localId,name,localName,CADID及关系类型信息,对象分类信息创建关系
     * @param requestData : 请求参数
     * @return : com.persagy.dmp.common.model.response.CommonResult<java.util.List<com.persagy.dmp.digital.entity.ObjectRelation>>
     * @author : lijie
     * @date :2021/9/2 20:20
     * Update By lijie 2021/9/2 20:20
     */
    @PostMapping("createObjRelationsByKeywordAndObjType")
    public CommonResult<List<ObjectRelation>> createObjRelationsByKeywordAndObjType(@RequestBody RequestData requestData){
        // 校验必填项
        if (StrUtil.isBlank(requestData.getGraphCode())
                || StrUtil.isBlank(requestData.getRelCode())
                || StrUtil.isBlank(requestData.getMasterObjType())
                || StrUtil.isBlank(requestData.getSlaveObjType())
                || StrUtil.isBlank(requestData.getType())
                || StrUtil.isBlank(requestData.getMainContent())
                || CollUtil.isEmpty(requestData.getSlaveContent())){
            throw new BusinessException(ResponseCode.A0400.getCode(),ResponseCode.A0400.getDesc());
        }
        if (StrUtil.isNotBlank(requestData.getSign()) && !NumberUtil.isInteger(requestData.getSign())){
            throw new BusinessException(ResponseCode.A0400.getCode(),ResponseCode.A0400.getDesc());
        }
        return relationCaclService.createObjRelationsByKeywordAndObjType(requestData);
    }
    /***
     * Description: 根据fromId,toId,relType,graphCode删除关系
     * @param objectRelation : 请求参数
     * @return : com.persagy.dmp.common.model.response.CommonResult<java.util.List<com.persagy.dmp.digital.entity.ObjectRelation>>
     * @author : lijie
     * @date :2021/9/2 20:20
     * Update By lijie 2021/9/2 20:20
     */
    @PostMapping("deleteObjRelationsByRelId")
    public CommonResult<List<ObjectRelation>> deleteObjRelationsByRelId(@Validated @RequestBody ObjectRelation objectRelation){
        return relationCaclService.deleteObjRelationsByRelId(objectRelation);
    }
    /***
     * Description: 关系计算接口
     * @param requestData : 请求参数
     * @return : com.persagy.dmp.common.model.response.CommonResult<java.util.List<com.persagy.dmp.digital.entity.ObjectRelation>>
     * @author : lijie
     * @date :2021/9/2 20:20
     * Update By lijie 2021/9/2 20:20
     */
    @PostMapping("calculatingObjRelation")
    public CommonResult<List<ObjectRelation>> calculatingObjRelation(@RequestBody RequestData requestData){
        if (StrUtil.isBlank(requestData.getGraphCode())
                || StrUtil.isBlank(requestData.getRelCode())){
            throw new BusinessException(ResponseCode.A0400.getCode(),ResponseCode.A0400.getDesc());
        }
        return relationCaclService.calculatingObjRelation(requestData);
    }




}
