package com.qnfhq.modules.c2c.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.qnfhq.annotation.RepeatSubmit;
import com.qnfhq.common.constant.Constant;
import com.qnfhq.common.page.PageData;
import com.qnfhq.common.utils.MessageUtils;
import com.qnfhq.common.utils.Result;
import com.qnfhq.common.validator.ValidatorUtils;
import com.qnfhq.modules.c2c.dto.UserFollowsDTO;
import com.qnfhq.modules.c2c.dto.UserFollowsListDTO;
import com.qnfhq.modules.c2c.entity.C2cMerchantEntity;
import com.qnfhq.modules.c2c.entity.UserFollowsEntity;
import com.qnfhq.modules.c2c.service.C2cMerchantService;
import com.qnfhq.modules.c2c.service.UserFollowsService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;


/**
 * c2c用户和商家关注关系
 *
 * @author yelz 30262728@qq.com
 * @since 1.0.0 2025-11-29
 */
@RestController
@RequestMapping("/follows")
@Tag(name="c2c用户和商家关注关系")
public class UserFollowsController {
    @Resource
    private UserFollowsService userFollowsService;
    @Resource
    private C2cMerchantService c2cMerchantService;


    @PostMapping("/followList")
    @Operation(summary = "已关注列表")
    public Result<PageData<UserFollowsEntity>> followList(@RequestBody UserFollowsListDTO dto){
        ValidatorUtils.validateEntity(dto);
        Map<String, Object> params = new HashMap<String, Object>();
        params.put(Constant.PAGE, dto.getPageNum());
        params.put(Constant.LIMIT, dto.getPageSize());
        params.put(Constant.ORDER_FIELD, "id");
        params.put(Constant.ORDER, "desc");
        params.put("followerId", StpUtil.getLoginIdAsLong());
        PageData<UserFollowsEntity> page = userFollowsService.selectPageByFollowerId(params);
        page.getList().forEach(item -> item.setFollowingInfo(c2cMerchantService.getById(item.getFollowingId())));

        return new Result<PageData<UserFollowsEntity>>().ok(page);
    }

    @PostMapping("/followMeList")
    @Operation(summary = "关注我列表")
    public Result<PageData<UserFollowsEntity>> followMeList(@RequestBody UserFollowsListDTO dto){
        ValidatorUtils.validateEntity(dto);
        Map<String, Object> params = new HashMap<String, Object>();
        params.put(Constant.PAGE, dto.getPageNum());
        params.put(Constant.LIMIT, dto.getPageSize());
        params.put(Constant.ORDER_FIELD, "id");
        params.put(Constant.ORDER, "desc");
        params.put("followingId", StpUtil.getLoginIdAsLong());
        PageData<UserFollowsEntity> page = userFollowsService.selectPageByFollowingId(params);
        page.getList().forEach(item -> item.setFollowerInfo(c2cMerchantService.getById(item.getFollowerId())));

        return new Result<PageData<UserFollowsEntity>>().ok(page);
    }

    @PostMapping("/follow")
    @Operation(summary = "关注")
    @RepeatSubmit(interval = 3000, message = "请求过于频繁")
    public Result follow(@RequestBody UserFollowsDTO dto){
        //效验数据
        ValidatorUtils.validateEntity(dto);

        Long followingId = dto.getFollowingId();
        C2cMerchantEntity merchant = c2cMerchantService.getById(followingId);
        if (merchant==null) {
            return new Result().error(MessageUtils.message("c2c.follow.merchant.notexists"));//"商家不存在"
        }
        if(merchant.getUserId().longValue() == StpUtil.getLoginIdAsLong()) {
            return new Result().error(MessageUtils.message("c2c.follow.self"));//"不能关注自己"
        }

        UserFollowsEntity userFollowsExist = userFollowsService.selectByFollowerIdFollowingId(StpUtil.getLoginIdAsLong(),dto.getFollowingId());
        if(userFollowsExist!=null) {
            return new Result();
        } else {
            UserFollowsEntity userFollows = new UserFollowsEntity();
            userFollows.setFollowerId(StpUtil.getLoginIdAsLong());
            userFollows.setFollowingId(dto.getFollowingId());
            userFollows.setCreateTime(new Date());
            userFollowsService.insert(userFollows);
            return new Result();
        }
    }

    @PostMapping("/unfollow")
    @Operation(summary = "取消关注")
    @RepeatSubmit(interval = 3000, message = "请求过于频繁")
    public Result unfollow(@RequestBody UserFollowsDTO dto){
        //效验数据
        ValidatorUtils.validateEntity(dto);

        userFollowsService.deleteByFollowerIdFollowingId(StpUtil.getLoginIdAsLong(),dto.getFollowingId());

        return new Result();
    }



}
