package com.fs.watch.service.impl;

import com.fs.common.utils.DateUtils;
import com.fs.common.utils.StringUtils;
import com.fs.watch.domain.WatchFsUser;
import com.fs.watch.domain.WatchThirdUaData;
import com.fs.watch.mapper.WatchThirdUaDataMapper;
import com.fs.watch.service.WatchThirdUaDataService;
import com.fs.watch.service.WatchUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
* @author Administrator
* @description 针对表【watch_third_ua_data(设备三方尿酸)】的数据库操作Service实现
* @createDate 2024-10-15 11:06:51
*/
@Service
@Slf4j
public class WatchThirdUaDataServiceImpl implements WatchThirdUaDataService {
    @Autowired
    private WatchThirdUaDataMapper mapper;

    @Autowired
    private WatchUserService userService;

    @Override
    public Boolean insert(WatchThirdUaData watchThirdUaData) {
        Integer num = mapper.queryByCreateTime(watchThirdUaData.getCreateTime());
        if (num != null && num > 0) {
            return false;
        }
        try {
            mapper.insert(watchThirdUaData);
        } catch (Exception e) {
            log.error("===============watchThirdUaData:insert=========================={}", e.getMessage());
            return false;
        }
        return true;
    }

    private Map<String, Object> getThirdUaDataMap(WatchThirdUaData watchThirdUaData) {
        Map<String, Object> params = new HashMap<>();
        params.put("id", watchThirdUaData.getId().toString());
        params.put("deviceId", watchThirdUaData.getDeviceId());
        params.put("val", watchThirdUaData.getVal());
        params.put("status", watchThirdUaData.getStatus());
        params.put("createTime", watchThirdUaData.getCreateTime());
        return params;
    }

    @Override
    public WatchThirdUaData getLatest(String deviceId) {
        return mapper.getLatest(deviceId);
    }

    /**
     * 获取尿酸状态
     * @param uricAcid
     * @return
     */
    @Override
    public Integer getStatus(String deviceId,int uricAcid) {
        Integer status = 0;//默认正常
        //获取设备用户的性别 根据性别判断是否异常
        WatchFsUser user = userService.selectWatchFsUserByDeviceId(deviceId);
        if (user != null){
            if (user.getSex() == 1 ) {
                //男 正常成年男性尿酸值在150～416μmol/L
                if (uricAcid < 150){
                    status = 1;
                } else if (uricAcid < 416){
                    status = 0;
                } else {
                    status = 2;
                }
            } else if (user.getSex() == 2) {
                //女 正常成年女性在89～357μmol/L
                if (uricAcid < 89){
                    status = 1;
                } else if (uricAcid < 357){
                    status = 0;
                } else {
                    status = 2;
                }
            }
        }
        return status;
    }

    /**
     * 获取尿酸状态文字
     * @param status
     * @return
     */
    @Override
    public String getStatusStr(int status) {
        String statusStr = "正常";//默认正常
        if(status == 1){
            statusStr = "偏低";
        }
        switch (status){
            case 1:
                statusStr = "偏低";
                break;
            case 2:
                statusStr = "偏高";
                break;
            default:
                break;
        }
        return statusStr;
    }

    @Override
    public List<WatchThirdUaData> queryByDateAndDeviceId(String date, String deviceId) {
        if (StringUtils.isBlank(date)) {
            date = DateUtils.dateTimeNow("yyyy-MM-dd");
        }
        return mapper.queryByDateAndDeviceId(date,deviceId);
    }

    @Override
    public List<WatchThirdUaData> list(Date startTime, Date endTime, String deviceId) {
        return mapper.list(startTime,endTime,deviceId);
    }

    @Override
    public List<Map<String, Object>> countUaByDate(Date startTime, Date endTime, String deviceId) {
        if (StringUtils.isBlank(deviceId)) {
            return null;
        }
        //返回对象
        List<Map<String, Object>> res = null;
        res = mapper.countUaByDate(DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, startTime),
                DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, endTime), deviceId);

        return res;
    }

    @Override
    public List<WatchThirdUaData> queryListByDate(Date startTime, Date endTime, String deviceId, Integer pageNum, Integer pageSize,Integer status) {
        if (StringUtils.isNotBlank(deviceId)) {
            return mapper.queryListByDate(DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, startTime),
                    DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, endTime),
                    deviceId, pageNum, pageSize,status);
        } else {
            return null;
        }
    }

    @Override
    public int countTotalDate(Date startTime, Date endTime, String deviceId,Integer status) {
        return mapper.countTotalDate(DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, startTime),
                DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, endTime),
                deviceId,status);
    }
}
