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.MAmVideoCoordinateOperationLog;
import cn.cecep.talroad.domain.MAmVideoDevFi;
import cn.cecep.talroad.enums.OperationTypeEnum;
import cn.cecep.talroad.mapper.MAmVideoDevFiMapper;
import cn.cecep.talroad.service.IMAmVideoCoordinateOperationLogService;
import cn.cecep.talroad.service.IMAmVideoDevFiService;
import cn.cecep.talroad.vo.MAmVideoDevFiVo;
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;


/**
 * 基础信息 与 产治排跟踪--视频设施清单Service业务层处理
 *
 * @author szhpt
 * @date 2023-02-10
 */
@Service
public class MAmVideoDevFiServiceImpl extends ServiceImpl<MAmVideoDevFiMapper, MAmVideoDevFi> implements IMAmVideoDevFiService {
    @Autowired
    private MAmVideoDevFiMapper mAmVideoDevFiMapper;
    @Autowired
    private IMAmVideoCoordinateOperationLogService imAmVideoCoordinateOperationLogService;

    /**
     * 查询基础信息 与 产治排跟踪--视频设施清单列表
     *
     * @param mAmVideoDevFi 基础信息 与 产治排跟踪--视频设施清单
     * @return 基础信息 与 产治排跟踪--视频设施清单
     */
    @Override
    public List<MAmVideoDevFi> selectMAmVideoDevFiList(MAmVideoDevFi mAmVideoDevFi) {
        return mAmVideoDevFiMapper.selectMAmVideoDevFiList(mAmVideoDevFi);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void editBatch(MAmVideoDevFiVo[] mAmVideoDevFiVoArr) {
        List<MAmVideoDevFiVo> mAmVideoDevFiVoList = Arrays.asList(mAmVideoDevFiVoArr);
        //赋值
        List<MAmVideoDevFi> mAmVideoDevFiList = new ArrayList<>();
        mAmVideoDevFiVoList.forEach(e -> {
            MAmVideoDevFi mAmVideoDevFi = new MAmVideoDevFi();
            BeanUtil.copyProperties(e, mAmVideoDevFi);
            mAmVideoDevFiList.add(mAmVideoDevFi);
        });


        //记录操作日志
        List<MAmVideoCoordinateOperationLog> mAmVideoCoordinateOperationLogList = new ArrayList<>();
        mAmVideoDevFiList.forEach(e -> {
            MAmVideoCoordinateOperationLog mAmVideoCoordinateOperationLog = new MAmVideoCoordinateOperationLog();

            MAmVideoDevFi mAmVideoDevFi = mAmVideoDevFiMapper.selectById(e.getId());
            String uuid = UUID.randomUUID().toString().trim().replaceAll("-", "");
            mAmVideoCoordinateOperationLog.setLogId(uuid);
            mAmVideoCoordinateOperationLog.setFactoryId(mAmVideoDevFi.getFactoryId());
            mAmVideoCoordinateOperationLog.setOperationTime(DateUtils.parseFormatDate("yyyy-MM-dd HH:mm:ss", new Date()));
            mAmVideoCoordinateOperationLog.setOperationStatus(OperationTypeEnum.UPDATE_OPERATION.getName());
            mAmVideoCoordinateOperationLog.setOperator(SecurityUtils.getLoginUser().getSysUser().getUserName());
            mAmVideoCoordinateOperationLog.setOperatorId(SecurityUtils.getLoginUser().getSysUser().getUserId() + "");
            mAmVideoCoordinateOperationLog.setVideoStationName(mAmVideoDevFi.getDevName());
            mAmVideoCoordinateOperationLog.setBeforeModifyLag(mAmVideoDevFi.getLatMap());
            mAmVideoCoordinateOperationLog.setBeforeModifyLng(mAmVideoDevFi.getLonMap());
            mAmVideoCoordinateOperationLog.setVideoChannel(mAmVideoDevFi.getChannelId());
            mAmVideoCoordinateOperationLog.setAfterModifyLag(e.getLatMap());
            mAmVideoCoordinateOperationLog.setAfterModifyLng(e.getLonMap());
            mAmVideoCoordinateOperationLogList.add(mAmVideoCoordinateOperationLog);

        });
        mAmVideoDevFiMapper.updateBatchCoordinate(mAmVideoDevFiVoList);
        imAmVideoCoordinateOperationLogService.saveBatch(mAmVideoCoordinateOperationLogList);
    }

}
