package com.ho.business.service.impl;

import com.ho.business.entity.OneClickSequentialControl;
import com.ho.business.mapper.OneClickSequentialControlMapper;
import com.ho.business.service.OneClickSequentialControlService;
import com.ho.business.vo.req.oneClickSequentialControl.OneClickSequentialControlAddReq;
import com.ho.business.vo.req.oneClickSequentialControl.OneClickSequentialControlQueryReq;
import com.ho.business.vo.resp.oneClickSequentialControl.OneClickSequentialControlResp;
import com.ho.common.tools.exception.BaseResponseCode;
import com.ho.common.tools.exception.BusinessException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@Slf4j
public class OneClickSequentialControlServiceImpl implements OneClickSequentialControlService {

    @Autowired
    OneClickSequentialControlMapper controlMapper;

    @Override
    public List<OneClickSequentialControl> getListByParam(OneClickSequentialControlQueryReq vo,String type) {
        String controlName = vo.getControlName();
        if(null!=controlName){
            controlName = controlName.trim();
            vo.setControlName("".equals(controlName)?null:controlName);
        }
        List<OneClickSequentialControl> list=null;
        if("1".equals(type)){
            list = controlMapper.getListLikeParam(vo);
        }else{
            list = controlMapper.getListByParam(vo);
        }
        return list;
    }

    @Override
    public List<OneClickSequentialControlResp> getGroupList(OneClickSequentialControlQueryReq vo) {
        List<OneClickSequentialControl> list = getListByParam(vo,"1");
        Map<String, List<OneClickSequentialControl>> mapList = list.stream().collect(Collectors.groupingBy(OneClickSequentialControl::getControlName));
        List<OneClickSequentialControlResp> respList = new ArrayList<>();
        OneClickSequentialControlResp resp = null;
        for (Map.Entry<String, List<OneClickSequentialControl>> m:mapList.entrySet()) {
            resp = new OneClickSequentialControlResp();
            resp.setControlName(m.getKey());
            resp.setList(m.getValue());
            respList.add(resp);
        }
        return respList;
    }

    @Override
    public int addBatch(OneClickSequentialControlAddReq vo) {
        int count=0;
        if(vo!=null){
            OneClickSequentialControlQueryReq queryReq = new OneClickSequentialControlQueryReq();
            queryReq.setControlName(vo.getControlNewName());
            queryReq.setStationId(vo.getStationId());
            List<OneClickSequentialControl> listByParam = getListByParam(queryReq,"2");
            if(!listByParam.isEmpty()){
                throw new BusinessException(BaseResponseCode.CONTROL_ALREADY_EXISTS);
            }
            if(vo.getList()!=null&&vo.getList().size()>0){
                count= controlMapper.insertBatch(vo.getList());
            }
        }
        return count;
    }

    @Override
    public int updateBatch(OneClickSequentialControlAddReq vo) {
        List<OneClickSequentialControl> list = vo.getList();
        if(vo!=null){
            //如果新旧名字不一致，说明标题被修改，需要去库里查询是否存在
            if(!vo.getControlName().equals(vo.getControlNewName())){
                OneClickSequentialControlQueryReq queryReq = new OneClickSequentialControlQueryReq();
                queryReq.setControlName(vo.getControlNewName());
                queryReq.setStationId(vo.getStationId());
                List<OneClickSequentialControl> listByParam = getListByParam(queryReq,"2");
                if(!listByParam.isEmpty()){
                    throw new BusinessException(BaseResponseCode.CONTROL_ALREADY_EXISTS);
                }
            }
            // 进来先删除
            controlMapper.deleteByControlName(vo.getControlName(),vo.getStationId());
        }
        List<OneClickSequentialControl> addList = new ArrayList<>();
        for (OneClickSequentialControl control:list) {
            addList.add(control);
        }
        int count = 0;
        if(!addList.isEmpty()){
            count = controlMapper.insertBatch(addList);
            log.info("更新一键顺控，更新{}",count);
        }
        return count;
    }

    @Override
    public int deleteByIds(List<Integer> ids) {
        int count = controlMapper.deleteByIds(ids);
        return count;
    }
}
