package com.emr.service.emrLock;

import com.emr.dao.CommomMapper;
import com.emr.dao.recordLock.Emr_LockMapper;
import com.emr.entity.Power_User;
import com.emr.entity.recordLock.Emr_Lock;
import com.emr.vo.EmrLockVo;
import com.emr.vo.FontVo.LockVo;
import com.emr.vo.commomSearch.CommomVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class EmrLockServiceImpl{
    @Autowired
    private Emr_LockMapper lockMapper;
    @Autowired
    private CommomMapper commomMapper;

    public void insert(List<CommomVo> commomtables, HttpServletRequest request) {
        Power_User user = (Power_User)request.getSession().getAttribute("CURRENT_USER");
        Integer userId = user.getUserId();
        SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //查询已存在未锁的记录
        List<Emr_Lock> locks = lockMapper.selectLockByPatientIds(commomtables, 0);
        List<Emr_Lock> list = new ArrayList<>();
        if(null != locks && !locks.isEmpty()){
            //存在未锁定记录
            //批量修改
            Emr_Lock lock = new Emr_Lock();
            lock.setUserId(userId);
            lock.setLocker(user.getUserName());
            lock.setLockeTime(fmt.format(new Date()));
            lockMapper.udpateLockByPatientIds(commomtables,lock);
            for (int i = 0; i < commomtables.size(); i++) {
                for (Emr_Lock lock1 : locks) {
                    if (lock1.getPatientId().equals(commomtables.get(i).getPatientId())) {
                        commomtables.remove(i);
                    }
                }
            }
        }
        if(null != commomtables && !commomtables.isEmpty()){
            ///查询表列数
            int colCount = commomMapper.selectColByTableName("emr_lock");
            int simpleInsertCount = 2100/colCount;
            for (int i = 0; i < commomtables.size(); i++) {
                //处理批量添加sql太长问题，设定个数为180
                if(i != 0 && list.size()%simpleInsertCount == 0){
                    list.clear();
                }
                Emr_Lock lock = new Emr_Lock();
                lock.setUserId(userId);
                lock.setLocker(user.getUserName());
                lock.setLockeTime(fmt.format(new Date()));
                lock.setPatientId(commomtables.get(i).getPatientId());
                if(null != commomtables.get(i).getAdmissTimes()){
                    lock.setAdmissTimes(commomtables.get(i).getAdmissTimes().toString());
                }else{
                    lock.setAdmissTimes("1");
                }
                lock.setInpatientNo(commomtables.get(i).getInpatientNo());
                lock.setLockState(1);
                list.add(lock);
                //是180的倍数或最后一个
                if(list.size()%simpleInsertCount == 0 || i == commomtables.size() - 1){
                    lockMapper.SimpleInsert(list);
                }
            }
        }
    }

    public void udpate(Emr_Lock lock,HttpServletRequest request) {
        Power_User user = (Power_User)request.getSession().getAttribute("CURRENT_USER");
        SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        if(lock.getLockState() == 1){
            lock.setLockState(0);
            lock.setUnlocker(user.getUserName());
            lock.setUnlockeTime(fmt.format(new Date()));
        }else{
            lock.setLockState(1);
            lock.setLocker(user.getUserName());
            lock.setLockeTime(fmt.format(new Date()));
        }
        lockMapper.updateByPrimaryKeySelective(lock);
    }

    public List<Emr_Lock> selectLockListByStatus(Integer lockStatus) {
        return lockMapper.selectLockListByStatus(lockStatus);
    }

    public void udpateUnlockByPatients(String patientIds,HttpServletRequest request) {
        Power_User user = (Power_User)request.getSession().getAttribute("CURRENT_USER");
        String userName = user.getUserName();
        SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String unlockeTime = fmt.format(new Date());
        lockMapper.udpateUnlockByPatients(patientIds,userName,unlockeTime);
    }


    public List<EmrLockVo> getLockList(EmrLockVo lockVo){
        return lockMapper.getLockList(lockVo);
    }
}
