package com.emr.service.emrFunGroupDistribution;

import com.emr.dao.CommomMapper;
import com.emr.dao.Emr_Fun_Group_DistributionMapper;
import com.emr.entity.Emr_Fun_Group_Distribution;
import com.emr.entity.Power_User;
import com.emr.vo.Emr_Fun_Group_DistributionVo;
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;

/**
 * @ProjectName:
 * @Description:
 * @Param 传输参数
 * @Return
 * @Author: 曾文和
 * @CreateDate: 2019/11/14 16:32
 * @UpdateUser: 曾文和
 * @UpdateDate: 2019/11/14 16:32
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
@Service
@Transactional
public class FunGroupDistributionServiceImpl implements FunGroupDistributionService
{
    @Autowired
    private Emr_Fun_Group_DistributionMapper distributionMapper;
    @Autowired
    private CommomMapper commomMapper;
    @Override
    public int SimpleInsert(Emr_Fun_Group_DistributionVo distributionVo, 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 userIds = distributionVo.getUserIds();
        String userNames = distributionVo.getUserNames();
        if(StringUtils.isNoneBlank(userIds)){
            //查询表列数
            int colCount = commomMapper.selectColByTableName("emr_fun_group_distribution");
            int simpleInsertCount = 2100/colCount;
            List<Emr_Fun_Group_Distribution> list = new ArrayList<>();
            String[] userIdList = userIds.split(",");
            String[] userNameList = userNames.split(",");
            for (int i = 0; i < userIdList.length; i++) {
                //处理批量添加sql太长问题，设定个数为180
                if(i > 0 && list.size()%simpleInsertCount == 0){
                    list.clear();
                }
                if(StringUtils.isNotBlank(userIdList[i])){
                    Emr_Fun_Group_Distribution distribution = new Emr_Fun_Group_Distribution();
                    distribution.setTypeFlag(distributionVo.getTypeFlag());
                    distribution.setUserId(Integer.valueOf(userIdList[i]));
                    distribution.setUserName(userNameList[i]);
                    distribution.setEffective(distributionVo.getEffective());
                    distribution.setEffectiveTime(distributionVo.getEffectiveTime());
                    distribution.setRemark(distributionVo.getRemark());
                    distribution.setCreater(userName);
                    distribution.setUpdater(userName);
                    distribution.setCreateDate(fmt.format(new Date()));
                    distribution.setUpdateDate(fmt.format(new Date()));
                    list.add(distribution);
                }
                //是180的倍数或最后一个
                if((list.size()%simpleInsertCount == 0 || i == userIdList.length - 1)){
                    distributionMapper.SimpleInsert(list);
                }
            }
            return 1;
        }else{
            return 0;
        }
    }

    @Override
    public List<Emr_Fun_Group_Distribution> selectUserIdsByTypeFlag(String typeFlag,Integer userId) {
        return distributionMapper.selectUserIdsByTypeFlag(typeFlag,userId);
    }

    @Override
    public List<Emr_Fun_Group_DistributionVo> selectAll(Emr_Fun_Group_Distribution record, String startTime, String endTime) {
        return distributionMapper.selectAll(record, startTime, endTime);
    }

    @Override
    public Emr_Fun_Group_Distribution selectByPrimaryKey(Integer id) {
        return distributionMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(Emr_Fun_Group_Distribution record,HttpServletRequest request) {
        Power_User user = (Power_User)request.getSession().getAttribute("CURRENT_USER");
        record.setUpdater(user.getUserName());
        SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        record.setUpdateDate(fmt.format(new Date()));
        return distributionMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public Emr_Fun_Group_Distribution checkTypeFlagAndUserName(String typeFlag, String userName) {
        return distributionMapper.checkTypeFlagAndUserName(typeFlag, userName);
    }

    @Override
    public int deleteByPrimaryKey(Integer id) {
        return distributionMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int deleteByIds(String ids) {
        return distributionMapper.deleteByIds(ids);
    }

    @Override
    public List<Emr_Fun_Group_DistributionVo> selectAllByIds(String ids) {
        return distributionMapper.selectAllByIds(ids);
    }
}
