package com.zd.airbottle.service.impl;

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

import com.zd.airbottle.domain.vo.QpFlowVO;
import com.zd.common.core.exception.ServiceException;
import com.zd.common.security.service.TokenService;

import com.zd.system.api.domain.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.zd.airbottle.mapper.QpFlowMapper;
import com.zd.airbottle.domain.QpFlow;
import com.zd.airbottle.service.IQpFlowService;

/**
 * 气瓶审核流程Service业务层处理
 * 
 * @author zd
 * @date 2022-06-13
 */
@Service
public class QpFlowServiceImpl implements IQpFlowService 
{
    @Autowired
    private QpFlowMapper qpFlowMapper;

    //@Autowired
    //private QpFlowDetailMapper qpFlowDetailMapper;

    @Autowired
    private TokenService tokenService;


    /**
     * 查询气瓶审核流程
     * 
     * @param id 气瓶审核流程主键
     * @return 气瓶审核流程
     */
    @Override
    public QpFlow selectQpFlowById(Long id)
    {
        return qpFlowMapper.selectQpFlowById(id);
    }

    /**
     * 查询气瓶审核流程列表
     * 
     * @param qpFlow 气瓶审核流程
     * @return 气瓶审核流程
     */
    @Override
    public List<QpFlow> selectQpFlowList(QpFlow qpFlow)
    {
        return qpFlowMapper.selectQpFlowList(qpFlow);
    }

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

    /**
     * 新增气瓶审核流程
     * 
     * @param qpFlow 气瓶审核流程
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertQpFlow(QpFlowVO qpFlow)
    {
        QpFlow flow = new QpFlow();
        flow.setFlowType(qpFlow.getFlowType());
        List<QpFlow> list =  qpFlowMapper.selectQpFlowList(flow);
        if(list.size()>0){
            throw new ServiceException("该类型的流程已存在，无需重复添加！");
        }

        SysUser sysUser = tokenService.getLoginUser().getSysUser();
        String uuid = UUID.randomUUID().toString();
        qpFlow.setUuid(uuid);
        qpFlow.setDeptId(sysUser.getDept().getDeptId());
        qpFlow.setDeptName(sysUser.getDept().getDeptName());
        qpFlow.setCreateUserid(sysUser.getUserId());
        qpFlow.setCreateUser(sysUser.getNickName());
        qpFlow.setCreateTime(new Date());

        /*List<QpFlowDetail> listDetail = qpFlow.getFlowDetailList();
        if(StringUtils.isNotEmpty(listDetail)){
            for (QpFlowDetail detail:listDetail) {
                // 审核流程详情表数据录入
                detail.setUuid(uuid);
                qpFlowDetailMapper.insertQpFlowDetail(detail);
            }
        }*/
        //流程数据录入
        return qpFlowMapper.insertQpFlow(qpFlow);
    }

    /**
     * 修改气瓶审核流程
     * 
     * @param qpFlow 气瓶审核流程
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateQpFlow(QpFlow qpFlow)
    {
        return qpFlowMapper.updateQpFlow(qpFlow);
    }

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

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