package com.yunfeiyun.agmp.pts.service.impl;

import java.util.List;
import java.util.stream.Collectors;

import com.yunfeiyun.agmp.pts.mapper.PtsTutorialMapper;
import com.yunfeiyun.agmp.pts.service.IPtsTutorialService;
import com.yunfeiyun.agmp.common.constant.ResConstants;
import com.yunfeiyun.agmp.common.core.domain.model.LoginUser;
import com.yunfeiyun.agmp.common.framework.manager.ResManager;
import com.yunfeiyun.agmp.common.utils.DateUtils;
import com.yunfeiyun.agmp.common.web.system.domain.SysRes;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.yunfeiyun.agmp.pts.domain.PtsTutorial;

/**
 * Service业务层处理
 * 
 * @author 杨晓辉
 * @date 2023-05-23
 */
@Service
public class PtsTutorialServiceImpl implements IPtsTutorialService
{
    @Autowired
    private PtsTutorialMapper ptsTutorialMapper;

    @Autowired
    ResManager resManager;


    /**
         * 查询列表
     * 
     * @param ptsTutorial
     * @return
     */
    @Override
    public List<PtsTutorial> selectPtsTutorialList(PtsTutorial ptsTutorial)
    {
        List<PtsTutorial> list = ptsTutorialMapper.selectPtsTutorialList(ptsTutorial);
        return list;
    }


    /**
     * 查询
     *
     * @param tutorialId 主键
     * @return
     */
    @Override
    public PtsTutorial selectPtsTutorialByTutorialId(String tutorialId)
    {
        PtsTutorial ptsTutorial = new PtsTutorial();
        ptsTutorial.setTutorialId( tutorialId );
        PtsTutorial tutorial = ptsTutorialMapper.selectPtsTutorialByTutorialId(ptsTutorial);
        return tutorial;
    }



    /**
     * 新增
     * 
     * @param ptsTutorial
     * @return 结果
     */
    @Override
    public int insertPtsTutorial(PtsTutorial ptsTutorial, LoginUser user)
    {
        try {
            ptsTutorial.setTutorialCreator(user.getUserId());
            ptsTutorial.setTutorialCreatedate(DateUtils.dateTimeNow());
            ptsTutorial.setTutorialId(ptsTutorial.getId());
            int n = ptsTutorialMapper.insertPtsTutorial(ptsTutorial);
            ptsTutorial.getResList().stream().map(v->{
                v.setResBusId(ptsTutorial.getTutorialId());
                return v;
            }).collect(Collectors.toList());
            SysRes.add(ptsTutorial.getResList(),ResConstants.formType.ADD);
            return n;
        }catch (Exception e){
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * 修改
     * 
     * @param ptsTutorial
     * @return 结果
     */
    @Override
    public int updatePtsTutorial(PtsTutorial ptsTutorial,LoginUser user)
    {
        try {
            ptsTutorial.setTutorialCreator(user.getUserId());
            ptsTutorial.setTutorialCreatedate(DateUtils.dateTimeNow());
            int n = ptsTutorialMapper.updatePtsTutorial(ptsTutorial);
            SysRes.add(ptsTutorial.getResList(),ResConstants.formType.ADD);
            return n;
        }catch (Exception e){
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * 批量删除
     * 
     * @param tutorialIds 需要删除的主键
     * @return 结果
     */
    @Override
    public int deletePtsTutorialByTutorialIds(String[] tutorialIds)
    {
        return ptsTutorialMapper.deletePtsTutorialByTutorialIds(tutorialIds);
    }

    /**
     * 删除信息
     * 
     * @param tutorialId 主键
     * @return 结果
     */
    @Override
    public int deletePtsTutorialByTutorialId(String tutorialId)
    {
        return ptsTutorialMapper.deletePtsTutorialByTutorialId(tutorialId);
    }
}
