package com.xinelu.mobile.controller.appletpersoncenter;

import com.xinelu.common.core.controller.BaseController;
import com.xinelu.common.core.domain.AjaxResult;
import com.xinelu.common.core.page.TableDataInfo;
import com.xinelu.mobile.dto.appletpersoncenter.HealthRecordDTO;
import com.xinelu.mobile.dto.appletpersoncenter.UnbindRequestDTO;
import com.xinelu.mobile.dto.verifysmscode.VerifySmsCodeDTO;
import com.xinelu.mobile.service.appletpersoncenter.AppletPersonCenterService;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Description 院后小程序个人中心控制器
 * @Author 纪寒
 * @Date 2024-04-16 10:49:14
 * @Version 1.0
 */
@RestController
@RequestMapping("/postDischargeApplet")
public class AppletPersonCenterController extends BaseController {

    @Resource
    private AppletPersonCenterService appletPersonCenterService;

    /**
     * 院后微信小程序一键登录接口
     *
     * @param loginCode 登录凭证
     * @param phoneCode 获取手机号登录凭证
     * @return 微信小程序用户登录信息
     */
    @GetMapping("/appletLogin")
    public AjaxResult appletLogin(@RequestParam("loginCode") String loginCode, @RequestParam("phoneCode") String phoneCode) {
        if (StringUtils.isBlank(loginCode)) {
            return AjaxResult.error("登录凭证编码不能为空！");
        }
        if (StringUtils.isBlank(phoneCode)) {
            return AjaxResult.error("用户手机号凭证不存在");
        }
        return appletPersonCenterService.appletLogin(loginCode, phoneCode);
    }

    /**
     * 根据居民表id查询患者个人信息
     *
     * @param residentId 居民表id
     * @return 个人信息
     */
    @GetMapping("/getResidentInfoById")
    public AjaxResult getResidentInfoById(Long residentId) {
        return appletPersonCenterService.getResidentInfoById(residentId);
    }

    /**
     * 根据居民表id查询患者健康档案信息
     *
     * @param residentId 居民患者id
     * @return 分页列表数据
     */
    @GetMapping("/getHealthRecordListByResidentId")
    public TableDataInfo getHealthRecordListById(Long residentId) {
        startPage();
        List<HealthRecordDTO> list = appletPersonCenterService.getHealthRecordListByResidentId(residentId);
        return getDataTable(list);
    }

    /**
     * 根据id获取患者患者健康档案信息
     *
     * @param id 居民患者id
     * @return PatientInfo
     */
    @GetMapping("/getHealthRecordInfoById")
    public AjaxResult getPatientInfoById(Long id) {
        return AjaxResult.success(appletPersonCenterService.getHealthRecordInfoById(id));
    }

    @ApiOperation("院后微信小程序登录接口")
    @GetMapping("/appletLoginV1")
    public AjaxResult appletLoginV1(@RequestParam("loginCode") String loginCode) {
        if (StringUtils.isBlank(loginCode)) {
            return AjaxResult.error("登录凭证编码不能为空！");
        }
        return appletPersonCenterService.appletLoginV1(loginCode);
    }

    @ApiOperation("小程序端手机号与openid解绑")
    @PostMapping("/unbindOpenId")
    public AjaxResult unbindOpenId(@RequestBody UnbindRequestDTO unbindRequestDTO) {
        return appletPersonCenterService.unbindOpenId(unbindRequestDTO.getPhoneNum(), unbindRequestDTO.getOpenId());
    }


    @ApiOperation("小程序登录发送短信验证码")
    @GetMapping("/sendSms")
    public AjaxResult sendSms(String phoneNum) {
        if (StringUtils.isBlank(phoneNum)) {
            return AjaxResult.error("手机号码不能为空!");
        }
        return appletPersonCenterService.sendSms(phoneNum);
    }

    @ApiOperation("小程序登录验证短信验证码")
    @PostMapping("/verifySmsCode")
    public AjaxResult verifySmsCode(@RequestBody VerifySmsCodeDTO verifySmsCodeDTO) {
        if (StringUtils.isBlank(verifySmsCodeDTO.getOpenId())) {
            return AjaxResult.error("微信小程序openid不能为空!");
        }
        if (StringUtils.isBlank(verifySmsCodeDTO.getPhoneNum())) {
            return AjaxResult.error("手机号不能为空!");
        }
        if (StringUtils.isBlank(verifySmsCodeDTO.getPatientName())) {
            return AjaxResult.error("姓名不能为空");
        }
        if (StringUtils.isBlank(verifySmsCodeDTO.getSmsCode())) {
            return AjaxResult.error("短信验证码不能为空!");
        }
        return appletPersonCenterService.verifySmsCode(verifySmsCodeDTO);
    }

}
