package com.jwipc.sys.controller.pc;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.jwipc.sys.entity.ColumnSort;
import com.jwipc.sys.service.IColumnSortService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import static com.jwipc.sys.constant.ModuleConst.MODULE_ROOT;

/**
 * @author shenyang.ou
 * @date 2024/2/28 16:31
 * @desc
 */
@RequestMapping(MODULE_ROOT + "/columnSort/rest")
@RequiredArgsConstructor
@RestController
public class ColumnSortRestController {
    private final IColumnSortService columnSortService;
    /**
     * 获取列排序
     */
    @RequestMapping("/getData")
    public ResponseEntity getData(String code) {
        final Long loginId = StpUtil.getLoginIdAsLong();
        return ResponseEntity.ok(columnSortService.getOne(Wrappers.lambdaQuery(ColumnSort.class).eq(ColumnSort::getCode,code).eq(ColumnSort::getUserId, loginId).last(" limit 1")));
    }

    @RequestMapping("/deleteData")
    public ResponseEntity deleteData(String code) {
        final Long loginId = StpUtil.getLoginIdAsLong();
        return ResponseEntity.ok(columnSortService.remove(Wrappers.lambdaQuery(ColumnSort.class).eq(ColumnSort::getCode,code).eq(ColumnSort::getUserId, loginId)));
    }

    @RequestMapping("/saveData")
    public ResponseEntity saveData(String code,String columnSortData) {
        final Long loginId = StpUtil.getLoginIdAsLong();
        ColumnSort columnSort = columnSortService.getOne(Wrappers.lambdaQuery(ColumnSort.class).eq(ColumnSort::getCode,code).eq(ColumnSort::getUserId,loginId).last(" limit 1"));
        if (ObjectUtil.isNotNull(columnSort)){
            columnSort.setColumnSortData(columnSortData);
            columnSortService.updateById(columnSort);
        }else {
            ColumnSort columnSort1 = new ColumnSort();
            columnSort1.setCode(code);
            columnSort1.setUserId(loginId);
            columnSort1.setColumnSortData(columnSortData);
            columnSortService.save(columnSort1);
        }
        return ResponseEntity.ok(true);
    }

    @RequestMapping("/remove")
    public ResponseEntity remove(String code) {
        final Long loginId = StpUtil.getLoginIdAsLong();
        columnSortService.remove(Wrappers.lambdaQuery(ColumnSort.class).eq(ColumnSort::getCode,code).eq(ColumnSort::getUserId, loginId));
        return ResponseEntity.ok(true);
    }
}
