package com.ruoyi.models.service.impl;

import java.util.List;

import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.models.mapper.BizSubModuleMapper;
import com.ruoyi.models.mapper.BizModuleMapper;
import com.ruoyi.models.domain.BizSubModule;
import com.ruoyi.models.domain.BizModule;
import com.ruoyi.models.service.IBizSubModuleService;

/**
 * 子模块Service业务层处理
 */
@Service
public class BizSubModuleServiceImpl implements IBizSubModuleService
{
    @Autowired
    private BizSubModuleMapper subMapper;

    @Autowired
    private BizModuleMapper moduleMapper;

    @Override
    public BizSubModule selectBizSubModuleBySubId(Long subId)
    {
        return subMapper.selectBizSubModuleBySubId(subId);
    }

    @Override
    public List<BizSubModule> selectBizSubModuleList(BizSubModule subModule)
    {
        return subMapper.selectBizSubModuleList(subModule);
    }

    @Override
    public int insertBizSubModule(BizSubModule subModule)
    {
        subModule.setCreateTime(DateUtils.getNowDate());
        subModule.setStatus(subModule.getStatus() == null ? "0" : subModule.getStatus());
        return subMapper.insertBizSubModule(subModule);
    }

    @Override
    public int updateBizSubModule(BizSubModule subModule)
    {
        subModule.setUpdateTime(DateUtils.getNowDate());
        return subMapper.updateBizSubModule(subModule);
    }

    @Override
    public int deleteBizSubModuleBySubIds(Long[] subIds)
    {
        return subMapper.deleteBizSubModuleBySubIds(subIds);
    }

    @Override
    public int deleteBizSubModuleBySubId(Long subId)
    {
        return subMapper.deleteBizSubModuleBySubId(subId);
    }

    @Override
    public List<BizSubModule> selectByModuleId(Long moduleId)
    {
        return subMapper.selectByModuleId(moduleId);
    }

    @Override
    public int receiveSubModule(Long subId, Long userId)
    {
        BizSubModule sub = selectBizSubModuleBySubId(subId);
        if (sub == null || !"0".equals(sub.getStatus()))
        {
            throw new RuntimeException("子模块不存在或状态异常");
        }

        BizModule module = moduleMapper.selectBizModuleByModuleId(sub.getModuleId());
        if (module == null)
        {
            throw new RuntimeException("父模块不存在");
        }
        if (!String.valueOf(userId).equals(module.getAssignee()))
        {
            throw new RuntimeException("仅父模块接取人可接取子模块");
        }

        sub.setStatus("1");
        sub.setUpdateBy(String.valueOf(userId));
        sub.setUpdateTime(DateUtils.getNowDate());
        return updateBizSubModule(sub);
    }

    @Override
    public int completeSubModule(Long subId, Long userId)
    {
        BizSubModule sub = selectBizSubModuleBySubId(subId);
        if (sub == null || !"1".equals(sub.getStatus()))
        {
            throw new RuntimeException("子模块不存在或状态异常");
        }

        BizModule module = moduleMapper.selectBizModuleByModuleId(sub.getModuleId());
        if (module == null)
        {
            throw new RuntimeException("父模块不存在");
        }
        if (!String.valueOf(userId).equals(module.getAssignee()))
        {
            throw new RuntimeException("仅父模块接取人可完成子模块");
        }

        sub.setStatus("2");
        sub.setUpdateBy(String.valueOf(userId));
        sub.setUpdateTime(DateUtils.getNowDate());
        return updateBizSubModule(sub);
    }
}


