package com.fs.his.service.impl;

import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.fs.aiTongueApi.domain.AITongueResult;
import com.fs.aiTongueApi.domain.QueryQuanXi;
import com.fs.aiTongueApi.domain.inner.TongueData;
import com.fs.aiTongueApi.service.AiTongueService;
import com.fs.aiTongueApi.service.impl.AiTongueServiceImpl;
import com.fs.common.core.domain.R;
import com.fs.common.utils.DateUtils;
import com.fs.his.param.FsHealthTongueListUParam;
import com.fs.his.param.FsHealthTongueParam;
import com.fs.his.param.FsHealthTongueUParam;
import com.fs.his.vo.FsHealthTongueListUVO;
import com.fs.his.vo.FsHealthTongueListVO;
import com.fs.his.vo.FsHealthTongueUVO;
import com.qiniu.util.Json;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.fs.his.mapper.FsHealthTongueMapper;
import com.fs.his.domain.FsHealthTongue;
import com.fs.his.service.IFsHealthTongueService;

/**
 * 舌苔Service业务层处理
 *
 * @author fs
 * @date 2024-09-19
 */
@Service
public class FsHealthTongueServiceImpl implements IFsHealthTongueService
{
    @Autowired
    private FsHealthTongueMapper fsHealthTongueMapper;
    @Autowired
    private AiTongueService aiTongueService;
    /**
     * 查询舌苔
     *
     * @param id 舌苔主键
     * @return 舌苔
     */
    @Override
    public FsHealthTongue selectFsHealthTongueById(Long id)
    {
        return fsHealthTongueMapper.selectFsHealthTongueById(id);
    }

    /**
     * 查询舌苔列表
     *
     * @param fsHealthTongue 舌苔
     * @return 舌苔
     */
    @Override
    public List<FsHealthTongue> selectFsHealthTongueList(FsHealthTongue fsHealthTongue)
    {
        return fsHealthTongueMapper.selectFsHealthTongueList(fsHealthTongue);
    }

    /**
     * 新增舌苔
     *
     * @param fsHealthTongue 舌苔
     * @return 结果
     */
    @Override
    public int insertFsHealthTongue(FsHealthTongue fsHealthTongue)
    {
        fsHealthTongue.setCreateTime(DateUtils.getNowDate());
        return fsHealthTongueMapper.insertFsHealthTongue(fsHealthTongue);
    }

    /**
     * 修改舌苔
     *
     * @param fsHealthTongue 舌苔
     * @return 结果
     */
    @Override
    public int updateFsHealthTongue(FsHealthTongue fsHealthTongue)
    {
        return fsHealthTongueMapper.updateFsHealthTongue(fsHealthTongue);
    }

    /**
     * 批量删除舌苔
     *
     * @param ids 需要删除的舌苔主键
     * @return 结果
     */
    @Override
    public int deleteFsHealthTongueByIds(Long[] ids)
    {
        return fsHealthTongueMapper.deleteFsHealthTongueByIds(ids);
    }

    /**
     * 删除舌苔信息
     *
     * @param id 舌苔主键
     * @return 结果
     */
    @Override
    public int deleteFsHealthTongueById(Long id)
    {
        return fsHealthTongueMapper.deleteFsHealthTongueById(id);
    }

    @Override
    public List<FsHealthTongueListUVO> selectFsHealthTongueUListVO(FsHealthTongueListUParam param) {
        return fsHealthTongueMapper.selectFsHealthTongueUListVO(param);
    }

    @Override
    public Long selectFsHealthTongueCountByUserId(long userId) {
        return fsHealthTongueMapper.selectFsHealthTongueCountByUserId(userId);
    }

    @Override
    public R insertFsHealthTongueByImgUrl(FsHealthTongueUParam param) {

        QueryQuanXi queryQuanXi = new QueryQuanXi();
        if (param.getName()!=null&&!param.getName().equals("")){
            queryQuanXi.setName(param.getName());
        }{
            queryQuanXi.setName("匿名");
        }
        if (param.getAge()!=null){
            queryQuanXi.setAge(param.getAge()+"");
        }else {
            queryQuanXi.setAge("55");
        }

        queryQuanXi.setFile(param.getTongueUrl());
        if (param.getSex()!=null){
            queryQuanXi.setMale(param.getSex()==1?"1":"0");
        }else {
            queryQuanXi.setMale("0");
        }

        AITongueResult<TongueData> tongueDataAITongueResult = aiTongueService.quanXiTongue(queryQuanXi);

        if (tongueDataAITongueResult.getCode().equals("40001")){
            FsHealthTongue fsHealthTongue = new FsHealthTongue();
            fsHealthTongue.setAge(param.getAge());
            fsHealthTongue.setPatientId(param.getPatientId());
            fsHealthTongue.setName(param.getName());
            fsHealthTongue.setSex(param.getSex());
            fsHealthTongue.setStatus(0);
            fsHealthTongue.setUserId(param.getUserId());
            fsHealthTongue.setTongueUrl(param.getTongueUrl());
            TongueData data = tongueDataAITongueResult.getData();
            fsHealthTongue.setTongueId(data.getId());
            fsHealthTongue.setCreateTime(new Date());
            fsHealthTongue.setTypeName(data.getTypeName()+"质");

            fsHealthTongue.setBotai(data.getBotai());
            fsHealthTongue.setBotaiDesc(data.getBotaiDesc());
            fsHealthTongue.setChihen(data.getChihen());
            fsHealthTongue.setChihenDesc(data.getChihenDesc());
            fsHealthTongue.setLiewen(data.getLiewen());
            fsHealthTongue.setLiewenDesc(data.getLiewenDesc());
            fsHealthTongue.setShemianName(data.getShemianName());
            fsHealthTongue.setShemianDesc(data.getShemianDesc());
            fsHealthTongue.setTaiseName(data.getTaiseName());
            fsHealthTongue.setTaiseDesc(data.getTaiseDesc());
            String json =fsHealthTongueMapper.selectTypeJSON();
            String typeName = fsHealthTongue.getTypeName();
            String itemTypeJSonValue="[]";
            List<Map<String, Object>> itemTypeJsonList = (List<Map<String, Object>>) JSON.parse(json);
            for (Map<String, Object> stringStringMap : itemTypeJsonList) {
                String itemType = (String) stringStringMap.get("itemType");
                if (itemType.equals(typeName)){
                    itemTypeJSonValue= Json.encode(stringStringMap.get("item"));
                }
            }
            fsHealthTongue.setTypeJson(itemTypeJSonValue);
            fsHealthTongueMapper.insertFsHealthTongue(fsHealthTongue);
            return R.ok().put("data",fsHealthTongue);
        }else {
            return R.error(tongueDataAITongueResult.getMeta().getMsg());
        }
    }

    @Override
    public FsHealthTongueUVO selectFsHealthTongueByUserId(long userId) {
        return fsHealthTongueMapper.selectFsHealthTongueByUserId(userId);
    }

    @Override
    public List<FsHealthTongueListVO> selectFsHealthTongueListVO(FsHealthTongueParam param) {
        return fsHealthTongueMapper.selectFsHealthTongueListVO(param);
    }

    @Override
    public FsHealthTongueUVO selectFsHealthTongueUVOById(Long id) {
        return fsHealthTongueMapper.selectFsHealthTongueUVOById(id);
    }

    @Override
    public Long selectFsHealthTongueAllCountByUserId(Long id) {
        return fsHealthTongueMapper.selectFsHealthTongueAllCountByUserId(id);
    }
}
