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());
        //若Long[] ProductIds不为空，则分批添加
        if (dmsBaseMaintStation.getProductIds().length > 0) {
            List<DmsBaseStationProject> dmsBaseStationProjectList = new ArrayList<>();
            for (Long productId : dmsBaseMaintStation.getProductIds()) {
                DmsBaseStationProject dmsBaseStationProject = new DmsBaseStationProject();
                dmsBaseStationProject.setMaintStationId(dmsBaseMaintStation.getMaintStationId());
                dmsBaseStationProject.setMaintProjectId(productId);
                dmsBaseStationProjectList.add(dmsBaseStationProject);
            }
            dmsBaseStationProjectService.batchInsertDmsBaseStationProject(dmsBaseStationProjectList);
        }

        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.size() > 0 && !dmsBaseMaintStationlist.get(0).getMaintStationId()
                .equals(dmsBaseMaintStation.getMaintStationId())) {
            throw new ServiceException("保养部位信息编码已存在");
        }
        LoginUser user = SecurityUtils.getLoginUser();
        dmsBaseMaintStation.setUpdateBy(user.getUserId());
        dmsBaseMaintStation.setUpdateTime(DateUtils.getNowDate());

        //若Long[] ProductIds不为空
        if (dmsBaseMaintStation.getProductIds().length > 0) {
            //根据部位id先去若StaionProject表中查询是否已有数据
            DmsBaseStationProject dmsBaseStationProjectSearch = new DmsBaseStationProject();
            dmsBaseStationProjectSearch.setMaintStationId(dmsBaseMaintStation.getMaintStationId());
            List<DmsBaseStationProject> dmsBaseStationProjectSerchList = dmsBaseStationProjectService
                    .selectDmsBaseStationProjectList(dmsBaseStationProjectSearch);
            //list存储每个实体
            List<DmsBaseStationProject> dmsBaseStationProjectList = new ArrayList<>();
            //若StaionProject表中有数据，则更新（先根据部位id删除，再分批添加）
            if (dmsBaseStationProjectSerchList.size() > 0) {
                //list存储每个实体，每个实体设置属性
                for (Long productId : dmsBaseMaintStation.getProductIds()) {
                    DmsBaseStationProject dmsBaseStationProject = new DmsBaseStationProject();
                    dmsBaseStationProject.setMaintStationId(dmsBaseMaintStation.getMaintStationId());
                    dmsBaseStationProject.setMaintProjectId(productId);
                    dmsBaseStationProjectList.add(dmsBaseStationProject);
                }
                dmsBaseStationProjectService.batchUpdateDmsBaseStationProject(dmsBaseStationProjectList);
            } else {
                //若StaionProject无数据，则添加
                for (Long productId : dmsBaseMaintStation.getProductIds()) {
                    DmsBaseStationProject dmsBaseStationProject = new DmsBaseStationProject();
                    dmsBaseStationProject.setMaintStationId(dmsBaseMaintStation.getMaintStationId());
                    dmsBaseStationProject.setMaintProjectId(productId);
                    dmsBaseStationProjectList.add(dmsBaseStationProject);
                }
                dmsBaseStationProjectService.batchInsertDmsBaseStationProject(dmsBaseStationProjectList);
            }
        } else {
            //若ProductIds为空，则根据maintStationId删除相关
            dmsBaseStationProjectService.deleteDmsBaseStationProjectByMaintStationIds(dmsBaseMaintStation.getMaintStationId());
        }

        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);
    }
}
