package com.zd.airbottle.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.zd.airbottle.domain.vo.QpFlowDetailVO;
import com.zd.airbottle.mapper.QpFlowMapper;
import com.zd.common.core.domain.per.PerPrefix;
import com.zd.common.datascope.annotation.DataScope;
import com.zd.common.security.service.TokenService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.zd.airbottle.mapper.QpFlowDetailMapper;
import com.zd.airbottle.domain.QpFlowDetail;
import com.zd.airbottle.service.IQpFlowDetailService;

/**
 * 气瓶流程详情Service业务层处理
 *
 * @author zd
 * @date 2022-06-13
 */
@Service
public class QpFlowDetailServiceImpl implements IQpFlowDetailService
{
    @Autowired
    private QpFlowDetailMapper qpFlowDetailMapper;

    @Autowired
    private TokenService tokenService;

    @Autowired
    private QpFlowMapper qpFlowMapper;


    /**
     * 查询气瓶流程详情
     *
     * @param id 气瓶流程详情主键
     * @return 气瓶流程详情
     */
    @Override
    public QpFlowDetail selectQpFlowDetailById(Long id)
    {
        return qpFlowDetailMapper.selectQpFlowDetailById(id);
    }

    /**
     * 查询气瓶流程详情列表
     *
     * @param qpFlowDetail 气瓶流程详情
     * @return 气瓶流程详情
     */
    @Override
    @DataScope(deptAlias = "d", userAlias = "u", permi = PerPrefix.AIRBOTTLE_FLOWDETAIL)
    public List<QpFlowDetailVO> selectQpFlowDetailList(QpFlowDetailVO qpFlowDetail)
    {
        return qpFlowDetailMapper.selectQpFlowDetailList(qpFlowDetail);
    }

    @Override
    public List<QpFlowDetail> getListByIds(List<Long> ids){
        if (ids.isEmpty()){
            new ArrayList<>();
        }
        return qpFlowDetailMapper.getListByIds(ids);
    }

    /**
     * 新增气瓶流程详情
     *
     * @param qpFlow 气瓶流程详情
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertQpFlowDetail(QpFlowDetailVO qpFlow)
    {
        //设置其他公共字段
        //SaveUtil.setCommonAttr(qpFlow);


        List<QpFlowDetail> list = qpFlow.getFlowDetailList();
        if(list!=null && list.size()>0){
            for (QpFlowDetail detail:list) {
                detail.setCreateTime(new Date());
                detail.setUserType(2L);//实验中心

                QpFlowDetailVO fd= new QpFlowDetailVO();
                fd.setUserId(detail.getUserId());
                List<QpFlowDetailVO> fdlist = qpFlowDetailMapper.selectQpFlowDetailList(fd);
                if(fdlist.size()==0){
                    qpFlowDetailMapper.insertQpFlowDetail(detail);
                }
            }
        }
        return 1;
    }

    /**
     * 修改气瓶流程详情
     *
     * @param qpFlowDetail 气瓶流程详情
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateQpFlowDetail(QpFlowDetail qpFlowDetail)
    {
        return qpFlowDetailMapper.updateQpFlowDetail(qpFlowDetail);
    }

    /**
     * 批量删除气瓶流程详情
     *
     * @param ids 需要删除的气瓶流程详情主键
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteQpFlowDetailByIds(Long[] ids)
    {
        return qpFlowDetailMapper.deleteQpFlowDetailByIds(ids);
    }

    /**
     * 删除气瓶流程详情信息
     *
     * @param id 气瓶流程详情主键
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteQpFlowDetailById(Long id)
    {
        return qpFlowDetailMapper.deleteQpFlowDetailById(id);
    }

    /***
     * 实验中心-资格审核权限查询
     * @param userId
     * @return
     */
    @Override
    public QpFlowDetail selectZgCenterByUserIdIs(Long userId) {
        return qpFlowDetailMapper.selectZgCenterByUserIdIs(userId);
    }

    /***
     * 实验中心-用气审核权限查询
     * @param userId
     * @return
     */
    @Override
    public QpFlowDetail selectYqCenterByUserIdIs(Long userId) {
        return qpFlowDetailMapper.selectYqCenterByUserIdIs(userId);
    }
}
