package com.hw.dms.service.impl;

import java.util.List;
import com.hw.common.core.utils.DateUtils;
import com.hw.system.api.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.hw.common.core.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.hw.dms.domain.DmsFlowCheckplanDetail;
import com.hw.dms.mapper.DmsFlowCheckplanMapper;
import com.hw.dms.domain.DmsFlowCheckplan;
import com.hw.dms.service.IDmsFlowCheckplanService;

/**
 * 设备盘点计划Service业务层处理
 *
 * @author xins
 * @date 2023-12-29
 */
@Service
public class DmsFlowCheckplanServiceImpl implements IDmsFlowCheckplanService
{
    @Autowired
    private DmsFlowCheckplanMapper dmsFlowCheckplanMapper;

    /**
     * 查询设备盘点计划
     *
     * @param flowCheckplanId 设备盘点计划主键
     * @return 设备盘点计划
     */
    @Override
    public DmsFlowCheckplan selectDmsFlowCheckplanByFlowCheckplanId(Long flowCheckplanId)
    {
        return dmsFlowCheckplanMapper.selectDmsFlowCheckplanByFlowCheckplanId(flowCheckplanId);
    }

    /**
     * 查询设备盘点计划列表
     *
     * @param dmsFlowCheckplan 设备盘点计划
     * @return 设备盘点计划
     */
    @Override
    public List<DmsFlowCheckplan> selectDmsFlowCheckplanList(DmsFlowCheckplan dmsFlowCheckplan)
    {
        return dmsFlowCheckplanMapper.selectDmsFlowCheckplanList(dmsFlowCheckplan);
    }

    /**
     * 新增设备盘点计划
     *
     * @param dmsFlowCheckplan 设备盘点计划
     * @return 结果
     */
    @Transactional
    @Override
    public int insertDmsFlowCheckplan(DmsFlowCheckplan dmsFlowCheckplan)
    {
        LoginUser loginUser = new LoginUser();
        dmsFlowCheckplan.setCreateBy(loginUser.getUsername());
        dmsFlowCheckplan.setCreateTime(DateUtils.getNowDate());
        int rows = dmsFlowCheckplanMapper.insertDmsFlowCheckplan(dmsFlowCheckplan);
        insertDmsFlowCheckplanDetail(dmsFlowCheckplan);
        return rows;
    }

    /**
     * 修改设备盘点计划
     *
     * @param dmsFlowCheckplan 设备盘点计划
     * @return 结果
     */
    @Transactional
    @Override
    public int updateDmsFlowCheckplan(DmsFlowCheckplan dmsFlowCheckplan)
    {
        LoginUser loginUser = new LoginUser();
        dmsFlowCheckplan.setUpdateBy(loginUser.getUsername());
        dmsFlowCheckplan.setUpdateTime(DateUtils.getNowDate());
        dmsFlowCheckplanMapper.deleteDmsFlowCheckplanDetailByFlowCheckplanId(dmsFlowCheckplan.getFlowCheckplanId());
        insertDmsFlowCheckplanDetail(dmsFlowCheckplan);
        return dmsFlowCheckplanMapper.updateDmsFlowCheckplan(dmsFlowCheckplan);
    }

    /**
     * 批量删除设备盘点计划
     *
     * @param flowCheckplanIds 需要删除的设备盘点计划主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteDmsFlowCheckplanByFlowCheckplanIds(Long[] flowCheckplanIds)
    {
        dmsFlowCheckplanMapper.deleteDmsFlowCheckplanDetailByFlowCheckplanIds(flowCheckplanIds);
        return dmsFlowCheckplanMapper.deleteDmsFlowCheckplanByFlowCheckplanIds(flowCheckplanIds);
    }

    /**
     * 删除设备盘点计划信息
     *
     * @param flowCheckplanId 设备盘点计划主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteDmsFlowCheckplanByFlowCheckplanId(Long flowCheckplanId)
    {
        dmsFlowCheckplanMapper.deleteDmsFlowCheckplanDetailByFlowCheckplanId(flowCheckplanId);
        return dmsFlowCheckplanMapper.deleteDmsFlowCheckplanByFlowCheckplanId(flowCheckplanId);
    }

    /**
     * 新增设备盘点明细信息
     *
     * @param dmsFlowCheckplan 设备盘点计划对象
     */
    public void insertDmsFlowCheckplanDetail(DmsFlowCheckplan dmsFlowCheckplan)
    {
        List<DmsFlowCheckplanDetail> dmsFlowCheckplanDetailList = dmsFlowCheckplan.getDmsFlowCheckplanDetailList();
        Long flowCheckplanId = dmsFlowCheckplan.getFlowCheckplanId();
        if (StringUtils.isNotNull(dmsFlowCheckplanDetailList))
        {
            List<DmsFlowCheckplanDetail> list = new ArrayList<DmsFlowCheckplanDetail>();
            for (DmsFlowCheckplanDetail dmsFlowCheckplanDetail : dmsFlowCheckplanDetailList)
            {
                dmsFlowCheckplanDetail.setFlowCheckplanId(flowCheckplanId);
                list.add(dmsFlowCheckplanDetail);
            }
            if (list.size() > 0)
            {
                dmsFlowCheckplanMapper.batchDmsFlowCheckplanDetail(list);
            }
        }
    }
}
