package com.yanzhu.jh.trouble.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.yanzhu.jh.trouble.domain.SmzSspFileinfo;
import com.yanzhu.jh.trouble.domain.SmzSspProblemmodify;
import com.yanzhu.jh.trouble.mapper.SmzSspProblemmodifyMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.yanzhu.jh.trouble.mapper.SmzSspAuditinfoMapper;
import com.yanzhu.jh.trouble.domain.SmzSspAuditinfo;
import com.yanzhu.jh.trouble.service.ISmzSspAuditinfoService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 随手拍流程信息Service业务层处理
 * 
 * @author JiangYuQi
 * @date 2023-08-07
 */
@Service
public class SmzSspAuditinfoServiceImpl implements ISmzSspAuditinfoService 
{
    @Autowired
    private SmzSspAuditinfoMapper smzSspAuditinfoMapper;

    @Autowired
    private SmzSspProblemmodifyMapper smzSspProblemmodifyMapper;

    /**
     * 查询随手拍流程信息
     * 
     * @param id 随手拍流程信息主键
     * @return 随手拍流程信息
     */
    @Override
    public SmzSspAuditinfo selectSmzSspAuditinfoById(Long id)
    {
        return smzSspAuditinfoMapper.selectSmzSspAuditinfoById(id);
    }

    /**
     * 查询随手拍流程信息
     *
     * @param mainId 随手拍流程信息主键
     * @return 随手拍流程信息
     */
    @Override
    public SmzSspAuditinfo selectLastSmzSspAuditinfoByMainId(Long mainId) {
        return smzSspAuditinfoMapper.selectLastSmzSspAuditinfoByMainId(mainId);
    }
    /**
     * 查询随手拍流程信息列表
     * 
     * @param smzSspAuditinfo 随手拍流程信息
     * @return 随手拍流程信息
     */
    @Override
    public List<SmzSspAuditinfo> selectSmzSspAuditinfoList(SmzSspAuditinfo smzSspAuditinfo)
    {
        return smzSspAuditinfoMapper.selectSmzSspAuditinfoList(smzSspAuditinfo);
    }

    /**
     * 新增随手拍流程信息
     * 
     * @param smzSspAuditinfo 随手拍流程信息
     * @return 结果
     */
    @Override
    @Transactional
    public int insertSmzSspAuditinfo(SmzSspAuditinfo smzSspAuditinfo)
    {
        smzSspAuditinfo.setCreateTime(DateUtils.getNowDate());
        int res = smzSspAuditinfoMapper.insertSmzSspAuditinfo(smzSspAuditinfo);
        SmzSspProblemmodify smzSspProblemmodify = smzSspProblemmodifyMapper.selectSmzSspProblemmodifyById(smzSspAuditinfo.getMainId());
        switch (smzSspAuditinfo.getProcessState()){
            case "0": smzSspProblemmodify.setCheckState(1L); break;
            case "2": smzSspProblemmodify.setCheckState(3L); break;
            case "4": smzSspProblemmodify.setCheckState(4L); break;
            default:break;
        }
        smzSspProblemmodifyMapper.updateSmzSspProblemmodify(smzSspProblemmodify);
        insertSmzSspFileinfo(smzSspAuditinfo);
        return res;
    }

    /**
     * 新增随手拍文件信息信息
     *
     * @param smzSspAuditinfo 随手拍流程信息对象
     */
    private void insertSmzSspFileinfo(SmzSspAuditinfo smzSspAuditinfo)
    {
        if(smzSspAuditinfo.getFileUrls()!=null){
            String fileUrls[] = smzSspAuditinfo.getFileUrls().split(",");
            Long id = smzSspAuditinfo.getId();
            if (StringUtils.isNotNull(fileUrls))
            {
                List<SmzSspFileinfo> list = new ArrayList<SmzSspFileinfo>();
                for (String fileUrl: fileUrls)
                {
                    SmzSspFileinfo smzSspFileinfo = new SmzSspFileinfo();
                    smzSspFileinfo.setAuditId(id);
                    smzSspFileinfo.setFileType(0L);
                    smzSspFileinfo.setFileUrl(fileUrl);
                    smzSspFileinfo.setIsDel(0L);
                    smzSspFileinfo.setCreateUser(smzSspAuditinfo.getCreateUser());
                    smzSspFileinfo.setCreateTime(new Date());
                    list.add(smzSspFileinfo);
                }
                if (list.size() > 0)
                {
                    smzSspAuditinfoMapper.batchSmzSspFileinfo(list);
                }
            }
        }
    }

    /**
     * 修改随手拍流程信息
     * 
     * @param smzSspAuditinfo 随手拍流程信息
     * @return 结果
     */
    @Override
    public int updateSmzSspAuditinfo(SmzSspAuditinfo smzSspAuditinfo)
    {
        return smzSspAuditinfoMapper.updateSmzSspAuditinfo(smzSspAuditinfo);
    }

    /**
     * 批量删除随手拍流程信息
     * 
     * @param ids 需要删除的随手拍流程信息主键
     * @return 结果
     */
    @Override
    public int deleteSmzSspAuditinfoByIds(Long[] ids)
    {
        return smzSspAuditinfoMapper.deleteSmzSspAuditinfoByIds(ids);
    }

    /**
     * 删除随手拍流程信息信息
     * 
     * @param id 随手拍流程信息主键
     * @return 结果
     */
    @Override
    public int deleteSmzSspAuditinfoById(Long id)
    {
        return smzSspAuditinfoMapper.deleteSmzSspAuditinfoById(id);
    }
}
