package com.emr.service.recordType;

import com.emr.dao.CommomMapper;
import com.emr.dao.recordType.Emr_Type_RelatedMapper;
import com.emr.entity.Power_User;
import com.emr.entity.recordType.Emr_Type_Related;
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;

/**
 * @ProjectName:
 * @Description:
 * @Param 传输参数
 * @Return
 * @Author: 曾文和
 * @CreateDate: 2019/6/5 11:49
 * @UpdateUser: 曾文和
 * @UpdateDate: 2019/6/5 11:49
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
@Service
@Transactional
public class EmrTypeRelatedServiceImpl implements EmrTypeRelatedService{
    @Autowired
    private Emr_Type_RelatedMapper emrTypeRelatedMapper;
    @Autowired
    private CommomMapper commomMapper;
    @Override
    public List<Emr_Type_Related> selectAssortIdsByTypeId(Integer typeId) {
        return emrTypeRelatedMapper.selectAssortIdsByTypeId(typeId);
    }

    @Override
    public void deleteByTypeId(Integer typeId) {
        emrTypeRelatedMapper.deleteByTypeId(typeId);
    }

    @Override
    public int insert(Emr_Type_Related record, HttpServletRequest request) {
        Power_User user = (Power_User)request.getSession().getAttribute("CURRENT_USER");
        String creater = user.getUserName();
        SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        record.setCreater(creater);
        record.setCreateTime(fmt.format(new Date()));
        record.setUpdater(creater);
        record.setUpdateTime(fmt.format(new Date()));
        return emrTypeRelatedMapper.insert(record);
    }

    @Override
    public int SimpleInsert(String[] selectList,Integer typeId,HttpServletRequest request) {
        Power_User user = (Power_User)request.getSession().getAttribute("CURRENT_USER");
        String creater = user.getUserName();
        SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        List<Emr_Type_Related> list = new ArrayList<>();
        ///查询表列数
        int colCount = commomMapper.selectColByTableName("emr_type_related");
        int simpleInsertCount = 2100/colCount;
        for (int i = 0; i < selectList.length; i++) {
            //处理批量添加sql太长问题，设定个数为180
            if(i > 0 && list.size()%simpleInsertCount == 0){
                list.clear();
            }
            Emr_Type_Related related = new Emr_Type_Related();
            related.setTypeId(typeId);
            related.setAssortId(selectList[i]);
            related.setCreater(creater);
            related.setCreateTime(fmt.format(new Date()));
            related.setUpdater(creater);
            related.setUpdateTime(fmt.format(new Date()));
            list.add(related);
            //是180的倍数或最后一个
            if((list.size()%simpleInsertCount == 0 || i == selectList.length - 1)){
                emrTypeRelatedMapper.SimpleInsert(list);
            }
        }
        return 1;
    }
}
