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

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.persagy.dmp.basic.model.QueryCriteria;
import com.persagy.dmp.basic.utils.JsonNodeUtils;
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.mybatis.utils.ConditionUtil;
import com.persagy.dmp.rwd.digital.entity.ObjectDigital;
import com.persagy.dmp.rwd.digital.service.IObjectDigitalService;
import com.persagy.dmp.rwd.digital.service.ProjectBusinessService;
import com.persagy.dmp.rwd.digital.utils.ObjectDigitalCriteriaHelper;
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/object")
public class ObjectDigitalController{

    @Autowired
    private IObjectDigitalService service;
    @Autowired
    private ProjectBusinessService projectBusinessService;

    @PostMapping("/query")
    public CommonResult<List<ObjectNode>> query(@RequestBody QueryCriteria criteria) {
        if(criteria == null) {
            throw new BusinessException(ResponseCode.A0400.getCode(), ResponseCode.A0400.getDesc());
        }
        //基础参数校验
        ParamCheckUtil.checkParam(CommonConstant.QUERY_GROUPCODE,CommonConstant.QUERY_PROJECTID);

        QueryWrapper<ObjectDigital> wrapper = new QueryWrapper<>();
        // 添加所属项目条件
        ConditionUtil.ensureProjectCriteria(wrapper);
        // 处理扩展条件
        ObjectDigitalCriteriaHelper.ensureExtraCond(criteria.getCriteria());
        // 转换查询条件
        QueryCriteriaHelper.toWrapper(wrapper, criteria);
        Page page = QueryCriteriaHelper.toPage(criteria);
        if(criteria.isOnlyCount()) {
            Integer total = service.queryCount(wrapper);
            page.setTotal(total);
        } else {
            page = service.queryByCondition(QueryCriteriaHelper.toPage(criteria), wrapper);
        }
        // 处理扩展属性
        List<ObjectNode> records = JsonNodeUtils.toListNode(page.getRecords(), ObjectDigital.EXTRA_COLUMN, criteria.getWithColumns());
        return ResultHelper.multi(records, page.getTotal());
    }

    @PostMapping("/createOne")
    public CommonResult<ObjectDigital> create(@Valid @RequestBody ObjectNode node){
        //基础参数校验
        ParamCheckUtil.checkParam(CommonConstant.QUERY_GROUPCODE,CommonConstant.QUERY_PROJECTID);

        ObjectDigital vo = JsonNodeUtils.toEntity(node, ObjectDigital.class, ObjectDigital.EXTRA_COLUMN);
        List<ObjectDigital> voList = CollUtil.newArrayList(vo);
        service.insert(voList);
        return ResultHelper.single(vo);
    }

    @PostMapping("/create")
    public CommonResult<List<ObjectDigital>> createBatch(@Valid @RequestBody ArrayNode array){
        //基础参数校验
        ParamCheckUtil.checkParam(CommonConstant.QUERY_GROUPCODE,CommonConstant.QUERY_PROJECTID);

        List<ObjectDigital> voList = JsonNodeUtils.toEntity(array, ObjectDigital.class, ObjectDigital.EXTRA_COLUMN);
        voList = service.insert(voList);
        return ResultHelper.multi(voList);
    }

    @PostMapping("/updateOne")
    public CommonResult<ObjectDigital> update(@Valid @RequestBody ObjectNode node){
        //基础参数校验
        ParamCheckUtil.checkParam(CommonConstant.QUERY_GROUPCODE,CommonConstant.QUERY_PROJECTID);

        ObjectDigital vo = JsonNodeUtils.toEntity(node, ObjectDigital.class, ObjectDigital.EXTRA_COLUMN);
        List<ObjectDigital> voList = CollUtil.newArrayList(vo);
        service.update(voList);
        return ResultHelper.single(vo);
    }

    @PostMapping("/update")
    public CommonResult<List<ObjectDigital>> updateBatch(@Valid @RequestBody ArrayNode array){
        //基础参数校验
        ParamCheckUtil.checkParam(CommonConstant.QUERY_GROUPCODE,CommonConstant.QUERY_PROJECTID);

        List<ObjectDigital> voList = JsonNodeUtils.toEntity(array, ObjectDigital.class, ObjectDigital.EXTRA_COLUMN);
        voList = service.update(voList);
        return ResultHelper.multi(voList);
    }

    @PostMapping("/deleteOne")
    public CommonResult delete(@RequestBody String id){
        service.delete(CollUtil.newArrayList(id));
        return ResultHelper.success();
    }

    @PostMapping("/delete")
    public CommonResult deleteBatch(@RequestBody List<String> id){
        service.delete(id);
        return ResultHelper.success();
    }
    /***
     * Description: 根据项目id集合批量查询项目信息
     * @param idList : 项目id集合
     * @return : com.persagy.dmp.common.model.response.CommonResult<com.fasterxml.jackson.databind.node.ObjectNode>
     * @author : lijie
     * @date :2021/8/18 15:55
     * Update By lijie 2021/8/18 15:55
     */
    @PostMapping("/batchQueryProjectByIds")
    public CommonResult<List<ObjectDigital>> batchQueryProjectByIds(@RequestBody List<String> idList) {
        //基础参数校验
        ParamCheckUtil.checkParam(CommonConstant.QUERY_GROUPCODE);
        return projectBusinessService.batchQueryProjectByIds(idList);
    }

}
