package com.yihu.jw.base.service.equipment;


import com.alibaba.fastjson.JSONObject;
import com.yihu.jw.base.dao.equipment.BaseHospitalEquipmentDao;
import com.yihu.jw.base.dao.equipment.WlyyUserAreaDao;
import com.yihu.jw.entity.equipment.BaseHospitalEquipmentDO;
import com.yihu.jw.entity.equipment.WlyyUserAreaDO;
import com.yihu.jw.utils.StringUtil;
import com.yihu.mysql.query.BaseJpaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * Created by wangzhinan on 2021/09/16.
 * 设备管理
 */
@Service
public class BaseHospitalEquipmentService extends BaseJpaService<BaseHospitalEquipmentDO, BaseHospitalEquipmentDao> {

    @Autowired
    private BaseHospitalEquipmentDao baseHospitalEquipmentDao;
    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Autowired
    private JuMiAiService juMiAiService;

    public List<Map<String,Object>> selectByHospitalOrTown(String town,String hospital,String name){
        String sql ="SELECT\n" +
                "\tid AS \"id\",\n" +
                "\tCODE AS \"code\",\n" +
                "\tNAME AS \"name\",\n" +
                "\tnetwork_status AS \"networkStatus\",\n" +
                "\tselling_status AS \"sellingStatus\",\n" +
                "\ttype as \"type\",\n" +
                "\ttype_name as \"typeName\",\n" +
                "\tinfo as \"info\",\n" +
                "\thospital as \"hospital\",\n" +
                "\thospital_name as \"hospitalName\",\n" +
                "\tbinding_status as \"bindingStatus\",\n" +
                "\tstatus as \"status\",\n" +
                "\taddress as \"address\"\n" +
                "FROM\n" +
                "\tbase_hospital_equipment\n" +
                "WHERE\n" +
                "\t1 = 1 ";
        if (town!=null&&town!=""){
            sql +=" and hospital IN(select * from dm_hospital dm where dm.town ='"+town+"') ";
        }

        if (hospital!=null&&hospital!=""){
            sql +=" and hospital ='"+hospital+"' ";
        }

        if (name!=null&&name!=""){
            sql +=" and name like'%"+hospital+"%' ";
        }

        List<Map<String,Object>> map = jdbcTemplate.queryForList(sql);
        return map;
    }

    public List<Map<String, Object>> selectEquipment(String code, String typeName, String address, String hospitalName) {
        String sql = "SELECT\n" +
                        "\t\tid,\n" +
                        "\t\tname,\n" +
                        "\t\tcode,\n" +
                        "\t\tnetwork_status,\n" +
                        "\t\tselling_status,\n" +
                        "\t\ttype,\n" +
                        "\t\ttype_name,\n" +
                        "\t\tinfo,\n" +
                        "\t\thospital,\n" +
                        "\t\thospital_name,\n" +
                        "\t\tbinding_status,\n" +
                        "\t\tstatus,\n" +
                        "\t\taddress,\n" +
                        "\t\tcreate_user,\n" +
                        "\t\tcreate_user_name,\n" +
                        "\t\tcreate_time,\n" +
                        "\t\tupdate_user,\n" +
                        "\t\tupdate_user_name,\n" +
                        "\t\tupdate_time\n" +
                        "FROM base_hospital_equipment\n" +
                        "\n" +
                        "WHERE\n" +
                        "\t1=1 ";
        if (code != null && code!=""){
            sql += "and code = '" +code+ "' ";
        }
        if (typeName != null && typeName != ""){
            sql += "and type_name = '" +typeName+"' ";
        }
        if (address != null && address != ""){
            sql += "and address = '" +address+"' ";
        }
        if (hospitalName != null && hospitalName != ""){
            sql += "and hospital_name = '" +hospitalName+"' ";
        }
        List<Map<String, Object>> map = jdbcTemplate.queryForList(sql);
        return map;
    }

//    public List<BaseHospitalEquipmentDO> synEquipment(String deviceNo){
//        String str[]=deviceNo.split(",");
//        for (int i=0;i<str.length;i++){
//            String deviceNoString  = str[i];
//            //获取第三方设备详情
//            String result = juMiAiService.getEquipmentInfo(deviceNoString);
//            if (result!=null&&result!=""){
//                JSONObject jsonObject = JSONObject.parseObject(result);
//                String code = jsonObject.getString("code");
//                if (code.equalsIgnoreCase("0")){
//                    JSONObject object = jsonObject.getJSONObject("data");
//                    String device_no= object.getString("device_no");
//                    String device_name = object.getString("device_name");
//                    Integer isonline = object.getInteger("isonline");//是否在线 0离线 1在线
//                    Integer state= object.getInteger("state");//状态1开启 3关闭(故障) 0已解绑
//                    String riss = object.getString("riss");//信号强度 长度限制为1~10个字符
//                    String device_category = object.getString("device_category");//设备分类
//                    String device_category_name = object.getString("device_category_name");//设备分类名称
//                    BaseHospitalEquipmentDO equipmentDO = baseHospitalEquipmentDao.findAllByCode(device_no);
//                    if (equipmentDO!=null){
//                        equipmentDO.setName(device_name);
//                        equipmentDO.
//                    }
//
//                }
//            }
//        }
//
//
//        return map;
//    }
}
