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.dto.PointHealthAct;
import com.xinelu.familydoctor.applet.pojo.query.DictHealthActQuery;
import com.xinelu.familydoctor.applet.pojo.query.PatientScoreQuery;
import com.xinelu.familydoctor.applet.pojo.vo.DictHealthActVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

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

    @ApiOperation(value = "获取居民总积分", notes = "region: (1: 德州 2：东营)")
    @GetMapping("/total/{cardNo}")
    public R<String> schemaList(@PathVariable String cardNo, @RequestHeader("region") String region) {
        String result = HttpUtils.sendGet(SpringUtils.getFdUrl(region) + "/patient/score/record/total/" + cardNo);
        JSONObject jsonObject = JSONObject.parseObject(result);
        if (!jsonObject.get("code").toString().equals("1")) {
            return R.fail(jsonObject.get("msg").toString());
        }
        return R.ok(jsonObject.getString("data"));
    }

    @ApiOperation("获取居民积分记录列表")
    @GetMapping("/list")
    public TableDataInfo schemaList(PatientScoreQuery 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) + "/patient/score/record/list" + params.toString());
        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("/healthAct/list")
    @ApiOperation("健康行为内容列表")
    public TableDataInfo healthActList(DictHealthActQuery query, @RequestHeader("region") String region) {
        StringBuffer params = new StringBuffer();
        params.append("?pageNum=").append(query.getPageNum() != null && query.getPageNum() > 0 ? query.getPageNum() : 1);
        params.append("&pageSize=").append(query.getPageSize() != null && query.getPageSize() > 0 ? query.getPageSize() : 15);
        params.append("&actId=").append(!StringUtils.isBlank(query.getActId()) ? query.getActId() : "");
        params.append("&targetGroups=").append(!StringUtils.isBlank(query.getTargetGroups()) ? query.getTargetGroups() : "");
        params.append("&actName=").append(!StringUtils.isBlank(query.getActName()) ? query.getActName() : "");
        params.append("&period=").append(!StringUtils.isBlank(query.getPeriod()) ? query.getPeriod() : "");
        params.append("&identity=").append(!StringUtils.isBlank(query.getIdentity()) ? query.getIdentity() : "");
        String result = HttpUtils.sendGet(SpringUtils.getFdUrl(region) + "/patient/score/list" + params.toString());
        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("/healthAct/detail/{actId}")
    @ApiOperation("健康行为积分明细")
    public R<PointHealthAct> healthActDetail(@PathVariable String actId, @RequestHeader("region") String region) {
        String result = HttpUtils.sendGet(SpringUtils.getFdUrl(region) + "/patient/score/detail/" + actId);
        JSONObject jsonObject = JSONObject.parseObject(result);
        if (!jsonObject.get("code").toString().equals("1")) {
            throw new ServiceException(jsonObject.get("msg").toString());
        }
        if (jsonObject.get("data") != null) {
            return R.ok(JSONObject.parseObject(jsonObject.getJSONObject("data").toJSONString(), PointHealthAct.class));
        }
        return R.ok();
    }

}
