package com.eitc.patient.controller;

import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.eitc.common.annotation.Log;
import com.eitc.common.core.controller.BaseController;
import com.eitc.common.core.domain.AjaxResult;
import com.eitc.common.enums.BusinessType;
import com.eitc.common.utils.InterfaceVerificationUtil;
import com.eitc.patient.domain.AppUser;
import com.eitc.patient.domain.ProtocolTemplate;
import com.eitc.patient.service.IAppUserService;
import com.eitc.patient.service.IClinicInfoService;
import com.eitc.patient.service.IProtocolTemplateService;
import com.eitc.patient.service.IProtocolTypeService;
import com.eitc.patient.vo.ClinicInfoVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;

/**
 * ApiController
 *
 * @author mtx
 * @date: 2024/06/20 8:37
 */
@Api(tags = "API接口")
@RestController
@RequestMapping("/app/api")
public class ApiAppController extends BaseController {

    @Resource
    private IAppUserService appUserService;
    @Resource
    private IClinicInfoService clinicInfoService;
    @Resource
    private RestTemplate restTemplate;
    @Value("${Total.ip}")
    private String fileUrl;
    @Resource
    private IProtocolTemplateService protocolTemplateService;
    @Resource
    private IProtocolTypeService protocolTypeService;


    /**
     * APP切换诊所时同步数据
     *
     * @param beforeAppUser 前一次登录用户信息
     * @return 返回结果
     * @author mtx
     * @date: 2024/6/18 14:27
     */
    @GetMapping("/synchronizeData")
    @ApiOperation("APP切换诊所时同步数据")
    public AjaxResult synchronizeData(AppUser beforeAppUser) {
        return appUserService.synchronizeData(beforeAppUser);
    }

    /**
     * 诊所列表
     *
     * @return
     */
    @GetMapping(value = "/clinicList")
    @ApiOperation("诊所列表")
    public JSONObject clinicList() {
        // 这个是为了防止恶意请求来做的参数增加
        String saSignStr = InterfaceVerificationUtil.getSaSignStr(new HashMap<>());
        String url = fileUrl + "api/sys/clinict/appGetClinictList?" + saSignStr;
        String object = restTemplate.getForObject(url, String.class);
        return JSONObject.parseObject(object);
    }

    /**
     * 新增门诊信息
     */
    @Log(title = "新增门诊信息", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation("新增门诊信息")
    public AjaxResult add(@ApiParam(name = "clinicInfo") @Valid @RequestBody ClinicInfoVO clinicInfo) {
        return clinicInfoService.addOrUpdate(clinicInfo);
    }

    /**
     * 用户须知
     */
    @GetMapping("/user-notice")
    @ApiOperation("用户须知")
    public AjaxResult userNotice() {
        Object userNoticeContent = "";
        String typeId = protocolTypeService.getProtocolTypes("用户须知");
        List<ProtocolTemplate> protocolTemplateList = protocolTemplateService.list(Wrappers.<ProtocolTemplate>lambdaQuery()
                .eq(ProtocolTemplate::getProtocolType, typeId).orderByDesc(ProtocolTemplate::getCreateDate));
        if (CollectionUtils.isNotEmpty(protocolTemplateList)) {
            userNoticeContent = protocolTemplateList.get(0).getProtocolTemplateContent();
        }
        return success(userNoticeContent);
    }
}
