package com.ho.business.service.impl;

import com.ho.business.entity.StationSn;
import com.ho.business.mapper.StationSnMapper;
import com.ho.business.service.StationSnService;
import com.ho.business.vo.req.StationPutReqVO;
import com.ho.common.tools.exception.BaseResponseCode;
import com.ho.common.tools.exception.BusinessException;
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;
import java.util.stream.Collectors;

/**
 * @Description TODO
 * Author yule
 * Date 2023/2/16 11:07
 */
@Service
public class StationSnServiceImpl implements StationSnService {

    @Autowired
    StationSnMapper stationSnMapper;

    @Override
    public void addSnList(Integer id, List<String> snList) {

        if (!snList.isEmpty()) {
            //判断是否重复
            snRepeatOrNot(snList);

            List<StationSn> stationSnList = new ArrayList<>();
            for (String s : snList) {
                int length = s.length();
                if (length > 16) {
                    throw new BusinessException(BaseResponseCode.STATION_SN_NOT_MORE_THAN_SIXTEEN);
                }
                StationSn stationSn = new StationSn();
                stationSn.setStationId(id);
                stationSn.setSn(s);
                stationSnList.add(stationSn);
            }
            stationSnMapper.insertSNList(stationSnList);
        }
    }

    @Override
    public void deleteByStationIds(List<Integer> ids) {
        if (ids != null) {
            stationSnMapper.deleteByStationIds(ids);
        }else {
            throw new BusinessException(BaseResponseCode.STATION_PLEASE_SELECT);
        }
    }

    @Override
    public List<StationSn> getSnListByStationId(Integer id) {
        List<StationSn> list = stationSnMapper.selectByStationId(id);
        return list;
    }

    @Override
    public StationSn selectBySn(String sn) {
      StationSn stationSn =stationSnMapper.selectBySn(sn);
      return stationSn;
    }

    @Override
    public List<StationSn> selectBySnList(List<String> snList) {
        List<StationSn> list = stationSnMapper.selectBySnList(snList);
        return list;
    }

    @Override
    @Transactional
    public void update(List<Integer> stationIds, StationPutReqVO vo) {
        stationSnMapper.deleteByStationIds(stationIds);
        snRepeatOrNot(vo.getSnList());
        //在新增
        addSnList(vo.getId(),vo.getSnList());
    }

    @Override
    public List<StationSn> selectByStationIds(List<Integer> stationIds) {
        return stationSnMapper.selectByStationIds(stationIds);
    }

    @Override
    public List<StationSn> selectByStationId(Integer stationId) {
        return stationSnMapper.selectByStationId(stationId);
    }

    /**
     * 判断snList是否重复(数据库和当前)
     * @param snList
     */
    private void snRepeatOrNot(List<String> snList) {
        if (snList != null) {
            if (!snList.isEmpty()) {
                snList.stream().collect(Collectors.groupingBy(s -> {
                    return s;
                })).forEach((key, value) -> {
                    if (value.size() > 1) {
                        throw new BusinessException(BaseResponseCode.STATION_SN_DUPLICATE_INPUT);
                    }
                });

                //查询数据库是否存在sn编码是否存在
                List<StationSn> stationSnList = selectBySnList(snList);
                if (!stationSnList.isEmpty()){
                    throw new BusinessException(BaseResponseCode.STATION_SN_ALREADY_EXISTS);
                }
            }
        }
    }
}
