package com.xinelu.web.controller.applet;

import com.alibaba.fastjson2.JSONObject;
import com.github.pagehelper.PageInfo;
import com.xinelu.common.core.controller.BaseController;
import com.xinelu.common.core.domain.R;
import com.xinelu.common.core.page.TableDataInfo;
import com.xinelu.common.exception.ServiceException;
import com.xinelu.common.utils.http.HttpUtils;
import com.xinelu.common.utils.spring.SpringUtils;
import com.xinelu.familydoctor.applet.pojo.query.PrizeQuery;
import com.xinelu.familydoctor.applet.pojo.vo.PrizeVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

/**
 * @Author mengkuiliang
 * @Description 健康行为积分商城奖品控制器
 * @Date 2023-09-21 021 17:50
 * @Param
 * @return
 **/
@Api(tags = "健康行为积分商城奖品控制器")
@RestController
@RequestMapping("/applet/score/prize")
public class PrizeController extends BaseController {

    @ApiOperation("获取奖品列表")
    @GetMapping("/list")
    public TableDataInfo getPrizeList(PrizeQuery query, @RequestHeader("region") String region) {
        StringBuffer params = new StringBuffer();
        params.append("?identity=").append(query.getIdentity());
        params.append("&pageSize=").append(query.getPageSize() != null && query.getPageSize() > 0 ? query.getPageSize() : 15);
        params.append("&pageNum=").append(query.getPageNum() != null && query.getPageNum() > 0 ? query.getPageNum() : 1);
        String result = HttpUtils.sendGet(SpringUtils.getFdUrl(region) + "/prize/list" + params);
        JSONObject jsonObject = JSONObject.parseObject(result);
        if (!jsonObject.get("code").toString().equals("1")) {
            throw new ServiceException(jsonObject.get("msg").toString());
        }
        if (jsonObject.get("data") != null) {
            JSONObject jsonObject1 = (JSONObject) jsonObject.get("data");
            return getDataTable(jsonObject1.getJSONArray("list"), jsonObject1.getInteger("total"));
        }
        return getDataTable(null, 0);
    }

    @GetMapping("/getByPrizeId/{prizeId}")
    @ApiOperation("奖品详情")
    public R<PrizeVo> getByPrizeId(@PathVariable String prizeId, @RequestHeader("region") String region) {
        String result = HttpUtils.sendGet(SpringUtils.getFdUrl(region) + "/prize/getByPrizeId/" + prizeId);
        JSONObject jsonObject = JSONObject.parseObject(result);
        if (!jsonObject.get("code").toString().equals("1")) {
            return R.fail(jsonObject.get("msg").toString());
        }
        if (jsonObject.get("data") != null) {
            return R.ok(JSONObject.parseObject(jsonObject.getJSONObject("data").toJSONString(), PrizeVo.class));
        }
        return R.ok();
    }

    @GetMapping("/getImgById/{prizeId}")
    @ApiOperation("奖品图片查询")
    public R<String> getImgById(@PathVariable String prizeId, @RequestHeader("region") String region) {
        String result = HttpUtils.sendGet(SpringUtils.getFdUrl(region) + "/prize/getImgById/" + prizeId);
        JSONObject jsonObject = JSONObject.parseObject(result);
        if (!jsonObject.get("code").toString().equals("1")) {
            return R.fail(jsonObject.get("msg").toString());
        }
        if (jsonObject.get("data") != null) {
            return R.ok(jsonObject.getString("data"));
        }
        return R.ok();
    }
}
