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

import com.yihu.jw.entity.hospital.prescription.WlyyPrescriptionDO;
import com.yihu.jw.entity.hospital.prescription.WlyyPrescriptionLogDO;
import com.yihu.jw.hospital.prescription.dao.PrescriptionDao;
import com.yihu.jw.hospital.prescription.dao.PrescriptionLogDao;
import com.yihu.mysql.query.BaseJpaService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by Trick on 2019/9/26.
 */
@Service
@Transactional
public class PrescriptionLogService  extends BaseJpaService<WlyyPrescriptionLogDO, PrescriptionLogDao> {

    private static Logger logger = LoggerFactory.getLogger(PrescriptionLogService.class);

    @Autowired
    private PrescriptionLogDao logDao;

    @Autowired
    private PrescriptionDao prescriptionDao;

    /**
     * 处方日志表
     * @param prescriptionCode
     * @param status
     * @param userType
     * @param userCode
     * @param userName
     * @param datajson
     * @param date
     * @return
     */
    public Boolean addPrescriptionLog(String prescriptionCode, Integer status, Integer userType, String userCode, String userName, String datajson, Date date){
        try{
            WlyyPrescriptionDO prescriptionDO = prescriptionDao.findOne(prescriptionCode);
            WlyyPrescriptionLogDO log = new WlyyPrescriptionLogDO();
            log.setOutpatientId(prescriptionDO.getOutpatientId());
            log.setPrescriptionCode(prescriptionCode);
            log.setStatus(status);
            log.setUserType(userType);
            log.setUserCode(userCode);
            log.setUserName(userName);
            log.setDatajson(datajson);
            log.setCreateTime(date);
            logDao.save(log);
            return true;
        }catch (Exception e){
            logger.error(e.toString());
        }
        return false;
    }

    /**
     * 查询处方日志
     * @param outpatientId
     * @return
     */
    public List<WlyyPrescriptionLogDO> findByOutpatientId(String outpatientId){
        return logDao.findByOutpatientIdOrderByCreateTimeDesc(outpatientId);
    }

    /**
     * 查询处方日志
     * @param prescriptionCode
     * @return
     */
    public List<WlyyPrescriptionLogDO> findPrescriptionLogByPreId(String prescriptionCode){
        return logDao.findByPrescriptionCodeOrderByCreateTimeDesc(prescriptionCode);
    }


    /**
     * 查询处方流程
     * @param prescriptionCode
     * @return
     */
    public List<WlyyPrescriptionLogDO> findByPrescriptionCodeByStatus(String prescriptionCode){
        List<WlyyPrescriptionLogDO> wlyyPrescriptionLogDOS = logDao.findByPrescriptionCodeByStatus(prescriptionCode);
        List<Integer> list = new ArrayList<>();
        List<WlyyPrescriptionLogDO> wlyyPrescriptionLogDOList=new ArrayList<>();
        for (WlyyPrescriptionLogDO prescriptionLogDO:wlyyPrescriptionLogDOS){
            if (list!=null&&list.size()!=0){
                if (!list.contains(prescriptionLogDO.getStatus())){
                    list.add(prescriptionLogDO.getStatus());
                    wlyyPrescriptionLogDOList.add(prescriptionLogDO);
                }
            }else {
                wlyyPrescriptionLogDOList.add(prescriptionLogDO);
            }
        }
        list.clear();
        return wlyyPrescriptionLogDOList;
    }
}
