package com.ruoyi.factory.service.impl;

import java.util.List;

import com.ruoyi.common.constant.UserConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.factory.domain.CMaterialTypeFactory;
import com.ruoyi.factory.mapper.CFactoryMapper;
import com.ruoyi.factory.domain.CFactory;
import com.ruoyi.factory.service.ICFactoryService;

/**
 * 车间管理Service业务层处理
 *
 * @author ruoyi
 * @date 2024-02-29
 */
@Service
public class CFactoryServiceImpl implements ICFactoryService
{
    @Autowired
    private CFactoryMapper cFactoryMapper;

    /**
     * 查询车间管理
     *
     * @param factoryId 车间管理主键
     * @return 车间管理
     */
    @Override
    public CFactory selectCFactoryByFactoryId(Long factoryId)
    {
        return cFactoryMapper.selectCFactoryByFactoryId(factoryId);
    }

    /**
     * 查询车间管理列表
     *
     * @param cFactory 车间管理
     * @return 车间管理
     */
    @Override
    public List<CFactory> selectCFactoryList(CFactory cFactory)
    {
        return cFactoryMapper.selectCFactoryList(cFactory);
    }

    /*物料类别列表*/
    @Override
    public List<CMaterialTypeFactory> selectCTypelist() {
        return cFactoryMapper.selectCTypelist();
    }

    /**
     * 新增车间管理
     *
     * @param cFactory 车间管理
     * @return 结果
     */
    @Transactional
    @Override
    public int insertCFactory(CFactory cFactory)
    {
        int rows = cFactoryMapper.insertCFactory(cFactory);
        insertCMaterialType(cFactory);
        return rows;
    }

    /**
     * 修改车间管理
     *
     * @param cFactory 车间管理
     * @return 结果
     */
    @Transactional
    @Override
    public int updateCFactory(CFactory cFactory)
    {
        cFactoryMapper.deleteCMaterialTypeByFactoryId(cFactory.getFactoryNo());
        insertCMaterialType(cFactory);
        return cFactoryMapper.updateCFactory(cFactory);
    }

    /**
     * 批量删除车间管理
     *
     * @param factoryIds 需要删除的车间管理主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteCFactoryByFactoryIds(Long[] factoryIds)
    {
        cFactoryMapper.deleteCMaterialTypeByFactoryIds(factoryIds);
        return cFactoryMapper.deleteCFactoryByFactoryIds(factoryIds);
    }

    /**
     * 删除车间管理信息
     *
     * @param factoryId 车间管理主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteCFactoryByFactoryId(Long factoryId)
    {
        CFactory f = cFactoryMapper.selectCFactoryByFactoryId(factoryId);
        cFactoryMapper.deleteCMaterialTypeByFactoryId(f.getFactoryNo());
        return cFactoryMapper.deleteCFactoryByFactoryId(factoryId);
    }

    /**
     * 新增物料类别信息
     *
     * @param cFactory 车间管理对象
     */
    public void insertCMaterialType(CFactory cFactory)
    {
        List<CMaterialTypeFactory> cMaterialTypeList = cFactory.getCMaterialTypeList();
        String factoryNo = cFactory.getFactoryNo();
        if (StringUtils.isNotNull(cMaterialTypeList))
        {
            List<CMaterialTypeFactory> list = new ArrayList<CMaterialTypeFactory>();
            for (CMaterialTypeFactory cMaterialType : cMaterialTypeList)
            {
                cMaterialType.setFactoryId(factoryNo);
                list.add(cMaterialType);
            }
            if (list.size() > 0)
            {
                cFactoryMapper.batchCMaterialType(list);
            }
        }
    }

    /**
     * 校验车间编码是否存在
     * @param cFactory
     * @return
     */
    @Override
    public boolean checkFactoryNoUnique(CFactory cFactory) {
        Long factoryId = StringUtils.isNull(cFactory.getFactoryId()) ? -1L : cFactory.getFactoryId();
        CFactory info = cFactoryMapper.checkFactoryNoUnique(cFactory.getFactoryNo());
        if (StringUtils.isNotNull(info) && info.getFactoryId().longValue() != factoryId.longValue())
        {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }
}
