package com.zd.chemical.service.impl;

import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.zd.chemical.domain.HxpChemical;
import com.zd.chemical.domain.HxpChemicalClassify;
import com.zd.chemical.domain.vo.*;
import com.zd.common.core.domain.R;
import com.zd.common.core.exception.ServiceException;
import com.zd.common.core.utils.DateUtils;
import com.zd.common.core.utils.StringUtils;
import com.zd.common.security.utils.DictUtils;
import com.zd.common.security.utils.SaveUtil;

import com.zd.system.api.domain.SysDictData;
import com.zd.system.api.laboratory.RemoteLaboratoryService;
import com.zd.system.api.laboratory.RemoteSubQueryService;
import com.zd.system.api.laboratory.domain.LabSubjectVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zd.chemical.mapper.HxpSmartTerminalMapper;
import com.zd.chemical.domain.HxpSmartTerminal;
import com.zd.chemical.service.IHxpSmartTerminalService;
import com.zd.common.core.utils.SecurityUtils;

/**
 * 化学品设备管理Service业务层处理
 *
 * @author liubo
 * @date 2022-06-16
 */
@Service
public class HxpSmartTerminalServiceImpl implements IHxpSmartTerminalService {
    @Autowired
    private HxpSmartTerminalMapper hxpSmartTerminalMapper;

    @Autowired
    private RemoteSubQueryService remoteSubQueryService;

    @Autowired
    private IHxpSmartTerminalService iHxpSmartTerminalService;

    /**
     * 查询 化学品设备管理
     *
     * @param id 化学品设备管理主键
     * @return 化学品设备管理
     */
    @Override
    public HxpSmartTerminal selectHxpSmartTerminalById(Long id) {
        return hxpSmartTerminalMapper.selectHxpSmartTerminalById(id);
    }

    /**
     * 查询 化学品设备管理列表
     *
     * @param hxpSmartTerminalSearch 化学品设备管理
     * @return 化学品设备管理
     */
    @Override
    public List<HxpSmartTerminalVo> selectHxpSmartTerminalList(HxpSmartTerminalSearch hxpSmartTerminalSearch) {
        return hxpSmartTerminalMapper.selectHxpSmartTerminalList(hxpSmartTerminalSearch);
    }

    @Override
    public List <HxpSmartTerminalByExcel> importSmartTerminal(List <HxpSmartTerminalByExcel> smartTerminalList, String operName, HxpSmartTerminalByExcel hxpSmartTerminalByExcel) {
        if (StringUtils.isNull(smartTerminalList) || smartTerminalList.size() == 0) {
            throw new ServiceException("导入智能设备数据不能为空！");
        }
        List<HxpSmartTerminalByExcel> failSmartTerminalList = new ArrayList <>();
        for (HxpSmartTerminalByExcel smartTerminal : smartTerminalList) {
            //流水线验证
            flowLineCheck(smartTerminal,failSmartTerminalList, hxpSmartTerminalByExcel);
        }
        return failSmartTerminalList;
    }


    private void flowLineCheck(HxpSmartTerminalByExcel smartTerminal, List<HxpSmartTerminalByExcel> failSmartTerminalList, HxpSmartTerminalByExcel hxpSmartTerminalByExcel){
        // 校验是否装入错误数据
        boolean checkBox = true;

        HxpSmartTerminal hxpSmartTerminal = new HxpSmartTerminal();
        //验证设备编号不能为空
        if(StringUtils.isEmpty(smartTerminal.getTerminalNum())){
            if(checkBox){
                smartTerminal.getFailRemark().append("失败：");
                failSmartTerminalList.add(smartTerminal);
                checkBox = false;
            }
            smartTerminal.getFailRemark().append("设备编号不能为空、");
        }else{
            HxpSmartTerminalSearch hxpSmartTerminalSearch = new HxpSmartTerminalSearch();
            hxpSmartTerminalSearch.setTerminalNum(smartTerminal.getTerminalNum());
            List<HxpSmartTerminalVo> hxpSmartTerminalVoList = hxpSmartTerminalMapper.selectHxpSmartTerminalList(hxpSmartTerminalSearch);
            if(hxpSmartTerminalVoList.size()>0){
                if(checkBox){
                    smartTerminal.getFailRemark().append("失败：");
                    failSmartTerminalList.add(smartTerminal);
                    checkBox = false;
                }
                smartTerminal.getFailRemark().append("设备编号重复、");
            }
            if(smartTerminal.getTerminalNum().indexOf(" ")!=-1){
                if(checkBox){
                    smartTerminal.getFailRemark().append("失败：");
                    failSmartTerminalList.add(smartTerminal);
                    checkBox = false;
                }
                smartTerminal.getFailRemark().append("设备编号格式错误、");
            }
            if(!isLetterDigit(smartTerminal.getTerminalNum())){
                if(checkBox){
                    smartTerminal.getFailRemark().append("失败：");
                    failSmartTerminalList.add(smartTerminal);
                    checkBox = false;
                }
                smartTerminal.getFailRemark().append("设备编号只能是数字和字母、");
            }
            if(smartTerminal.getTerminalNum().length()>20){
                if(checkBox){
                    smartTerminal.getFailRemark().append("失败：");
                    failSmartTerminalList.add(smartTerminal);
                    checkBox = false;
                }
                smartTerminal.getFailRemark().append("设备编号超过二十个字符、");
            }
        }
        hxpSmartTerminal.setTerminalNum(smartTerminal.getTerminalNum());

        //实验室不能为空
        if(StringUtils.isEmpty(smartTerminal.getSubName())){
            if(checkBox){
                smartTerminal.getFailRemark().append("失败：");
                failSmartTerminalList.add(smartTerminal);
                checkBox = false;
            }
            smartTerminal.getFailRemark().append("实验室不能为空、");
        }else{
            Map<String,Object> appSubQuery = new HashMap<>();
            List<LinkedHashMap> subList = (List <LinkedHashMap>) remoteSubQueryService.querySubCListVO(appSubQuery).getData();
            for(int i=0;i<subList.size();i++){
                LinkedHashMap hashMap = subList.get(i);
                if(hashMap.get("name").equals(smartTerminal.getSubName())){
                    hxpSmartTerminal.setSubId(Long.parseLong(hashMap.get("id")+""));
                }
            }
            if(StringUtils.isNull(hxpSmartTerminal.getSubId())){
                if(checkBox){
                    smartTerminal.getFailRemark().append("失败：");
                    failSmartTerminalList.add(smartTerminal);
                    checkBox = false;
                }
                smartTerminal.getFailRemark().append("实验室不存在、");
            }
        }

        if(StringUtils.isNotEmpty(smartTerminal.getFailRemark())){
            smartTerminal.getFailRemark().deleteCharAt(smartTerminal.getFailRemark().length()-1);
        }

        if(StringUtils.isEmpty(smartTerminal.getFailRemark())){
            hxpSmartTerminal.setTerminalType(hxpSmartTerminalByExcel.getTerminalType());
            iHxpSmartTerminalService.insertHxpSmartTerminal(hxpSmartTerminal);
        }

    }

    public static boolean isLetterDigit(String str) {
        String regex = "^[a-z0-9A-Z]+$";
        return str.matches(regex);
    }


    /**
     * 新增 化学品设备管理
     *
     * @param hxpSmartTerminal 化学品设备管理
     * @return 结果
     */
    @Override
    public int insertHxpSmartTerminal(HxpSmartTerminal hxpSmartTerminal) {
        //设置其他公共字段
        SaveUtil.setCommonAttr(hxpSmartTerminal);
        return hxpSmartTerminalMapper.insertHxpSmartTerminal(hxpSmartTerminal);
    }

    /**
     * 修改 化学品设备管理
     *
     * @param hxpSmartTerminal 化学品设备管理
     * @return 结果
     */
    @Override
    public int updateHxpSmartTerminal(HxpSmartTerminal hxpSmartTerminal) {
        hxpSmartTerminal.setUpdateTime(DateUtils.getNowDate());
        hxpSmartTerminal.setUpdateBy(SecurityUtils.getUsername());
        return hxpSmartTerminalMapper.updateHxpSmartTerminal(hxpSmartTerminal);
    }

    /**
     * 批量删除 化学品设备管理
     *
     * @param ids 需要删除的 化学品设备管理主键
     * @return 结果
     */
    @Override
    public int deleteHxpSmartTerminalByIds(Long[] ids) {
        return hxpSmartTerminalMapper.deleteHxpSmartTerminalByIds(ids);
    }

    /**
     * 删除 化学品设备管理信息
     *
     * @param id 化学品设备管理主键
     * @return 结果
     */
    @Override
    public int deleteHxpSmartTerminalById(Long id) {
        return hxpSmartTerminalMapper.deleteHxpSmartTerminalById(id);
    }

    @Override
    public Long selectSubIdByTerminalNum(String terminalNum) {
        return hxpSmartTerminalMapper.selectSubIdByTerminalNum(terminalNum);
    }

    @Override
    public int updateStatus(String ipAddress, Integer terminalStatus) {
        return hxpSmartTerminalMapper.updateStatus(ipAddress,terminalStatus);
    }
}
