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.ResponseCode;
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.ObjectInfoDefine;
import com.persagy.dmp.mybatis.utils.ConditionUtil;
import com.persagy.dmp.rwd.define.service.IObjectInfoService;
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/funcid")
public class ObjectInfoController {

    @Autowired
    private IObjectInfoService service;

    @PostMapping("")
    public CommonResult<List<ObjectInfoDefine>> query(@RequestBody QueryCriteria criteria) {
        if(criteria == null) {
            throw new BusinessException(ResponseCode.A0400.getCode(), ResponseCode.A0400.getDesc());
        }
        QueryWrapper<ObjectInfoDefine> wrapper = new QueryWrapper<>();
        // 添加所属项目条件
        ConditionUtil.ensureProjectCriteria(wrapper);
        // 转换查询条件
        QueryCriteriaHelper.toWrapper(wrapper, criteria);
        Page page = QueryCriteriaHelper.toPage(criteria);
        // 没有排序条件时，按默认条件排序
        if(CollUtil.isEmpty(page.getOrders())) {
            wrapper.orderByAsc("show_order");
        }
        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<ObjectInfoDefine> create(@Valid @RequestBody ObjectInfoDefine vo){
        List<ObjectInfoDefine> voList = CollUtil.newArrayList(vo);
        service.insert(voList);
        return ResultHelper.single(vo);
    }

    @PostMapping("/createBatch")
    public CommonResult<List<ObjectInfoDefine>> createBatch(@Valid @RequestBody List<ObjectInfoDefine> voList){
        voList = service.insert(voList);
        return ResultHelper.multi(voList);
    }

    @PostMapping("/update")
    public CommonResult<ObjectInfoDefine> update(@Valid @RequestBody ObjectInfoDefine vo){
        List<ObjectInfoDefine> voList = CollUtil.newArrayList(vo);
        service.update(voList);
        return ResultHelper.single(vo);
    }

    @PostMapping("/updateBatch")
    public CommonResult<List<ObjectInfoDefine>> updateBatch(@Valid @RequestBody List<ObjectInfoDefine> voList){
        voList = service.update(voList);
        return ResultHelper.multi(voList);
    }

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

    @PostMapping("/deleteBatch")
    public CommonResult deleteBatch(@RequestBody List<String> id){
        service.delete(id);
        return ResultHelper.success();
    }
}
