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

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.persagy.dmp.basic.model.QueryCriteria;
import com.persagy.dmp.basic.utils.QueryCriteriaHelper;
import com.persagy.dmp.common.constant.CommonConstant;
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.common.utils.ParamCheckUtil;
import com.persagy.dmp.common.utils.ResultHelper;
import com.persagy.dmp.digital.entity.ObjectRelation;
import com.persagy.dmp.mybatis.utils.ConditionUtil;
import com.persagy.dmp.rwd.digital.service.IObjectRelationService;
import org.springframework.beans.factory.annotation.Autowired;
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 javax.validation.Valid;
import java.util.List;

/**
 * 关系数据 Controller
 * @author Charlie Yu
 * @date 2021-07-08
 */
@RestController
@RequestMapping("/rwd/instance/relation")
public class ObjectRelationController {

    @Autowired
    private IObjectRelationService service;

    @PostMapping("query")
    public CommonResult<List<ObjectRelation>> query(@RequestBody QueryCriteria criteria) {
        if(criteria == null) {
            throw new BusinessException(ResponseCode.A0400.getCode(), ResponseCode.A0400.getDesc());
        }
        // 2021年9月8日18:23:25 放开单项目限制
        ParamCheckUtil.checkParam(CommonConstant.QUERY_GROUPCODE);
        QueryWrapper<ObjectRelation> wrapper = new QueryWrapper<>();
        // 添加所属项目条件
        ConditionUtil.ensureProjectCriteriaDirectly(wrapper);
        // 转换查询条件
        QueryCriteriaHelper.toWrapper(wrapper, criteria, ObjectRelation.class);
        Page page = QueryCriteriaHelper.toPage(criteria);
        if(criteria.isOnlyCount()) {
            Integer total = service.count(wrapper);
            page.setTotal(total);
        } else {
            page = service.page(page, wrapper);
        }
        return ResultHelper.multi(page.getRecords(), page.getTotal());
    }

    @PostMapping("/createOne")
    public CommonResult<ObjectRelation> create(@Valid @RequestBody ObjectRelation vo){
        ParamCheckUtil.checkParam(CommonConstant.QUERY_GROUPCODE,CommonConstant.QUERY_PROJECTID);
        service.save(CollUtil.newArrayList(vo));
        return ResultHelper.single(vo);
    }

    @PostMapping("/create")
    public CommonResult<List<ObjectRelation>> createBatch(@Valid @RequestBody List<ObjectRelation> voList){
        ParamCheckUtil.checkParam(CommonConstant.QUERY_GROUPCODE,CommonConstant.QUERY_PROJECTID);
        service.save(voList);
        return ResultHelper.multi(voList);
    }

    @PostMapping("/updateOne")
    public CommonResult<ObjectRelation> update(@Valid @RequestBody ObjectRelation vo) {
        ParamCheckUtil.checkParam(CommonConstant.QUERY_GROUPCODE,CommonConstant.QUERY_PROJECTID);
        service.save(CollUtil.newArrayList(vo));
        return ResultHelper.single(vo);
    }

    @PostMapping("/update")
    public CommonResult<List<ObjectRelation>> updateBatch(@Valid @RequestBody List<ObjectRelation> voList){
        ParamCheckUtil.checkParam(CommonConstant.QUERY_GROUPCODE,CommonConstant.QUERY_PROJECTID);
        service.save(voList);
        return ResultHelper.multi(voList);
    }

    @PostMapping("/deleteOne")
    public CommonResult delete(@RequestBody String id){
        if(StrUtil.isBlank(id)) {
            throw new BusinessException(ResponseCode.A0400.getCode(), ResponseCode.A0400.getDesc());
        }
        service.removeById(id);
        return ResultHelper.success();
    }

    /**
     * 删除关系
     * @param criteria
     * @return
     */
    @PostMapping("/delete")
    public CommonResult<List<ObjectRelation>> deleteBatch(@RequestBody QueryCriteria criteria){
        if(criteria == null) {
            throw new BusinessException(ResponseCode.A0400.getCode(), ResponseCode.A0400.getDesc());
        }
        QueryWrapper<ObjectRelation> wrapper = new QueryWrapper<>();
        // 添加所属项目条件
        ConditionUtil.ensureProjectCriteriaDirectly(wrapper);
        // 转换查询条件
        QueryCriteriaHelper.toWrapper(wrapper, criteria, ObjectRelation.class);
        List<ObjectRelation> delList = service.delete(wrapper);
        return ResultHelper.multi(delList);
    }
}
