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.DmsPlanMaintDetail;
import com.hw.dms.mapper.DmsPlanMaintMapper;
import com.hw.dms.domain.DmsPlanMaint;
import com.hw.dms.service.IDmsPlanMaintService;

/**
 * 保养计划信息Service业务层处理
 *
 * @author xins
 * @date 2024-01-08
 */
@Service
public class DmsPlanMaintServiceImpl implements IDmsPlanMaintService
{
    @Autowired
    private DmsPlanMaintMapper dmsPlanMaintMapper;

    /**
     * 查询保养计划信息
     *
     * @param planMaintId 保养计划信息主键
     * @return 保养计划信息
     */
    @Override
    public DmsPlanMaint selectDmsPlanMaintByPlanMaintId(Long planMaintId)
    {
        return dmsPlanMaintMapper.selectDmsPlanMaintByPlanMaintId(planMaintId);
    }

    /**
     * 查询保养计划信息列表
     *
     * @param dmsPlanMaint 保养计划信息
     * @return 保养计划信息
     */
    @Override
    public List<DmsPlanMaint> selectDmsPlanMaintList(DmsPlanMaint dmsPlanMaint)
    {
        return dmsPlanMaintMapper.selectDmsPlanMaintList(dmsPlanMaint);
    }

    /**
     * 新增保养计划信息
     *
     * @param dmsPlanMaint 保养计划信息
     * @return 结果
     */
    @Transactional
    @Override
    public int insertDmsPlanMaint(DmsPlanMaint dmsPlanMaint)
    {
        dmsPlanMaint.setIsFlag(1l);
        LoginUser user = new LoginUser();
        dmsPlanMaint.setCreateBy(user.getUsername());
        dmsPlanMaint.setCreateTime(DateUtils.getNowDate());
        int rows = dmsPlanMaintMapper.insertDmsPlanMaint(dmsPlanMaint);
        insertDmsPlanMaintDetail(dmsPlanMaint);
        return rows;
    }

    /**
     * 修改保养计划信息
     *
     * @param dmsPlanMaint 保养计划信息
     * @return 结果
     */
    @Transactional
    @Override
    public int updateDmsPlanMaint(DmsPlanMaint dmsPlanMaint)
    {
        dmsPlanMaint.setIsFlag(1l);
        LoginUser user = new LoginUser();
        dmsPlanMaint.setUpdateBy(user.getUsername());
        dmsPlanMaint.setUpdateTime(DateUtils.getNowDate());
        dmsPlanMaintMapper.deleteDmsPlanMaintDetailByPlanMaintId(dmsPlanMaint.getPlanMaintId());
        insertDmsPlanMaintDetail(dmsPlanMaint);
        return dmsPlanMaintMapper.updateDmsPlanMaint(dmsPlanMaint);
    }

    /**
     * 批量删除保养计划信息
     *
     * @param planMaintIds 需要删除的保养计划信息主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteDmsPlanMaintByPlanMaintIds(Long[] planMaintIds)
    {
        dmsPlanMaintMapper.deleteDmsPlanMaintDetailByPlanMaintIds(planMaintIds);
        return dmsPlanMaintMapper.deleteDmsPlanMaintByPlanMaintIds(planMaintIds);
    }

    /**
     * 删除保养计划信息信息
     *
     * @param planMaintId 保养计划信息主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteDmsPlanMaintByPlanMaintId(Long planMaintId)
    {
        dmsPlanMaintMapper.deleteDmsPlanMaintDetailByPlanMaintId(planMaintId);
        return dmsPlanMaintMapper.deleteDmsPlanMaintByPlanMaintId(planMaintId);
    }

    /**
     * 新增保养计划明细信息
     *
     * @param dmsPlanMaint 保养计划信息对象
     */
    public void insertDmsPlanMaintDetail(DmsPlanMaint dmsPlanMaint)
    {
        List<DmsPlanMaintDetail> dmsPlanMaintDetailList = dmsPlanMaint.getDmsPlanMaintDetailList();
        Long planMaintId = dmsPlanMaint.getPlanMaintId();
        LoginUser user = new LoginUser();
        if (StringUtils.isNotNull(dmsPlanMaintDetailList))
        {
            List<DmsPlanMaintDetail> list = new ArrayList<DmsPlanMaintDetail>();
            for (DmsPlanMaintDetail dmsPlanMaintDetail : dmsPlanMaintDetailList)
            {
                dmsPlanMaintDetail.setIsFlag(1l);
                dmsPlanMaintDetail.setCreateBy(user.getUsername());
                dmsPlanMaintDetail.setPlanMaintId(planMaintId);
                list.add(dmsPlanMaintDetail);
            }
            if (list.size() > 0)
            {
                dmsPlanMaintMapper.batchDmsPlanMaintDetail(list);
            }
        }
    }
}
