package com.persagy.dmp.rwd.define.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.ObjectNode;
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.context.AppContext;
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.define.entity.ObjectTypeDefine;
import com.persagy.dmp.mybatis.utils.ConditionUtil;
import com.persagy.dmp.basic.utils.QueryCriteriaHelper;
import com.persagy.dmp.rwd.define.service.IObjectTypeService;
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-06-25
 */
@RestController
@RequestMapping("/rwd/def/class")
public class ObjectTypeController {

    @Autowired
    private IObjectTypeService service;

    @PostMapping("")
    public CommonResult<List<ObjectTypeDefine>> query(@RequestBody QueryCriteria criteria) {
        if(criteria == null) {
            throw new BusinessException(ResponseCode.A0400.getCode(), ResponseCode.A0400.getDesc());
        }
        // 2021年8月18日20:06:24 兼容旧的查询
        ObjectNode objCriteria = criteria.getCriteria();
        if (null!=objCriteria && objCriteria.size()>0){
            objCriteria.remove(CommonConstant.TYPE);
        }
        QueryWrapper<ObjectTypeDefine> wrapper = new QueryWrapper<>();
        // 添加所属项目条件
        ConditionUtil.ensureProjectCriteria(wrapper);
        // 转换查询条件
        QueryCriteriaHelper.toWrapper(wrapper, criteria, ObjectTypeDefine.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("/create")
	public CommonResult<ObjectTypeDefine> create(@Valid @RequestBody ObjectTypeDefine vo){
        List<ObjectTypeDefine> voList = CollUtil.newArrayList(vo);
        //创建默认条件
        createDefaultValue(voList);
        //新增类型定义
        service.insert(voList);
		return ResultHelper.single(vo);
	}

    @PostMapping("/createBatch")
    public CommonResult<List<ObjectTypeDefine>> createBatch(@Valid @RequestBody List<ObjectTypeDefine> voList){
        //创建默认条件
        createDefaultValue(voList);
        //新增类型定义
        voList = service.insert(voList);
        return ResultHelper.multi(voList);
    }

	@PostMapping("/update")
	public CommonResult<ObjectTypeDefine> update(@Valid @RequestBody ObjectTypeDefine vo){
        List<ObjectTypeDefine> voList = CollUtil.newArrayList(vo);
        //创建默认条件
        createDefaultValue(voList);
        //修改类型定义
        service.update(voList);
		return ResultHelper.single(vo);
	}

    @PostMapping("/updateBatch")
    public CommonResult<List<ObjectTypeDefine>> updateBatch(@Valid @RequestBody List<ObjectTypeDefine> voList){
        //创建默认条件
        createDefaultValue(voList);
        //修改类型定义
        voList = service.update(voList);
        return ResultHelper.multi(voList);
    }

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

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

    /**
     * 创建默认条件
     * @param voList
     */
    private void createDefaultValue(List<ObjectTypeDefine> 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 (ObjectTypeDefine vo : voList) {
            vo.setGroupCode(groupCode);
            vo.setProjectId(projectId);
        }
    }
}
