package com.ho.business.service.impl;

import com.ho.business.constant.ForwardColTemplateConstant;
import com.ho.business.entity.DeviceTypeCol;
import com.ho.business.entity.ForwardCol;
import com.ho.business.mapper.ForwardColTemplateMapper;
import com.ho.business.service.ForwardColTemplateService;
import com.ho.business.vo.req.ForwardColTemplateReq;
import com.ho.business.vo.resp.ForwardColTemplateResp;
import com.ho.common.tools.constant.CommonConstant;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class ForwardColTemplateServiceImpl implements ForwardColTemplateService {

    @Autowired
    ForwardColTemplateMapper forwardColTemplateMapper;

    @Override
    public int addForwardColTemplate(ForwardColTemplateReq template) {
        List<ForwardCol> forwardCols = template.getForwardCols();
        int result = CommonConstant.ZERO;
        if(forwardCols.size()>0){
            int i = checkName(template.getTemplateName(),null);
            if(i>0){
                log.info("模板名称重复");
                return result;
            }
            result = forwardColTemplateMapper.addForwardColTemplate(template);
            log.info("增加模板结果：{}",result);
            result = addForwardCol(forwardCols, template.getId());
            log.info("增加模板测点结果：{}",result);
        }
        return result;
    }

    @Override
    public int addForwardCol(List<ForwardCol> list, Integer planningTemplateId) {
        int result = forwardColTemplateMapper.addForwardCol(list,planningTemplateId);
        return result;
    }

    @Override
    public int modifyForwardColTemplate(ForwardColTemplateReq template) {
        List<ForwardCol> forwardCols = template.getForwardCols();
        int result = CommonConstant.ZERO;
        if(forwardCols.size()>0){
            int i = checkName(template.getTemplateName(),template.getId());
            if(i>CommonConstant.ONE){
                return result;
            } else if(CommonConstant.ZERO.equals(i)){
                i = checkName(template.getTemplateName(),null);
                if(i>0){
                    log.info(ForwardColTemplateConstant.TEMPLATE_NAME_DUPLICATION);
                    return result;
                }
            }
            result = forwardColTemplateMapper.modifyForwardColTemplate(template);
            if(result>0){
                //删除配置点表
                //增加新的配置点表
            }
        }
        return result;
    }

    @Override
    public int deleteForwardCol(Integer id) {
        int result = forwardColTemplateMapper.deleteForwardCol(id);
        return result;
    }

    @Override
    public int deleteForwardColTemplates(List<Integer> ids) {
        int result = forwardColTemplateMapper.deleteForwardColTemplates(ids);
        return result;
    }

    @Override
    public int deleteForwardCols(List<Integer> ids) {
        int result = forwardColTemplateMapper.deleteForwardCols(ids);
        return result;
    }

    @Override
    public int checkName(String templateName,Integer tempId) {
        int result = forwardColTemplateMapper.checkName(templateName,tempId);
        return result;
    }

    @Override
    public List<ForwardColTemplateResp> getForwardColTemplateDetail(ForwardColTemplateReq template) {
        List<ForwardColTemplateResp> forwardColTemplateDetail = forwardColTemplateMapper.getForwardColTemplateDetail(template);
        return forwardColTemplateDetail;
    }

    @Override
    public List<DeviceTypeCol> getForwardCol(List<String> typeList,Integer tempId) {
        List<DeviceTypeCol> forwardCol = forwardColTemplateMapper.getForwardCol(typeList,tempId);
        return forwardCol;
    }

}
