package com.aucma.dms.service.impl;



import com.aucma.common.core.domain.model.LoginUser;
import com.aucma.common.exception.ServiceException;
import com.aucma.common.utils.DateUtils;
import com.aucma.common.utils.SecurityUtils;
import com.aucma.dms.domain.DmsBaseMaintStation;
import com.aucma.dms.domain.DmsBaseStationProject;
import com.aucma.dms.domain.DmsMaintInstanceActivity;
import com.aucma.dms.mapper.DmsBaseMaintStationMapper;
import com.aucma.dms.service.IDmsBaseMaintStationService;
import com.aucma.dms.service.IDmsBaseStationProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 保养部位信息Service业务层处理
 *
 * @author xins
 * @date 2024-01-05
 */
@Service
public class DmsBaseMaintStationServiceImpl implements IDmsBaseMaintStationService
{
    @Autowired
    private DmsBaseMaintStationMapper dmsBaseMaintStationMapper;

    @Autowired
    private IDmsBaseStationProjectService dmsBaseStationProjectService;

    /**
     * 查询保养部位信息
     *
     * @param maintStationId 保养部位信息主键
     * @return 保养部位信息
     */
    @Override
    public DmsBaseMaintStation selectDmsBaseMaintStationByMaintStationId(Long maintStationId)
    {
        return dmsBaseMaintStationMapper.selectDmsBaseMaintStationByMaintStationId(maintStationId);
    }

    /**
     * 查询保养部位信息列表
     *
     * @param dmsBaseMaintStation 保养部位信息
     * @return 保养部位信息
     */
    @Override
    public List<DmsBaseMaintStation> selectDmsBaseMaintStationList(DmsBaseMaintStation dmsBaseMaintStation)
    {
        return dmsBaseMaintStationMapper.selectDmsBaseMaintStationList(dmsBaseMaintStation);
    }

    /**
     * 新增保养部位信息
     *
     * @param dmsBaseMaintStation 保养部位信息
     * @return 结果
     */
    @Override
    @Transactional
    public int insertDmsBaseMaintStation(DmsBaseMaintStation dmsBaseMaintStation) {
        DmsBaseMaintStation MaintStation = new DmsBaseMaintStation();
        MaintStation.setMaintStationCode(dmsBaseMaintStation.getMaintStationCode());
        List<DmsBaseMaintStation> dmsBaseMaintStationlist = dmsBaseMaintStationMapper
                .selectDmsBaseMaintStationList(MaintStation);
        if (dmsBaseMaintStationlist.size() > 0) {
            throw new ServiceException("保养部位信息编码已存在");
        }
        LoginUser user = SecurityUtils.getLoginUser();
        dmsBaseMaintStation.setIsFlag(1l);
        dmsBaseMaintStation.setCreateBy(user.getUserId());
        dmsBaseMaintStation.setCreateTime(DateUtils.getNowDate());

/*因为MaintStationId在前端不可见不可编辑，是数据库自增主键，
所以新增保养部位以及相关的保养项目时，前端传来的实体类没有MaintStationId，所以需要先插入保养部位，再获取自增主键MaintStationId，再赋值给dmsBaseMaintStation
方法添加了事务@Transactional保证原子性*/
        //先新增保养部位
        int i = dmsBaseMaintStationMapper.insertDmsBaseMaintStation(dmsBaseMaintStation);//先插入保养部位信息，数据库自增主键maint_station_id
        //查询出来MaintStationId，再赋值给dmsBaseMaintStation
        List<DmsBaseMaintStation> dmsBaseMaintStationsNew = dmsBaseMaintStationMapper.
                selectDmsBaseMaintStationList(dmsBaseMaintStation);
        dmsBaseMaintStation.setMaintStationId(dmsBaseMaintStationsNew.get(0).getMaintStationId());
        saveStationProjects(dmsBaseMaintStation.getMaintStationId(), dmsBaseMaintStation.getProductIds(), false);

        return i;
    }

    /**
     * 修改保养部位信息
     *
     * @param dmsBaseMaintStation 保养部位信息
     * @return 结果
     */
    @Override
    @Transactional
    public int updateDmsBaseMaintStation(DmsBaseMaintStation dmsBaseMaintStation) {
        DmsBaseMaintStation MaintStation = new DmsBaseMaintStation();
        MaintStation.setMaintStationCode(dmsBaseMaintStation.getMaintStationCode());
        List<DmsBaseMaintStation> dmsBaseMaintStationlist = dmsBaseMaintStationMapper
                .selectDmsBaseMaintStationList(MaintStation);
        if (!dmsBaseMaintStationlist.isEmpty() && !dmsBaseMaintStationlist.get(0).getMaintStationId()
                .equals(dmsBaseMaintStation.getMaintStationId())) {
            throw new ServiceException("保养部位信息编码已存在");
        }
        LoginUser user = SecurityUtils.getLoginUser();
        dmsBaseMaintStation.setUpdateBy(user.getUserId());
        dmsBaseMaintStation.setUpdateTime(DateUtils.getNowDate());

        saveStationProjects(dmsBaseMaintStation.getMaintStationId(), dmsBaseMaintStation.getProductIds(), true);

        return dmsBaseMaintStationMapper.updateDmsBaseMaintStation(dmsBaseMaintStation);
    }

    /**
     * 批量删除保养部位信息
     *
     * @param maintStationIds 需要删除的保养部位信息主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteDmsBaseMaintStationByMaintStationIds(Long[] maintStationIds)
    {
        dmsBaseStationProjectService.deleteDmsBaseStationProjectByMaintStationIds(maintStationIds);
        return dmsBaseMaintStationMapper.deleteDmsBaseMaintStationByMaintStationIds(maintStationIds);
    }

    @Override
    public List<DmsMaintInstanceActivity> selectDmsMaintInfo(Long maintInstanceId) {
        return dmsBaseMaintStationMapper.selectDmsMainInfo(maintInstanceId);
    }

    /**
     * 删除保养部位信息信息
     *
     * @param maintStationId 保养部位信息主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteDmsBaseMaintStationByMaintStationId(Long maintStationId)
    {
        dmsBaseStationProjectService.deleteDmsBaseStationProjectByMaintStationIds(maintStationId);
        return dmsBaseMaintStationMapper.deleteDmsBaseMaintStationByMaintStationId(maintStationId);
    }

    private void saveStationProjects(Long maintStationId, Long[] productIds, boolean clearBeforeInsert)
    {
        if (maintStationId == null) {
            return;
        }

        if (clearBeforeInsert) {
            // 修改时先清空旧关联，再写入当前勾选结果，避免残留旧项目或重复关系。
            dmsBaseStationProjectService.deleteDmsBaseStationProjectByMaintStationIds(maintStationId);
        }

        if (productIds == null || productIds.length == 0) {
            return;
        }

        List<DmsBaseStationProject> dmsBaseStationProjectList = buildStationProjectList(maintStationId, productIds);
        if (dmsBaseStationProjectList.isEmpty()) {
            return;
        }
        dmsBaseStationProjectService.batchInsertDmsBaseStationProject(dmsBaseStationProjectList);
    }

    private List<DmsBaseStationProject> buildStationProjectList(Long maintStationId, Long[] productIds)
    {
        List<DmsBaseStationProject> dmsBaseStationProjectList = new ArrayList<>();
        for (Long productId : productIds) {
            if (productId == null) {
                continue;
            }
            // 这里过滤空项目ID，避免脏数据进入批量SQL后导致整批回滚。
            DmsBaseStationProject dmsBaseStationProject = new DmsBaseStationProject();
            dmsBaseStationProject.setMaintStationId(maintStationId);
            dmsBaseStationProject.setMaintProjectId(productId);
            dmsBaseStationProjectList.add(dmsBaseStationProject);
        }
        return dmsBaseStationProjectList;
    }
}
