package com.xinelu.web.controller.applet;

import com.xinelu.common.config.XinELuConfig;
import com.xinelu.common.core.domain.AjaxResult;

import com.xinelu.familydoctor.applet.pojo.body.PatientInfoBody;
import com.xinelu.familydoctor.applet.pojo.entity.PatientInfo;
import com.xinelu.familydoctor.applet.service.IResidentPatientInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.xinelu.common.core.controller.BaseController;
import com.xinelu.common.core.domain.R;

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

/**
 * @Author mengkuiliang
 * @Description 小程序注册控制器
 * @Date 2023-09-25 025 10:24
 * @Param
 * @return
 **/
@RestController
@RequestMapping("/applet/register")
@Api(tags = "小程序注册控制器")
public class ResidentPatientInfoController extends BaseController {

    @Resource
    private IResidentPatientInfoService residentPatientInfoService;
    @Resource
    private XinELuConfig xinELuConfig;

    @ApiOperation("获取OpenId")
    @GetMapping("/getOpenId/{code}")
    public R<String> getOpenId(@PathVariable String code) {
        try {
            return R.ok(residentPatientInfoService.getOpenId(code));
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }
    }

    @ApiOperation("注册完善信息")
    @PostMapping("")
    public R<String> register(@Validated @RequestBody PatientInfoBody body) {
//        if(body.getDiseaseList() == null || body.getDiseaseList().size() == 0) {
//            return R.fail("请选择基础疾病");
//        }
        try {
            residentPatientInfoService.register(body);
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return R.fail(e.getMessage());
        }
    }

//    @ApiOperation("修改注册信息")
//    @Log(title = "修改注册信息", businessType = BusinessType.UPDATE)
//    @PostMapping("/update")
//    public R<String> update(@Validated @RequestBody PatientInfo body) {
//        try {
//            residentPatientInfoService.updatePatientInfo(body);
//            return R.ok();
//        } catch (Exception e) {
//            return R.fail(e.getMessage());
//        }
//    }

    @ApiOperation("解绑")
    @GetMapping("/unbinding/{patientCode}")
    public R<String> unbinding(@PathVariable String patientCode) {
        residentPatientInfoService.unbinding(patientCode);
        return R.ok();
    }

    @ApiOperation("获取已注册列表")
    @GetMapping("/getList/{openid}")
    public R<List<PatientInfo>> getList(@PathVariable String openid) {
        return R.ok(residentPatientInfoService.getList(openid, null));
    }

    @ApiOperation("切换账号")
    @GetMapping("/switchResident/{openid}/{patientCode}")
    public R<PatientInfo> switchResident(@PathVariable String openid, @PathVariable String patientCode) {
        try {
            return R.ok(residentPatientInfoService.switchResident(openid, patientCode));
        } catch (Exception e) {
            e.printStackTrace();
            return R.fail(e.getMessage());
        }
    }

    @ApiOperation("获取注册详细信息")
    @GetMapping(value = "/getInfo/{patientCode}")
    public R<PatientInfo> getInfo(@PathVariable("patientCode") String patientCode) {
        return R.ok(residentPatientInfoService.selectPatientInfoByCode(patientCode));
    }

    @ApiOperation("获取当前注册居民")
    @GetMapping(value = "/getCurrentResident/{openid}")
    public R<PatientInfo> getCurrentResident(@PathVariable("openid") String openid) {
        return R.ok(residentPatientInfoService.getCurrentResident(openid, null));
    }

    /*@ApiOperation("是否已注册")
    @GetMapping(value = "/isRegistered/{code}")
    public R<PatientInfo> isRegistered(@PathVariable("code") String code) {
        return R.ok(residentPatientInfoService.isRegistered(code));
    }*/

    @ApiOperation("是否已注册")
    @GetMapping(value = "/login/{loginCode}/{phoneCode}")
    public AjaxResult isRegistered(@PathVariable("loginCode") String loginCode, @PathVariable("phoneCode") String phoneCode) throws Exception {
        return AjaxResult.success(residentPatientInfoService.login(loginCode,phoneCode));
    }

    /**
     * @Author mengkuiliang
     * @Description 居民头像上传
     * @Date 2023-10-09 009 13:41
     * @Param
     * @return
     **/
//    @ApiOperation("居民头像上传")
//    @PostMapping(value = "/headPictureUpload")
//    public AjaxResult headPictureUpload(MultipartFile file) throws Exception {
//        if (StringUtils.isBlank(file.getOriginalFilename())) {
//            return AjaxResult.error("请选择所要上传的头像！");
//        }
//        //获取路径名称
//        String uploadPathUrl = XinELuConfig.getProfile() + xinELuConfig.getHeadPictureUrl();
//        //上传图片
//        String pictureName = FileUploadUtils.uploadNurseStationPath(uploadPathUrl, file, MimeTypeUtils.IMAGE_EXTENSION);
//        if (StringUtils.isBlank(pictureName)) {
//            throw new ServiceException(pictureName + "居民头像上传失败！");
//        }
//        //获取返回值
//        AjaxResult ajax = AjaxResult.success("上传成功！");
//        ajax.put("imgUrl", pictureName);
//        return ajax;
//    }
}
