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.ObjectDigital;
import com.persagy.dmp.mybatis.utils.ConditionUtil;
import com.persagy.dmp.rwd.version.entity.ObjectDigitalHistory;
import com.persagy.dmp.rwd.version.service.IObjectDigitalHistoryService;
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/object/his")
public class ObjectDigitalHistoryController {

    @Autowired
    private IObjectDigitalHistoryService service;

    /**
     * 查询对象历史记录
     * @param criteria
     * @return
     */
    @PostMapping("/query")
    public CommonResult<List<ObjectDigitalHistory>> query(@RequestBody QueryCriteria criteria) {
        if(criteria == null) {
            throw new BusinessException(ResponseCode.A0400.getCode(), ResponseCode.A0400.getDesc());
        }
        QueryWrapper<ObjectDigitalHistory> wrapper = new QueryWrapper<>();
        // 添加所属项目条件
        ConditionUtil.ensureProjectCriteria(wrapper);
        // 转换查询条件
        QueryCriteriaHelper.toWrapper(wrapper, criteria, ObjectDigitalHistory.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<ObjectDigitalHistory> create(@Valid @RequestBody ObjectDigital vo) throws Exception{
        ObjectDigitalHistory insert = service.insert(vo);
        return ResultHelper.single(insert);
    }

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

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



}
