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.PcFactoryAccessControl;
import cn.cecep.talroad.domain.PcFactoryAccessControlCoordinateOperationLog;
import cn.cecep.talroad.enums.OperationTypeEnum;
import cn.cecep.talroad.mapper.PcFactoryAccessControlMapper;
import cn.cecep.talroad.service.IPcFactoryAccessControlCoordinateOperationLogService;
import cn.cecep.talroad.service.IPcFactoryAccessControlService;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.*;

/**
 * @Description: 企业门禁信息
 * @Author: code-generate
 * @Date:   2023-07-18
 * @Version: V1.0
 */
@Service
public class PcFactoryAccessControlServiceImpl extends ServiceImpl<PcFactoryAccessControlMapper, PcFactoryAccessControl> implements IPcFactoryAccessControlService {
    @Autowired
    private IPcFactoryAccessControlCoordinateOperationLogService pcFactoryAccessControlCoordinateOperationLogService;
    @Override
    public void editBatch(PcFactoryAccessControl[] pcFactoryAccessControlArr) {
        List<PcFactoryAccessControl> pcFactoryAccessControlList = Arrays.asList(pcFactoryAccessControlArr);

        //记录操作日志
        List<PcFactoryAccessControlCoordinateOperationLog> logList = new ArrayList<>();
        QueryWrapper<PcFactoryAccessControl> queryWrapper = new QueryWrapper<>();
        pcFactoryAccessControlList.forEach(e -> {
            PcFactoryAccessControlCoordinateOperationLog log = new PcFactoryAccessControlCoordinateOperationLog();
            queryWrapper.clear();
            queryWrapper.eq("factory_id",e.getFactoryId());
            queryWrapper.eq("access_control_code",e.getAccessControlCode());
            PcFactoryAccessControl pcFactoryAccessControl = baseMapper.selectOne(queryWrapper);
            String uuid = UUID.randomUUID().toString().trim().replaceAll("-", "");
            log.setLogId(uuid);
            log.setFactoryId(pcFactoryAccessControl.getFactoryId());
            log.setOperationTime(DateUtils.parseFormatDate("yyyy-MM-dd HH:mm:ss", new Date()));
            log.setOperationStatus(OperationTypeEnum.UPDATE_OPERATION.getName());
            log.setOperator(SecurityUtils.getLoginUser().getSysUser().getUserName());
            log.setOperatorId(SecurityUtils.getLoginUser().getSysUser().getUserId() + "");
            log.setAccessControlName(pcFactoryAccessControl.getAccessControlName());
            log.setBeforeModifyLag(StrUtil.toStringOrNull(pcFactoryAccessControl.getAccessControlLat()));
            log.setBeforeModifyLng(StrUtil.toStringOrNull(pcFactoryAccessControl.getAccessControlLng()));
            log.setAccessControlCode(pcFactoryAccessControl.getAccessControlCode());
            log.setAfterModifyLag(StrUtil.toStringOrNull(e.getAccessControlLat()));
            log.setAfterModifyLng(StrUtil.toStringOrNull(e.getAccessControlLng()));
            logList.add(log);
        });
        baseMapper.updateBatchCoordinate(pcFactoryAccessControlList);
        pcFactoryAccessControlCoordinateOperationLogService.saveBatch(logList);
    }
}
