package com.aucma.dms.service.impl;

import com.aucma.common.exception.ServiceException;
import com.aucma.common.utils.DateUtils;
import com.aucma.common.utils.SecurityUtils;
import com.aucma.dms.domain.DmsPlanMaintDetail;
import com.aucma.dms.mapper.DmsPlanMaintDetailMapper;
import com.aucma.dms.service.IDmsPlanMaintDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 保养计划明细Service业务层处理
 *
 * @author xins
 * @date 2024-01-10
 */
@Service
public class DmsPlanMaintDetailServiceImpl implements IDmsPlanMaintDetailService
{
    @Autowired
    private DmsPlanMaintDetailMapper dmsPlanMaintDetailMapper;

    /**
     * 查询保养计划明细
     *
     * @param planMaintDetailId 保养计划明细主键
     * @return 保养计划明细
     */
    @Override
    public DmsPlanMaintDetail selectDmsPlanMaintDetailByPlanMaintDetailId(Long planMaintDetailId)
    {
        return dmsPlanMaintDetailMapper.selectDmsPlanMaintDetailByPlanMaintDetailId(planMaintDetailId);
    }

    /**
     * 查询保养计划明细列表
     *
     * @param dmsPlanMaintDetail 保养计划明细
     * @return 保养计划明细
     */
    @Override
    public List<DmsPlanMaintDetail> selectDmsPlanMaintDetailList(DmsPlanMaintDetail dmsPlanMaintDetail)
    {
        return dmsPlanMaintDetailMapper.selectDmsPlanMaintDetailList(dmsPlanMaintDetail);
    }

    /**
     * 新增保养计划明细
     *
     * @param dmsPlanMaintDetail 保养计划明细
     * @return 结果
     */
    @Override
    public int insertDmsPlanMaintDetail(DmsPlanMaintDetail dmsPlanMaintDetail)
    {
       if (dmsPlanMaintDetail.getDeviceId()==null){throw new ServiceException("设备名称不能为空");}
       dmsPlanMaintDetail.setCreateBy(SecurityUtils.getLoginUser()==null ? SecurityUtils.getUserId() :SecurityUtils.getLoginUser().getUser().getUserId());
        dmsPlanMaintDetail.setCreateTime(DateUtils.getNowDate());
        dmsPlanMaintDetail.setIsFlag(1l);
        Long stationId = dmsPlanMaintDetailMapper.selectStationIdByCode(dmsPlanMaintDetail.getMaintStationCode());
        dmsPlanMaintDetail.setMaintStationId(stationId);
        Long standardId = dmsPlanMaintDetailMapper.selectStandardId(stationId);
        dmsPlanMaintDetail.setMaintStandardId(standardId);
        return dmsPlanMaintDetailMapper.insertDmsPlanMaintDetail(dmsPlanMaintDetail);
    }

    /**
     * 修改保养计划明细
     *
     * @param dmsPlanMaintDetail 保养计划明细
     * @return 结果
     */
    @Override
    public int updateDmsPlanMaintDetail(DmsPlanMaintDetail dmsPlanMaintDetail)
    {
        dmsPlanMaintDetail.setUpdateBy(SecurityUtils.getLoginUser()==null ? SecurityUtils.getUserId() :SecurityUtils.getLoginUser().getUser().getUserId());
        dmsPlanMaintDetail.setUpdateTime(DateUtils.getNowDate());
        Long stationId = dmsPlanMaintDetailMapper.selectStationIdByCode(dmsPlanMaintDetail.getMaintStationCode());
        dmsPlanMaintDetail.setMaintStationId(stationId);
        return dmsPlanMaintDetailMapper.updateDmsPlanMaintDetail(dmsPlanMaintDetail);
    }

    /**
     * 批量删除保养计划明细
     *
     * @param planMaintDetailIds 需要删除的保养计划明细主键
     * @return 结果
     */
    @Override
    public int deleteDmsPlanMaintDetailByPlanMaintDetailIds(Long[] planMaintDetailIds)
    {
        return dmsPlanMaintDetailMapper.deleteDmsPlanMaintDetailByPlanMaintDetailIds(planMaintDetailIds);
    }

    /**
     * 删除保养计划明细信息
     *
     * @param planMaintDetailId 保养计划明细主键
     * @return 结果
     */
    @Override
    public int deleteDmsPlanMaintDetailByPlanMaintDetailId(Long planMaintDetailId)
    {
        return dmsPlanMaintDetailMapper.deleteDmsPlanMaintDetailByPlanMaintDetailId(planMaintDetailId);
    }




    /**
     * 查询保养计划明细列表，Join station and standard
     *
     * @param dmsPlanMaintDetail 保养计划明细
     * @return 保养计划明细
     */
    @Override
    public List<DmsPlanMaintDetail> selectDmsPlanMaintDetailJoinList(DmsPlanMaintDetail dmsPlanMaintDetail)
    {
        return dmsPlanMaintDetailMapper.selectDmsPlanMaintDetailJoinList(dmsPlanMaintDetail);
    }

    @Override
    public List<DmsPlanMaintDetail> selectDmsPlanMaintDetailListByPlanMaintId(Long planMaintId) {
        return dmsPlanMaintDetailMapper.selectDmsPlanMaintDetailListByPlanMaintId(planMaintId);
    }
}
