package com.emr.service.batchApply;

import com.emr.dao.BatchApplyDetailsMapper;
import com.emr.dao.BatchApplyMapper;
import com.emr.dao.Emr_DictionaryMapper;
import com.emr.entity.BatchApply;
import com.emr.entity.BatchApplyDetails;
import com.emr.entity.Emr_Dictionary;
import com.emr.entity.Power_User;
import com.emr.util.ListUtil;
import com.emr.util.ListUtils;
import com.emr.util.Setters;
import com.emr.vo.BatchApplyDetailsVo;
import com.emr.vo.BatchApplyVo;
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 org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

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

/**
 * @ClassName BatchApplyServiceImpl
 * @Description 批量借阅实现类
 * @Author linjj
 * @Date 2023/8/28 17:10
 * @Version 1.0
 */
@Service
@Transactional
public class BatchApplyServiceImpl implements BatchApplyService {
    @Autowired
    BatchApplyMapper batchApplyMapper;
    @Autowired
    Emr_DictionaryMapper emrDictionaryMapper;
    @Autowired
    BatchApplyDetailsMapper batchApplyDetailsMapper;
    @Override
    public List<BatchApply> selectBatchApply(BatchApplyVo batchApplyVo) {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
                .getRequest();
        // 从session获取用户名
        Power_User user = (Power_User) request.getSession().getAttribute("CURRENT_USER");
        if (user != null) {
            Integer roleId = user.getRoleId();
            if (roleId != 0 || roleId != -100) {
                batchApplyVo.setCreateName(user.getUserName());
            }
        }
        List<BatchApply> batchApplies = batchApplyMapper.selectBatchApply(batchApplyVo);
        for (BatchApply list:batchApplies){
            //判断该批次审批状态
            if (list.getReadNums()==0){
                list.setStatc(0);
                continue;
            }
            if (list.getReadNums()==list.getBorrowingNum()){
                list.setStatc(2);
                continue;
            }
            if (list.getReadNums()<list.getBorrowingNum()){
                list.setStatc(3);
            }
        }
        return batchApplies;
    }

    @Override
    public int insertBatch(BatchApplyVo batchApplyVo) {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
                .getRequest();
        // 从session获取用户名
        Power_User user = (Power_User) request.getSession().getAttribute("CURRENT_USER");
        if (user != null) {
            //初始化批量申请
            String username = user.getUserName();
            batchApplyVo.setCreateName(username);
            batchApplyVo.setStatc(0);
            batchApplyVo.setReadNum(0);
            batchApplyVo.setBorrowingNum(0);
            //获取系统当前时间
            SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
            batchApplyVo.setCreateDate(fmt.format(new Date()));
        }
        return batchApplyMapper.insertBatch(batchApplyVo);
    }

    @Override
    public int updateBatch(BatchApplyVo batchApplyVo) {
        return batchApplyMapper.updateBatch(batchApplyVo);
    }

    @Override
    public int deleteById(int id) {
        batchApplyDetailsMapper.deleteByBatchId(id);
        return batchApplyMapper.deleteById(id);
    }

}
