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

import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.yihu.jw.base.dao.dict.DictDiseaseDao;
import com.yihu.jw.base.dao.dict.DiseaseManageDao;
import com.yihu.jw.base.enums.SystemDictEnum;
import com.yihu.jw.entity.base.dict.DictDiseaseDO;
import com.yihu.jw.entity.base.dict.DiseaseManageDO;
import com.yihu.jw.entity.base.org.BaseOrgDO;
import com.yihu.jw.entity.hospital.consult.WlyyHospitalSysDictDO;
import com.yihu.jw.hospital.dict.WlyyHospitalSysDictDao;
import com.yihu.jw.org.dao.BaseOrgDao;
import com.yihu.jw.restmodel.web.MixEnvelop;
import com.yihu.jw.utils.StringUtil;
import com.yihu.jw.utils.hibernate.HibenateUtils;
import com.yihu.mysql.query.BaseJpaService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.*;

/**
 * 
 * 病种字典服务service
 * 
 * @version 
 * <pre>
 * Author	Version		Date		Changes
 * Administrator    1.0  2018年09月05日 Created
 *
 * </pre>
 * @since 1.
 */
@Service
public class DiseaseManageService extends BaseJpaService<DiseaseManageDO, DiseaseManageDao> {

    @Autowired
    private DiseaseManageDao diseaseManageDao;
    @Autowired
    private ObjectMapper objectMapper;
    @Autowired
    private WlyyHospitalSysDictDao wlyyHospitalSysDictDao;
    @Autowired
    private BaseOrgDao baseOrgDao;
    @Autowired
    private HibenateUtils hibenateUtils;
    public DiseaseManageDO saveDisease(String jsonData) throws Exception {
        DiseaseManageDO diseaseManageDO = objectMapper.readValue(jsonData,DiseaseManageDO.class);
        if (StringUtils.isNoneBlank(diseaseManageDO.getId())){
            DiseaseManageDO dictDiseasepre = diseaseManageDao.findOne(diseaseManageDO.getId());
            dictDiseasepre.setCode(diseaseManageDO.getCode());
            dictDiseasepre.setExplain(diseaseManageDO.getExplain());
            dictDiseasepre.setName(diseaseManageDO.getName());
            dictDiseasepre.setPrevent(diseaseManageDO.getPrevent());
            dictDiseasepre.setReason(diseaseManageDO.getReason());
            dictDiseasepre.setImg(diseaseManageDO.getImg());
            if (StringUtils.isNoneBlank(diseaseManageDO.getOrgCode())){
                BaseOrgDO baseOrgDO  = baseOrgDao.findByCode(diseaseManageDO.getCode());
                dictDiseasepre.setOrgCode(diseaseManageDO.getOrgCode());
                if (baseOrgDO!=null){
                    dictDiseasepre.setOrgName(baseOrgDO.getName());
                }
            }
            dictDiseasepre.setIsDel("1");
            dictDiseasepre.setStatus(diseaseManageDO.getStatus());
            diseaseManageDO = diseaseManageDao.save(dictDiseasepre);
        }else {
            List<DiseaseManageDO> diseaseManageDOList = diseaseManageDao.findByOrgCodeAndCode(diseaseManageDO.getOrgCode(),diseaseManageDO.getCode());
            if (diseaseManageDOList!=null&&diseaseManageDOList.size()>0){
                throw new Exception("该医院已存在此种疾病");
            }
            if (StringUtils.isNoneBlank(diseaseManageDO.getOrgCode())){
                BaseOrgDO baseOrgDO  = baseOrgDao.findByCode(diseaseManageDO.getCode());
                if (baseOrgDO!=null){
                    diseaseManageDO.setOrgName(baseOrgDO.getName());
                }
            }
            diseaseManageDO.setIsDel("1");
            diseaseManageDO.setCreateTime(new Date());
            diseaseManageDO = diseaseManageDao.save(diseaseManageDO);
        }
        return diseaseManageDO;
    }
    public Map deleteById(String id,String isDel,String status){
        DiseaseManageDO diseaseManageDO =  diseaseManageDao.findOne(id);
        Map result = new HashMap();
        if (diseaseManageDO!=null){
            if (StringUtils.isNoneBlank(isDel)){
                diseaseManageDO.setIsDel(isDel);
            }
            if (StringUtils.isNoneBlank(status)){
                diseaseManageDO.setStatus(status);
            }
            diseaseManageDao.save(diseaseManageDO);
            result.put("status",200);
            result.put("msg","操作成功");
        }else{
            result.put("status",-1);
            result.put("msg","操作失败");
        }
        return  result;
    }
    public MixEnvelop findList(String orgCode, String status, String name, Integer page , Integer pageSize){
        MixEnvelop mixEnvelop = new MixEnvelop();
        String sql = "select a.id as \"id\"," +
                " a.saas_id as \"saasId\"," +
                "a.code as \"code\"," +
                "a.name as \"name\"," +
                "a.create_time as \"createTime\"," +
                "a.disease_explain as \"diseaseExplain\"," +
                "a.reason as \"reason\"," +
                "a.prevent as \"prevent\","+
                "a.org_code as \"orgCode\"," +
                "a.status as \"status\"," +
                "b.name as \"orgName\"," +
                "a.is_del as \"isDel\" from disease_manage a left join base_org b on a.org_code = b.code where is_del = '1' " ;
        if (StringUtils.isNoneBlank(orgCode)){
            sql+=" and a.org_code = '"+orgCode+"'";
        }
        if (StringUtils.isNoneBlank(status)){
            sql+=" and a.status = '"+status+"'";
        }
        if (StringUtils.isNoneBlank(name)){
            sql+=" and a.name like '%"+name+"%'";
        }
        List<Map<String ,Object>> list = hibenateUtils.createSQLQuery(sql,page,pageSize);
        for (Map<String ,Object>map :list){
            if (map.get("status")!=null){
                List<WlyyHospitalSysDictDO> dictNameAndDictCode = wlyyHospitalSysDictDao.findByDictNameAndDictCode("drugStoreStatus",map.get("status").toString());
                if (dictNameAndDictCode!=null&&dictNameAndDictCode.size()>0){
                    map.put("statusName",dictNameAndDictCode.get(0).getDictValue());
                }
            }
        }
        List<Map<String ,Object>> listCount = hibenateUtils.createSQLQuery(sql);
        mixEnvelop.setPageSize(pageSize);
        mixEnvelop.setCurrPage(page);
        mixEnvelop.setDetailModelList(list);
        mixEnvelop.setTotalCount(listCount==null?0:listCount.size());
        return mixEnvelop;
    }
    public DiseaseManageDO findOneById(String id){
        DiseaseManageDO diseaseManageDO = diseaseManageDao.findOne(id);
        if (diseaseManageDO!=null){
            if (StringUtils.isNotBlank(diseaseManageDO.getStatus())){
                List<WlyyHospitalSysDictDO> dictNameAndDictCode = wlyyHospitalSysDictDao.findByDictNameAndDictCode("drugStoreStatus",diseaseManageDO.getStatus());
                if (dictNameAndDictCode!=null&&dictNameAndDictCode.size()>0){
                    diseaseManageDO.setStatusName(dictNameAndDictCode.get(0).getDictValue());
                }
                if (StringUtils.isNoneBlank(diseaseManageDO.getOrgCode())){
                    BaseOrgDO baseOrgDO  = baseOrgDao.findByCode(diseaseManageDO.getCode());
                    if (baseOrgDO!=null){
                        diseaseManageDO.setOrgName(baseOrgDO.getName());
                    }
                }
            }
            return diseaseManageDO;
        }
        return null;
    }
}
