/*
 * Copyright [2022] [https://www.baiduc.com]
 *
 * Snowy采用APACHE LICENSE 2.0开源协议，您在使用过程中，需要注意以下几点：
 *
 * 1.请不要删除和修改根目录下的LICENSE文件。
 * 2.请不要删除和修改Snowy源码头部的版权声明。
 * 3.本项目代码可免费商业使用，商业使用请保留源码和相关描述文件的项目出处，作者声明等。
 * 4.分发源码时候，请注明软件出处 https://www.baiduc.com
 * 5.不可二次分发开源参与同类竞品，如有想法可联系团队xiaonuobase@qq.com商议合作。
 * 6.若您的项目无法满足以上几点，需要更多功能代码，获取Snowy商业授权许可，请在官网购买授权，地址为 https://www.baiduc.com
 */
package vip.xiaonuo.erp.controller;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import vip.xiaonuo.auth.core.pojo.SaBaseLoginUser;
import vip.xiaonuo.auth.core.util.StpLoginUserUtil;
import vip.xiaonuo.common.pojo.CommonResult;
import vip.xiaonuo.common.pojo.CommonValidList;
import vip.xiaonuo.erp.entity.SysBaseFilterInfo;
import vip.xiaonuo.erp.param.SysBaseFilterInfoAddParam;
import vip.xiaonuo.erp.param.SysBaseFilterInfoEditParam;
import vip.xiaonuo.erp.param.SysBaseFilterInfoIdParam;
import vip.xiaonuo.erp.param.SysBaseFilterInfoPageParam;
import vip.xiaonuo.erp.service.SysBaseFilterInfoService;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;

/**
 * 保存每个用户的查询选择控制器
 *
 * @author 1
 * @date  2025/03/01 16:53
 */
@Api(tags = "保存每个用户的查询选择控制器")
@ApiSupport(author = "SNOWY_TEAM", order = 1)
@RestController
@Validated
public class SysBaseFilterInfoController {

    @Resource
    private SysBaseFilterInfoService sysBaseFilterInfoService;


    @ApiOperationSupport(order = 3)
    @ApiOperation("编辑用户查询选择")
    @PostMapping("/biz/basefilterinfo/save")
    public synchronized CommonResult<String> edit(@RequestBody  SysBaseFilterInfo sysBaseFilterInfo) {
        SaBaseLoginUser loginUser = StpLoginUserUtil.getLoginUser();
//        String orgId = sysBaseFilterInfo.getOrgId();
//        if (StringUtils.isBlank(orgId)){
//            orgId= "0";
//        }

        SysBaseFilterInfo one = sysBaseFilterInfoService.getOne(new QueryWrapper<SysBaseFilterInfo>()
                .eq("module_type", sysBaseFilterInfo.getModuleType())
                .eq("user_id", loginUser.getId()));
        if (ObjectUtil.isEmpty(one)) {
            sysBaseFilterInfo.setUserId(loginUser.getId());
            sysBaseFilterInfoService.save(sysBaseFilterInfo);
        } else {
            sysBaseFilterInfo.setId(one.getId());
            sysBaseFilterInfoService.updateById(sysBaseFilterInfo);
        }
        return CommonResult.ok();
    }
    @ApiOperationSupport(order = 3)
    @ApiOperation("查询用户查询选择")
    @PostMapping("/biz/basefilterinfo/listByUser")
    public CommonResult<SysBaseFilterInfo> listByUser(@RequestBody  SysBaseFilterInfo sysBaseFilterInfo) {
//        SaBaseLoginUser loginUser = (SaBaseLoginUser)StpUtil.getSession().get("loginUser");
        SaBaseLoginUser loginUser = StpLoginUserUtil.getLoginUser();
//        String orgId = sysBaseFilterInfo.getOrgId();
//        if (StringUtils.isBlank(orgId)){
//            orgId= "0";
//        }
        SysBaseFilterInfo one = sysBaseFilterInfoService.getOne(new QueryWrapper<SysBaseFilterInfo>()
                .eq("module_type", sysBaseFilterInfo.getModuleType())
                .eq("user_id", loginUser.getId()));
        return CommonResult.data(one);
    }





}
