package com.yihu.jw.hospital.prescription.service;

import com.yihu.jw.entity.hospital.prescription.BaseNationalDrugDictDO;
import com.yihu.jw.hospital.prescription.dao.BaseNationalDrugDictDao;
import com.yihu.jw.restmodel.web.MixEnvelop;
import com.yihu.jw.util.common.ConvertToSpellUtils;
import com.yihu.jw.util.common.PingyinUtils;
import com.yihu.mysql.query.BaseJpaService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.util.Date;
import java.util.List;
import java.util.Map;

import static com.yihu.jw.util.common.BeanUtils.getNullPropertyNames;

@Service
@Transactional
public class NationalDrugDictService extends BaseJpaService<BaseNationalDrugDictDO, BaseNationalDrugDictDao> {
    @Autowired
    private BaseNationalDrugDictDao nationalDrugDictDao;
    @Autowired
    private JdbcTemplate jdbcTemplate;

    public BaseNationalDrugDictDO saveNationalDrug(BaseNationalDrugDictDO nationalDrugDictDO) {
        //判断新增或编辑
        if (nationalDrugDictDO.getId() == null) {
            nationalDrugDictDO.setPyName(ConvertToSpellUtils.changeToTonePinYin(nationalDrugDictDO.getName()));
            nationalDrugDictDO.setPyCommonName(ConvertToSpellUtils.changeToTonePinYin(nationalDrugDictDO.getCommonName()));
            nationalDrugDictDO.setCreateTime(new Date());
            nationalDrugDictDO.setStatus(1);
            nationalDrugDictDO.setUpdateTime(new Date());
            return nationalDrugDictDao.save(nationalDrugDictDO);
        }else {
            BaseNationalDrugDictDO oldDurgDictDO =  nationalDrugDictDao.findOne(nationalDrugDictDO.getId());
            BeanUtils.copyProperties(nationalDrugDictDO, oldDurgDictDO, getNullPropertyNames(nationalDrugDictDO));
            oldDurgDictDO.setUpdateTime(new Date());
            return nationalDrugDictDao.save(oldDurgDictDO);
        }

    }

    /**
     * 获取药品列表
     *
     * @param name
     * @param number
     * @param hospital
     * @param status
     * @param page
     * @param pageSize
     * @return
     * @throws ParseException
     */
    public MixEnvelop getNationalDrugList(String name, String number, String hospital, Integer status, Integer page, Integer pageSize) throws ParseException {
        MixEnvelop mixEnvelop = new MixEnvelop();
        String sql = "SELECT id,number,name,py_name as pyName,common_name as commonName,py_common_name as pyCommonName,subject_class as subjectClass,subject_class_name as subjectClassName," +
                "drug_code as drugCode,drug_name as drugName,hospital,hospital_name as hospitalName,relation_code as relationCode,`status` " +
                "FROM `base_national_drug_dict` WHERE 1=1 ";
        if (StringUtils.isNotBlank(name)) {
            sql += "and name like '%" + name + "%' ";
        }
        if (StringUtils.isNotBlank(number)) {
            sql += "and number like '%" + number + "%' ";
        }
        if (StringUtils.isNotBlank(hospital)) {
            sql += "and hospital = '" + hospital + "' ";
        } else {
            sql += "and hospital is null ";
        }
        if (status != null) {
            sql += "status = " + status + " ";
        }
        Integer count = jdbcTemplate.queryForList(sql).size();
        if (page != null && pageSize != null) {
            sql += "order by create_time desc limit " + (page - 1) * pageSize + "," + pageSize;
        }
        List<Map<String,Object>> list = jdbcTemplate.queryForList(sql);
        mixEnvelop.setMessage("获取成功！");
        mixEnvelop.setCurrPage(page);
        mixEnvelop.setPageSize(pageSize);
        mixEnvelop.setTotalCount(count);
        mixEnvelop.setDetailModelList(list);
        return mixEnvelop;
    }

    public void updateNationalDrugStatus(Integer id, Integer status) {
        nationalDrugDictDao.updateStatusById(id, status);
    }

    public BaseNationalDrugDictDO getNationalDrugById(Integer id) {
        return nationalDrugDictDao.findOne(id);
    }
}
