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.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.ObjectInfoCollect;
import com.persagy.dmp.rwd.define.service.IObjectInfoCollectService;
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;

/**
 * 平台、项目级信息点采集
 * @author:linhuili
 * @date:2021/10/13
 */
@RestController
@RequestMapping("/rwd/def/funcid/collect")
public class ObjectInfoCollectController {

    @Autowired
    private IObjectInfoCollectService service;

    /**
     * 查询采集的信息点
     * @param criteria
     * @return
     */
    @PostMapping("/query")
    public CommonResult<List<ObjectInfoCollect>> query(@RequestBody QueryCriteria criteria) {
        if(criteria == null) {
            throw new BusinessException(ResponseCode.A0400.getCode(), ResponseCode.A0400.getDesc());
        }
        QueryWrapper<ObjectInfoCollect> wrapper = new QueryWrapper<>();
        // 转换查询条件
        QueryCriteriaHelper.toWrapper(wrapper, criteria, ObjectInfoCollect.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<ObjectInfoCollect> create(@Valid @RequestBody ObjectInfoCollect vo) throws Exception{
        List<ObjectInfoCollect> voList = CollUtil.newArrayList(vo);
        //创建默认条件
        createDefaultValue(voList);
        //新增信息点
        service.insert(voList);
        return ResultHelper.single(vo);
    }

    /**
     * 批量添加采集的信息点
     * @param voList
     * @return
     */
    @PostMapping("/createBatch")
    public CommonResult<List<ObjectInfoCollect>> createBatch(@Valid @RequestBody List<ObjectInfoCollect> voList) throws Exception{
        //创建默认条件
        createDefaultValue(voList);
        //批量新增信息点
        voList = service.insert(voList);
        return ResultHelper.multi(voList);
    }

    /**
     * 修改采集的信息点
     * @param vo
     * @return
     */
    @PostMapping("/update")
    public CommonResult<ObjectInfoCollect> update(@Valid @RequestBody ObjectInfoCollect vo) throws Exception{
        List<ObjectInfoCollect> voList = CollUtil.newArrayList(vo);
        //创建默认条件
        createDefaultValue(voList);
        //修改信息点
        service.update(voList);
        return ResultHelper.single(vo);
    }

    /**
     * 批量修改采集的信息点
     * @param voList
     * @return
     */
    @PostMapping("/updateBatch")
    public CommonResult<List<ObjectInfoCollect>> updateBatch(@Valid @RequestBody List<ObjectInfoCollect> voList) throws Exception{
        //创建默认条件
        createDefaultValue(voList);
        //修改信息点
        voList = service.update(voList);
        return ResultHelper.multi(voList);
    }

    /**
     * 删除采集的信息点
     * @param id
     * @return
     */
    @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();
    }

    /**
     * 批量删除采集的信息点
     * @param ids
     * @return
     */
    @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<ObjectInfoCollect> 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 (ObjectInfoCollect vo : voList) {
            if(StringUtils.isEmpty(vo.getGroupCode())){
                vo.setGroupCode(groupCode);
            }
            if(StringUtils.isEmpty(vo.getProjectId())){
                vo.setProjectId(projectId);
            }
            //默认有效标识
            if(vo.getValid() == null){
                vo.setValid(1);
            }
            if(vo.getKeyWord() == null){
                vo.setKeyWord(false);
            }
        }
    }

}
