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.basic.utils.QueryCriteriaHelper;
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.ObjectRelationProjectCal;
import com.persagy.dmp.mybatis.utils.ConditionUtil;
import com.persagy.dmp.rwd.digital.service.RelationProjectCalService;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang.StringUtils;
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;

/***
 * Description: 关系计算入口
 * @author : lijie
 * @date :2021/9/2 21:19
 * Update By lijie 2021/9/2 21:19
 */
@RestController
@RequestMapping("/rwd/instance/relation/project/cal/")
@RequiredArgsConstructor
public class RelationProjectCalController {

    private final RelationProjectCalService service;

    @PostMapping("query")
    public CommonResult<List<ObjectRelationProjectCal>> 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<ObjectRelationProjectCal> wrapper = new QueryWrapper<>();
        // 添加所属项目条件
        ConditionUtil.ensureProjectCriteriaDirectly(wrapper);
        // 转换查询条件
        QueryCriteriaHelper.toWrapper(wrapper, criteria, ObjectRelationProjectCal.class);
        Page page = QueryCriteriaHelper.toPage(criteria);
        if(criteria.isOnlyCount()) {
            int total = service.count(wrapper);
            page.setTotal(total);
        } else {
            page = service.page(QueryCriteriaHelper.toPage(criteria), wrapper);
        }
        return ResultHelper.multi(page.getRecords(), page.getTotal());
    }

    @PostMapping("/createOne")
    public CommonResult<ObjectRelationProjectCal> create(@Valid @RequestBody ObjectRelationProjectCal vo){
        //基础参数校验
        ParamCheckUtil.checkParam(CommonConstant.QUERY_GROUPCODE,CommonConstant.QUERY_PROJECTID);
        List<ObjectRelationProjectCal> voList = CollUtil.newArrayList(vo);
        //创建默认条件
        createDefaultValue(voList);
        //新增对象关系
        service.saveBatch(voList);
        return ResultHelper.single(vo);
    }

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

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

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

    @PostMapping("/deleteOne")
    @Deprecated
    public CommonResult delete(@RequestBody String id){
        if(StringUtils.isEmpty(id)){
            throw new BusinessException(ResponseCode.A0400.getCode(), "id不能为空");
        }
        service.removeById(id);
        return ResultHelper.success();
    }

    @PostMapping("/delete")
    @Deprecated
    public CommonResult deleteBatch(@RequestBody List<String> ids){
        if(ids == null || ids.size()<=0){
            throw new BusinessException(ResponseCode.A0400.getCode(), "id不能为空");
        }
        service.removeByIds(ids);
        return ResultHelper.success();
    }

    /**
     * 创建默认条件
     * @param voList
     */
    private void createDefaultValue(List<ObjectRelationProjectCal> 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 (ObjectRelationProjectCal vo : voList) {
            vo.setProjectId(projectId);
            if(vo.getValid() == null) {
                vo.setValid(ValidEnum.TRUE.getType());
            }
        }
    }




}
