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.persagy.dmp.basic.model.QueryCriteria;
import com.persagy.dmp.common.constant.CommonConstant;
import com.persagy.dmp.common.constant.ResponseCode;
import com.persagy.dmp.common.constant.ValidEnum;
import com.persagy.dmp.common.context.AppContext;
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.basic.utils.QueryCriteriaHelper;
import com.persagy.dmp.rwd.digital.service.IObjectRelationService;
import org.apache.commons.lang.StringUtils;
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());
        }
        //基础参数校验
        // ParamCheckUtil.checkParam(CommonConstant.QUERY_GROUPCODE,CommonConstant.QUERY_PROJECTID);
        // 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.queryCount(wrapper);
            page.setTotal(total);
        } else {
            page = service.queryByCondition(QueryCriteriaHelper.toPage(criteria), 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);
        List<ObjectRelation> voList = CollUtil.newArrayList(vo);
        //创建默认条件
        createDefaultValue(voList);
        //新增对象关系
        service.insert(voList);
        return ResultHelper.single(vo);
    }

    @PostMapping("/create")
    public CommonResult<List<ObjectRelation>> createBatch(@Valid @RequestBody List<ObjectRelation> voList){
        //基础参数校验
        ParamCheckUtil.checkParam(CommonConstant.QUERY_GROUPCODE,CommonConstant.QUERY_PROJECTID);
        //创建默认条件
        createDefaultValue(voList);
        voList = service.insert(voList);
        //批量新增对象关系
        return ResultHelper.multi(voList);
    }

    @PostMapping("/updateOne")
    public CommonResult<ObjectRelation> update(@Valid @RequestBody ObjectRelation vo){
        //基础参数校验
        ParamCheckUtil.checkParam(CommonConstant.QUERY_GROUPCODE,CommonConstant.QUERY_PROJECTID);
        List<ObjectRelation> voList = CollUtil.newArrayList(vo);
        //修改默认条件
        createDefaultValue(voList);
        //修改对象关系
        service.update(voList);
        return ResultHelper.single(vo);
    }

    @PostMapping("/update")
    public CommonResult<List<ObjectRelation>> updateBatch(@Valid @RequestBody List<ObjectRelation> voList){
        //基础参数校验
        ParamCheckUtil.checkParam(CommonConstant.QUERY_GROUPCODE,CommonConstant.QUERY_PROJECTID);
        //修改默认条件
        createDefaultValue(voList);
        //批量修改对象关系
        voList = service.update(voList);
        return ResultHelper.multi(voList);
    }

    /*@PostMapping("/deleteOne")
    public CommonResult delete(@RequestBody String id){
        service.delete(CollUtil.newArrayList(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,delList.size());
    }

    /**
     * 创建默认条件
     * @param voList
     */
    private void createDefaultValue(List<ObjectRelation> voList){
        String groupCode = AppContext.getContext().getGroupCode();
        if(StringUtils.isEmpty(groupCode)){
            //默认集团
            groupCode = CommonConstant.DEFAULT_ID;
        }
        String projectId = AppContext.getContext().getProjectId();
        if(StringUtils.isEmpty(projectId)){
            //默认项目
            projectId = CommonConstant.DEFAULT_ID;
        }
        for (ObjectRelation vo : voList) {
            vo.setGroupCode(groupCode);
            vo.setProjectId(projectId);
            if(vo.getValid() == null) {
                vo.setValid(ValidEnum.TRUE.getType());
            }
        }
    }
}
