package com.jjt.ws.service.impl;

import com.jjt.common.utils.StringUtils;
import com.jjt.ws.domain.TwinWorkshop;
import com.jjt.ws.domain.TwinWorkshopEnergy;
import com.jjt.ws.mapper.TwinWorkshopMapper;
import com.jjt.ws.service.ITwinWorkshopService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 车间管理Service业务层处理
 *
 * @author wukai
 * @date 2025-01-17
 */
@Service
public class TwinWorkshopServiceImpl implements ITwinWorkshopService {
    @Resource
    private TwinWorkshopMapper twinWorkshopMapper;

    /**
     * 查询车间管理
     *
     * @param wsId 车间管理主键
     * @return 车间管理
     */
    @Override
    public TwinWorkshop selectTwinWorkshopByWsId(Long wsId) {
        return twinWorkshopMapper.selectTwinWorkshopByWsId(wsId);
    }

    /**
     * 查询车间管理列表
     *
     * @param twinWorkshop 车间管理
     * @return 车间管理
     */
    @Override
    public List<TwinWorkshop> selectTwinWorkshopList(TwinWorkshop twinWorkshop) {
        return twinWorkshopMapper.selectTwinWorkshopList(twinWorkshop);
    }

    /**
     * 新增车间管理
     *
     * @param twinWorkshop 车间管理
     * @return 结果
     */
    @Transactional
    @Override
    public int insertTwinWorkshop(TwinWorkshop twinWorkshop) {
        int rows = twinWorkshopMapper.insertTwinWorkshop(twinWorkshop);
        insertTwinWorkshopEnergy(twinWorkshop);
        return rows;
    }

    /**
     * 修改车间管理
     *
     * @param twinWorkshop 车间管理
     * @return 结果
     */
    @Transactional
    @Override
    public int updateTwinWorkshop(TwinWorkshop twinWorkshop) {
        twinWorkshopMapper.deleteTwinWorkshopEnergyByWsId(twinWorkshop.getWsId());
        insertTwinWorkshopEnergy(twinWorkshop);
        return twinWorkshopMapper.updateTwinWorkshop(twinWorkshop);
    }

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

    /**
     * 删除车间管理信息
     *
     * @param wsId 车间管理主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteTwinWorkshopByWsId(Long wsId) {
        twinWorkshopMapper.deleteTwinWorkshopEnergyByWsId(wsId);
        return twinWorkshopMapper.deleteTwinWorkshopByWsId(wsId);
    }

    /**
     * 新增车间能源管理信息
     *
     * @param twinWorkshop 车间管理对象
     */
    public void insertTwinWorkshopEnergy(TwinWorkshop twinWorkshop) {
        List<TwinWorkshopEnergy> twinWorkshopEnergyList = twinWorkshop.getTwinWorkshopEnergyList();
        Long wsId = twinWorkshop.getWsId();
        if (StringUtils.isNotNull(twinWorkshopEnergyList)) {
            List<TwinWorkshopEnergy> list = new ArrayList<TwinWorkshopEnergy>();
            for (TwinWorkshopEnergy twinWorkshopEnergy : twinWorkshopEnergyList) {
                twinWorkshopEnergy.setWsId(wsId);
                list.add(twinWorkshopEnergy);
            }
            if (list.size() > 0) {
                twinWorkshopMapper.batchTwinWorkshopEnergy(list);
            }
        }
    }

    /**
     * 查询车间管理
     *
     * @param wsCode 车间编码
     * @return 车间管理
     */
    @Override
    public TwinWorkshop selectTwinWorkshopByWsCode(String wsCode) {
        return twinWorkshopMapper.selectTwinWorkshopByWsCode(wsCode);
    }
}
