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.HttpService;
import com.xinelu.common.utils.http.HttpUtils;
import com.xinelu.common.utils.spring.SpringUtils;
import com.xinelu.familydoctor.applet.pojo.body.PrizeExchangeBody;
import com.xinelu.familydoctor.applet.pojo.query.PrizeQuery;
import com.xinelu.familydoctor.applet.pojo.vo.PrizeExchangeVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @Author mengkuiliang
 * @Description 积分兑换记录控制器
 * @Date 2023-09-21 021 17:50
 * @Param
 * @return
 **/
@Api(tags = "积分兑换记录控制器")
@RestController
@RequestMapping("/applet/score/prizeExchange")
public class PrizeExchangeController extends BaseController {
    @Resource
    private HttpService httpService;

    @ApiOperation("兑换奖品")
    @PostMapping("/save")
    public R<String> save(@RequestBody PrizeExchangeBody body, @RequestHeader("region") String region) {
        if (StringUtils.isBlank(body.getIdentity())) {
            return R.fail("请求参数异常");
        }
        JSONObject params = new JSONObject();
        params.put("identity", body.getIdentity());
        params.put("prizeId", body.getPrizeId());
        JSONObject jsonObject = httpService.post(SpringUtils.getFdUrl(region) + "/prizeExchange/insert", null, params);
        if (!jsonObject.get("code").toString().equals("1")) {
            return R.fail(jsonObject.get("msg").toString());
        }
        return R.ok();
    }

    @ApiOperation("查询兑换记录列表")
    @GetMapping("/list")
    public TableDataInfo getList(PrizeQuery query, @RequestHeader("region") String region) {
        StringBuffer params = new StringBuffer();
        params.append("?identity=").append(query.getIdentity());
        if (!StringUtils.isBlank(query.getStatus())) {
            params.append("&status=").append(query.getStatus());
        }
        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) + "/prizeExchange/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("/getById/{exchangeId}")
    @ApiOperation("兑换记录详情")
    public R<PrizeExchangeVo> getById(@PathVariable String exchangeId, @RequestHeader("region") String region) {
        String result = HttpUtils.sendGet(SpringUtils.getFdUrl(region) + "/prizeExchange/getById/" + exchangeId);
        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(), PrizeExchangeVo.class));
        }
        return R.ok();
    }
}
