package shop.alien.second.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import shop.alien.entity.result.R;
import shop.alien.entity.second.SecondGoods;
import shop.alien.entity.second.vo.SecondGoodsVo;
import shop.alien.entity.second.vo.SellGoodsVo;
import shop.alien.second.service.SecondGoodsService;
import shop.alien.util.common.JwtUtil;

/**
 * 二手商品控制器
 */
@Slf4j
@Api(tags = {"二手平台-商品管理-我的相关"})
@ApiSort(1)
@CrossOrigin
@RestController
@RequestMapping("/secondGoods")
@RequiredArgsConstructor
public class UserGoodsController {

    private final SecondGoodsService secondGoodsService;

    /**
     * 获取用户屏蔽的商品列表（分页）
     */
    @GetMapping("/getShieldedGoodsPage")
    @ApiOperation("获取用户屏蔽的商品列表（分页）")
    @ApiImplicitParams({@ApiImplicitParam(name = "pageNum", value = "分页页数", dataType = "Integer", paramType = "query", required = true),
            @ApiImplicitParam(name = "pageSize", value = "分页条数", dataType = "Integer", paramType = "query", required = true)})
    public R<IPage<SecondGoodsVo>> getShieldedGoodsPage(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        log.info("SecondGoodsController.getShieldedGoodsPage?page={},size={},", pageNum,pageSize);
        R<IPage<SecondGoodsVo>> result = new R<>();
        IPage<SecondGoodsVo> page = new Page<>(pageNum, pageSize);
        JSONObject data = JwtUtil.getCurrentUserInfo();
        if (null != data) {
            int userId = data.getInteger("userId");
            result = R.data(secondGoodsService.getShieldedGoodsPage(page,userId));
        }
        return result;
    }

    /**
     * 我收藏的商品列表-分页
     */
    @GetMapping("/getCollectGoodsPage")
    @ApiOperation("我收藏的商品列表-分页")
    @ApiImplicitParams({@ApiImplicitParam(name = "pageNum", value = "分页页数", dataType = "Integer", paramType = "query", required = true),
            @ApiImplicitParam(name = "pageSize", value = "分页条数", dataType = "Integer", paramType = "query", required = true)})
    public R<IPage<SecondGoodsVo>> getCollectGoodsPage(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        log.info("SecondGoodsController.getCollectGoodsPage?page={},size={},", pageNum,pageSize);
        R<IPage<SecondGoodsVo>> result = new R<>();
        IPage<SecondGoodsVo> page = new Page<>(pageNum,pageSize);
        JSONObject data = JwtUtil.getCurrentUserInfo();
        if (null != data) {
            int userId = data.getInteger("userId");
            result = R.data(secondGoodsService.getCollectGoodsPage(page,userId));
        }
        return result;
    }

    /**
     * 我的商品-个人主页用户发布的商品
     */
    @GetMapping("/getMyGoodsPage")
    @ApiOperation("我的商品-个人主页用户发布的商品")
    @ApiImplicitParams({@ApiImplicitParam(name = "pageNum", value = "分页页数", dataType = "Integer", paramType = "query", required = true),
            @ApiImplicitParam(name = "pageSize", value = "分页条数", dataType = "Integer", paramType = "query", required = true)})
    public R<IPage<SecondGoodsVo>> getMyGoodsPage(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        log.info("SecondGoodsController.getMyGoodsPage?page={},size={},", pageNum,pageSize);
        R<IPage<SecondGoodsVo>> result = new R<>();
        IPage<SecondGoodsVo> page = new Page<>(pageNum,pageSize);
        JSONObject data = JwtUtil.getCurrentUserInfo();
        if (null != data) {
            int userId = data.getInteger("userId");
            result = R.data(secondGoodsService.getMyGoodsPage(page,userId));
        }
        return result;
    }

    /**
     * 获取商品草稿列表
     */
    @GetMapping("/getDraftList")
    @ApiOperation("获取商品草稿列表")
    @ApiImplicitParams({@ApiImplicitParam(name = "pageNum", value = "分页页数", dataType = "Integer", paramType = "query", required = true),
            @ApiImplicitParam(name = "pageSize", value = "分页条数", dataType = "Integer", paramType = "query", required = true)})
    public R<IPage<SecondGoodsVo>> getDraftList(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        log.info("SecondGoodsController.getDraftList?page={},size={},", pageNum,pageSize);
        R<IPage<SecondGoodsVo>> result = new R<>();
        IPage<SecondGoodsVo> page = new Page<>(pageNum,pageSize);
        JSONObject data = JwtUtil.getCurrentUserInfo();
        if (null != data) {
            int userId = data.getInteger("userId");
            result = R.data(secondGoodsService.getDraftList(page,userId));
        }
        return result;
    }

    /**
     * 获取我的商品草稿数量
     */
    @GetMapping("/getDraftCount")
    @ApiOperation("获取我的商品草稿数量")
    public R<Integer> getDraftCount() {
        JSONObject data = JwtUtil.getCurrentUserInfo();
        R<Integer> result = new R<>();
        // 获取商品草稿数量
        QueryWrapper<SecondGoods> queryWrapper = new QueryWrapper<>();
        if (null != data) {
            int userId = data.getInteger("userId");
            queryWrapper.lambda()
                    .eq(SecondGoods::getDeleteFlag, 0)
                    .eq(SecondGoods::getUserId, userId)
                    .eq(SecondGoods::getGoodsStatus, 0); // 0-草稿
            return R.data(secondGoodsService.count(queryWrapper));
        }
        return result;
    }

    /**
     * 我购买的商品列表-分页
     */
    @GetMapping("/getBuyGoodsPage")
    @ApiOperation("我购买的商品列表-分页")
    @ApiImplicitParams({@ApiImplicitParam(name = "pageNum", value = "分页页数", dataType = "Integer", paramType = "query", required = true),
            @ApiImplicitParam(name = "pageSize", value = "分页条数", dataType = "Integer", paramType = "query", required = true)})
    public R<IPage<SecondGoodsVo>> getBuyGoodsPage(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        log.info("SecondGoodsController.getDraftList?page={},size={},", pageNum,pageSize);
        R<IPage<SecondGoodsVo>> result = new R<>();
        IPage<SecondGoodsVo> page = new Page<>(pageNum, pageSize);
        JSONObject data = JwtUtil.getCurrentUserInfo();
        if (null != data) {
            int userId = data.getInteger("userId");
            result = R.data(secondGoodsService.getBuyGoodsPage(page,userId));
        }
        return result;
    }

    /**
     * 他的商品-个人主页用户发布的商品
     */
    @GetMapping("/getUserGoodsPage")
    @ApiOperation("他的商品-个人主页用户发布的商品")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "主页用户id", dataType = "Integer", paramType = "query", required = true),
            @ApiImplicitParam(name = "currentLongitude", value = "经度", dataType = "Double", paramType = "query", required = true),
            @ApiImplicitParam(name = "currentLatitude", value = "纬度", dataType = "Double", paramType = "query", required = true),
            @ApiImplicitParam(name = "pageNum", value = "分页页数", dataType = "Integer", paramType = "query", required = true),
            @ApiImplicitParam(name = "pageSize", value = "分页条数", dataType = "Integer", paramType = "query", required = true)})
    public R<IPage<SecondGoodsVo>> getUserGoodsPage(
            @RequestParam Integer userId,
            @RequestParam Double currentLongitude,
            @RequestParam Double currentLatitude,
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        log.info("SecondGoodsController.getDraftList?userId={},currentLongitude={},currentLatitude={},page={},size={},",userId,currentLongitude,currentLatitude, pageNum,pageSize);
        IPage<SecondGoodsVo> page = new Page<>(pageNum, pageSize);
        return R.data(secondGoodsService.getUserGoodsPage(page,currentLatitude,currentLongitude, userId));
    }


    /**
     * 我的 - 点赞商品列表
     */
    @GetMapping("/getLikeGoodsPage")
    @ApiOperation("我的 - 点赞商品列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "分页页数", dataType = "Integer", paramType = "query", required = true),
            @ApiImplicitParam(name = "pageSize", value = "分页条数", dataType = "Integer", paramType = "query", required = true)})
    public R<IPage<SecondGoodsVo>> getLikeGoodsPage(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        log.info("SecondGoodsController.getLikeGoodsPage?page={},size={},", pageNum,pageSize);
        IPage<SecondGoodsVo> page = new Page<>(pageNum, pageSize);
        R<IPage<SecondGoodsVo>> result = new R<>();
        JSONObject data = JwtUtil.getCurrentUserInfo();
        if (null != data) {
            int userId = data.getInteger("userId");
            String phone = data.getString("phone");
            result = R.data(secondGoodsService.getLikeGoodsPage(page,userId,phone));
        }
        return result;
    }

    /**
     * 我的- 交易列表
     */
    @GetMapping("/getTransactionList")
    @ApiOperation("我的- 交易列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "分页页数", dataType = "Integer", paramType = "query", required = true),
            @ApiImplicitParam(name = "pageSize", value = "分页条数", dataType = "Integer", paramType = "query", required = true)})
    public R<IPage<SellGoodsVo>> getTransactionList(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        log.info("SecondGoodsController.getTransactionList?page={},size={},", pageNum,pageSize);
        IPage<SellGoodsVo> page = new Page<>(pageNum, pageSize);
        R<IPage<SellGoodsVo>> result = new R<>();
        JSONObject data = JwtUtil.getCurrentUserInfo();
        if (null != data) {
            Integer userId = data.getInteger("userId");
            result = R.data(secondGoodsService.getTransactionList(page,userId));
        }
        return result;
    }

}