package com.ruoyi.busi.controller;

import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.busi.domain.BusiSubscribe;
import com.ruoyi.busi.service.IBusiSubscribeService;
import com.ruoyi.busi.vo.SubscribeDataObj;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.DlBaseEntity;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 订阅Controller
 *
 * @author pqz
 * @date 2025-04-01
 */
@RestController
@RequestMapping("/busi/subscribe")
public class BusiSubscribeController extends BaseController {
    @Autowired
    private IBusiSubscribeService busiSubscribeService;

    /**
     * 根据用户id查询订阅内容
     *
     * @return com.ruoyi.common.core.domain.AjaxResult
     * @author PQZ
     * @date 10:29 2025/4/1
     **/
    @GetMapping(value = "/getUserSub")
    public AjaxResult getInfo() {
        Long userId = SecurityUtils.getUserId();
        LambdaQueryWrapper<BusiSubscribe> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(BusiSubscribe::getUserId, userId)
                .eq(DlBaseEntity::getDelFlag, 0);
        BusiSubscribe subscribe = busiSubscribeService.getOne(lambdaQueryWrapper);
        return success(subscribe);
    }


    /**
     * 保存订阅内容
     *
     * @param busiSubscribe {@link BusiSubscribe}
     * @return com.ruoyi.common.core.domain.AjaxResult
     * @author PQZ
     * @date 10:30 2025/4/1
     **/
    @Log(title = "订阅", businessType = BusinessType.INSERT)
    @PostMapping(value = "/save")
    public AjaxResult save(@RequestBody BusiSubscribe busiSubscribe) {
        Long userId = SecurityUtils.getUserId();
        busiSubscribe.setUserId(userId);
        SubscribeDataObj subscribeDataObj = JSONObject.parseObject(busiSubscribe.getJsonObj(), SubscribeDataObj.class);
        busiSubscribe.setNewNotice(subscribeDataObj.getNewNotice()?"1":"0");
        busiSubscribe.setForkNotice(subscribeDataObj.getForkNotice()?"1":"0");
        busiSubscribeService.saveOrUpdate(busiSubscribe);
        return success();
    }
}
