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

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.ResponseCode;
import com.persagy.dmp.common.exception.BusinessException;
import com.persagy.dmp.common.model.response.CommonResult;
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.version.entity.ObjectRelationHistory;
import com.persagy.dmp.rwd.version.service.IObjectRelationHistoryService;
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;

/**
 * 对象关系历史记录管理
 * @author:linhuili
 * @date:2021/11/15
 */
@RestController
@RequestMapping("/rwd/instance/relation/his")
public class ObjectRelationHistoryController {

    @Autowired
    private IObjectRelationHistoryService service;

    /**
     * 查询对象关系历史记录
     * @param criteria
     * @return
     */
    @PostMapping("/query")
    public CommonResult<List<ObjectRelationHistory>> query(@RequestBody QueryCriteria criteria) {
        if(criteria == null) {
            throw new BusinessException(ResponseCode.A0400.getCode(), ResponseCode.A0400.getDesc());
        }
        QueryWrapper<ObjectRelationHistory> wrapper = new QueryWrapper<>();
        // 添加所属项目条件
        ConditionUtil.ensureProjectCriteria(wrapper);
        // 转换查询条件
        QueryCriteriaHelper.toWrapper(wrapper, criteria, ObjectRelationHistory.class);
        Page page = QueryCriteriaHelper.toPage(criteria);
        if(criteria.isOnlyCount()) {
            Integer total = service.queryCount(wrapper);
            page.setTotal(total);
        } else {
            page = service.queryByCondition(QueryCriteriaHelper.toPage(criteria), wrapper);
        }
        return ResultHelper.multi(page.getRecords(), page.getTotal());
    }

    /**
     * 添加对象关系历史记录
     * @param vo
     * @return
     */
    @PostMapping("/create")
    public CommonResult<ObjectRelationHistory> create(@Valid @RequestBody ObjectRelation vo) throws Exception{
        ObjectRelationHistory insert = service.insert(vo);
        return ResultHelper.single(insert);
    }

    /**
     * 修改对象关系历史记录
     * @param vo
     * @return
     */
    @PostMapping("/update")
    public CommonResult<ObjectRelationHistory> update(@Valid @RequestBody ObjectRelation vo) throws Exception{
        ObjectRelationHistory update = service.update(vo);
        return ResultHelper.single(update);
    }

    /**
     * 删除对象关系时，进行历史记录
     * @return
     */
    @PostMapping("/delete")
    public CommonResult<ObjectRelationHistory> delete(@Valid @RequestBody ObjectRelation vo) throws Exception{
        ObjectRelationHistory objHistory = service.delete(vo);
        return ResultHelper.single(objHistory);
    }

}
