package cn.cecep.talroad.service.impl;

import cn.cecep.talroad.common.core.utils.DateUtils;
import cn.cecep.talroad.common.security.utils.SecurityUtils;
import cn.cecep.talroad.domain.BasDcsOperationLog;
import cn.cecep.talroad.domain.BasDevice;
import cn.cecep.talroad.domain.BasDeviceParam;
import cn.cecep.talroad.mapper.BasDeviceParamMapper;
import cn.cecep.talroad.service.IBasDcsOperationLogService;
import cn.cecep.talroad.service.IBasDeviceParamService;
import cn.cecep.talroad.service.IBasDeviceService;
import cn.cecep.talroad.vo.*;
import cn.hutool.core.bean.BeanUtil;
import cn.cecep.talroad.vo.BasDeviceParamVo;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.seata.common.util.CollectionUtils;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.LongStream;




/**
 * 各设备的监测参数属性Service业务层处理
 *
 * @author szhpt
 * @date 2023-01-10
 */
@Service
public class BasDeviceParamServiceImpl extends ServiceImpl<BasDeviceParamMapper, BasDeviceParam> implements IBasDeviceParamService {
    @Autowired
    private BasDeviceParamMapper basDeviceParamMapper;
    @Autowired
    private IBasDcsOperationLogService basDcsOperationLogService;
    @Autowired
    private IBasDeviceService basDeviceService;

    /**
     * 查询各设备的监测参数属性列表
     *
     * @param basDeviceParam 各设备的监测参数属性
     * @return 各设备的监测参数属性
     */
    @Override
    public List<BasDeviceParam> selectBasDeviceParamList(BasDeviceParam basDeviceParam) {
        return basDeviceParamMapper.selectBasDeviceParamList(basDeviceParam);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void editBatch(BasDeviceParamVo[] basDeviceParamVoArr) {
        List<BasDeviceParamVo> basDeviceParamVoList = Arrays.asList(basDeviceParamVoArr);
        //赋值
        List<BasDeviceParam> basDeviceParamList = new ArrayList<>();
        basDeviceParamVoList.forEach(e -> {
            BasDeviceParam basDeviceParam = new BasDeviceParam();
            BeanUtil.copyProperties(e, basDeviceParam);
            basDeviceParamList.add(basDeviceParam);
        });

        //记录操作日志
        List<BasDcsOperationLog> basDcsOperationLogList = new ArrayList<>();
        basDeviceParamList.forEach(e -> {
            boolean isInsert = false;
            BasDeviceParam basDeviceParam = this.getById(e.getId());
            BasDevice basDevice = basDeviceService.getById(basDeviceParam.getDeviceId());

            BasDcsOperationLog basDcsOperationLog = new BasDcsOperationLog();
            String uuid = UUID.randomUUID().toString().trim().replaceAll("-", "");
            basDcsOperationLog.setLogId(uuid);
            basDcsOperationLog.setFactoryId(SecurityUtils.getLoginUser().getSysUser().getFactoryId());
            basDcsOperationLog.setDeviceId(basDeviceParam.getDeviceId());
            basDcsOperationLog.setWorkProcedure(basDevice.getWorkStep());
            basDcsOperationLog.setDeviceName(basDevice.getName());
            basDcsOperationLog.setOperationTime(DateUtils.parseFormatDate("yyyy-MM-dd HH:mm:ss", new Date()));
//            basDcsOperationLog.setOperationStatus(OperationTypeEnum.UPDATE_OPERATION.getName());
            basDcsOperationLog.setOperator(SecurityUtils.getLoginUser().getSysUser().getUserName());
            basDcsOperationLog.setOperatorId(SecurityUtils.getLoginUser().getSysUser().getUserId() + "");
            basDcsOperationLog.setParamName(e.getParamName());
            if ((e.getParamUnit() != null && !e.getParamUnit().equals(basDeviceParam.getParamUnit()))
                    || (basDeviceParam.getParamUnit() != null && !basDeviceParam.getParamUnit().equals(e.getParamUnit()))) {
                basDcsOperationLog.setBeforeModifyParamUnit(basDeviceParam.getParamUnit());
                basDcsOperationLog.setAfterModifyParamUnit(e.getParamUnit());
                isInsert = true;
            }
            if ((e.getMaxValue() != null && !e.getMaxValue().equals(basDeviceParam.getMaxValue()))
                    || (basDeviceParam.getMaxValue() != null && !basDeviceParam.getMaxValue().equals(e.getMaxValue()))) {
                basDcsOperationLog.setBeforeModifyMaxValue(basDeviceParam.getMaxValue());
                basDcsOperationLog.setAfterModifyMaxValue(e.getMaxValue());
                isInsert = true;

            }

            if ((e.getMinValue() != null && !e.getMinValue().equals(basDeviceParam.getMinValue()))
                    || (basDeviceParam.getMinValue() != null && !basDeviceParam.getMinValue().equals(e.getMinValue()))) {
                basDcsOperationLog.setBeforeModifyMinValue(basDeviceParam.getMinValue());
                basDcsOperationLog.setAfterModifyMinValue(e.getMinValue());
                isInsert = true;

            }

            if ((e.getNormalValue() != null && !e.getNormalValue().equals(basDeviceParam.getNormalValue()))
                    || (basDeviceParam.getNormalValue() != null && !basDeviceParam.getNormalValue().equals(e.getNormalValue()))) {
                basDcsOperationLog.setBeforeModifyNormalValue(basDeviceParam.getNormalValue());
                basDcsOperationLog.setAfterModifyNormalValue(e.getNormalValue());
                isInsert = true;

            }
            if (isInsert) {
                basDcsOperationLogList.add(basDcsOperationLog);
            }
        });
        basDeviceParamMapper.updateValue(basDeviceParamVoList);
        if (CollectionUtils.isNotEmpty(basDcsOperationLogList)) {
            basDcsOperationLogService.saveBatch(basDcsOperationLogList);
        }
    }

    @Override
    public int setParamInfo() {
        PrimitiveIterator.OfLong iterator = LongStream.iterate(1L, k -> k + 1L).iterator();
        List<BasDeviceParam> setParamInfoVoList = basDeviceParamMapper.queryNeedUpdateParamInfo();

        for (BasDeviceParam basDeviceParam : setParamInfoVoList) {
            basDeviceParam.setId(iterator.next());
            basDeviceParam.setFacilityType("");
            basDeviceParam.setIsOnline(1L);
            basDeviceParam.setStandbyNum(1L);
            basDeviceParam.setMultiNum(1L);
        }
        for (int i = 0; i < setParamInfoVoList.size(); i += 1000) {
            int start = i;
            int end = Math.min(i + 1000, setParamInfoVoList.size());
            List<BasDeviceParam> subList = setParamInfoVoList.subList(start, end);
            basDeviceParamMapper.batchInsert(subList);
        }

        return 1;
    }


    @Override
    public List<BasDeviceParamVo> selectBasDeviceParamByCondition(String deviceId, List<String> asList) {

        return basDeviceParamMapper.selectBasDeviceParamByCondition(deviceId,asList);
    }

    @Override
    public List<BasDeviceParamListVo> queryByDeviceIdListAndIndustry(List<String> deviceIdList, String industry, String paramName) {
        return basDeviceParamMapper.queryByDeviceIdListAndIndustry(deviceIdList,industry, paramName);
    }

    @Override
    public List<BasDeviceParamVo> selectBasDeviceParamByAnalyse(String deviceId,String dataType, List<String> codeList) {
        return basDeviceParamMapper.selectBasDeviceParamByAnalyse(deviceId,dataType,codeList);
    }

    @Override
    public List<String> getParamCodeSvcByDeviceParamIds(List<Long> list){
        if(CollectionUtils.isEmpty(list)){
            return new ArrayList<>();
        }
        return basDeviceParamMapper.getParamCodeSvcByDeviceParamIds(list);
    }
}
