package shop.alien.store.controller;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiOperationSupport;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import shop.alien.entity.result.R;
import shop.alien.entity.store.LifeUserExpertWorks;
import shop.alien.mapper.LifeUserExpertWorksMapper;
import shop.alien.store.service.LifeUserExpertWorksService;

import java.util.Map;
import java.util.Objects;

/**
 * <p>
 * 达人作品表 前端控制器
 * </p>
 *
 * @author ssk
 * @since 2025-02-19
 */
@Api(tags = {"达人—作品"})
@Slf4j
@CrossOrigin
@RestController
@RequestMapping("/lifeUserExpertWorks")
@RequiredArgsConstructor
public class LifeUserExpertWorksController {

    private final LifeUserExpertWorksMapper lifeUserExpertWorksMapper;

    private final LifeUserExpertWorksService lifeUserExpertWorksService;

    @ApiOperation("用户对作品进行播放点赞评论分享埋点")
    @ApiOperationSupport(order = 1)
    @PostMapping("/updateWorksCount")
    public Integer updateWorksCount(
            @RequestBody LifeUserExpertWorks lifeUserExpertWorks) {
        // 输入验证
        if (Objects.isNull(lifeUserExpertWorks) || Objects.isNull(lifeUserExpertWorks.getId())) {
            log.error("传入的 LifeUserExpertWorks 对象或其 id 为空，无法进行更新操作");
            return 0;
        }
        log.info("LifeUserExpertWorksController.updateWorksCount?updateWorksCount={}", lifeUserExpertWorks);

        String countColumn = getCountColumn(lifeUserExpertWorks);
        if (countColumn == null) {
            log.error("未找到有效的计数类型，无法进行更新操作，传入的对象：{}", lifeUserExpertWorks);
            return 0;
        }

        LambdaUpdateWrapper<LifeUserExpertWorks> wrapper = Wrappers.lambdaUpdate();
        wrapper.eq(LifeUserExpertWorks::getId, lifeUserExpertWorks.getId());
        wrapper.setSql(countColumn + " = " + countColumn + " + 1");

        try {
            int result = lifeUserExpertWorksMapper.update(null, wrapper);
            if (result > 0) {
                log.info("作品 ID 为 {} 的 {} 计数更新成功", lifeUserExpertWorks.getId(), countColumn);
            } else {
                log.warn("作品 ID 为 {} 的 {} 计数更新失败", lifeUserExpertWorks.getId(), countColumn);
            }
            return result;
        } catch (Exception e) {
            log.error("更新作品计数时发生异常，作品 ID：{}，计数类型：{}", lifeUserExpertWorks.getId(), countColumn, e);
            return 0;
        }
    }

    // 根据传入的对象确定要更新的计数列名
    private String getCountColumn(LifeUserExpertWorks lifeUserExpertWorks) {
        if (lifeUserExpertWorks.getPlayCount() != null) {
            return "play_count";
        } else if (lifeUserExpertWorks.getLikeCount() != null) {
            return "like_count";
        } else if (lifeUserExpertWorks.getCommentCount() != null) {
            return "comment_count";
        } else if (lifeUserExpertWorks.getShareCount() != null) {
            return "share_count";
        }
        return null;
    }

    @ApiOperation("商户查看推广订单")
    @ApiOperationSupport(order = 2)
    @GetMapping("/getExpertOrderByStoreId")
    public R<Map<String, Object>> getExpertOrderByStoreId(@RequestParam("storeId") Long storeId) {
        log.info("StoreUserExpertOrderController.getExpertOrderByStoreId?id={}", storeId);
        return R.data(lifeUserExpertWorksService.getExpertOrderByStoreId(storeId));
    }

}
