package cn.sh.stc.sict.theme.common.controller;

import cn.sh.stc.sict.cloud.common.core.util.NumberUtil;
import cn.sh.stc.sict.cloud.common.core.util.R;
import cn.sh.stc.sict.theme.common.model.ThemeTableColumnInfo;
import cn.sh.stc.sict.theme.common.service.ThemeTableColumnInfoService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;


/**
 * 糖尿病-字段表
 *
 * @author F_xh。
 * @date 2020-07-24 13:54:35
 */
@RestController
@RequestMapping("/table/column/info")
@AllArgsConstructor
@Slf4j
@Api(tags = "主题库表字段控制器")
public class ThemeTableColumnInfoController {

    private ThemeTableColumnInfoService themeTableColumnInfoService;

    @ApiOperation("根据表 id 查询表字段 list")
    @GetMapping("/list")
    public R listByTableId(@RequestParam("tableId") Long tableId) {
        LambdaQueryWrapper<ThemeTableColumnInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ThemeTableColumnInfo::getTableId, tableId)
                .orderByDesc(ThemeTableColumnInfo::getSort);
        List<ThemeTableColumnInfo> list = themeTableColumnInfoService.list(wrapper);

        return new R<>(list);
    }

    @ApiOperation("修改字段显示/查询配置")
    @PutMapping
    @Transactional
    public R updateById(@RequestBody List<ThemeTableColumnInfo> list) {
        AtomicBoolean flag = new AtomicBoolean(false);
        list.forEach(c -> {
            if (NumberUtil.isNullOrZero(c.getId())) {
                flag.set(true);
                return;
            }
        });
        if (flag.get()) {
            log.error("参数错误!!, {}", list.toString());
            return new R().error("参数错误！");
        }
        themeTableColumnInfoService.updateBatchById(list);
        return new R<>();
    }

}
